From c5eea16cfe48fc3df1222f795adede27c5d6b8d0 Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Mon, 9 Aug 2021 09:19:46 -0400 Subject: [PATCH] Logs panel: Set default scrolling for ascending order to bottom (#37634) * LogsPanel: When ascending order, scroll to bottom * Add tests * Refactor style * Update scroll position when logs change --- .../app/plugins/panel/logs/LogsPanel.test.tsx | 18 +++++++- public/app/plugins/panel/logs/LogsPanel.tsx | 41 +++++++++++++------ 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/public/app/plugins/panel/logs/LogsPanel.test.tsx b/public/app/plugins/panel/logs/LogsPanel.test.tsx index 9274d668cda..029b2392dfe 100644 --- a/public/app/plugins/panel/logs/LogsPanel.test.tsx +++ b/public/app/plugins/panel/logs/LogsPanel.test.tsx @@ -1,6 +1,6 @@ import React, { ComponentProps } from 'react'; import { render, screen } from '@testing-library/react'; -import { LoadingState, MutableDataFrame, FieldType } from '@grafana/data'; +import { LoadingState, MutableDataFrame, FieldType, LogsSortOrder } from '@grafana/data'; import { LogsPanel } from './LogsPanel'; type LogsPanelProps = ComponentProps; @@ -38,6 +38,22 @@ describe('LogsPanel', () => { expect(screen.getByText(/common_app/i)).toBeInTheDocument(); expect(screen.getByText(/common_job/i)).toBeInTheDocument(); }); + it('shows common labels on top when descending sort order', () => { + const { container } = setup({ + data: { series: seriesWithCommonLabels }, + options: { showCommonLabels: true, sortOrder: LogsSortOrder.Descending }, + }); + + expect(container.firstChild?.childNodes[0].textContent).toMatch(/^Common labels:common_appcommon_job/); + }); + it('shows common labels on bottom when ascending sort order', () => { + const { container } = setup({ + data: { series: seriesWithCommonLabels }, + options: { showCommonLabels: true, sortOrder: LogsSortOrder.Ascending }, + }); + + expect(container.firstChild?.childNodes[0].textContent).toMatch(/Common labels:common_appcommon_job$/); + }); it('does not show common labels when showCommonLabels is set to false', () => { setup({ data: { series: seriesWithCommonLabels }, options: { showCommonLabels: false } }); diff --git a/public/app/plugins/panel/logs/LogsPanel.tsx b/public/app/plugins/panel/logs/LogsPanel.tsx index 1cfb4db2fda..7446ff246f2 100644 --- a/public/app/plugins/panel/logs/LogsPanel.tsx +++ b/public/app/plugins/panel/logs/LogsPanel.tsx @@ -1,7 +1,7 @@ -import React, { useCallback, useMemo } from 'react'; +import React, { useCallback, useMemo, useRef, useLayoutEffect, useState } from 'react'; import { css } from '@emotion/css'; import { LogRows, CustomScrollbar, LogLabels, useStyles2 } from '@grafana/ui'; -import { PanelProps, Field, Labels, GrafanaTheme2 } from '@grafana/data'; +import { PanelProps, Field, Labels, GrafanaTheme2, LogsSortOrder } from '@grafana/data'; import { Options } from './types'; import { dataFrameToLogsModel, dedupLogRows } from 'app/core/logs_model'; import { getFieldLinksForExplore } from 'app/features/explore/utils/links'; @@ -24,7 +24,10 @@ export const LogsPanel: React.FunctionComponent = ({ }, title, }) => { - const style = useStyles2(getStyles(title)); + const isAscending = sortOrder === LogsSortOrder.Ascending; + const style = useStyles2(getStyles(title, isAscending)); + const [scrollTop, setScrollTop] = useState(0); + const logsContainerRef = useRef(null); // Important to memoize stuff here, as panel rerenders a lot for example when resizing. const [logRows, deduplicatedRows, commonLabels] = useMemo(() => { @@ -35,6 +38,14 @@ export const LogsPanel: React.FunctionComponent = ({ return [logRows, deduplicatedRows, commonLabels]; }, [data, dedupStrategy]); + useLayoutEffect(() => { + if (isAscending && logsContainerRef.current) { + setScrollTop(logsContainerRef.current.offsetHeight); + } else { + setScrollTop(0); + } + }, [isAscending, logRows]); + const getFieldLinks = useCallback( (field: Field, rowIndex: number) => { return getFieldLinksForExplore({ field, rowIndex, range: data.timeRange }); @@ -50,15 +61,17 @@ export const LogsPanel: React.FunctionComponent = ({ ); } + const renderCommonLabels = () => ( +
+ Common labels: + +
+ ); + return ( - -
- {showCommonLabels && ( -
- Common labels: - -
- )} + +
+ {showCommonLabels && !isAscending && renderCommonLabels()} = ({ getFieldLinks={getFieldLinks} logsSortOrder={sortOrder} enableLogDetails={enableLogDetails} + previewLimit={isAscending ? logRows.length : undefined} /> + {showCommonLabels && isAscending && renderCommonLabels()}
); }; -const getStyles = (title: string) => (theme: GrafanaTheme2) => ({ +const getStyles = (title: string, isAscending: boolean) => (theme: GrafanaTheme2) => ({ container: css` margin-bottom: ${theme.spacing(1.5)}; //We can remove this hot-fix when we fix panel menu with no title overflowing top of all panels margin-top: ${theme.spacing(!title ? 2.5 : 0)}; `, labelContainer: css` - margin: ${theme.spacing(0, 0, 0.5, 0.5)}; + margin: ${isAscending ? theme.spacing(0.5, 0, 0.5, 0) : theme.spacing(0, 0, 0.5, 0.5)}; display: flex; align-items: center; `,