From 598a2a6fa9bc64164a5f60787c5e13ef005b0bfe Mon Sep 17 00:00:00 2001 From: Galen Kistler <109082771+gtk-grafana@users.noreply.github.com> Date: Tue, 19 Dec 2023 07:10:27 -0600 Subject: [PATCH] Logs Panel: Table UI - Explore default state (#79653) * save explore visualisation type in local storage --- .../app/features/explore/Logs/Logs.test.tsx | 16 +++++++++++++++- public/app/features/explore/Logs/Logs.tsx | 19 ++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/public/app/features/explore/Logs/Logs.test.tsx b/public/app/features/explore/Logs/Logs.test.tsx index b14f3db4cd9..6f5f4eee7be 100644 --- a/public/app/features/explore/Logs/Logs.test.tsx +++ b/public/app/features/explore/Logs/Logs.test.tsx @@ -18,7 +18,7 @@ import { organizeFieldsTransformer } from '@grafana/data/src/transformations/tra import { config } from '@grafana/runtime'; import { extractFieldsTransformer } from 'app/features/transformers/extractFields/extractFields'; -import { Logs } from './Logs'; +import { Logs, visualisationTypeKey } from './Logs'; import { getMockElasticFrame, getMockLokiFrame } from './utils/testMocks.test'; const reportInteraction = jest.fn(); @@ -480,6 +480,20 @@ describe('Logs', () => { expect(table).toBeInTheDocument(); }); + it('should use default state from localstorage - table', async () => { + localStorage.setItem(visualisationTypeKey, 'table'); + setup({}); + const table = await screen.findByTestId('logRowsTable'); + expect(table).toBeInTheDocument(); + }); + + it('should use default state from localstorage - logs', async () => { + localStorage.setItem(visualisationTypeKey, 'logs'); + setup({}); + const table = await screen.findByTestId('logRows'); + expect(table).toBeInTheDocument(); + }); + it('should change visualisation to table on toggle (elastic)', async () => { setup({}, getMockElasticFrame()); const logsSection = screen.getByRole('radio', { name: 'Show results in table visualisation' }); diff --git a/public/app/features/explore/Logs/Logs.tsx b/public/app/features/explore/Logs/Logs.tsx index 1b2a439c3af..293ca610a56 100644 --- a/public/app/features/explore/Logs/Logs.tsx +++ b/public/app/features/explore/Logs/Logs.tsx @@ -137,6 +137,16 @@ const DEDUP_OPTIONS = [ LogsDedupStrategy.signature, ]; +export const visualisationTypeKey = 'grafana.explore.logs.visualisationType'; + +const getDefaultVisualisationType = (): LogsVisualisationType => { + const visualisationType = store.get(visualisationTypeKey); + if (visualisationType === 'table') { + return 'table'; + } + return 'logs'; +}; + class UnthemedLogs extends PureComponent { flipOrderTimer?: number; cancelFlippingTimer?: number; @@ -157,7 +167,7 @@ class UnthemedLogs extends PureComponent { contextOpen: false, contextRow: undefined, tableFrame: undefined, - visualisationType: this.props.panelState?.logs?.visualisationType ?? 'logs', + visualisationType: this.props.panelState?.logs?.visualisationType ?? getDefaultVisualisationType(), logsContainer: undefined, }; @@ -219,9 +229,12 @@ class UnthemedLogs extends PureComponent { ); } if (this.props.panelState?.logs?.visualisationType !== prevProps.panelState?.logs?.visualisationType) { + const visualisationType = this.props.panelState?.logs?.visualisationType ?? getDefaultVisualisationType(); + this.setState({ - visualisationType: this.props.panelState?.logs?.visualisationType ?? 'logs', + visualisationType: visualisationType, }); + store.set(visualisationTypeKey, visualisationType); } } @@ -436,7 +449,7 @@ class UnthemedLogs extends PureComponent { const urlState = getUrlStateFromPaneState(getState().explore.panes[this.props.exploreId]!); urlState.panelsState = { ...this.props.panelState, - logs: { id: row.uid, visualisationType: this.state.visualisationType ?? 'logs' }, + logs: { id: row.uid, visualisationType: this.state.visualisationType ?? getDefaultVisualisationType() }, }; urlState.range = { from: new Date(this.props.absoluteRange.from).toISOString(),