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.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
chore: variable store set up (#10145)
Ashwin Bhatkalf497a154a2a7
diff --git a/frontend/package.json b/frontend/package.jsonindex 42e9ef9381..ceed180607 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -105,6 +105,7 @@"i18next": "^21.6.12","i18next-browser-languagedetector": "^6.1.3","i18next-http-backend": "^1.3.2",+ "immer": "11.1.3","jest": "^27.5.1","js-base64": "^3.7.2","less": "^4.1.2",diff --git a/frontend/src/hooks/dashboard/useDashboardVariables.ts b/frontend/src/hooks/dashboard/useDashboardVariables.tsnew file mode 100644index 0000000000..5ceb8b1e1f--- /dev/null+++ b/frontend/src/hooks/dashboard/useDashboardVariables.ts@@ -0,0 +1,19 @@+import { useSyncExternalStore } from 'react';++import {+ dashboardVariablesStore,+ IDashboardVariables,+} from '../../providers/Dashboard/store/dashboardVariablesStore';++export const useDashboardVariables = (): {+ dashboardVariables: IDashboardVariables;+} => {+ const dashboardVariables = useSyncExternalStore(+ dashboardVariablesStore.subscribe,+ dashboardVariablesStore.getSnapshot,+ );++ return {+ dashboardVariables,+ };+};diff --git a/frontend/src/hooks/dashboard/useDashboardVariablesByType.ts b/frontend/src/hooks/dashboard/useDashboardVariablesByType.tsnew file mode 100644index 0000000000..7d1898c56b--- /dev/null+++ b/frontend/src/hooks/dashboard/useDashboardVariablesByType.ts@@ -0,0 +1,30 @@+import { useMemo } from 'react';+import {+ IDashboardVariable,+ TVariableQueryType,+} from 'types/api/dashboard/getAll';++import { useDashboardVariables } from './useDashboardVariables';++export function useDashboardVariablesByType(+ variableType: TVariableQueryType,+ returnType: 'values',+): IDashboardVariable[];+export function useDashboardVariablesByType(+ variableType: TVariableQueryType,+ returnType?: 'entries',+): [string, IDashboardVariable][];+export function useDashboardVariablesByType(+ variableType: TVariableQueryType,+ returnType?: 'values' | 'entries',+): IDashboardVariable[] | [string, IDashboardVariable][] {+ const { dashboardVariables } = useDashboardVariables();++ return useMemo(() => {+ const entries = Object.entries(dashboardVariables || {}).filter(+ (entry): entry is [string, IDashboardVariable] =>+ Boolean(entry[1].name) && entry[1].type === variableType,+ );+ return returnType === 'values' ? entries.map(([, value]) => value) : entries;+ }, [dashboardVariables, variableType, returnType]);+}diff --git a/frontend/src/hooks/dashboard/useWidgetsByDynamicVariableId.ts b/frontend/src/hooks/dashboard/useWidgetsByDynamicVariableId.tsnew file mode 100644index 0000000000..cd30dd306d--- /dev/null+++ b/frontend/src/hooks/dashboard/useWidgetsByDynamicVariableId.ts@@ -0,0 +1,28 @@+import { useMemo } from 'react';+import { PANEL_GROUP_TYPES } from 'constants/queryBuilder';+import { createDynamicVariableToWidgetsMap } from 'hooks/dashboard/utils';+import { useDashboard } from 'providers/Dashboard/Dashboard';+import { Widgets } from 'types/api/dashboard/getAll';++import { useDashboardVariablesByType } from './useDashboardVariablesByType';++/**+ * Hook to get a map of dynamic variable IDs to widget IDs that use them.+ * This is useful for determining which widgets need to be refreshed when a dynamic variable changes.+ */+export function useWidgetsByDynamicVariableId(): Record<string, string[]> {+ const dynamicVariables = useDashboardVariablesByType('DYNAMIC', 'values');+ const { selectedDashboard } = useDashboard();++ return useMemo(() => {+ const widgets =+ selectedDashboard?.data?.widgets?.filter(+ (widget) => widget.panelTypes !== PANEL_GROUP_TYPES.ROW,+ ) || [];++ return createDynamicVariableToWidgetsMap(+ dynamicVariables,+ widgets as Widgets[],+ );+ }, [selectedDashboard, dynamicVariables]);+}diff --git a/frontend/src/providers/Dashboard/Dashboard.tsx b/frontend/src/providers/Dashboard/Dashboard.tsxindex 7b55193f06..a53dfcd792 100644--- a/frontend/src/providers/Dashboard/Dashboard.tsx+++ b/frontend/src/providers/Dashboard/Dashboard.tsx@@ -45,6 +45,8 @@ import APIError from 'types/api/error';import { GlobalReducer } from 'types/reducer/globalTime';import { v4 as generateUUID } from 'uuid';+import { useDashboardVariables } from '../../hooks/dashboard/useDashboardVariables';+import { updateDashboardVariablesStore } from './store/dashboardVariablesStore';import {DashboardSortOrder,IDashboardContext,@@ -196,6 +198,16 @@ export function DashboardProvider({: isDashboardWidgetPage?.params.dashboardId) || '';const [selectedDashboard, setSelectedDashboard] = useState<Dashboard>();+ const dashboardVariables = useDashboardVariables();++ useEffect(() => {+ const existingVariables = dashboardVariables;+ const updatedVariables = selectedDashboard?.data.variables || {};++ if (!isEqual(existingVariables, updatedVariables)) {+ updateDashboardVariablesStore(updatedVariables);+ }+ }, [selectedDashboard]);const {currentDashboard,diff --git a/frontend/src/providers/Dashboard/__tests__/Dashboard.test.tsx b/frontend/src/providers/Dashboard/__tests__/Dashboard.test.tsxindex f57f232b40..42fb399091 100644--- a/frontend/src/providers/Dashboard/__tests__/Dashboard.test.tsx+++ b/frontend/src/providers/Dashboard/__tests__/Dashboard.test.tsx@@ -8,6 +8,7 @@ import ROUTES from 'constants/routes';import { DashboardProvider, useDashboard } from 'providers/Dashboard/Dashboard';import { IDashboardVariable } from 'types/api/dashboard/getAll';+import { useDashboardVariables } from '../../../hooks/dashboard/useDashboardVariables';import { initializeDefaultVariables } from '../initializeDefaultVariables';import { normalizeUrlValueForVariable } from '../normalizeUrlValue';@@ -55,6 +56,7 @@ jest.mock('uuid', () => ({ v4: jest.fn(() => 'mock-uuid') }));function TestComponent(): JSX.Element {const { dashboardResponse, dashboardId, selectedDashboard } = useDashboard();+ const { dashboardVariables } = useDashboardVariables();return (<div>@@ -65,9 +67,7 @@ function TestComponent(): JSX.Element {{dashboardResponse.isFetching.toString()}</div><div data-testid="dashboard-variables">- {selectedDashboard?.data?.variables- ? JSON.stringify(selectedDashboard.data.variables)- : 'null'}+ {dashboardVariables ? JSON.stringify(dashboardVariables) : 'null'}</div><div data-testid="dashboard-data">{selectedDashboard?.data?.title || 'No Title'}diff --git a/frontend/src/providers/Dashboard/store/dashboardVariablesStore.ts b/frontend/src/providers/Dashboard/store/dashboardVariablesStore.tsnew file mode 100644index 0000000000..6f64d310ba--- /dev/null+++ b/frontend/src/providers/Dashboard/store/dashboardVariablesStore.ts@@ -0,0 +1,17 @@+import { IDashboardVariable } from 'types/api/dashboard/getAll';++import createStore from './store';++// export type IDashboardVariables = DashboardData['variables'];+export type IDashboardVariables = Record<string, IDashboardVariable>;++export const dashboardVariablesStore = createStore<IDashboardVariables>({});++export function updateDashboardVariablesStore(+ variables: Partial<IDashboardVariables>,+): void {+ dashboardVariablesStore.update((currentVariables) => ({+ ...currentVariables,+ ...variables,+ }));+}diff --git a/frontend/src/providers/Dashboard/store/store.ts b/frontend/src/providers/Dashboard/store/store.tsnew file mode 100644index 0000000000..f7b150a543--- /dev/null+++ b/frontend/src/providers/Dashboard/store/store.ts@@ -0,0 +1,44 @@+import { produce } from 'immer';+type ListenerFn = () => void;++export default function createStore<T>(+ init: T,+): {+ set: (setter: any) => void;+ update: (updater: (draft: T) => void) => void;+ subscribe: (listener: ListenerFn) => () => void;+ getSnapshot: () => T;+} {+ let listeners: ListenerFn[] = [];+ let state = init;++ function emitChange(): void {+ for (const listener of listeners) {+ listener();+ }+ }++ function set(setter: any): void {+ state = produce(state, setter);+ emitChange();+ }++ function update(updater: (draft: T) => void): void {+ state = produce(state, updater);+ emitChange();+ }++ return {+ set,+ update,+ subscribe(listener: ListenerFn): () => void {+ listeners = [...listeners, listener];+ return (): void => {+ listeners = listeners.filter((l) => l !== listener);+ };+ },+ getSnapshot(): T {+ return state;+ },+ };+}diff --git a/frontend/yarn.lock b/frontend/yarn.lockindex 0b1c649ff1..b3fee19f24 100644--- a/frontend/yarn.lock+++ b/frontend/yarn.lock@@ -12030,6 +12030,11 @@ immediate@~3.0.5:resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b"integrity sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==+ version "11.1.3"+ resolved "https://registry.yarnpkg.com/immer/-/immer-11.1.3.tgz#78681e1deb6cec39753acf04eb16d7576c04f4d6"+ integrity sha512-6jQTc5z0KJFtr1UgFpIL3N9XSC3saRaI9PwWtzM2pSqkNGtiNkYY2OSwkOGDK2XcTRcLb1pi/aNkKZz0nxVH4Q==+immer@^9.0.6:version "9.0.21"resolved "https://registry.yarnpkg.com/immer/-/immer-9.0.21.tgz#1e025ea31a40f24fb064f1fef23e931496330176"