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
fix(global-time): initialize store with default value instead of 0 (#11257)
Vinicius Lourenço3787715fd800
diff --git a/frontend/src/store/globalTime/__tests__/globalTimeStore.test.tsx b/frontend/src/store/globalTime/__tests__/globalTimeStore.test.tsxindex 22013270b7..4e187444a6 100644--- a/frontend/src/store/globalTime/__tests__/globalTimeStore.test.tsx+++ b/frontend/src/store/globalTime/__tests__/globalTimeStore.test.tsx@@ -54,9 +54,23 @@ describe('globalTimeStore', () => {expect(result.current.lastRefreshTimestamp).toBe(0);});- it('should have lastComputedMinMax with default values', () => {+ it('should have lastComputedMinMax computed from initial selectedTime', () => {const { result } = renderHook(() => useGlobalTimeStore());- expect(result.current.lastComputedMinMax).toStrictEqual({+ // Now computes min/max on store creation, no longer starts with 0+ expect(result.current.lastComputedMinMax.minTime).toBeGreaterThan(0);+ expect(result.current.lastComputedMinMax.maxTime).toBeGreaterThan(0);+ });++ it('should not crash with invalid relative time format', () => {+ // Invalid time formats should not crash store creation+ const store = createGlobalTimeStore({+ selectedTime: 'invalid_time' as GlobalTimeSelectedTime,+ });++ // Store should be created successfully+ expect(store.getState().selectedTime).toBe('invalid_time');+ // Should fallback to {0, 0} when parsing fails+ expect(store.getState().lastComputedMinMax).toStrictEqual({minTime: 0,maxTime: 0,});@@ -724,8 +738,8 @@ describe('globalTimeStore', () => {const wrapper = createIsolatedWrapper();const { result } = renderHook(() => useGlobalTime(), { wrapper });- // Initial state has 0 values- expect(result.current.lastComputedMinMax.maxTime).toBe(0);+ // Initial state now has computed values (no longer 0)+ expect(result.current.lastComputedMinMax.maxTime).toBeGreaterThan(0);act(() => {result.current.setSelectedTime('15m');diff --git a/frontend/src/store/globalTime/__tests__/hooks.test.tsx b/frontend/src/store/globalTime/__tests__/hooks.test.tsxindex 0d252c6f9f..f970f4f2e3 100644--- a/frontend/src/store/globalTime/__tests__/hooks.test.tsx+++ b/frontend/src/store/globalTime/__tests__/hooks.test.tsx@@ -134,17 +134,17 @@ describe('useComputedMinMaxSync', () => {jest.useRealTimers();});- it('should compute min/max on mount when store has zero values', () => {+ it('should have computed min/max on store creation (no longer needs mount sync)', () => {const contextStore = createGlobalTimeStore({ selectedTime: '15m' });- expect(contextStore.getState().lastComputedMinMax).toStrictEqual({- minTime: 0,- maxTime: 0,- });+ // Store now computes min/max on creation, not on mount+ expect(contextStore.getState().lastComputedMinMax.minTime).toBeGreaterThan(0);+ expect(contextStore.getState().lastComputedMinMax.maxTime).toBeGreaterThan(0);+ // Hook still works but is a no-op when values already existrenderHook(() => useComputedMinMaxSync(contextStore));- // Should have computed values now+ // Values remain computedexpect(contextStore.getState().lastComputedMinMax.maxTime).toBeGreaterThan(0);expect(contextStore.getState().lastComputedMinMax.minTime).toBeGreaterThan(0);});diff --git a/frontend/src/store/globalTime/globalTimeStore.ts b/frontend/src/store/globalTime/globalTimeStore.tsindex 4ce0453112..86acdebc01 100644--- a/frontend/src/store/globalTime/globalTimeStore.ts+++ b/frontend/src/store/globalTime/globalTimeStore.ts@@ -12,6 +12,7 @@ import {computeRounded5sMinMax,isCustomTimeRange,parseSelectedTime,+ safeParseSelectedTime,} from './utils';export type GlobalTimeStoreApi = StoreApi<GlobalTimeStore>;@@ -40,7 +41,7 @@ export function createGlobalTimeStore(refreshInterval,isRefreshEnabled: computeIsRefreshEnabled(selectedTime, refreshInterval),lastRefreshTimestamp: 0,- lastComputedMinMax: { minTime: 0, maxTime: 0 },+ lastComputedMinMax: safeParseSelectedTime(selectedTime),setSelectedTime: (time: GlobalTimeSelectedTime,diff --git a/frontend/src/store/globalTime/utils.ts b/frontend/src/store/globalTime/utils.tsindex 6ad7f65d41..7fcc5c4da4 100644--- a/frontend/src/store/globalTime/utils.ts+++ b/frontend/src/store/globalTime/utils.ts@@ -61,6 +61,8 @@ const fallbackDurationInNanoSeconds = 30 * 1000 * NANO_SECOND_MULTIPLIER; // 30s* Parse the selectedTime string to get min/max time values.* For relative times, computes fresh values based on Date.now().* For custom times, extracts the stored min/max values.+ *+ * @throws Error - When selectedTime is relativeTime and it's invalid*/export function parseSelectedTime(selectedTime: string): ParsedTimeRange {if (isCustomTimeRange(selectedTime)) {@@ -78,6 +80,18 @@ export function parseSelectedTime(selectedTime: string): ParsedTimeRange {return getMinMaxForSelectedTime(selectedTime as Time, 0, 0);}+/**+ * The {@ref parseSelectedTime} can throw errors, this handles and fallbacks to 0,0 if invalid selected time is provided+ */+export function safeParseSelectedTime(selectedTime: string): ParsedTimeRange {+ try {+ return parseSelectedTime(selectedTime);+ } catch (e) {+ console.error('Error parsing selected time:', e);+ return { minTime: 0, maxTime: 0 };+ }+}+/*** @deprecated Use store.getAutoRefreshQueryKey() instead.* Access via: const getAutoRefreshQueryKey = useGlobalTime((s) => s.getAutoRefreshQueryKey);