From ceec3a9b19356125fb0d720875d72241eb6436b8 Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Mon, 30 Oct 2023 09:56:49 +0000 Subject: [PATCH] [v10.2.x] Explore: Avoid reinitializing graph on every query run (#77290) Explore: Avoid reinitializing graph on every query run (#77281) (cherry picked from commit bf554d121cc116457f3432f2448314381b234016) Co-authored-by: Giordano Ricci --- .../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.