From bf554d121cc116457f3432f2448314381b234016 Mon Sep 17 00:00:00 2001 From: Giordano Ricci Date: Fri, 27 Oct 2023 14:37:23 +0100 Subject: [PATCH] Explore: Avoid reinitializing graph on every query run (#77281) --- .../app/features/explore/Graph/useStructureRev.test.ts | 9 +++++++++ public/app/features/explore/Graph/useStructureRev.ts | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/public/app/features/explore/Graph/useStructureRev.test.ts b/public/app/features/explore/Graph/useStructureRev.test.ts index dd808005929..650e1ac0a72 100644 --- a/public/app/features/explore/Graph/useStructureRev.test.ts +++ b/public/app/features/explore/Graph/useStructureRev.test.ts @@ -51,6 +51,15 @@ beforeAll(() => { describe('useStructureRev', () => { afterEach(() => resetCounters()); + // mirrors the logic in componentDidUpdate in packages/grafana-ui/src/components/GraphNG/GraphNG.tsx, + // which treats all falsy values for structureRev as a signal to reconfig the graph + it('should start from a thruthy value', () => { + let frames: DataFrame[] = [toDataFrame({ fields: [{ name: 'time', type: FieldType.time, values: [1, 2, 3] }] })]; + const { result } = renderHook((frames) => useStructureRev(frames), { initialProps: frames }); + + expect(result.current).not.toBeFalsy(); + }); + it('should increment only when relevant fields in frame change', () => { let frames: DataFrame[] = [toDataFrame({ fields: [{ name: 'time', type: FieldType.time, values: [1, 2, 3] }] })]; const { result, rerender } = renderHook((frames) => useStructureRev(frames), { initialProps: frames }); diff --git a/public/app/features/explore/Graph/useStructureRev.ts b/public/app/features/explore/Graph/useStructureRev.ts index 23490e4d170..cb51571a42c 100644 --- a/public/app/features/explore/Graph/useStructureRev.ts +++ b/public/app/features/explore/Graph/useStructureRev.ts @@ -4,7 +4,7 @@ import { useCounter, usePrevious } from 'react-use'; import { DataFrame, compareArrayValues, compareDataFrameStructures } from '@grafana/data'; export function useStructureRev(frames: DataFrame[]) { - const [structureRev, { inc }] = useCounter(0); + const [structureRev, { inc }] = useCounter(1); const previousFrames = usePrevious(frames); // We need to increment structureRev when the number of series changes.