Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

chore: variables based panel fetching (#10292)

Ashwin Bhatkal8898f02698c9
diff --git a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/__test__/dashboardVariables.test.tsx b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/__test__/dashboardVariables.test.tsx
index 06837c5560..904f6b0cc3 100644
--- a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/__test__/dashboardVariables.test.tsx
+++ b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/__test__/dashboardVariables.test.tsx
@@ -144,6 +144,72 @@ describe('dashboardVariables - utilities and processors', () => {
expect(buildDependencyGraph(graph)).toEqual(expected);
});
+
+ it('should return empty transitiveDescendants for an empty graph', () => {
+ const result = buildDependencyGraph({});
+ expect(result.transitiveDescendants).toEqual({});
+ expect(result.order).toEqual([]);
+ expect(result.hasCycle).toBe(false);
+ });
+
+ it('should compute transitiveDescendants for a linear chain (a -> b -> c)', () => {
+ const linearGraph: VariableGraph = {
+ a: ['b'],
+ b: ['c'],
+ c: [],
+ };
+ const result = buildDependencyGraph(linearGraph);
+ expect(result.transitiveDescendants).toEqual({
+ a: ['b', 'c'],
+ b: ['c'],
+ c: [],
+ });
+ });
+
+ it('should compute transitiveDescendants for a diamond dependency (a -> b, a -> c, b -> d, c -> d)', () => {
+ const diamondGraph: VariableGraph = {
+ a: ['b', 'c'],
+ b: ['d'],
+ c: ['d'],
+ d: [],
+ };
+ const result = buildDependencyGraph(diamondGraph);
+ expect(result.transitiveDescendants.a).toEqual(
+ expect.arrayContaining(['b', 'c', 'd']),
+ );
+ expect(result.transitiveDescendants.a).toHaveLength(3);
+ expect(result.transitiveDescendants.b).toEqual(['d']);
+ expect(result.transitiveDescendants.c).toEqual(['d']);
+ expect(result.transitiveDescendants.d).toEqual([]);
+ });
+
+ it('should handle disconnected components in transitiveDescendants', () => {
+ const disconnectedGraph: VariableGraph = {
+ a: ['b'],
+ b: [],
+ x: ['y'],
+ y: [],
+ };
+ const result = buildDependencyGraph(disconnectedGraph);
+ expect(result.transitiveDescendants.a).toEqual(['b']);
+ expect(result.transitiveDescendants.b).toEqual([]);
+ expect(result.transitiveDescendants.x).toEqual(['y']);
+ expect(result.transitiveDescendants.y).toEqual([]);
+ });
+
+ it('should return empty transitiveDescendants for all leaf nodes', () => {
+ const leafOnlyGraph: VariableGraph = {
+ a: [],
+ b: [],
+ c: [],
+ };
+ const result = buildDependencyGraph(leafOnlyGraph);
+ expect(result.transitiveDescendants).toEqual({
+ a: [],
+ b: [],
+ c: [],
+ });
+ });
});
describe('buildDependencies', () => {
diff --git a/frontend/src/container/GridCardLayout/GridCard/index.tsx b/frontend/src/container/GridCardLayout/GridCard/index.tsx
index 8329055896..de5287834c 100644
--- a/frontend/src/container/GridCardLayout/GridCard/index.tsx
+++ b/frontend/src/container/GridCardLayout/GridCard/index.tsx
@@ -6,10 +6,12 @@ import { QueryParams } from 'constants/query';
import { PANEL_TYPES } from 'constants/queryBuilder';
import { populateMultipleResults } from 'container/NewWidget/LeftContainer/WidgetGraph/util';
import { CustomTimeType } from 'container/TopNav/DateTimeSelectionV2/types';
+import { useIsPanelWaitingOnVariable } from 'hooks/dashboard/useVariableFetchState';
import { useGetQueryRange } from 'hooks/queryBuilder/useGetQueryRange';
import { useIntersectionObserver } from 'hooks/useIntersectionObserver';
import { GetQueryResultsProps } from 'lib/dashboard/getQueryResults';
import { getDashboardVariables } from 'lib/dashboardVariables/getDashboardVariables';
+import { getVariableReferencesInQuery } from 'lib/dashboardVariables/variableReference';
import getTimeString from 'lib/getTimeString';
import { isEqual } from 'lodash-es';
import isEmpty from 'lodash-es/isEmpty';
@@ -53,7 +55,6 @@ function GridCardGraph({
customOnRowClick,
customTimeRangeWindowForCoRelation,
enableDrillDown,
- widgetsByDynamicVariableId,
}: GridCardGraphProps): JSX.Element {
const dispatch = useDispatch();
const [errorMessage, setErrorMessage] = useState<string>();
@@ -64,8 +65,8 @@ function GridCardGraph({
toScrollWidgetId,
setToScrollWidgetId,
setDashboardQueryRangeCalled,
- variablesToGetUpdated,
} = useDashboard();
+
const { minTime, maxTime, selectedTime: globalSelectedInterval } = useSelector<
AppState,
GlobalReducer
@@ -117,10 +118,25 @@ function GridCardGraph({
const updatedQuery = widget?.query;
+ const referencedVariableNames = useMemo(() => {
+ if (!variables || !updatedQuery) {
+ return [];
+ }
+ const allNames = Object.values(variables)
+ .map((v) => v.name)
+ .filter((name): name is string => !!name);
+ return getVariableReferencesInQuery(updatedQuery, allNames);
+ }, [updatedQuery, variables]);
+
const isEmptyWidget =
widget?.id === PANEL_TYPES.EMPTY_WIDGET || isEmpty(widget);
- const queryEnabledCondition = isVisible && !isEmptyWidget && isQueryEnabled;
+ const isPanelWaitingOnAnyVariable = useIsPanelWaitingOnVariable(
+ referencedVariableNames,
+ );
+
+ const queryEnabledCondition =
+ isVisible && !isEmptyWidget && isQueryEnabled && !isPanelWaitingOnAnyVariable;
const [requestData, setRequestData] = useState<GetQueryResultsProps>(() => {
if (widget.panelTypes !== PANEL_TYPES.LIST) {
@@ -177,27 +193,6 @@ function GridCardGraph({
[requestData.query],
);
- // Bring back dependency on variable chaining for panels to refetch,
- // but only for non-dynamic variables. We derive a stable token from
- // the head of the variablesToGetUpdated queue when it's non-dynamic.
- const nonDynamicVariableChainToken = useMemo(() => {
- if (!variablesToGetUpdated || variablesToGetUpdated.length === 0) {
- return undefined;
- }
- if (!variables) {
- return undefined;
- }
- const headName = variablesToGetUpdated[0];
- const variableObj = Object.values(variables).find(
- (variable) => variable?.name === headName,
- );
- if (variableObj && variableObj.type !== 'DYNAMIC') {
- return headName;
- }
- return undefined;
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [variablesToGetUpdated, variables]);
-
const queryResponse = useGetQueryRange(
{
...requestData,
@@ -224,11 +219,7 @@ function GridCardGraph({
requestData,
variables
? Object.entries(variables).reduce((acc, [id, variable]) => {
- if (
- variable.type !== 'DYNAMIC' ||
- (widgetsByDynamicVariableId?.[variable.id] &&
- widgetsByDynamicVariableId?.[variable.id].includes(widget.id))
- ) {
+ if (variable.name && referencedVariableNames.includes(variable.name)) {
return { ...acc, [id]: variable.selectedValue };
}
return acc;
@@ -237,9 +228,6 @@ function GridCardGraph({
...(customTimeRange && customTimeRange.startTime && customTimeRange.endTime
? [customTimeRange.startTime, customTimeRange.endTime]
: []),
- // Include non-dynamic variable chaining token to drive refetches
- // only when a non-dynamic variable is at the head of the queue
- ...(nonDynamicVariableChainToken ? [nonDynamicVariableChainToken] : []),
],
retry(failureCount, error): boolean {
if (
@@ -252,7 +240,7 @@ function GridCardGraph({
return failureCount < 2;
},
keepPreviousData: true,
- enabled: queryEnabledCondition && !nonDynamicVariableChainToken,
+ enabled: queryEnabledCondition,
refetchOnMount: false,
onError: (error) => {
const errorMessage =
@@ -318,7 +306,9 @@ function GridCardGraph({
version={version}
threshold={threshold}
headerMenuList={menuList}
- isFetchingResponse={queryResponse.isFetching}
+ isFetchingResponse={
+ queryResponse.isFetching || isPanelWaitingOnAnyVariable
+ }
setRequestData={setRequestData}
onClickHandler={onClickHandler}
onDragSelect={onDragSelect}
diff --git a/frontend/src/container/GridCardLayout/GridCard/types.ts b/frontend/src/container/GridCardLayout/GridCard/types.ts
index b7c3c4c97f..4aaf3fa26a 100644
--- a/frontend/src/container/GridCardLayout/GridCard/types.ts
+++ b/frontend/src/container/GridCardLayout/GridCard/types.ts
@@ -72,7 +72,6 @@ export interface GridCardGraphProps {
customOnRowClick?: (record: RowData) => void;
customTimeRangeWindowForCoRelation?: string | undefined;
enableDrillDown?: boolean;
- widgetsByDynamicVariableId?: Record<string, string[]>;
}
export interface GetGraphVisibilityStateOnLegendClickProps {
diff --git a/frontend/src/container/GridCardLayout/GridCardLayout.tsx b/frontend/src/container/GridCardLayout/GridCardLayout.tsx
index c11c05112e..86ef9f46ac 100644
--- a/frontend/src/container/GridCardLayout/GridCardLayout.tsx
+++ b/frontend/src/container/GridCardLayout/GridCardLayout.tsx
@@ -16,7 +16,6 @@ import { themeColors } from 'constants/theme';
import { DEFAULT_ROW_NAME } from 'container/DashboardContainer/DashboardDescription/utils';
import { useDashboardVariables } from 'hooks/dashboard/useDashboardVariables';
import { useUpdateDashboard } from 'hooks/dashboard/useUpdateDashboard';
-import { useWidgetsByDynamicVariableId } from 'hooks/dashboard/useWidgetsByDynamicVariableId';
import useComponentPermission from 'hooks/useComponentPermission';
import { useIsDarkMode } from 'hooks/useDarkMode';
import { useSafeNavigate } from 'hooks/useSafeNavigate';
@@ -102,8 +101,6 @@ function GraphLayout(props: GraphLayoutProps): JSX.Element {
Record<string, { widgets: Layout[]; collapsed: boolean }>
>({});
- const widgetsByDynamicVariableId = useWidgetsByDynamicVariableId();
-
useEffect(() => {
setCurrentPanelMap(panelMap);
}, [panelMap]);
@@ -617,7 +614,6 @@ function GraphLayout(props: GraphLayoutProps): JSX.Element {
onDragSelect={onDragSelect}
dataAvailable={checkIfDataExists}
enableDrillDown={enableDrillDown}
- widgetsByDynamicVariableId={widgetsByDynamicVariableId}
/>
</Card>
</CardContainer>
diff --git a/frontend/src/hooks/dashboard/__test__/useIsPanelWaitingOnVariable.test.ts b/frontend/src/hooks/dashboard/__test__/useIsPanelWaitingOnVariable.test.ts
new file mode 100644
index 0000000000..814b9f8072
--- /dev/null
+++ b/frontend/src/hooks/dashboard/__test__/useIsPanelWaitingOnVariable.test.ts
@@ -0,0 +1,316 @@
+import { act, renderHook } from '@testing-library/react';
+import { dashboardVariablesStore } from 'providers/Dashboard/store/dashboardVariables/dashboardVariablesStore';
+import { IDashboardVariablesStoreState } from 'providers/Dashboard/store/dashboardVariables/dashboardVariablesStoreTypes';
+import {
+ VariableFetchState,
+ variableFetchStore,
+} from 'providers/Dashboard/store/variableFetchStore';
+import { IDashboardVariable } from 'types/api/dashboard/getAll';
+
+import { useIsPanelWaitingOnVariable } from '../useVariableFetchState';
+
+function makeVariable(
+ overrides: Partial<IDashboardVariable> & { id: string },
+): IDashboardVariable {
+ return {
+ name: overrides.id,
+ description: '',
+ type: 'QUERY',
+ sort: 'DISABLED',
+ multiSelect: false,
+ showALLOption: false,
+ ...overrides,
+ };
+}
+
+function resetStores(): void {
+ variableFetchStore.set(() => ({
+ states: {},
+ lastUpdated: {},
+ cycleIds: {},
+ }));
+ dashboardVariablesStore.set(() => ({
+ dashboardId: '',
+ variables: {},
+ sortedVariablesArray: [],
+ dependencyData: null,
+ variableTypes: {},
+ dynamicVariableOrder: [],
+ }));
+}
+
+function setFetchStates(states: Record<string, VariableFetchState>): void {
+ variableFetchStore.set(() => ({
+ states,
+ lastUpdated: {},
+ cycleIds: {},
+ }));
+}
+
+function setDashboardVariables(
+ overrides: Partial<IDashboardVariablesStoreState>,
+): void {
+ dashboardVariablesStore.set(() => ({
+ dashboardId: '',
+ variables: {},
+ sortedVariablesArray: [],
+ dependencyData: null,
+ variableTypes: {},
+ dynamicVariableOrder: [],
+ ...overrides,
+ }));
+}
+
+describe('useIsPanelWaitingOnVariable', () => {
+ beforeEach(() => {
+ resetStores();
+ });
+
+ it('should return false when variableNames is empty', () => {
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable([]));
+ expect(result.current).toBe(false);
+ });
+
+ it('should return false when all referenced variables are idle', () => {
+ setFetchStates({ a: 'idle', b: 'idle' });
+ setDashboardVariables({
+ variables: {
+ a: makeVariable({ id: 'a', selectedValue: 'val1' }),
+ b: makeVariable({ id: 'b', selectedValue: 'val2' }),
+ },
+ variableTypes: { a: 'QUERY', b: 'QUERY' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['a', 'b']));
+ expect(result.current).toBe(false);
+ });
+
+ it('should return true when a variable is loading with empty selectedValue', () => {
+ setFetchStates({ a: 'loading' });
+ setDashboardVariables({
+ variables: {
+ a: makeVariable({ id: 'a', selectedValue: undefined }),
+ },
+ variableTypes: { a: 'QUERY' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['a']));
+ expect(result.current).toBe(true);
+ });
+
+ it('should return true when a variable is waiting with empty selectedValue', () => {
+ setFetchStates({ a: 'waiting' });
+ setDashboardVariables({
+ variables: {
+ a: makeVariable({ id: 'a', selectedValue: '' }),
+ },
+ variableTypes: { a: 'QUERY' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['a']));
+ expect(result.current).toBe(true);
+ });
+
+ it('should return true when a variable is revalidating with empty selectedValue', () => {
+ setFetchStates({ a: 'revalidating' });
+ setDashboardVariables({
+ variables: {
+ a: makeVariable({ id: 'a', selectedValue: undefined }),
+ },
+ variableTypes: { a: 'QUERY' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['a']));
+ expect(result.current).toBe(true);
+ });
+
+ it('should return false when a variable is loading but has a selectedValue', () => {
+ setFetchStates({ a: 'loading' });
+ setDashboardVariables({
+ variables: {
+ a: makeVariable({ id: 'a', selectedValue: 'some-value' }),
+ },
+ variableTypes: { a: 'QUERY' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['a']));
+ expect(result.current).toBe(false);
+ });
+
+ it('should return true for DYNAMIC variable with allSelected=true that is loading', () => {
+ setFetchStates({ dyn: 'loading' });
+ setDashboardVariables({
+ variables: {
+ dyn: makeVariable({
+ id: 'dyn',
+ type: 'DYNAMIC',
+ selectedValue: 'some-val',
+ allSelected: true,
+ }),
+ },
+ variableTypes: { dyn: 'DYNAMIC' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['dyn']));
+ expect(result.current).toBe(true);
+ });
+
+ it('should return true for DYNAMIC variable with allSelected=true that is waiting', () => {
+ setFetchStates({ dyn: 'waiting' });
+ setDashboardVariables({
+ variables: {
+ dyn: makeVariable({
+ id: 'dyn',
+ type: 'DYNAMIC',
+ selectedValue: 'val',
+ allSelected: true,
+ }),
+ },
+ variableTypes: { dyn: 'DYNAMIC' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['dyn']));
+ expect(result.current).toBe(true);
+ });
+
+ it('should return false for DYNAMIC variable with allSelected=true that is idle', () => {
+ setFetchStates({ dyn: 'idle' });
+ setDashboardVariables({
+ variables: {
+ dyn: makeVariable({
+ id: 'dyn',
+ type: 'DYNAMIC',
+ selectedValue: 'val',
+ allSelected: true,
+ }),
+ },
+ variableTypes: { dyn: 'DYNAMIC' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['dyn']));
+ expect(result.current).toBe(false);
+ });
+
+ it('should return false for non-DYNAMIC variable with allSelected=false and non-empty value that is loading', () => {
+ setFetchStates({ a: 'loading' });
+ setDashboardVariables({
+ variables: {
+ a: makeVariable({
+ id: 'a',
+ selectedValue: 'val',
+ allSelected: false,
+ }),
+ },
+ variableTypes: { a: 'QUERY' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['a']));
+ expect(result.current).toBe(false);
+ });
+
+ it('should return true if any one of multiple variables is blocking', () => {
+ setFetchStates({ a: 'idle', b: 'loading' });
+ setDashboardVariables({
+ variables: {
+ a: makeVariable({ id: 'a', selectedValue: 'val' }),
+ b: makeVariable({ id: 'b', selectedValue: undefined }),
+ },
+ variableTypes: { a: 'QUERY', b: 'QUERY' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['a', 'b']));
+ expect(result.current).toBe(true);
+ });
+
+ it('should return false when variable has no entry in fetch store (treated as idle)', () => {
+ setFetchStates({}); // no state entry for 'a'
+ setDashboardVariables({
+ variables: {
+ a: makeVariable({ id: 'a', selectedValue: 'val' }),
+ },
+ variableTypes: { a: 'QUERY' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['a']));
+ expect(result.current).toBe(false);
+ });
+
+ it('should return false when variable is in error state with empty selectedValue', () => {
+ setFetchStates({ a: 'error' });
+ setDashboardVariables({
+ variables: {
+ a: makeVariable({ id: 'a', selectedValue: undefined }),
+ },
+ variableTypes: { a: 'QUERY' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['a']));
+ expect(result.current).toBe(false);
+ });
+
+ it('should react to store updates', () => {
+ setFetchStates({ a: 'loading' });
+ setDashboardVariables({
+ variables: {
+ a: makeVariable({ id: 'a', selectedValue: undefined }),
+ },
+ variableTypes: { a: 'QUERY' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['a']));
+ expect(result.current).toBe(true);
+
+ // Simulate variable fetch completing
+ act(() => {
+ variableFetchStore.update((d) => {
+ d.states.a = 'idle';
+ });
+ });
+
+ expect(result.current).toBe(false);
+ });
+
+ it('should handle DYNAMIC variable with allSelected=false and empty selectedValue as blocking', () => {
+ setFetchStates({ dyn: 'loading' });
+ setDashboardVariables({
+ variables: {
+ dyn: makeVariable({
+ id: 'dyn',
+ type: 'DYNAMIC',
+ selectedValue: undefined,
+ allSelected: false,
+ }),
+ },
+ variableTypes: { dyn: 'DYNAMIC' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['dyn']));
+ expect(result.current).toBe(true);
+ });
+
+ it('should handle variable with array selectedValue as non-blocking when loading', () => {
+ setFetchStates({ a: 'loading' });
+ setDashboardVariables({
+ variables: {
+ a: makeVariable({ id: 'a', selectedValue: ['val1', 'val2'] }),
+ },
+ variableTypes: { a: 'QUERY' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['a']));
+ expect(result.current).toBe(false);
+ });
+
+ it('should handle variable with empty array selectedValue as blocking when loading', () => {
+ setFetchStates({ a: 'loading' });
+ setDashboardVariables({
+ variables: {
+ a: makeVariable({ id: 'a', selectedValue: [] }),
+ },
+ variableTypes: { a: 'QUERY' },
+ });
+
+ const { result } = renderHook(() => useIsPanelWaitingOnVariable(['a']));
+ expect(result.current).toBe(true);
+ });
+});
diff --git a/frontend/src/providers/Dashboard/types.ts b/frontend/src/providers/Dashboard/types.ts
index 91393ca7c3..c302519828 100644
--- a/frontend/src/providers/Dashboard/types.ts
+++ b/frontend/src/providers/Dashboard/types.ts
@@ -47,7 +47,6 @@ export interface IDashboardContext {
allSelected: boolean,
isDynamic?: boolean,
) => void;
- variablesToGetUpdated?: string[];
dashboardQueryRangeCalled: boolean;
setDashboardQueryRangeCalled: (value: boolean) => void;
selectedRowWidgetId: string | null;