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.

feat(infra-monitoring): persist custom time on drawer across categories (#12038)

Vinicius Lourenço6d0f618a9e7c
diff --git a/frontend/src/__tests__/fields_api.util.ts b/frontend/src/__tests__/fields_api.util.ts
new file mode 100644
index 0000000000..8514940599
--- /dev/null
+++ b/frontend/src/__tests__/fields_api.util.ts
@@ -0,0 +1,20 @@
+import { ENVIRONMENT } from 'constants/env';
+import { server } from 'mocks-server/server';
+import { rest } from 'msw';
+
+export function mockFieldsAPIsWithEmptyResponse(): void {
+ server.use(
+ rest.get(`${ENVIRONMENT.baseURL}/api/v1/fields/keys`, (_, res, ctx) =>
+ res(
+ ctx.status(200),
+ ctx.json({ status: 'success', data: { keys: {}, complete: true } }),
+ ),
+ ),
+ rest.get(`${ENVIRONMENT.baseURL}/api/v1/fields/values`, (_, res, ctx) =>
+ res(
+ ctx.status(200),
+ ctx.json({ status: 'success', data: { values: {}, complete: true } }),
+ ),
+ ),
+ );
+}
diff --git a/frontend/src/container/InfraMonitoringK8sV2/Base/K8sBaseDetails.tsx b/frontend/src/container/InfraMonitoringK8sV2/Base/K8sBaseDetails.tsx
index 4d3ce22432..13db22f17b 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/Base/K8sBaseDetails.tsx
+++ b/frontend/src/container/InfraMonitoringK8sV2/Base/K8sBaseDetails.tsx
@@ -1,148 +1,39 @@
-import React, {
- useCallback,
- useEffect,
- useMemo,
- useRef,
- useState,
-} from 'react';
+import { useCallback, useEffect, useMemo } from 'react';
import { useQuery } from 'react-query';
import { Color, Spacing } from '@signozhq/design-tokens';
-import { Button, Drawer, Tooltip } from 'antd';
-import { ToggleGroupSimple } from '@signozhq/ui/toggle-group';
+import { X } from '@signozhq/icons';
import { Divider } from '@signozhq/ui/divider';
import { Typography } from '@signozhq/ui/typography';
+import { Drawer } from 'antd';
import logEvent from 'api/common/logEvent';
import ErrorContent from 'components/ErrorModal/components/ErrorContent';
import APIError from 'types/api/error';
-import { combineInitialAndUserExpression } from 'components/QueryBuilderV2/QueryV2/QuerySearch/utils';
import { InfraMonitoringEvents } from 'constants/events';
-import { QueryParams } from 'constants/query';
-import {
- initialQueryBuilderFormValuesMap,
- initialQueryState,
-} from 'constants/queryBuilder';
-import ROUTES from 'constants/routes';
-import { DEFAULT_TIME_RANGE } from 'container/TopNav/DateTimeSelectionV2/constants';
-import {
- CustomTimeType,
- Time,
-} from 'container/TopNav/DateTimeSelectionV2/types';
import { useIsDarkMode } from 'hooks/useDarkMode';
-import { GetQueryResultsProps } from 'lib/dashboard/getQueryResults';
-import GetMinMax from 'lib/getMinMax';
-import {
- BarChart,
- ChevronsLeftRight,
- Compass,
- DraftingCompass,
- ScrollText,
- X,
-} from '@signozhq/icons';
-import { isCustomTimeRange, useGlobalTimeStore } from 'store/globalTime';
-import { NANO_SECOND_MULTIPLIER } from 'store/globalTime/utils';
import {
- LogsAggregatorOperator,
- TracesAggregatorOperator,
-} from 'types/common/queryBuilder';
-import { openInNewTab } from 'utils/navigation';
+ GlobalTimeProvider,
+ NANO_SECOND_MULTIPLIER,
+ useGlobalTimeStore,
+} from 'store/globalTime';
-import { InfraMonitoringEntity, VIEW_TYPES } from '../constants';
-import EntityEvents from '../EntityDetailsUtils/EntityEvents';
-import EntityLogs from '../EntityDetailsUtils/EntityLogs';
-import { K8S_ENTITY_LOGS_EXPRESSION_KEY } from '../EntityDetailsUtils/EntityLogs/hooks';
-import EntityMetrics from '../EntityDetailsUtils/EntityMetrics';
-import EntityTraces from '../EntityDetailsUtils/EntityTraces';
-import { K8S_ENTITY_TRACES_EXPRESSION_KEY } from '../EntityDetailsUtils/EntityTraces/hooks';
-import {
- SelectedItemParams,
- useInfraMonitoringEventsFilters,
- useInfraMonitoringLogFilters,
- useInfraMonitoringSelectedItemParams,
- useInfraMonitoringTracesFilters,
- useInfraMonitoringView,
-} from '../hooks';
+import { INFRA_MONITORING_K8S_PARAMS_KEYS } from '../constants';
+import { useInfraMonitoringSelectedItemParams } from '../hooks';
import LoadingContainer from '../LoadingContainer';
-import '../EntityDetailsUtils/entityDetails.styles.scss';
-import { parseAsString, useQueryState } from 'nuqs';
-import {
- EntityCountConfig,
- EntityCountsSection,
-} from './components/EntityCountsSection/EntityCountsSection';
-
-const TimeRangeOffset = 1000000000;
-
-export interface K8sDetailsMetadataConfig<T> {
- label: string;
- getValue: (entity: T) => string | number;
- render?: (value: string | number, entity: T) => React.ReactNode;
-}
-
-export type K8sDetailsCountConfig<T> = EntityCountConfig<T>;
+import K8sBaseDetailsContent from './K8sBaseDetailsContent';
+import { K8sBaseDetailsProps } from './types';
-export interface K8sDetailsFilters {
- filter: { expression: string };
- start: number;
- end: number;
-}
-
-export interface CustomTabRenderProps<T> {
- entity: T;
- timeRange: { startTime: number; endTime: number };
- selectedInterval: Time;
- handleTimeChange: (
- interval: Time | CustomTimeType,
- dateTimeRange?: [number, number],
- ) => void;
-}
-
-export interface CustomTab<T> {
- key: string;
- label: string;
- icon: React.ReactNode;
- render: (props: CustomTabRenderProps<T>) => React.ReactNode;
-}
+import '../EntityDetailsUtils/entityDetails.styles.scss';
-export interface K8sBaseDetailsProps<T> {
- category: InfraMonitoringEntity;
- eventCategory: string;
- // Data fetching configuration
- getSelectedItemExpression: (params: SelectedItemParams) => string;
- fetchEntityData: (
- filters: K8sDetailsFilters,
- signal?: AbortSignal,
- ) => Promise<{ data: T | null; error?: APIError | null }>;
- // Entity configuration
- getEntityName: (entity: T) => string;
- getInitialLogTracesExpression: (entity: T) => string;
- getInitialEventsExpression: (entity: T) => string;
- metadataConfig: K8sDetailsMetadataConfig<T>[];
- countsConfig?: K8sDetailsCountConfig<T>[];
- getCountsFilterExpression?: (entity: T) => string;
- entityWidgetInfo: {
- title: string;
- yAxisUnit: string;
- docPath?: string;
- }[];
- getEntityQueryPayload: (
- entity: T,
- start: number,
- end: number,
- dotMetricsEnabled: boolean,
- ) => GetQueryResultsProps[];
- queryKeyPrefix: string;
- /** When true, only metrics are shown and the Metrics/Logs/Traces/Events tab bar is hidden. */
- hideDetailViewTabs?: boolean;
- tabsConfig?: {
- showMetrics?: boolean;
- showLogs?: boolean;
- showTraces?: boolean;
- showEvents?: boolean;
- };
- customTabs?: Array<CustomTab<T>>;
-}
+export type {
+ CustomTab,
+ CustomTabRenderProps,
+ K8sBaseDetailsProps,
+ K8sDetailsCountConfig,
+ K8sDetailsFilters,
+ K8sDetailsMetadataConfig,
+} from './types';
-// eslint-disable-next-line sonarjs/cognitive-complexity
export default function K8sBaseDetails<T>({
category,
eventCategory,
@@ -163,7 +54,6 @@ export default function K8sBaseDetails<T>({
}: K8sBaseDetailsProps<T>): JSX.Element {
const selectedTime = useGlobalTimeStore((s) => s.selectedTime);
const getMinMaxTime = useGlobalTimeStore((s) => s.getMinMaxTime);
- const lastComputedMinMax = useGlobalTimeStore((s) => s.lastComputedMinMax);
const getAutoRefreshQueryKey = useGlobalTimeStore(
(s) => s.getAutoRefreshQueryKey,
);
@@ -237,86 +127,9 @@ export default function K8sBaseDetails<T>({
const entityName = entity ? getEntityName(entity) : '';
- // Content state (previously in K8sBaseDetailsContent)
- const tabVisibility = useMemo(
- () => ({
- showMetrics: true,
- showLogs: true,
- showTraces: true,
- showEvents: true,
- ...tabsConfig,
- }),
- [tabsConfig],
- );
-
- const { startMs, endMs } = useMemo(
- () => ({
- startMs: Math.floor(lastComputedMinMax.minTime / NANO_SECOND_MULTIPLIER),
- endMs: Math.floor(lastComputedMinMax.maxTime / NANO_SECOND_MULTIPLIER),
- }),
- [lastComputedMinMax],
- );
-
- const [modalTimeRange, setModalTimeRange] = useState(() => ({
- startTime: startMs,
- endTime: endMs,
- }));
-
- // TODO(h4ad): Remove this and use context/zustand
- const lastSelectedInterval = useRef<Time | null>(null);
- const [selectedInterval, setSelectedInterval] = useState<Time>(
- lastSelectedInterval.current
- ? lastSelectedInterval.current
- : isCustomTimeRange(selectedTime)
- ? DEFAULT_TIME_RANGE
- : selectedTime,
- );
-
- const [selectedView, setSelectedView] = useInfraMonitoringView();
- const effectiveView = hideDetailViewTabs ? VIEW_TYPES.METRICS : selectedView;
-
- const validTabs = useMemo(() => {
- const tabs: string[] = [];
- if (tabVisibility.showMetrics) {
- tabs.push(VIEW_TYPES.METRICS);
- }
- if (tabVisibility.showLogs) {
- tabs.push(VIEW_TYPES.LOGS);
- }
- if (tabVisibility.showTraces) {
- tabs.push(VIEW_TYPES.TRACES);
- }
- if (tabVisibility.showEvents) {
- tabs.push(VIEW_TYPES.EVENTS);
- }
- if (customTabs) {
- tabs.push(...customTabs.map((t) => t.key));
- }
- return tabs;
- }, [tabVisibility, customTabs]);
-
- useEffect(() => {
- if (!hideDetailViewTabs && !validTabs.includes(selectedView)) {
- const firstValid = validTabs[0] || VIEW_TYPES.METRICS;
- void setSelectedView(firstValid);
- }
- }, [hideDetailViewTabs, selectedView, validTabs, setSelectedView]);
-
- const [, setLogFiltersParam] = useInfraMonitoringLogFilters();
- const [, setTracesFiltersParam] = useInfraMonitoringTracesFilters();
- const [, setEventsFiltersParam] = useInfraMonitoringEventsFilters();
- const [userLogsExpression] = useQueryState(
- K8S_ENTITY_LOGS_EXPRESSION_KEY,
- parseAsString,
- );
- const [userTracesExpression] = useQueryState(
- K8S_ENTITY_TRACES_EXPRESSION_KEY,
- parseAsString,
- );
-
useEffect(() => {
if (entity) {
- logEvent(InfraMonitoringEvents.PageVisited, {
+ void logEvent(InfraMonitoringEvents.PageVisited, {
entity: InfraMonitoringEvents.K8sEntity,
page: InfraMonitoringEvents.DetailedPage,
category: eventCategory,
@@ -324,136 +137,6 @@ export default function K8sBaseDetails<T>({
}
}, [entity, eventCategory]);
- useEffect(() => {
- const currentSelectedInterval = lastSelectedInterval.current || selectedTime;
- if (!isCustomTimeRange(currentSelectedInterval)) {
- setSelectedInterval(currentSelectedInterval);
- const { minTime, maxTime } = getMinMaxTime();
-
- setModalTimeRange({
- startTime: Math.floor(minTime / TimeRangeOffset),
- endTime: Math.floor(maxTime / TimeRangeOffset),
- });
- }
- }, [getMinMaxTime, selectedTime]);
-
- const handleTabChange = (value: string | null): void => {
- if (!value) {
- return;
- }
- setSelectedView(value);
- setLogFiltersParam(null);
- setTracesFiltersParam(null);
- setEventsFiltersParam(null);
- logEvent(InfraMonitoringEvents.TabChanged, {
- entity: InfraMonitoringEvents.K8sEntity,
- page: InfraMonitoringEvents.DetailedPage,
- category: eventCategory,
- view: value,
- });
- };
-
- const handleTimeChange = useCallback(
- (interval: Time | CustomTimeType, dateTimeRange?: [number, number]): void => {
- lastSelectedInterval.current = interval as Time;
- setSelectedInterval(interval as Time);
-
- if (interval === 'custom' && dateTimeRange) {
- setModalTimeRange({
- startTime: Math.floor(dateTimeRange[0] / 1000),
- endTime: Math.floor(dateTimeRange[1] / 1000),
- });
- } else {
- const { maxTime, minTime } = GetMinMax(interval);
-
- setModalTimeRange({
- startTime: Math.floor(minTime / TimeRangeOffset),
- endTime: Math.floor(maxTime / TimeRangeOffset),
- });
- }
-
- logEvent(InfraMonitoringEvents.TimeUpdated, {
- entity: InfraMonitoringEvents.K8sEntity,
- page: InfraMonitoringEvents.DetailedPage,
- category: eventCategory,
- interval,
- view: effectiveView,
- });
- },
- [eventCategory, effectiveView],
- );
-
- const handleExplorePagesRedirect = (): void => {
- const urlQuery = new URLSearchParams();
-
- if (selectedInterval !== 'custom') {
- urlQuery.set(QueryParams.relativeTime, selectedInterval);
- } else {
- urlQuery.delete(QueryParams.relativeTime);
- urlQuery.set(QueryParams.startTime, modalTimeRange.startTime.toString());
- urlQuery.set(QueryParams.endTime, modalTimeRange.endTime.toString());
- }
-
- logEvent(InfraMonitoringEvents.ExploreClicked, {
- entity: InfraMonitoringEvents.K8sEntity,
- page: InfraMonitoringEvents.DetailedPage,
- category: eventCategory,
- view: selectedView,
- });
-
- if (selectedView === VIEW_TYPES.LOGS) {
- const fullExpression = combineInitialAndUserExpression(
- logsAndTracesInitialExpression,
- userLogsExpression || '',
- );
-
- const compositeQuery = {
- ...initialQueryState,
- queryType: 'builder',
- builder: {
- ...initialQueryState.builder,
- queryData: [
- {
- ...initialQueryBuilderFormValuesMap.logs,
- aggregateOperator: LogsAggregatorOperator.NOOP,
- expression: fullExpression,
- filter: { expression: fullExpression },
- },
- ],
- },
- };
-
- urlQuery.set('compositeQuery', JSON.stringify(compositeQuery));
-
- openInNewTab(`${ROUTES.LOGS_EXPLORER}?${urlQuery.toString()}`);
- } else if (selectedView === VIEW_TYPES.TRACES) {
- const fullExpression = combineInitialAndUserExpression(
- logsAndTracesInitialExpression,
- userTracesExpression || '',
- );
-
- const compositeQuery = {
- ...initialQueryState,
- queryType: 'builder',
- builder: {
- ...initialQueryState.builder,
- queryData: [
- {
- ...initialQueryBuilderFormValuesMap.traces,
- aggregateOperator: TracesAggregatorOperator.NOOP,
- expression: fullExpression,
- filter: { expression: fullExpression },
- },
- ],
- },
- };
-
- urlQuery.set('compositeQuery', JSON.stringify(compositeQuery));
-
- openInNewTab(`${ROUTES.TRACES_EXPLORER}?${urlQuery.toString()}`);
- }
- };
-
return (
<Drawer
width="70%"
@@ -498,207 +181,33 @@ export default function K8sBaseDetails<T>({
</div>
)}
{entity && !isEntityLoading && !hasResponseError && (
- <>
- <div className="entity-detail-drawer__entity">
- <div className="entity-details-grid">
- <div className="labels-row">
- {metadataConfig.map((config) => (
- <Typography.Text
- key={config.label}
- color="muted"
- className="entity-details-metadata-label"
- >
- {config.label}
- </Typography.Text>
- ))}
- </div>
-
- <div className="values-row">
- {metadataConfig.map((config) => {
- const value = config.getValue(entity);
- const displayValue = String(value);
- return (
- <Typography.Text
- key={config.label}
- className="entity-details-metadata-value"
- >
- {config.render ? (
- config.render(value, entity)
- ) : (
- <Tooltip title={displayValue}>{displayValue}</Tooltip>
- )}
- </Typography.Text>
- );
- })}
- </div>
- </div>
-
- {countsConfig &&
- countsConfig.length > 0 &&
- selectedItem &&
- getCountsFilterExpression && (
- <EntityCountsSection
- entity={entity}
- countsConfig={countsConfig}
- selectedItem={selectedItem}
- filterExpression={getCountsFilterExpression(entity)}
- closeDrawer={handleClose}
- />
- )}
- </div>
-
- {!hideDetailViewTabs && (
- <div className="views-tabs-container">
- <ToggleGroupSimple
- type="single"
- className="views-tabs"
- onChange={handleTabChange}
- value={selectedView}
- items={[
- ...(tabVisibility.showMetrics
- ? [
- {
- value: VIEW_TYPES.METRICS,
- label: (
- <div className="view-title">
- <BarChart size={14} />
- Metrics
- </div>
- ),
- },
- ]
- : []),
- ...(tabVisibility.showLogs
- ? [
- {
- value: VIEW_TYPES.LOGS,
- label: (
- <div className="view-title">
- <ScrollText size={14} />
- Logs
- </div>
- ),
- },
- ]
- : []),
- ...(tabVisibility.showTraces
- ? [
- {
- value: VIEW_TYPES.TRACES,
- label: (
- <div className="view-title">
- <DraftingCompass size={14} />
- Traces
- </div>
- ),
- },
- ]
- : []),
- ...(tabVisibility.showEvents
- ? [
- {
- value: VIEW_TYPES.EVENTS,
- label: (
- <div className="view-title">
- <ChevronsLeftRight size={14} />
- Events
- </div>
- ),
- },
- ]
- : []),
- ...(customTabs?.map((tab) => ({
- value: tab.key,
- label: (
- <div className="view-title">
- {tab.icon}
- {tab.label}
- </div>
- ),
- })) ?? []),
- ]}
- />
-
- {selectedView === VIEW_TYPES.LOGS && (
- <Tooltip title="Go to Logs Explorer" placement="left">
- <Button
- icon={<Compass size={18} />}
- className="compass-button"
- onClick={handleExplorePagesRedirect}
- />
- </Tooltip>
- )}
- {selectedView === VIEW_TYPES.TRACES && (
- <Tooltip title="Go to Traces Explorer" placement="left">
- <Button
- icon={<Compass size={18} />}
- className="compass-button"
- onClick={handleExplorePagesRedirect}
- />
- </Tooltip>
- )}
- </div>
- )}
-
- {effectiveView === VIEW_TYPES.METRICS && (
- <EntityMetrics<T>
- entity={entity}
- selectedInterval={selectedInterval}
- timeRange={modalTimeRange}
- handleTimeChange={handleTimeChange}
- isModalTimeSelection
- entityWidgetInfo={entityWidgetInfo}
- getEntityQueryPayload={getEntityQueryPayload}
- category={category}
- queryKey={`${queryKeyPrefix}Metrics`}
- />
- )}
- {effectiveView === VIEW_TYPES.LOGS && (
- <EntityLogs
- timeRange={modalTimeRange}
- isModalTimeSelection
- handleTimeChange={handleTimeChange}
- selectedInterval={selectedInterval}
- queryKey={`${queryKeyPrefix}Logs`}
- category={category}
- initialExpression={logsAndTracesInitialExpression}
- />
- )}
- {effectiveView === VIEW_TYPES.TRACES && (
- <EntityTraces
- timeRange={modalTimeRange}
- isModalTimeSelection
- handleTimeChange={handleTimeChange}
- selectedInterval={selectedInterval}
- queryKey={`${queryKeyPrefix}Traces`}
- category={category}
- initialExpression={logsAndTracesInitialExpression}
- />
- )}
- {effectiveView === VIEW_TYPES.EVENTS && tabVisibility.showEvents && (
- <EntityEvents
- timeRange={modalTimeRange}
- isModalTimeSelection
- handleTimeChange={handleTimeChange}
- selectedInterval={selectedInterval}
- category={category}
- queryKey={`${queryKeyPrefix}Events`}
- initialExpression={eventsInitialExpression}
- />
- )}
- {customTabs?.map((tab) =>
- selectedView === tab.key ? (
- <React.Fragment key={tab.key}>
- {tab.render({
- entity,
- timeRange: modalTimeRange,
- selectedInterval,
- handleTimeChange,
- })}
- </React.Fragment>
- ) : null,
- )}
- </>
+ <GlobalTimeProvider
+ inheritGlobalTime
+ enableUrlParams={{
+ relativeTimeKey: INFRA_MONITORING_K8S_PARAMS_KEYS.DETAIL_RELATIVE_TIME,
+ startTimeKey: INFRA_MONITORING_K8S_PARAMS_KEYS.DETAIL_START_TIME,
+ endTimeKey: INFRA_MONITORING_K8S_PARAMS_KEYS.DETAIL_END_TIME,
+ }}
+ >
+ <K8sBaseDetailsContent<T>
+ entity={entity}
+ category={category}
+ eventCategory={eventCategory}
+ metadataConfig={metadataConfig}
+ countsConfig={countsConfig}
+ getCountsFilterExpression={getCountsFilterExpression}
+ selectedItem={selectedItem}
+ handleClose={handleClose}
+ entityWidgetInfo={entityWidgetInfo}
+ getEntityQueryPayload={getEntityQueryPayload}
+ queryKeyPrefix={queryKeyPrefix}
+ hideDetailViewTabs={hideDetailViewTabs}
+ tabsConfig={tabsConfig}
+ customTabs={customTabs}
+ logsAndTracesInitialExpression={logsAndTracesInitialExpression}
+ eventsInitialExpression={eventsInitialExpression}
+ />
+ </GlobalTimeProvider>
)}
</Drawer>
);
diff --git a/frontend/src/container/InfraMonitoringK8sV2/Base/K8sBaseDetailsContent.tsx b/frontend/src/container/InfraMonitoringK8sV2/Base/K8sBaseDetailsContent.tsx
new file mode 100644
index 0000000000..c865181c31
--- /dev/null
+++ b/frontend/src/container/InfraMonitoringK8sV2/Base/K8sBaseDetailsContent.tsx
@@ -0,0 +1,401 @@
+import { Fragment, useEffect, useMemo } from 'react';
+import {
+ BarChart,
+ ChevronsLeftRight,
+ Compass,
+ DraftingCompass,
+ ScrollText,
+} from '@signozhq/icons';
+import { ToggleGroupSimple } from '@signozhq/ui/toggle-group';
+import { Typography } from '@signozhq/ui/typography';
+import { Button, Tooltip } from 'antd';
+import logEvent from 'api/common/logEvent';
+import { combineInitialAndUserExpression } from 'components/QueryBuilderV2/QueryV2/QuerySearch/utils';
+import { InfraMonitoringEvents } from 'constants/events';
+import { QueryParams } from 'constants/query';
+import {
+ initialQueryBuilderFormValuesMap,
+ initialQueryState,
+} from 'constants/queryBuilder';
+import ROUTES from 'constants/routes';
+import { parseAsString, useQueryState } from 'nuqs';
+import {
+ LogsAggregatorOperator,
+ TracesAggregatorOperator,
+} from 'types/common/queryBuilder';
+import { openInNewTab } from 'utils/navigation';
+
+import { VIEW_TYPES } from '../constants';
+import { useEntityDetailsTime } from '../EntityDetailsUtils/EntityDateTimeSelector/useEntityDetailsTime';
+import EntityEvents from '../EntityDetailsUtils/EntityEvents';
+import EntityLogs from '../EntityDetailsUtils/EntityLogs';
+import { K8S_ENTITY_LOGS_EXPRESSION_KEY } from '../EntityDetailsUtils/EntityLogs/hooks';
+import EntityMetrics from '../EntityDetailsUtils/EntityMetrics';
+import EntityTraces from '../EntityDetailsUtils/EntityTraces';
+import { K8S_ENTITY_TRACES_EXPRESSION_KEY } from '../EntityDetailsUtils/EntityTraces/hooks';
+import {
+ useInfraMonitoringEventsFilters,
+ useInfraMonitoringLogFilters,
+ useInfraMonitoringTracesFilters,
+ useInfraMonitoringView,
+} from '../hooks';
+
+import { EntityCountsSection } from './components/EntityCountsSection/EntityCountsSection';
+import { K8sBaseDetailsContentProps } from './types';
+
+// eslint-disable-next-line sonarjs/cognitive-complexity
+export default function K8sBaseDetailsContent<T>({
+ entity,
+ category,
+ eventCategory,
+ metadataConfig,
+ countsConfig,
+ getCountsFilterExpression,
+ selectedItem,
+ handleClose,
+ entityWidgetInfo,
+ getEntityQueryPayload,
+ queryKeyPrefix,
+ hideDetailViewTabs,
+ tabsConfig,
+ customTabs,
+ logsAndTracesInitialExpression,
+ eventsInitialExpression,
+}: K8sBaseDetailsContentProps<T>): JSX.Element {
+ const { timeRange, selectedInterval, handleTimeChange } =
+ useEntityDetailsTime();
+
+ const tabVisibility = useMemo(
+ () => ({
+ showMetrics: true,
+ showLogs: true,
+ showTraces: true,
+ showEvents: true,
+ ...tabsConfig,
+ }),
+ [tabsConfig],
+ );
+
+ const [selectedView, setSelectedView] = useInfraMonitoringView();
+
+ const validTabs = useMemo(() => {
+ const tabs: string[] = [];
+ if (tabVisibility.showMetrics) {
+ tabs.push(VIEW_TYPES.METRICS);
+ }
+ if (tabVisibility.showLogs) {
+ tabs.push(VIEW_TYPES.LOGS);
+ }
+ if (tabVisibility.showTraces) {
+ tabs.push(VIEW_TYPES.TRACES);
+ }
+ if (tabVisibility.showEvents) {
+ tabs.push(VIEW_TYPES.EVENTS);
+ }
+ if (customTabs) {
+ tabs.push(...customTabs.map((t) => t.key));
+ }
+ return tabs;
+ }, [tabVisibility, customTabs]);
+
+ useEffect(() => {
+ if (!hideDetailViewTabs && !validTabs.includes(selectedView)) {
+ const firstValid = validTabs[0] || VIEW_TYPES.METRICS;
+ void setSelectedView(firstValid);
+ }
+ }, [hideDetailViewTabs, selectedView, validTabs, setSelectedView]);
+
+ const effectiveView = hideDetailViewTabs ? VIEW_TYPES.METRICS : selectedView;
+
+ const [, setLogFiltersParam] = useInfraMonitoringLogFilters();
+ const [, setTracesFiltersParam] = useInfraMonitoringTracesFilters();
+ const [, setEventsFiltersParam] = useInfraMonitoringEventsFilters();
+ const [userLogsExpression] = useQueryState(
+ K8S_ENTITY_LOGS_EXPRESSION_KEY,
+ parseAsString,
+ );
+ const [userTracesExpression] = useQueryState(
+ K8S_ENTITY_TRACES_EXPRESSION_KEY,
+ parseAsString,
+ );
+
+ const handleTabChange = (value: string | null): void => {
+ if (!value) {
+ return;
+ }
+ void setSelectedView(value);
+ void setLogFiltersParam(null);
+ void setTracesFiltersParam(null);
+ void setEventsFiltersParam(null);
+ void logEvent(InfraMonitoringEvents.TabChanged, {
+ entity: InfraMonitoringEvents.K8sEntity,
+ page: InfraMonitoringEvents.DetailedPage,
+ category: eventCategory,
+ view: value,
+ });
+ };
+
+ const handleExplorePagesRedirect = (): void => {
+ const urlQuery = new URLSearchParams();
+
+ if (selectedInterval !== 'custom') {
+ urlQuery.set(QueryParams.relativeTime, selectedInterval);
+ } else {
+ urlQuery.delete(QueryParams.relativeTime);
+ // Explorer URL params are in milliseconds, timeRange is in seconds
+ urlQuery.set(QueryParams.startTime, (timeRange.startTime * 1000).toString());
+ urlQuery.set(QueryParams.endTime, (timeRange.endTime * 1000).toString());
+ }
+
+ void logEvent(InfraMonitoringEvents.ExploreClicked, {
+ entity: InfraMonitoringEvents.K8sEntity,
+ page: InfraMonitoringEvents.DetailedPage,
+ category: eventCategory,
+ view: selectedView,
+ });
+
+ if (selectedView === VIEW_TYPES.LOGS) {
+ const fullExpression = combineInitialAndUserExpression(
+ logsAndTracesInitialExpression,
+ userLogsExpression || '',
+ );
+
+ const compositeQuery = {
+ ...initialQueryState,
+ queryType: 'builder',
+ builder: {
+ ...initialQueryState.builder,
+ queryData: [
+ {
+ ...initialQueryBuilderFormValuesMap.logs,
+ aggregateOperator: LogsAggregatorOperator.NOOP,
+ expression: fullExpression,
+ filter: { expression: fullExpression },
+ },
+ ],
+ },
+ };
+
+ urlQuery.set('compositeQuery', JSON.stringify(compositeQuery));
+
+ openInNewTab(`${ROUTES.LOGS_EXPLORER}?${urlQuery.toString()}`);
+ } else if (selectedView === VIEW_TYPES.TRACES) {
+ const fullExpression = combineInitialAndUserExpression(
+ logsAndTracesInitialExpression,
+ userTracesExpression || '',
+ );
+
+ const compositeQuery = {
+ ...initialQueryState,
+ queryType: 'builder',
+ builder: {
+ ...initialQueryState.builder,
+ queryData: [
+ {
+ ...initialQueryBuilderFormValuesMap.traces,
+ aggregateOperator: TracesAggregatorOperator.NOOP,
+ expression: fullExpression,
+ filter: { expression: fullExpression },
+ },
+ ],
+ },
+ };
+
+ urlQuery.set('compositeQuery', JSON.stringify(compositeQuery));
+
+ openInNewTab(`${ROUTES.TRACES_EXPLORER}?${urlQuery.toString()}`);
+ }
+ };
+
+ return (
+ <>
+ <div className="entity-detail-drawer__entity">
+ <div className="entity-details-grid">
+ <div className="labels-row">
+ {metadataConfig.map((config) => (
+ <Typography.Text
+ key={config.label}
+ color="muted"
+ className="entity-details-metadata-label"
+ >
+ {config.label}
+ </Typography.Text>
+ ))}
+ </div>
+
+ <div className="values-row">
+ {metadataConfig.map((config) => {
+ const value = config.getValue(entity);
+ const displayValue = String(value);
+ return (
+ <Typography.Text
+ key={config.label}
+ className="entity-details-metadata-value"
+ >
+ {config.render ? (
+ config.render(value, entity)
+ ) : (
+ <Tooltip title={displayValue}>{displayValue}</Tooltip>
+ )}
+ </Typography.Text>
+ );
+ })}
+ </div>
+ </div>
+
+ {countsConfig &&
+ countsConfig.length > 0 &&
+ selectedItem &&
+ getCountsFilterExpression && (
+ <EntityCountsSection
+ entity={entity}
+ countsConfig={countsConfig}
+ selectedItem={selectedItem}
+ filterExpression={getCountsFilterExpression(entity)}
+ closeDrawer={handleClose}
+ />
+ )}
+ </div>
+
+ {!hideDetailViewTabs && (
+ <div className="views-tabs-container">
+ <ToggleGroupSimple
+ type="single"
+ className="views-tabs"
+ onChange={handleTabChange}
+ value={selectedView}
+ items={[
+ ...(tabVisibility.showMetrics
+ ? [
+ {
+ value: VIEW_TYPES.METRICS,
+ label: (
+ <div className="view-title">
+ <BarChart size={14} />
+ Metrics
+ </div>
+ ),
+ },
+ ]
+ : []),
+ ...(tabVisibility.showLogs
+ ? [
+ {
+ value: VIEW_TYPES.LOGS,
+ label: (
+ <div className="view-title">
+ <ScrollText size={14} />
+ Logs
+ </div>
+ ),
+ },
+ ]
+ : []),
+ ...(tabVisibility.showTraces
+ ? [
+ {
+ value: VIEW_TYPES.TRACES,
+ label: (
+ <div className="view-title">
+ <DraftingCompass size={14} />
+ Traces
+ </div>
+ ),
+ },
+ ]
+ : []),
+ ...(tabVisibility.showEvents
+ ? [
+ {
+ value: VIEW_TYPES.EVENTS,
+ label: (
+ <div className="view-title">
+ <ChevronsLeftRight size={14} />
+ Events
+ </div>
+ ),
+ },
+ ]
+ : []),
+ ...(customTabs?.map((tab) => ({
+ value: tab.key,
+ label: (
+ <div className="view-title">
+ {tab.icon}
+ {tab.label}
+ </div>
+ ),
+ })) ?? []),
+ ]}
+ />
+
+ {selectedView === VIEW_TYPES.LOGS && (
+ <Tooltip title="Go to Logs Explorer" placement="left">
+ <Button
+ icon={<Compass size={18} />}
+ className="compass-button"
+ onClick={handleExplorePagesRedirect}
+ />
+ </Tooltip>
+ )}
+ {selectedView === VIEW_TYPES.TRACES && (
+ <Tooltip title="Go to Traces Explorer" placement="left">
+ <Button
+ icon={<Compass size={18} />}
+ className="compass-button"
+ onClick={handleExplorePagesRedirect}
+ />
+ </Tooltip>
+ )}
+ </div>
+ )}
+
+ {effectiveView === VIEW_TYPES.METRICS && (
+ <EntityMetrics<T>
+ entity={entity}
+ eventEntity={eventCategory}
+ entityWidgetInfo={entityWidgetInfo}
+ getEntityQueryPayload={getEntityQueryPayload}
+ category={category}
+ queryKey={`${queryKeyPrefix}Metrics`}
+ />
+ )}
+ {effectiveView === VIEW_TYPES.LOGS && (
+ <EntityLogs
+ eventEntity={eventCategory}
+ queryKey={`${queryKeyPrefix}Logs`}
+ category={category}
+ initialExpression={logsAndTracesInitialExpression}
+ />
+ )}
+ {effectiveView === VIEW_TYPES.TRACES && (
+ <EntityTraces
+ eventEntity={eventCategory}
+ queryKey={`${queryKeyPrefix}Traces`}
+ category={category}
+ initialExpression={logsAndTracesInitialExpression}
+ />
+ )}
+ {effectiveView === VIEW_TYPES.EVENTS && tabVisibility.showEvents && (
+ <EntityEvents
+ eventEntity={eventCategory}
+ queryKey={`${queryKeyPrefix}Events`}
+ initialExpression={eventsInitialExpression}
+ category={category}
+ />
+ )}
+ {customTabs?.map((tab) =>
+ selectedView === tab.key ? (
+ <Fragment key={tab.key}>
+ {tab.render({
+ entity,
+ timeRange,
+ selectedInterval,
+ handleTimeChange,
+ })}
+ </Fragment>
+ ) : null,
+ )}
+ </>
+ );
+}
diff --git a/frontend/src/container/InfraMonitoringK8sV2/Base/K8sHeader.tsx b/frontend/src/container/InfraMonitoringK8sV2/Base/K8sHeader.tsx
index 631c92a5ec..5c63c7af41 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/Base/K8sHeader.tsx
+++ b/frontend/src/container/InfraMonitoringK8sV2/Base/K8sHeader.tsx
@@ -245,6 +245,7 @@ function K8sHeader<TData>({
showAutoRefresh={showAutoRefresh}
showRefreshText={false}
hideShareModal
+ defaultRelativeTime="30m"
/>
</div>
diff --git a/frontend/src/container/InfraMonitoringK8sV2/Base/components/EntityCountsSection/EntityCountsSection.tsx b/frontend/src/container/InfraMonitoringK8sV2/Base/components/EntityCountsSection/EntityCountsSection.tsx
index 9972a7569b..5196a35326 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/Base/components/EntityCountsSection/EntityCountsSection.tsx
+++ b/frontend/src/container/InfraMonitoringK8sV2/Base/components/EntityCountsSection/EntityCountsSection.tsx
@@ -52,7 +52,6 @@ export function EntityCountsSection<T>({
},
};
- // TODO(H4ad): After https://github.com/SigNoz/signoz/pull/12038, inherit custom time of drawer to list
const urlParams = new URLSearchParams();
urlParams.set(INFRA_MONITORING_K8S_PARAMS_KEYS.CATEGORY, targetCategory);
urlParams.set(
@@ -60,6 +59,44 @@ export function EntityCountsSection<T>({
encodeURIComponent(JSON.stringify(compositeQuery)),
);
+ const currentSearchParams = new URLSearchParams(window.location.search);
+ const detailRelativeTime = currentSearchParams.get(
+ INFRA_MONITORING_K8S_PARAMS_KEYS.DETAIL_RELATIVE_TIME,
+ );
+ const detailStartTime = currentSearchParams.get(
+ INFRA_MONITORING_K8S_PARAMS_KEYS.DETAIL_START_TIME,
+ );
+ const detailEndTime = currentSearchParams.get(
+ INFRA_MONITORING_K8S_PARAMS_KEYS.DETAIL_END_TIME,
+ );
+
+ const listRelativeTime = currentSearchParams.get(QueryParams.relativeTime);
+ const listStartTime = currentSearchParams.get(QueryParams.startTime);
+ const listEndTime = currentSearchParams.get(QueryParams.endTime);
+
+ if (listRelativeTime) {
+ urlParams.set(QueryParams.relativeTime, listRelativeTime);
+ } else if (listStartTime && listEndTime) {
+ urlParams.set(QueryParams.startTime, listStartTime);
+ urlParams.set(QueryParams.endTime, listEndTime);
+ }
+
+ if (detailRelativeTime) {
+ urlParams.set(
+ INFRA_MONITORING_K8S_PARAMS_KEYS.DETAIL_RELATIVE_TIME,
+ detailRelativeTime,
+ );
+ } else if (detailStartTime && detailEndTime) {
+ urlParams.set(
+ INFRA_MONITORING_K8S_PARAMS_KEYS.DETAIL_START_TIME,
+ detailStartTime,
+ );
+ urlParams.set(
+ INFRA_MONITORING_K8S_PARAMS_KEYS.DETAIL_END_TIME,
+ detailEndTime,
+ );
+ }
+
return `${ROUTES.INFRASTRUCTURE_MONITORING_KUBERNETES}?${urlParams.toString()}`;
};
diff --git a/frontend/src/container/InfraMonitoringK8sV2/Base/types.ts b/frontend/src/container/InfraMonitoringK8sV2/Base/types.ts
index e7e82c8af1..999e57d11c 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/Base/types.ts
+++ b/frontend/src/container/InfraMonitoringK8sV2/Base/types.ts
@@ -1,3 +1,17 @@
+import { ReactNode } from 'react';
+import {
+ CustomTimeType,
+ Time,
+} from 'container/TopNav/DateTimeSelectionV2/types';
+import { GetQueryResultsProps } from 'lib/dashboard/getQueryResults';
+import APIError from 'types/api/error';
+
+import { EntityCountConfig } from './components/EntityCountsSection/EntityCountsSection';
+import { InfraMonitoringEntity } from '../constants';
+import { SelectedItemParams } from '../hooks';
+
+export type K8sDetailsCountConfig<T> = EntityCountConfig<T>;
+
export type K8sBaseFilters = {
filter: {
expression: string;
@@ -33,3 +47,100 @@ export type K8sTableRowData<T> = T & {
/** Metadata about which attributes were used for grouping */
groupedByMeta?: Record<string, string>;
};
+
+export interface K8sDetailsMetadataConfig<T> {
+ label: string;
+ getValue: (entity: T) => string | number;
+ render?: (value: string | number, entity: T) => ReactNode;
+}
+
+export interface K8sDetailsFilters {
+ filter: { expression: string };
+ start: number;
+ end: number;
+}
+
+export interface K8sDetailsWidgetInfo {
+ title: string;
+ yAxisUnit: string;
+}
+
+export type GetEntityQueryPayload<T> = (
+ entity: T,
+ start: number,
+ end: number,
+ dotMetricsEnabled: boolean,
+) => GetQueryResultsProps[];
+
+export interface K8sDetailsTabsConfig {
+ showMetrics?: boolean;
+ showLogs?: boolean;
+ showTraces?: boolean;
+ showEvents?: boolean;
+}
+
+export interface K8sDetailsCustomTabRenderProps<T> {
+ entity: T;
+ /** Time range in seconds — see useEntityDetailsTime */
+ timeRange: { startTime: number; endTime: number };
+ selectedInterval: Time;
+ handleTimeChange: (
+ interval: Time | CustomTimeType,
+ dateTimeRange?: [number, number],
+ ) => void;
+}
+
+export interface K8sDetailsCustomTab<T> {
+ key: string;
+ label: string;
+ icon: ReactNode;
+ render: (props: K8sDetailsCustomTabRenderProps<T>) => ReactNode;
+}
+
+export interface K8sBaseDetailsProps<T> {
+ category: InfraMonitoringEntity;
+ eventCategory: string;
+ // Data fetching configuration
+ getSelectedItemExpression: (params: SelectedItemParams) => string;
+ fetchEntityData: (
+ filters: K8sDetailsFilters,
+ signal?: AbortSignal,
+ ) => Promise<{ data: T | null; error?: APIError | null }>;
+ // Entity configuration
+ getEntityName: (entity: T) => string;
+ getInitialLogTracesExpression: (entity: T) => string;
+ getInitialEventsExpression: (entity: T) => string;
+ metadataConfig: K8sDetailsMetadataConfig<T>[];
+ countsConfig?: K8sDetailsCountConfig<T>[];
+ getCountsFilterExpression?: (entity: T) => string;
+ entityWidgetInfo: K8sDetailsWidgetInfo[];
+ getEntityQueryPayload: GetEntityQueryPayload<T>;
+ queryKeyPrefix: string;
+ /** When true, only metrics are shown and the Metrics/Logs/Traces/Events tab bar is hidden. */
+ hideDetailViewTabs?: boolean;
+ tabsConfig?: K8sDetailsTabsConfig;
+ customTabs?: K8sDetailsCustomTab<T>[];
+}
+
+export interface K8sBaseDetailsContentProps<T> {
+ entity: T;
+ category: InfraMonitoringEntity;
+ eventCategory: string;
+ metadataConfig: K8sDetailsMetadataConfig<T>[];
+ countsConfig?: K8sDetailsCountConfig<T>[];
+ getCountsFilterExpression?: (entity: T) => string;
+ selectedItem: string | null;
+ handleClose: () => void;
+ entityWidgetInfo: K8sDetailsWidgetInfo[];
+ getEntityQueryPayload: GetEntityQueryPayload<T>;
+ queryKeyPrefix: string;
+ hideDetailViewTabs: boolean;
+ tabsConfig?: K8sDetailsTabsConfig;
+ customTabs?: K8sDetailsCustomTab<T>[];
+ logsAndTracesInitialExpression: string;
+ eventsInitialExpression: string;
+}
+
+// Aliases for backward compatibility
+export type CustomTab<T> = K8sDetailsCustomTab<T>;
+export type CustomTabRenderProps<T> = K8sDetailsCustomTabRenderProps<T>;
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityDateTimeSelector/EntityDateTimeSelector.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityDateTimeSelector/EntityDateTimeSelector.tsx
new file mode 100644
index 0000000000..b8a98b6555
--- /dev/null
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityDateTimeSelector/EntityDateTimeSelector.tsx
@@ -0,0 +1,78 @@
+import { useCallback } from 'react';
+import { Undo } from '@signozhq/icons';
+import { Button } from '@signozhq/ui/button';
+import { TooltipSimple } from '@signozhq/ui/tooltip';
+import logEvent from 'api/common/logEvent';
+import { InfraMonitoringEvents } from 'constants/events';
+import { InfraMonitoringEntity } from 'container/InfraMonitoringK8sV2/constants';
+import DateTimeSelectionV2 from 'container/TopNav/DateTimeSelectionV2';
+import {
+ CustomTimeType,
+ Time,
+} from 'container/TopNav/DateTimeSelectionV2/types';
+
+import { useEntityDetailsTime } from './useEntityDetailsTime';
+
+interface EntityDateTimeSelectorProps {
+ eventEntity: string;
+ category: InfraMonitoringEntity;
+ view: string;
+}
+
+function EntityDateTimeSelector({
+ eventEntity,
+ category,
+ view,
+}: EntityDateTimeSelectorProps): JSX.Element {
+ const {
+ timeRange,
+ selectedInterval,
+ handleTimeChange,
+ handleResetToParentTime,
+ hasTimeChanged,
+ } = useEntityDetailsTime();
+
+ const onTimeChange = useCallback(
+ (interval: Time | CustomTimeType, dateTimeRange?: [number, number]): void => {
+ void logEvent(InfraMonitoringEvents.TimeUpdated, {
+ entity: eventEntity,
+ page: InfraMonitoringEvents.DetailedPage,
+ category,
+ view,
+ interval,
+ });
+ handleTimeChange(interval, dateTimeRange);
+ },
+ [category, view, eventEntity, handleTimeChange],
+ );
+
+ return (
+ <div className="entity-date-time-selector">
+ {hasTimeChanged && (
+ <TooltipSimple title="Reset to list time" side="bottom">
+ <Button
+ variant="outlined"
+ color="secondary"
+ onClick={handleResetToParentTime}
+ data-testid="reset-to-list-time-button"
+ prefix={<Undo size={14} />}
+ >
+ Reset
+ </Button>
+ </TooltipSimple>
+ )}
+ <DateTimeSelectionV2
+ showAutoRefresh
+ showRefreshText={false}
+ hideShareModal
+ isModalTimeSelection
+ onTimeChange={onTimeChange}
+ modalSelectedInterval={selectedInterval}
+ modalInitialStartTime={timeRange.startTime * 1000}
+ modalInitialEndTime={timeRange.endTime * 1000}
+ />
+ </div>
+ );
+}
+
+export default EntityDateTimeSelector;
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityDateTimeSelector/useEntityDetailsTime.ts b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityDateTimeSelector/useEntityDetailsTime.ts
new file mode 100644
index 0000000000..ebd33c6788
--- /dev/null
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityDateTimeSelector/useEntityDetailsTime.ts
@@ -0,0 +1,95 @@
+import { useCallback, useMemo } from 'react';
+import {
+ CustomTimeType,
+ Time,
+} from 'container/TopNav/DateTimeSelectionV2/types';
+import {
+ createCustomTimeRange,
+ isCustomTimeRange,
+ NANO_SECOND_MULTIPLIER,
+ useGlobalTime,
+} from 'store/globalTime';
+
+export interface EntityDetailsTimeRange {
+ /** Unix timestamp in seconds — the unit API payload builders expect. Multiply by 1000 for ms-based UI (modals, URL params). */
+ startTime: number;
+ /** Unix timestamp in seconds — the unit API payload builders expect. Multiply by 1000 for ms-based UI (modals, URL params). */
+ endTime: number;
+}
+
+export interface UseEntityDetailsTimeResult {
+ /** Time range in seconds */
+ timeRange: EntityDetailsTimeRange;
+ selectedInterval: Time;
+ handleTimeChange: (
+ interval: Time | CustomTimeType,
+ dateTimeRange?: [number, number],
+ ) => void;
+ handleResetToParentTime: () => boolean;
+ canResetToParent: boolean;
+ hasTimeChanged: boolean;
+}
+
+export function useEntityDetailsTime(): UseEntityDetailsTimeResult {
+ const selectedTime = useGlobalTime((s) => s.selectedTime);
+ const lastComputedMinMax = useGlobalTime((s) => s.lastComputedMinMax);
+ const setSelectedTime = useGlobalTime((s) => s.setSelectedTime);
+ const handleResetToParentTime = useGlobalTime((s) => s.resetToParentTime);
+ const parentStore = useGlobalTime((s) => s.parentStore);
+
+ const canResetToParent = !!parentStore;
+
+ const hasTimeChanged = useMemo(() => {
+ if (!parentStore) {
+ return false;
+ }
+ return selectedTime !== parentStore.getState().selectedTime;
+ }, [parentStore, selectedTime]);
+
+ const timeRange = useMemo<EntityDetailsTimeRange>(
+ () => ({
+ startTime: Math.floor(
+ lastComputedMinMax.minTime / NANO_SECOND_MULTIPLIER / 1000,
+ ),
+ endTime: Math.floor(
+ lastComputedMinMax.maxTime / NANO_SECOND_MULTIPLIER / 1000,
+ ),
+ }),
+ [lastComputedMinMax],
+ );
+
+ const selectedInterval = useMemo<Time>(
+ () =>
+ isCustomTimeRange(selectedTime)
+ ? ('custom' as Time)
+ : (selectedTime as Time),
+ [selectedTime],
+ );
+
+ const handleTimeChange = useCallback(
+ (interval: Time | CustomTimeType, dateTimeRange?: [number, number]): void => {
+ if (interval === 'custom' && dateTimeRange) {
+ // DateTimeSelector delivers milliseconds; the store keys custom
+ // ranges by nanoseconds.
+ setSelectedTime(
+ createCustomTimeRange(
+ dateTimeRange[0] * NANO_SECOND_MULTIPLIER,
+ dateTimeRange[1] * NANO_SECOND_MULTIPLIER,
+ ),
+ );
+ } else {
+ setSelectedTime(interval as Time);
+ }
+ },
+ [setSelectedTime],
+ );
+
+ return {
+ timeRange,
+ selectedInterval,
+ handleTimeChange,
+ handleResetToParentTime,
+ canResetToParent,
+ hasTimeChanged,
+ };
+}
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityEvents/EntityEvents.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityEvents/EntityEvents.tsx
index 5a2552143d..2a80e059de 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityEvents/EntityEvents.tsx
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityEvents/EntityEvents.tsx
@@ -21,17 +21,14 @@ import Controls from 'container/Controls';
import { InfraMonitoringEntity } from 'container/InfraMonitoringK8sV2/constants';
import LoadingContainer from 'container/InfraMonitoringK8sV2/LoadingContainer';
import RunQueryBtn from 'container/QueryBuilder/components/RunQueryBtn/RunQueryBtn';
-import DateTimeSelectionV2 from 'container/TopNav/DateTimeSelectionV2';
-import {
- CustomTimeType,
- Time,
-} from 'container/TopNav/DateTimeSelectionV2/types';
import { ChevronDown, ChevronRight } from '@signozhq/icons';
import { useQueryState } from 'nuqs';
import { DataSource } from 'types/common/queryBuilder';
import { parseAsJsonNoValidate } from 'utils/nuqsParsers';
import { validateQuery } from 'utils/queryValidationUtils';
+import EntityDateTimeSelector from '../EntityDateTimeSelector/EntityDateTimeSelector';
+import { useEntityDetailsTime } from '../EntityDateTimeSelector/useEntityDetailsTime';
import EntityEmptyState from '../EntityEmptyState/EntityEmptyState';
import EntityError from '../EntityError/EntityError';
import { EventContents } from './EventsContent';
@@ -53,16 +50,7 @@ interface EventDataType {
}
interface Props {
- timeRange: {
- startTime: number;
- endTime: number;
- };
- isModalTimeSelection: boolean;
- handleTimeChange: (
- interval: Time | CustomTimeType,
- dateTimeRange?: [number, number],
- ) => void;
- selectedInterval: Time;
+ eventEntity: string;
queryKey: string;
category: InfraMonitoringEntity;
initialExpression: string;
@@ -77,13 +65,11 @@ const handleExpandRow = (record: EventDataType): JSX.Element => (
);
function EntityEventsContent({
- timeRange,
- isModalTimeSelection,
- handleTimeChange,
- selectedInterval,
+ eventEntity,
queryKey,
category,
}: Omit<Props, 'initialExpression'>): JSX.Element {
+ const { timeRange } = useEntityDetailsTime();
const expression = useExpression();
const inputExpression = useInputExpression();
const userExpression = useUserExpression();
@@ -131,8 +117,8 @@ function EntityEventsContent({
if (validation.isValid) {
querySearchOnRun(newUserExpression || '');
- logEvent(InfraMonitoringEvents.FilterApplied, {
- entity: InfraMonitoringEvents.K8sEntity,
+ void logEvent(InfraMonitoringEvents.FilterApplied, {
+ entity: eventEntity,
page: InfraMonitoringEvents.DetailedPage,
category,
view: InfraMonitoringEvents.EventsView,
@@ -141,7 +127,14 @@ function EntityEventsContent({
refetch();
}
},
- [inputExpression, initialExpression, refetch, querySearchOnRun, category],
+ [
+ inputExpression,
+ initialExpression,
+ refetch,
+ querySearchOnRun,
+ category,
+ eventEntity,
+ ],
);
const queryData = useMemo(
@@ -229,16 +222,10 @@ function EntityEventsContent({
<div className={styles.container}>
<div className={styles.filterContainer}>
<div className={styles.filterContainerTime}>
- <DateTimeSelectionV2
- showAutoRefresh
- showRefreshText={false}
- hideShareModal
- isModalTimeSelection={isModalTimeSelection}
- onTimeChange={handleTimeChange}
- defaultRelativeTime="5m"
- modalSelectedInterval={selectedInterval}
- modalInitialStartTime={timeRange.startTime * 1000}
- modalInitialEndTime={timeRange.endTime * 1000}
+ <EntityDateTimeSelector
+ eventEntity={eventEntity}
+ category={category}
+ view={InfraMonitoringEvents.EventsView}
/>
<RunQueryBtn
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityEvents/__tests__/EntityEvents.test.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityEvents/__tests__/EntityEvents.test.tsx
index 056ebc51d4..92a3a41523 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityEvents/__tests__/EntityEvents.test.tsx
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityEvents/__tests__/EntityEvents.test.tsx
@@ -29,25 +29,25 @@ function verifyEntityEventsV5Request({
expect(orderKeys).toContain('timestamp');
}
-jest.mock('container/TopNav/DateTimeSelectionV2/index.tsx', () => ({
+jest.mock('../../EntityDateTimeSelector/EntityDateTimeSelector', () => ({
__esModule: true,
- default: ({
- onTimeChange,
- }: {
- onTimeChange?: (interval: string, dateTimeRange?: [number, number]) => void;
- }): JSX.Element => (
- <button
- type="button"
- data-testid="mock-datetime-selection"
- onClick={(): void => {
- onTimeChange?.('5m');
- }}
- >
- Select Time
- </button>
+ default: (): JSX.Element => (
+ <div data-testid="mock-datetime-selection">Date Time</div>
),
}));
+jest.mock('../../EntityDateTimeSelector/useEntityDetailsTime', () => ({
+ useEntityDetailsTime: (): {
+ timeRange: { startTime: number; endTime: number };
+ selectedInterval: string;
+ handleTimeChange: jest.Mock;
+ } => ({
+ timeRange: { startTime: 1, endTime: 2 },
+ selectedInterval: '5m',
+ handleTimeChange: jest.fn(),
+ }),
+}));
+
describe('EntityEvents', () => {
let capturedQueryRangePayloads: QueryRangePayloadV5[] = [];
@@ -69,10 +69,7 @@ describe('EntityEvents', () => {
searchParams={`${K8S_ENTITY_EVENTS_EXPRESSION_KEY}=k8s.pod.name+%3D+%22x%22`}
>
<EntityEvents
- timeRange={{ startTime: 1, endTime: 2 }}
- isModalTimeSelection={false}
- handleTimeChange={jest.fn()}
- selectedInterval="5m"
+ eventEntity="test"
queryKey="test"
category={InfraMonitoringEntity.PODS}
initialExpression='k8s.pod.name = "x"'
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityEvents/__tests__/EntityEvents.timeRange.test.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityEvents/__tests__/EntityEvents.timeRange.test.tsx
new file mode 100644
index 0000000000..0f08097fb7
--- /dev/null
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityEvents/__tests__/EntityEvents.timeRange.test.tsx
@@ -0,0 +1,81 @@
+import { mockFieldsAPIsWithEmptyResponse } from '__tests__/fields_api.util';
+import { mockQueryRangeV5WithEventsResponse } from '__tests__/query_range_v5.util';
+import { InfraMonitoringEntity } from 'container/InfraMonitoringK8sV2/constants';
+import { NuqsTestingAdapter } from 'nuqs/adapters/testing';
+import {
+ createCustomTimeRange,
+ createGlobalTimeStore,
+ GlobalTimeContext,
+ NANO_SECOND_MULTIPLIER,
+} from 'store/globalTime';
+import { act, render, waitFor } from 'tests/test-utils';
+import { QueryRangePayloadV5 } from 'types/api/v5/queryRange';
+
+import EntityEvents from '../EntityEvents';
+import { K8S_ENTITY_EVENTS_EXPRESSION_KEY } from '../hooks';
+
+jest.mock('../../EntityDateTimeSelector/EntityDateTimeSelector', () => ({
+ __esModule: true,
+ default: (): JSX.Element => (
+ <div data-testid="mock-datetime-selection">Date Time</div>
+ ),
+}));
+
+const START_MS = 1705315200000; // 2024-01-15T10:40:00Z
+const END_MS = 1705318800000; // 2024-01-15T11:40:00Z
+
+describe('EntityEvents time range wiring', () => {
+ let capturedPayloads: QueryRangePayloadV5[] = [];
+
+ beforeEach(() => {
+ capturedPayloads = [];
+ mockQueryRangeV5WithEventsResponse({
+ onReceiveRequest: async (req) => {
+ const body = (await req.json()) as QueryRangePayloadV5;
+ capturedPayloads.push(body);
+ return {};
+ },
+ });
+ mockFieldsAPIsWithEmptyResponse();
+ });
+
+ it('should query the V5 API with the global time range converted to milliseconds', async () => {
+ const store = createGlobalTimeStore();
+ store
+ .getState()
+ .setSelectedTime(
+ createCustomTimeRange(
+ START_MS * NANO_SECOND_MULTIPLIER,
+ END_MS * NANO_SECOND_MULTIPLIER,
+ ),
+ );
+
+ const expression = 'k8s.pod.name = "test-pod"';
+
+ act(() => {
+ render(
+ <GlobalTimeContext.Provider value={store}>
+ <NuqsTestingAdapter
+ searchParams={`${K8S_ENTITY_EVENTS_EXPRESSION_KEY}=${encodeURIComponent(
+ expression,
+ )}`}
+ >
+ <EntityEvents
+ eventEntity="test"
+ queryKey="test"
+ category={InfraMonitoringEntity.PODS}
+ initialExpression={expression}
+ />
+ </NuqsTestingAdapter>
+ </GlobalTimeContext.Provider>,
+ );
+ });
+
+ await waitFor(() => {
+ expect(capturedPayloads).toHaveLength(1);
+ });
+
+ expect(capturedPayloads[0].start).toBe(START_MS);
+ expect(capturedPayloads[0].end).toBe(END_MS);
+ });
+});
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityLogs/EntityLogs.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityLogs/EntityLogs.tsx
index 6f2524e8ae..9a4190ccba 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityLogs/EntityLogs.tsx
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityLogs/EntityLogs.tsx
@@ -25,11 +25,6 @@ import { InfraMonitoringEntity } from 'container/InfraMonitoringK8sV2/constants'
import { LogsLoading } from 'container/LogsLoading/LogsLoading';
import { FontSize } from 'container/OptionsMenu/types';
import RunQueryBtn from 'container/QueryBuilder/components/RunQueryBtn/RunQueryBtn';
-import DateTimeSelectionV2 from 'container/TopNav/DateTimeSelectionV2';
-import {
- CustomTimeType,
- Time,
-} from 'container/TopNav/DateTimeSelectionV2/types';
import { getOldLogsOperatorFromNew } from 'hooks/logs/useActiveLog';
import useLogDetailHandlers from 'hooks/logs/useLogDetailHandlers';
import useScrollToLog from 'hooks/logs/useScrollToLog';
@@ -38,6 +33,8 @@ import { ILog } from 'types/api/logs/log';
import { DataSource } from 'types/common/queryBuilder';
import { validateQuery } from 'utils/queryValidationUtils';
+import EntityDateTimeSelector from '../EntityDateTimeSelector/EntityDateTimeSelector';
+import { useEntityDetailsTime } from '../EntityDateTimeSelector/useEntityDetailsTime';
import EntityEmptyState from '../EntityEmptyState/EntityEmptyState';
import EntityError from '../EntityError/EntityError';
import { isKeyNotFoundError } from '../utils';
@@ -47,29 +44,18 @@ import { getEntityLogsQueryPayload } from './utils';
import styles from './EntityLogs.module.scss';
interface Props {
- timeRange: {
- startTime: number;
- endTime: number;
- };
- isModalTimeSelection: boolean;
- handleTimeChange: (
- interval: Time | CustomTimeType,
- dateTimeRange?: [number, number],
- ) => void;
- selectedInterval: Time;
+ eventEntity: string;
queryKey: string;
category: InfraMonitoringEntity;
initialExpression: string;
}
function EntityLogsContent({
- timeRange,
- isModalTimeSelection,
- handleTimeChange,
- selectedInterval,
+ eventEntity,
queryKey,
category,
}: Omit<Props, 'initialExpression'>): JSX.Element {
+ const { timeRange } = useEntityDetailsTime();
const virtuosoRef = useRef<VirtuosoHandle>(null);
const expression = useExpression();
@@ -134,7 +120,7 @@ function EntityLogsContent({
if (validation.isValid) {
querySearchOnRun(newUserExpression);
- logEvent(InfraMonitoringEvents.FilterApplied, {
+ void logEvent(InfraMonitoringEvents.FilterApplied, {
entity: InfraMonitoringEvents.K8sEntity,
page: InfraMonitoringEvents.DetailedPage,
category,
@@ -239,16 +225,10 @@ function EntityLogsContent({
<div className={styles.container}>
<div className={styles.filterContainer}>
<div className={styles.filterContainerTime}>
- <DateTimeSelectionV2
- showAutoRefresh
- showRefreshText={false}
- hideShareModal
- isModalTimeSelection={isModalTimeSelection}
- onTimeChange={handleTimeChange}
- defaultRelativeTime="5m"
- modalSelectedInterval={selectedInterval}
- modalInitialStartTime={timeRange.startTime * 1000}
- modalInitialEndTime={timeRange.endTime * 1000}
+ <EntityDateTimeSelector
+ eventEntity={eventEntity}
+ category={category}
+ view={InfraMonitoringEvents.LogsView}
/>
<RunQueryBtn
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityLogs/__tests__/EntityLogs.test.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityLogs/__tests__/EntityLogs.test.tsx
index c55c18b60c..560352ba0b 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityLogs/__tests__/EntityLogs.test.tsx
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityLogs/__tests__/EntityLogs.test.tsx
@@ -50,25 +50,25 @@ jest.mock(
},
);
-jest.mock('container/TopNav/DateTimeSelectionV2/index.tsx', () => ({
+jest.mock('../../EntityDateTimeSelector/EntityDateTimeSelector', () => ({
__esModule: true,
- default: ({
- onTimeChange,
- }: {
- onTimeChange?: (interval: string, dateTimeRange?: [number, number]) => void;
- }): JSX.Element => (
- <button
- type="button"
- data-testid="mock-datetime-selection"
- onClick={(): void => {
- onTimeChange?.('5m');
- }}
- >
- Select Time
- </button>
+ default: (): JSX.Element => (
+ <div data-testid="mock-datetime-selection">Date Time</div>
),
}));
+jest.mock('../../EntityDateTimeSelector/useEntityDetailsTime', () => ({
+ useEntityDetailsTime: (): {
+ timeRange: { startTime: number; endTime: number };
+ selectedInterval: string;
+ handleTimeChange: jest.Mock;
+ } => ({
+ timeRange: { startTime: 1, endTime: 2 },
+ selectedInterval: '5m',
+ handleTimeChange: jest.fn(),
+ }),
+}));
+
describe('EntityLogs', () => {
let capturedQueryRangePayloads: QueryRangePayloadV5[] = [];
const itemHeight = 100;
@@ -94,10 +94,7 @@ describe('EntityLogs', () => {
>
<VirtuosoMockContext.Provider value={{ viewportHeight: 500, itemHeight }}>
<EntityLogs
- timeRange={{ startTime: 1, endTime: 2 }}
- isModalTimeSelection={false}
- handleTimeChange={jest.fn()}
- selectedInterval="5m"
+ eventEntity="test"
queryKey="test"
category={InfraMonitoringEntity.PODS}
initialExpression='k8s.pod.name = "x"'
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityLogs/__tests__/EntityLogs.timeRange.test.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityLogs/__tests__/EntityLogs.timeRange.test.tsx
new file mode 100644
index 0000000000..b335b59487
--- /dev/null
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityLogs/__tests__/EntityLogs.timeRange.test.tsx
@@ -0,0 +1,93 @@
+import { mockFieldsAPIsWithEmptyResponse } from '__tests__/fields_api.util';
+import { mockQueryRangeV5WithLogsResponse } from '__tests__/query_range_v5.util';
+import { InfraMonitoringEntity } from 'container/InfraMonitoringK8sV2/constants';
+import { NuqsTestingAdapter } from 'nuqs/adapters/testing';
+import {
+ createCustomTimeRange,
+ createGlobalTimeStore,
+ GlobalTimeContext,
+ NANO_SECOND_MULTIPLIER,
+} from 'store/globalTime';
+import { act, render, waitFor } from 'tests/test-utils';
+import { QueryRangePayloadV5 } from 'types/api/v5/queryRange';
+
+import EntityLogs from '../EntityLogs';
+import { K8S_ENTITY_LOGS_EXPRESSION_KEY } from '../hooks';
+
+jest.mock(
+ 'components/OverlayScrollbar/OverlayScrollbar',
+ () =>
+ function MockOverlayScrollbar({
+ children,
+ }: {
+ children: React.ReactNode;
+ }): JSX.Element {
+ return <div>{children}</div>;
+ },
+);
+
+jest.mock('../../EntityDateTimeSelector/EntityDateTimeSelector', () => ({
+ __esModule: true,
+ default: (): JSX.Element => (
+ <div data-testid="mock-datetime-selection">Date Time</div>
+ ),
+}));
+
+const START_MS = 1705315200000; // 2024-01-15T10:40:00Z
+const END_MS = 1705318800000; // 2024-01-15T11:40:00Z
+
+describe('EntityLogs time range wiring', () => {
+ let capturedPayloads: QueryRangePayloadV5[] = [];
+
+ beforeEach(() => {
+ capturedPayloads = [];
+ mockQueryRangeV5WithLogsResponse({
+ onReceiveRequest: async (req) => {
+ const body = (await req.json()) as QueryRangePayloadV5;
+ capturedPayloads.push(body);
+ return {};
+ },
+ });
+ mockFieldsAPIsWithEmptyResponse();
+ });
+
+ it('should query the V5 API with the global time range converted to milliseconds', async () => {
+ const store = createGlobalTimeStore();
+ store
+ .getState()
+ .setSelectedTime(
+ createCustomTimeRange(
+ START_MS * NANO_SECOND_MULTIPLIER,
+ END_MS * NANO_SECOND_MULTIPLIER,
+ ),
+ );
+
+ const expression = 'k8s.pod.name = "test-pod"';
+
+ act(() => {
+ render(
+ <GlobalTimeContext.Provider value={store}>
+ <NuqsTestingAdapter
+ searchParams={`${K8S_ENTITY_LOGS_EXPRESSION_KEY}=${encodeURIComponent(
+ expression,
+ )}`}
+ >
+ <EntityLogs
+ eventEntity="test"
+ queryKey="test"
+ category={InfraMonitoringEntity.PODS}
+ initialExpression={expression}
+ />
+ </NuqsTestingAdapter>
+ </GlobalTimeContext.Provider>,
+ );
+ });
+
+ await waitFor(() => {
+ expect(capturedPayloads.length).toBeGreaterThanOrEqual(1);
+ });
+
+ expect(capturedPayloads[0].start).toBe(START_MS);
+ expect(capturedPayloads[0].end).toBe(END_MS);
+ });
+});
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityMetrics/EntityMetrics.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityMetrics/EntityMetrics.tsx
index 2b158861fb..f49e99556b 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityMetrics/EntityMetrics.tsx
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityMetrics/EntityMetrics.tsx
@@ -2,15 +2,11 @@ import { useCallback, useMemo, useRef } from 'react';
import { UseQueryResult } from 'react-query';
import { Skeleton } from 'antd';
import cx from 'classnames';
+import { InfraMonitoringEvents } from 'constants/events';
import { PANEL_TYPES } from 'constants/queryBuilder';
import TimeSeries from 'container/DashboardContainer/visualization/charts/TimeSeries/TimeSeries';
import { LegendPosition } from 'lib/uPlotV2/components/types';
import { InfraMonitoringEntity } from 'container/InfraMonitoringK8sV2/constants';
-import DateTimeSelectionV2 from 'container/TopNav/DateTimeSelectionV2';
-import {
- CustomTimeType,
- Time,
-} from 'container/TopNav/DateTimeSelectionV2/types';
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
import { useIsDarkMode } from 'hooks/useDarkMode';
import { useResizeObserver } from 'hooks/useDimensions';
@@ -24,25 +20,17 @@ import { getMetricsExplorerUrl } from 'utils/explorerUtils';
import { buildEntityMetricsChartConfig } from './configBuilder';
import ChartHeader from './ChartHeader';
+import EntityDateTimeSelector from '../EntityDateTimeSelector/EntityDateTimeSelector';
+import { useEntityDetailsTime } from '../EntityDateTimeSelector/useEntityDetailsTime';
import { useEntityMetrics } from './hooks';
import { isKeyNotFoundError } from '../utils';
import styles from './EntityMetrics.module.scss';
import { MetricsTable } from './MetricsTable';
-import { DEFAULT_TIME_RANGE } from 'container/TopNav/DateTimeSelectionV2/constants';
interface EntityMetricsProps<T> {
- timeRange: {
- startTime: number;
- endTime: number;
- };
- isModalTimeSelection: boolean;
- handleTimeChange: (
- interval: Time | CustomTimeType,
- dateTimeRange?: [number, number],
- ) => void;
- selectedInterval: Time;
entity: T;
+ eventEntity: string;
entityWidgetInfo: {
title: string;
yAxisUnit: string;
@@ -59,16 +47,16 @@ interface EntityMetricsProps<T> {
}
function EntityMetrics<T>({
- selectedInterval,
entity,
- timeRange,
- handleTimeChange,
- isModalTimeSelection,
+ eventEntity,
entityWidgetInfo,
getEntityQueryPayload,
queryKey,
category,
}: EntityMetricsProps<T>): JSX.Element {
+ const { timeRange, selectedInterval, handleTimeChange } =
+ useEntityDetailsTime();
+
const { visibilities, setElement } = useMultiIntersectionObserver(
entityWidgetInfo.length,
{ threshold: 0.1 },
@@ -91,10 +79,8 @@ function EntityMetrics<T>({
const onDragSelect = useCallback(
(start: number, end: number): void => {
- const startTimestamp = Math.trunc(start);
- const endTimestamp = Math.trunc(end);
-
- handleTimeChange('custom', [startTimestamp, endTimestamp]);
+ // UPlotConfigBuilder's setSelect hook already delivers milliseconds
+ handleTimeChange('custom', [Math.trunc(start), Math.trunc(end)]);
},
[handleTimeChange],
);
@@ -188,16 +174,10 @@ function EntityMetrics<T>({
return (
<>
<div className={styles.metricsHeader}>
- <DateTimeSelectionV2
- showAutoRefresh
- showRefreshText={false}
- hideShareModal
- onTimeChange={handleTimeChange}
- defaultRelativeTime={DEFAULT_TIME_RANGE}
- isModalTimeSelection={isModalTimeSelection}
- modalSelectedInterval={selectedInterval}
- modalInitialStartTime={timeRange.startTime * 1000}
- modalInitialEndTime={timeRange.endTime * 1000}
+ <EntityDateTimeSelector
+ eventEntity={eventEntity}
+ category={category}
+ view={InfraMonitoringEvents.MetricsView}
/>
</div>
<div className={styles.entityMetricsContainer}>
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityMetrics/__tests__/EntityMetrics.test.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityMetrics/__tests__/EntityMetrics.test.tsx
index 744a2eaf99..521e05be2a 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityMetrics/__tests__/EntityMetrics.test.tsx
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityMetrics/__tests__/EntityMetrics.test.tsx
@@ -1,7 +1,6 @@
import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { InfraMonitoringEntity } from 'container/InfraMonitoringK8sV2/constants';
-import { Time } from 'container/TopNav/DateTimeSelectionV2/types';
import * as appContextHooks from 'providers/App/App';
import { LicenseEvent } from 'types/api/licensesV3/getActive';
import uPlot from 'uplot';
@@ -28,13 +27,28 @@ jest.mock('lib/uPlotV2/utils/dataUtils', () => ({
hasSingleVisiblePoint: jest.fn().mockReturnValue(false),
}));
-jest.mock('container/TopNav/DateTimeSelectionV2', () => ({
+jest.mock('../../EntityDateTimeSelector/EntityDateTimeSelector', () => ({
__esModule: true,
default: (): JSX.Element => (
<div data-testid="date-time-selection">Date Time</div>
),
}));
+const mockTimeRange = { startTime: 1705315200, endTime: 1705318800 };
+let mockSelectedInterval = '5m';
+
+jest.mock('../../EntityDateTimeSelector/useEntityDetailsTime', () => ({
+ useEntityDetailsTime: (): {
+ timeRange: { startTime: number; endTime: number };
+ selectedInterval: string;
+ handleTimeChange: jest.Mock;
+ } => ({
+ timeRange: mockTimeRange,
+ selectedInterval: mockSelectedInterval,
+ handleTimeChange: jest.fn(),
+ }),
+}));
+
jest.mock(
'container/DashboardContainer/visualization/charts/TimeSeries/TimeSeries',
() => ({
@@ -144,13 +158,6 @@ const mockGetEntityQueryPayload = jest.fn().mockReturnValue([
},
]);
-const mockTimeRange = {
- startTime: 1705315200,
- endTime: 1705318800,
-};
-
-const mockHandleTimeChange = jest.fn();
-
const mockQueries = [
{
data: {
@@ -283,10 +290,6 @@ const mockEmptyQueries = [
const renderEntityMetrics = (overrides = {}): any => {
const defaultProps = {
- timeRange: mockTimeRange,
- isModalTimeSelection: false,
- handleTimeChange: mockHandleTimeChange,
- selectedInterval: '5m' as Time,
entity: mockEntity,
entityWidgetInfo: mockEntityWidgetInfo,
getEntityQueryPayload: mockGetEntityQueryPayload,
@@ -298,11 +301,8 @@ const renderEntityMetrics = (overrides = {}): any => {
return render(
<MemoryRouter>
<EntityMetrics
- timeRange={defaultProps.timeRange}
- isModalTimeSelection={defaultProps.isModalTimeSelection}
- handleTimeChange={defaultProps.handleTimeChange}
- selectedInterval={defaultProps.selectedInterval}
entity={defaultProps.entity}
+ eventEntity="test"
entityWidgetInfo={defaultProps.entityWidgetInfo}
getEntityQueryPayload={defaultProps.getEntityQueryPayload}
queryKey={defaultProps.queryKey}
@@ -344,6 +344,7 @@ const mockQueryPayloads = [
describe('EntityMetrics', () => {
beforeEach(() => {
jest.clearAllMocks();
+ mockSelectedInterval = '5m';
mockUseEntityMetrics.mockReturnValue({
queries: mockQueries as any,
chartData: mockChartData,
@@ -454,7 +455,8 @@ describe('EntityMetrics', () => {
});
it('builds metrics explorer link with relativeTime when a relative interval is selected', () => {
- renderEntityMetrics({ selectedInterval: '5m' as Time });
+ mockSelectedInterval = '5m';
+ renderEntityMetrics();
const href = screen
.getByTestId('open-metrics-explorer-0')
.getAttribute('href');
@@ -464,7 +466,8 @@ describe('EntityMetrics', () => {
});
it('builds metrics explorer link with absolute time range in milliseconds for custom interval', () => {
- renderEntityMetrics({ selectedInterval: 'custom' as Time });
+ mockSelectedInterval = 'custom';
+ renderEntityMetrics();
const href = screen
.getByTestId('open-metrics-explorer-0')
.getAttribute('href');
@@ -478,7 +481,6 @@ describe('EntityMetrics', () => {
expect(mockUseEntityMetrics).toHaveBeenCalledWith(
expect.objectContaining({
queryKey: 'test-query-key',
- timeRange: mockTimeRange,
entity: mockEntity,
category: InfraMonitoringEntity.PODS,
}),
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityMetrics/__tests__/EntityMetrics.timeRange.test.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityMetrics/__tests__/EntityMetrics.timeRange.test.tsx
new file mode 100644
index 0000000000..1fe0eed5c3
--- /dev/null
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityMetrics/__tests__/EntityMetrics.timeRange.test.tsx
@@ -0,0 +1,119 @@
+import { PANEL_TYPES } from 'constants/queryBuilder';
+import { InfraMonitoringEntity } from 'container/InfraMonitoringK8sV2/constants';
+import { GetQueryResultsProps } from 'lib/dashboard/getQueryResults';
+import {
+ createCustomTimeRange,
+ createGlobalTimeStore,
+ GlobalTimeContext,
+ GlobalTimeStoreApi,
+ NANO_SECOND_MULTIPLIER,
+} from 'store/globalTime';
+import { act, render } from 'tests/test-utils';
+
+import { buildEntityMetricsChartConfig } from '../configBuilder';
+import EntityMetrics from '../EntityMetrics';
+
+jest.mock('../configBuilder', () => ({
+ buildEntityMetricsChartConfig: jest.fn().mockReturnValue({
+ getId: jest.fn().mockReturnValue('mock-id'),
+ }),
+}));
+
+jest.mock('../../EntityDateTimeSelector/EntityDateTimeSelector', () => ({
+ __esModule: true,
+ default: (): JSX.Element => (
+ <div data-testid="date-time-selection">Date Time</div>
+ ),
+}));
+
+const mockBuildChartConfig = buildEntityMetricsChartConfig as jest.Mock;
+
+const START_MS = 1705315200000; // 2024-01-15T10:40:00Z
+const END_MS = 1705318800000; // 2024-01-15T11:40:00Z
+const START_SECONDS = 1705315200;
+const END_SECONDS = 1705318800;
+
+const entity = { id: 'test-entity-1' };
+
+// The jsdom IntersectionObserver polyfill never reports visibility, so the
+// queries stay disabled and no network request is issued.
+const queryPayload = {
+ graphType: PANEL_TYPES.TIME_SERIES,
+} as unknown as GetQueryResultsProps;
+
+function createStoreWithCustomRange(): GlobalTimeStoreApi {
+ const store = createGlobalTimeStore();
+ store
+ .getState()
+ .setSelectedTime(
+ createCustomTimeRange(
+ START_MS * NANO_SECOND_MULTIPLIER,
+ END_MS * NANO_SECOND_MULTIPLIER,
+ ),
+ );
+ return store;
+}
+
+function renderEntityMetrics(store: GlobalTimeStoreApi): jest.Mock {
+ const getEntityQueryPayload = jest.fn().mockReturnValue([queryPayload]);
+ render(
+ <GlobalTimeContext.Provider value={store}>
+ <EntityMetrics
+ entity={entity}
+ eventEntity="test"
+ entityWidgetInfo={[{ title: 'CPU Usage', yAxisUnit: 'percentage' }]}
+ getEntityQueryPayload={getEntityQueryPayload}
+ queryKey="test-query-key"
+ category={InfraMonitoringEntity.PODS}
+ />
+ </GlobalTimeContext.Provider>,
+ );
+ return getEntityQueryPayload;
+}
+
+describe('EntityMetrics time range wiring', () => {
+ beforeEach(() => {
+ mockBuildChartConfig.mockClear();
+ });
+
+ it('should build the metrics query payloads and chart config from the global time range in seconds', () => {
+ const store = createStoreWithCustomRange();
+
+ const getEntityQueryPayload = renderEntityMetrics(store);
+
+ expect(getEntityQueryPayload).toHaveBeenCalledWith(
+ entity,
+ START_SECONDS,
+ END_SECONDS,
+ false,
+ );
+ expect(mockBuildChartConfig).toHaveBeenCalledWith(
+ expect.objectContaining({
+ minTimeScale: START_SECONDS,
+ maxTimeScale: END_SECONDS,
+ }),
+ );
+ });
+
+ it('should store a drag selection (received in milliseconds) as the equivalent custom range', () => {
+ const store = createStoreWithCustomRange();
+
+ renderEntityMetrics(store);
+
+ const { onDragSelect } = mockBuildChartConfig.mock.calls[0][0];
+
+ // UPlotConfigBuilder's setSelect hook calls onDragSelect with milliseconds
+ const dragStartMs = 1705316000000;
+ const dragEndMs = 1705317000000;
+ act(() => {
+ onDragSelect(dragStartMs, dragEndMs);
+ });
+
+ expect(store.getState().selectedTime).toBe(
+ createCustomTimeRange(
+ dragStartMs * NANO_SECOND_MULTIPLIER,
+ dragEndMs * NANO_SECOND_MULTIPLIER,
+ ),
+ );
+ });
+});
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/EntityTraces.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/EntityTraces.tsx
index f5072d91f6..aa961767c4 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/EntityTraces.tsx
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/EntityTraces.tsx
@@ -20,11 +20,6 @@ import { InfraMonitoringEvents } from 'constants/events';
import Controls from 'container/Controls';
import { InfraMonitoringEntity } from 'container/InfraMonitoringK8sV2/constants';
import RunQueryBtn from 'container/QueryBuilder/components/RunQueryBtn/RunQueryBtn';
-import DateTimeSelectionV2 from 'container/TopNav/DateTimeSelectionV2';
-import {
- CustomTimeType,
- Time,
-} from 'container/TopNav/DateTimeSelectionV2/types';
import { PER_PAGE_OPTIONS } from 'container/TracesExplorer/ListView/configs';
import { TracesLoading } from 'container/TracesExplorer/TraceLoading/TraceLoading';
import { useQueryState } from 'nuqs';
@@ -32,10 +27,11 @@ import { DataSource } from 'types/common/queryBuilder';
import { parseAsJsonNoValidate } from 'utils/nuqsParsers';
import { validateQuery } from 'utils/queryValidationUtils';
+import EntityDateTimeSelector from '../EntityDateTimeSelector/EntityDateTimeSelector';
+import { useEntityDetailsTime } from '../EntityDateTimeSelector/useEntityDetailsTime';
import EntityEmptyState from '../EntityEmptyState/EntityEmptyState';
import EntityError from '../EntityError/EntityError';
-import { selectedEntityTracesColumns } from '../utils';
-import { isKeyNotFoundError } from '../utils';
+import { isKeyNotFoundError, selectedEntityTracesColumns } from '../utils';
import { K8S_ENTITY_TRACES_EXPRESSION_KEY, useEntityTraces } from './hooks';
import { getTraceListColumns } from './traceListColumns';
import { getEntityTracesQueryPayload } from './utils';
@@ -44,29 +40,18 @@ import styles from './EntityTraces.module.scss';
import { useTimezone } from 'providers/Timezone';
interface Props {
- timeRange: {
- startTime: number;
- endTime: number;
- };
- isModalTimeSelection: boolean;
- handleTimeChange: (
- interval: Time | CustomTimeType,
- dateTimeRange?: [number, number],
- ) => void;
- selectedInterval: Time;
+ eventEntity: string;
queryKey: string;
category: InfraMonitoringEntity;
initialExpression: string;
}
function EntityTracesContent({
- timeRange,
- isModalTimeSelection,
- handleTimeChange,
- selectedInterval,
+ eventEntity,
queryKey,
category,
}: Omit<Props, 'initialExpression'>): JSX.Element {
+ const { timeRange } = useEntityDetailsTime();
const expression = useExpression();
const inputExpression = useInputExpression();
const userExpression = useUserExpression();
@@ -114,8 +99,8 @@ function EntityTracesContent({
if (validation.isValid) {
querySearchOnRun(newUserExpression || '');
- logEvent(InfraMonitoringEvents.FilterApplied, {
- entity: InfraMonitoringEvents.K8sEntity,
+ void logEvent(InfraMonitoringEvents.FilterApplied, {
+ entity: eventEntity,
page: InfraMonitoringEvents.DetailedPage,
category,
view: InfraMonitoringEvents.TracesView,
@@ -124,7 +109,14 @@ function EntityTracesContent({
refetch();
}
},
- [inputExpression, initialExpression, refetch, querySearchOnRun, category],
+ [
+ inputExpression,
+ initialExpression,
+ refetch,
+ querySearchOnRun,
+ category,
+ eventEntity,
+ ],
);
const queryData = useMemo(
@@ -152,7 +144,7 @@ function EntityTracesContent({
const hasAdditionalFilters = !!userExpression?.trim();
const handleRowClick = useCallback(() => {
- logEvent(InfraMonitoringEvents.ItemClicked, {
+ void logEvent(InfraMonitoringEvents.ItemClicked, {
entity: InfraMonitoringEvents.K8sEntity,
category,
view: InfraMonitoringEvents.TracesView,
@@ -169,16 +161,10 @@ function EntityTracesContent({
<div className={styles.container}>
<div className={styles.filterContainer}>
<div className={styles.filterContainerTime}>
- <DateTimeSelectionV2
- showAutoRefresh
- showRefreshText={false}
- hideShareModal
- isModalTimeSelection={isModalTimeSelection}
- onTimeChange={handleTimeChange}
- defaultRelativeTime="5m"
- modalSelectedInterval={selectedInterval}
- modalInitialStartTime={timeRange.startTime * 1000}
- modalInitialEndTime={timeRange.endTime * 1000}
+ <EntityDateTimeSelector
+ eventEntity={eventEntity}
+ category={category}
+ view={InfraMonitoringEvents.TracesView}
/>
<RunQueryBtn
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/__tests__/EntityTraces.default.test.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/__tests__/EntityTraces.default.test.tsx
index bddf114aa5..1ce6abffa4 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/__tests__/EntityTraces.default.test.tsx
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/__tests__/EntityTraces.default.test.tsx
@@ -34,10 +34,8 @@ describe('EntityTraces - Default Behavior', () => {
});
it('should pass time range to API (converted to milliseconds)', async () => {
- const timeRange = { startTime: 1000, endTime: 2000 };
-
act(() => {
- renderEntityTraces({ timeRange });
+ renderEntityTraces();
});
await waitFor(() => {
@@ -47,8 +45,8 @@ describe('EntityTraces - Default Behavior', () => {
verifyQueryPayload({
payload: capturedPayloads[0],
expectedTimeRange: {
- start: timeRange.startTime * 1000,
- end: timeRange.endTime * 1000,
+ start: 1 * 1000,
+ end: 2 * 1000,
},
});
});
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/__tests__/EntityTraces.timeRange.test.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/__tests__/EntityTraces.timeRange.test.tsx
new file mode 100644
index 0000000000..dfcbc68b6b
--- /dev/null
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/__tests__/EntityTraces.timeRange.test.tsx
@@ -0,0 +1,81 @@
+import { mockFieldsAPIsWithEmptyResponse } from '__tests__/fields_api.util';
+import { mockQueryRangeV5WithTracesResponse } from '__tests__/query_range_v5.util';
+import { InfraMonitoringEntity } from 'container/InfraMonitoringK8sV2/constants';
+import { NuqsTestingAdapter } from 'nuqs/adapters/testing';
+import {
+ createCustomTimeRange,
+ createGlobalTimeStore,
+ GlobalTimeContext,
+ NANO_SECOND_MULTIPLIER,
+} from 'store/globalTime';
+import { act, render, waitFor } from 'tests/test-utils';
+import { QueryRangePayloadV5 } from 'types/api/v5/queryRange';
+
+import EntityTraces from '../EntityTraces';
+import { K8S_ENTITY_TRACES_EXPRESSION_KEY } from '../hooks';
+
+jest.mock('../../EntityDateTimeSelector/EntityDateTimeSelector', () => ({
+ __esModule: true,
+ default: (): JSX.Element => (
+ <div data-testid="mock-datetime-selection">Date Time</div>
+ ),
+}));
+
+const START_MS = 1705315200000; // 2024-01-15T10:40:00Z
+const END_MS = 1705318800000; // 2024-01-15T11:40:00Z
+
+describe('EntityTraces time range wiring', () => {
+ let capturedPayloads: QueryRangePayloadV5[] = [];
+
+ beforeEach(() => {
+ capturedPayloads = [];
+ mockQueryRangeV5WithTracesResponse({
+ onReceiveRequest: async (req) => {
+ const body = (await req.json()) as QueryRangePayloadV5;
+ capturedPayloads.push(body);
+ return {};
+ },
+ });
+ mockFieldsAPIsWithEmptyResponse();
+ });
+
+ it('should query the V5 API with the global time range converted to milliseconds', async () => {
+ const store = createGlobalTimeStore();
+ store
+ .getState()
+ .setSelectedTime(
+ createCustomTimeRange(
+ START_MS * NANO_SECOND_MULTIPLIER,
+ END_MS * NANO_SECOND_MULTIPLIER,
+ ),
+ );
+
+ const expression = 'k8s.pod.name = "test-pod"';
+
+ act(() => {
+ render(
+ <GlobalTimeContext.Provider value={store}>
+ <NuqsTestingAdapter
+ searchParams={`${K8S_ENTITY_TRACES_EXPRESSION_KEY}=${encodeURIComponent(
+ expression,
+ )}`}
+ >
+ <EntityTraces
+ eventEntity="test"
+ queryKey="test"
+ category={InfraMonitoringEntity.PODS}
+ initialExpression={expression}
+ />
+ </NuqsTestingAdapter>
+ </GlobalTimeContext.Provider>,
+ );
+ });
+
+ await waitFor(() => {
+ expect(capturedPayloads).toHaveLength(1);
+ });
+
+ expect(capturedPayloads[0].start).toBe(START_MS);
+ expect(capturedPayloads[0].end).toBe(END_MS);
+ });
+});
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/__tests__/testUtils.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/__tests__/testUtils.tsx
index ee5840d52a..dac3135b16 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/__tests__/testUtils.tsx
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/EntityTraces/__tests__/testUtils.tsx
@@ -1,7 +1,5 @@
-import { ENVIRONMENT } from 'constants/env';
+import { mockFieldsAPIsWithEmptyResponse } from '__tests__/fields_api.util';
import { InfraMonitoringEntity } from 'container/InfraMonitoringK8sV2/constants';
-import { server } from 'mocks-server/server';
-import { rest } from 'msw';
import { NuqsTestingAdapter } from 'nuqs/adapters/testing';
import { render, RenderResult } from 'tests/test-utils';
import { QueryRangePayloadV5 } from 'types/api/v5/queryRange';
@@ -9,39 +7,19 @@ import { QueryRangePayloadV5 } from 'types/api/v5/queryRange';
import EntityTraces from '../EntityTraces';
import { K8S_ENTITY_TRACES_EXPRESSION_KEY } from '../hooks';
-// QuerySearch fires autocomplete requests on mount; without handlers MSW
-// passes them through to the real network and the resulting AxiosError fails
-// whichever test happens to be running.
beforeEach(() => {
- server.use(
- rest.get(`${ENVIRONMENT.baseURL}/api/v1/fields/keys`, (_, res, ctx) =>
- res(
- ctx.status(200),
- ctx.json({ status: 'success', data: { keys: {}, complete: true } }),
- ),
- ),
- rest.get(`${ENVIRONMENT.baseURL}/api/v1/fields/values`, (_, res, ctx) =>
- res(
- ctx.status(200),
- ctx.json({ status: 'success', data: { values: {}, complete: true } }),
- ),
- ),
- );
+ mockFieldsAPIsWithEmptyResponse();
});
export interface RenderEntityTracesOptions {
expression?: string;
- timeRange?: { startTime: number; endTime: number };
category?: InfraMonitoringEntity;
- selectedInterval?: '5m' | '15m' | '30m' | '1h';
pagination?: { offset: number; limit: number };
}
export function renderEntityTraces({
expression = 'k8s.pod.name = "test-pod"',
- timeRange = { startTime: 1, endTime: 2 },
category = InfraMonitoringEntity.PODS,
- selectedInterval = '5m',
pagination,
}: RenderEntityTracesOptions = {}): RenderResult {
const encodedExpression = encodeURIComponent(expression);
@@ -55,10 +33,7 @@ export function renderEntityTraces({
return render(
<NuqsTestingAdapter searchParams={searchParams}>
<EntityTraces
- timeRange={timeRange}
- isModalTimeSelection={false}
- handleTimeChange={jest.fn()}
- selectedInterval={selectedInterval}
+ eventEntity="test"
queryKey="test"
category={category}
initialExpression={expression}
@@ -101,21 +76,21 @@ export function verifyQueryPayload({
expect(orderKeys).toContain('timestamp');
}
-jest.mock('container/TopNav/DateTimeSelectionV2/index.tsx', () => ({
+jest.mock('../../EntityDateTimeSelector/EntityDateTimeSelector', () => ({
__esModule: true,
- default: ({
- onTimeChange,
- }: {
- onTimeChange?: (interval: string, dateTimeRange?: [number, number]) => void;
- }): JSX.Element => (
- <button
- type="button"
- data-testid="mock-datetime-selection"
- onClick={(): void => {
- onTimeChange?.('5m');
- }}
- >
- Select Time
- </button>
+ default: (): JSX.Element => (
+ <div data-testid="mock-datetime-selection">Date Time</div>
),
}));
+
+jest.mock('../../EntityDateTimeSelector/useEntityDetailsTime', () => ({
+ useEntityDetailsTime: (): {
+ timeRange: { startTime: number; endTime: number };
+ selectedInterval: string;
+ handleTimeChange: jest.Mock;
+ } => ({
+ timeRange: { startTime: 1, endTime: 2 },
+ selectedInterval: '5m',
+ handleTimeChange: jest.fn(),
+ }),
+}));
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/createPodMetricsTab.tsx b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/createPodMetricsTab.tsx
index 6badd3c252..1b13a9a920 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/createPodMetricsTab.tsx
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/createPodMetricsTab.tsx
@@ -1,4 +1,5 @@
import { Container } from '@signozhq/icons';
+import { InfraMonitoringEvents } from 'constants/events';
import { GetQueryResultsProps } from 'lib/dashboard/getQueryResults';
import { CustomTab } from '../Base/K8sBaseDetails';
@@ -10,6 +11,20 @@ import {
import EntityMetrics from './EntityMetrics';
+const categoryToEventEntity: Record<InfraMonitoringEntity, string> = {
+ [InfraMonitoringEntity.DAEMONSETS]: InfraMonitoringEvents.DaemonSet,
+ [InfraMonitoringEntity.DEPLOYMENTS]: InfraMonitoringEvents.Deployment,
+ [InfraMonitoringEntity.JOBS]: InfraMonitoringEvents.Job,
+ [InfraMonitoringEntity.NAMESPACES]: InfraMonitoringEvents.Namespace,
+ [InfraMonitoringEntity.STATEFULSETS]: InfraMonitoringEvents.StatefulSet,
+ [InfraMonitoringEntity.PODS]: InfraMonitoringEvents.Pod,
+ [InfraMonitoringEntity.NODES]: InfraMonitoringEvents.Node,
+ [InfraMonitoringEntity.CLUSTERS]: InfraMonitoringEvents.Cluster,
+ [InfraMonitoringEntity.VOLUMES]: InfraMonitoringEvents.Volume,
+ [InfraMonitoringEntity.HOSTS]: InfraMonitoringEvents.HostEntity,
+ [InfraMonitoringEntity.CONTAINERS]: 'container',
+};
+
interface CreatePodMetricsTabParams<T> {
getQueryPayload: (
entity: T,
@@ -17,30 +32,29 @@ interface CreatePodMetricsTabParams<T> {
end: number,
dotMetricsEnabled: boolean,
) => GetQueryResultsProps[];
- category: InfraMonitoringEntity;
queryKey: string;
+ category: InfraMonitoringEntity;
}
export function createPodMetricsTab<T>({
getQueryPayload,
- category,
queryKey,
+ category,
}: CreatePodMetricsTabParams<T>): CustomTab<T> {
+ const eventEntity = categoryToEventEntity[category];
+
return {
key: VIEW_TYPES.POD_METRICS,
label: 'Pod Metrics',
icon: <Container size={14} />,
- render: ({ entity, timeRange, selectedInterval, handleTimeChange }) => (
+ render: ({ entity }) => (
<EntityMetrics
entity={entity}
- selectedInterval={selectedInterval}
- timeRange={timeRange}
- handleTimeChange={handleTimeChange}
- isModalTimeSelection
+ eventEntity={eventEntity}
entityWidgetInfo={podUtilizationByPodWidgetInfo}
getEntityQueryPayload={getQueryPayload}
- category={category}
queryKey={queryKey}
+ category={category}
/>
),
};
diff --git a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/entityDetails.styles.scss b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/entityDetails.styles.scss
index 438320c6d4..7edc019f91 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/entityDetails.styles.scss
+++ b/frontend/src/container/InfraMonitoringK8sV2/EntityDetailsUtils/entityDetails.styles.scss
@@ -454,3 +454,9 @@
gap: 16px;
}
}
+
+.entity-date-time-selector {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+}
diff --git a/frontend/src/container/InfraMonitoringK8sV2/constants.ts b/frontend/src/container/InfraMonitoringK8sV2/constants.ts
index 01c07db8b7..fdad4ae64e 100644
--- a/frontend/src/container/InfraMonitoringK8sV2/constants.ts
+++ b/frontend/src/container/InfraMonitoringK8sV2/constants.ts
@@ -905,6 +905,9 @@ export const INFRA_MONITORING_K8S_PARAMS_KEYS = {
SELECTED_ITEM: 'selectedItem',
SELECTED_ITEM_CLUSTER_NAME: 'selectedItemClusterName',
SELECTED_ITEM_NAMESPACE_NAME: 'selectedItemNamespaceName',
+ DETAIL_RELATIVE_TIME: 'detailRelativeTime',
+ DETAIL_START_TIME: 'detailStartTime',
+ DETAIL_END_TIME: 'detailEndTime',
};
/** Metric namespace prefixes for /fields/keys and /fields/values APIs */
diff --git a/frontend/src/store/globalTime/GlobalTimeContext.tsx b/frontend/src/store/globalTime/GlobalTimeContext.tsx
index 57ce598e44..e4e25037a4 100644
--- a/frontend/src/store/globalTime/GlobalTimeContext.tsx
+++ b/frontend/src/store/globalTime/GlobalTimeContext.tsx
@@ -55,6 +55,7 @@ export function GlobalTimeProvider({
name,
selectedTime: resolveInitialTime(),
refreshInterval: initialRefreshInterval ?? 0,
+ parentStore: inheritGlobalTime ? globalStore : undefined,
}),
);
diff --git a/frontend/src/store/globalTime/__tests__/GlobalTimeContext.test.tsx b/frontend/src/store/globalTime/__tests__/GlobalTimeContext.test.tsx
index 73cea7b8f7..401a6b5024 100644
--- a/frontend/src/store/globalTime/__tests__/GlobalTimeContext.test.tsx
+++ b/frontend/src/store/globalTime/__tests__/GlobalTimeContext.test.tsx
@@ -38,6 +38,36 @@ const createWrapper = (
};
};
+const createNestedWrapper = (
+ parentProps: GlobalTimeProviderOptions,
+ childProps: GlobalTimeProviderOptions,
+ nuqsProps?: {
+ searchParams?: string;
+ onUrlUpdate?: (event: { queryString: string }) => void;
+ },
+) => {
+ const queryClient = createTestQueryClient();
+
+ return function NestedWrapper({
+ children,
+ }: {
+ children: ReactNode;
+ }): JSX.Element {
+ return (
+ <QueryClientProvider client={queryClient}>
+ <NuqsTestingAdapter
+ searchParams={nuqsProps?.searchParams}
+ onUrlUpdate={nuqsProps?.onUrlUpdate}
+ >
+ <GlobalTimeProvider {...parentProps}>
+ <GlobalTimeProvider {...childProps}>{children}</GlobalTimeProvider>
+ </GlobalTimeProvider>
+ </NuqsTestingAdapter>
+ </QueryClientProvider>
+ );
+ };
+};
+
describe('GlobalTimeProvider', () => {
describe('name prop', () => {
it('should pass name to store when provided', () => {
@@ -82,138 +112,73 @@ describe('GlobalTimeProvider', () => {
describe('inheritGlobalTime', () => {
it('should inherit time from parent store when inheritGlobalTime is true', () => {
- const queryClient = createTestQueryClient();
-
- const NestedWrapper = ({
- children,
- }: {
- children: React.ReactNode;
- }): JSX.Element => (
- <QueryClientProvider client={queryClient}>
- <NuqsTestingAdapter>
- <GlobalTimeProvider initialTime="6h">
- <GlobalTimeProvider inheritGlobalTime>{children}</GlobalTimeProvider>
- </GlobalTimeProvider>
- </NuqsTestingAdapter>
- </QueryClientProvider>
+ const wrapper = createNestedWrapper(
+ { initialTime: '6h' },
+ { inheritGlobalTime: true },
);
const { result } = renderHook(() => useGlobalTime((s) => s.selectedTime), {
- wrapper: NestedWrapper,
+ wrapper,
});
- // Should inherit '6h' from parent provider
expect(result.current).toBe('6h');
});
it('should use initialTime when inheritGlobalTime is false', () => {
- const queryClient = createTestQueryClient();
-
- const NestedWrapper = ({
- children,
- }: {
- children: React.ReactNode;
- }): JSX.Element => (
- <QueryClientProvider client={queryClient}>
- <NuqsTestingAdapter>
- <GlobalTimeProvider initialTime="6h">
- <GlobalTimeProvider inheritGlobalTime={false} initialTime="15m">
- {children}
- </GlobalTimeProvider>
- </GlobalTimeProvider>
- </NuqsTestingAdapter>
- </QueryClientProvider>
+ const wrapper = createNestedWrapper(
+ { initialTime: '6h' },
+ { inheritGlobalTime: false, initialTime: '15m' },
);
const { result } = renderHook(() => useGlobalTime((s) => s.selectedTime), {
- wrapper: NestedWrapper,
+ wrapper,
});
- // Should use its own initialTime, not parent's
expect(result.current).toBe('15m');
});
it('should prefer URL params over inheritGlobalTime when both are present', async () => {
- const queryClient = createTestQueryClient();
-
- const NestedWrapper = ({
- children,
- }: {
- children: React.ReactNode;
- }): JSX.Element => (
- <QueryClientProvider client={queryClient}>
- <NuqsTestingAdapter searchParams="?relativeTime=1h">
- <GlobalTimeProvider initialTime="6h">
- <GlobalTimeProvider inheritGlobalTime enableUrlParams>
- {children}
- </GlobalTimeProvider>
- </GlobalTimeProvider>
- </NuqsTestingAdapter>
- </QueryClientProvider>
+ const wrapper = createNestedWrapper(
+ { initialTime: '6h' },
+ { inheritGlobalTime: true, enableUrlParams: true },
+ { searchParams: '?relativeTime=1h' },
);
const { result } = renderHook(() => useGlobalTime((s) => s.selectedTime), {
- wrapper: NestedWrapper,
+ wrapper,
});
- // inheritGlobalTime sets initial value to '6h', but URL sync updates it to '1h'
await waitFor(() => {
expect(result.current).toBe('1h');
});
});
it('should use inherited time when URL params are empty', async () => {
- const queryClient = createTestQueryClient();
-
- const NestedWrapper = ({
- children,
- }: {
- children: React.ReactNode;
- }): JSX.Element => (
- <QueryClientProvider client={queryClient}>
- <NuqsTestingAdapter searchParams="">
- <GlobalTimeProvider initialTime="6h">
- <GlobalTimeProvider inheritGlobalTime enableUrlParams>
- {children}
- </GlobalTimeProvider>
- </GlobalTimeProvider>
- </NuqsTestingAdapter>
- </QueryClientProvider>
+ const wrapper = createNestedWrapper(
+ { initialTime: '6h' },
+ { inheritGlobalTime: true, enableUrlParams: true },
+ { searchParams: '' },
);
const { result } = renderHook(() => useGlobalTime((s) => s.selectedTime), {
- wrapper: NestedWrapper,
+ wrapper,
});
- // No URL params, should keep inherited value
expect(result.current).toBe('6h');
});
it('should prefer custom time URL params over inheritGlobalTime', async () => {
- const queryClient = createTestQueryClient();
const startTime = 1700000000000;
const endTime = 1700003600000;
- const NestedWrapper = ({
- children,
- }: {
- children: React.ReactNode;
- }): JSX.Element => (
- <QueryClientProvider client={queryClient}>
- <NuqsTestingAdapter
- searchParams={`?startTime=${startTime}&endTime=${endTime}`}
- >
- <GlobalTimeProvider initialTime="6h">
- <GlobalTimeProvider inheritGlobalTime enableUrlParams>
- {children}
- </GlobalTimeProvider>
- </GlobalTimeProvider>
- </NuqsTestingAdapter>
- </QueryClientProvider>
+ const wrapper = createNestedWrapper(
+ { initialTime: '6h' },
+ { inheritGlobalTime: true, enableUrlParams: true },
+ { searchParams: `?startTime=${startTime}&endTime=${endTime}` },
);
const { result } = renderHook(() => useGlobalTime(), {
- wrapper: NestedWrapper,
+ wrapper,
});
// URL custom time params should override inherited time
@@ -690,4 +655,151 @@ describe('GlobalTimeProvider', () => {
expect(result.current.isRefreshEnabled).toBe(true);
});
});
+
+ describe('resetToParentTime', () => {
+ it('should return false when no parent store exists', () => {
+ const wrapper = createWrapper({ initialTime: '1h' });
+
+ const { result } = renderHook(() => useGlobalTime(), { wrapper });
+
+ expect(result.current.resetToParentTime()).toBe(false);
+ expect(result.current.parentStore).toBeUndefined();
+ });
+
+ it('should have parentStore when inheritGlobalTime is true', () => {
+ const wrapper = createNestedWrapper(
+ { initialTime: '6h' },
+ { inheritGlobalTime: true },
+ );
+
+ const { result } = renderHook(() => useGlobalTime(), { wrapper });
+
+ expect(result.current.parentStore).toBeDefined();
+ });
+
+ it('should reset to parent time when called', () => {
+ const wrapper = createNestedWrapper(
+ { initialTime: '6h' },
+ { inheritGlobalTime: true },
+ );
+
+ const { result } = renderHook(() => useGlobalTime(), { wrapper });
+
+ expect(result.current.selectedTime).toBe('6h');
+ const initialMinMax = result.current.lastComputedMinMax;
+
+ act(() => {
+ result.current.setSelectedTime('15m');
+ });
+
+ expect(result.current.selectedTime).toBe('15m');
+ const changedMinMax = result.current.lastComputedMinMax;
+ expect(changedMinMax.maxTime - changedMinMax.minTime).toBeLessThan(
+ initialMinMax.maxTime - initialMinMax.minTime,
+ );
+
+ act(() => {
+ const success = result.current.resetToParentTime();
+ expect(success).toBe(true);
+ });
+
+ expect(result.current.selectedTime).toBe('6h');
+ const resetMinMax = result.current.lastComputedMinMax;
+ expect(resetMinMax.maxTime - resetMinMax.minTime).toBeGreaterThan(
+ changedMinMax.maxTime - changedMinMax.minTime,
+ );
+ });
+
+ it('should set shouldClearUrlParams flag when reset', () => {
+ const wrapper = createNestedWrapper(
+ { initialTime: '6h' },
+ { inheritGlobalTime: true },
+ );
+
+ const { result } = renderHook(() => useGlobalTime(), { wrapper });
+
+ act(() => {
+ result.current.setSelectedTime('15m');
+ });
+
+ act(() => {
+ result.current.resetToParentTime();
+ });
+
+ expect(result.current.shouldClearUrlParams).toBe(true);
+
+ act(() => {
+ result.current.clearUrlParamsFlag();
+ });
+
+ expect(result.current.shouldClearUrlParams).toBe(false);
+ });
+
+ it('should clear URL params when resetToParentTime is called with enableUrlParams', async () => {
+ let currentQueryString = 'relativeTime=15m';
+
+ const wrapper = createNestedWrapper(
+ { initialTime: '6h' },
+ { inheritGlobalTime: true, enableUrlParams: true },
+ {
+ searchParams: currentQueryString,
+ onUrlUpdate: (event): void => {
+ currentQueryString = event.queryString;
+ },
+ },
+ );
+
+ const { result } = renderHook(() => useGlobalTime(), { wrapper });
+
+ await waitFor(() => {
+ expect(result.current.selectedTime).toBe('15m');
+ });
+
+ act(() => {
+ result.current.resetToParentTime();
+ });
+
+ await waitFor(() => {
+ expect(currentQueryString).not.toContain('relativeTime');
+ expect(currentQueryString).not.toContain('startTime');
+ expect(currentQueryString).not.toContain('endTime');
+ });
+
+ expect(result.current.selectedTime).toBe('6h');
+ });
+
+ it('should clear custom time URL params when resetToParentTime is called', async () => {
+ const startTime = 1700000000000;
+ const endTime = 1700003600000;
+ let currentQueryString = `startTime=${startTime}&endTime=${endTime}`;
+
+ const wrapper = createNestedWrapper(
+ { initialTime: '6h' },
+ { inheritGlobalTime: true, enableUrlParams: true },
+ {
+ searchParams: currentQueryString,
+ onUrlUpdate: (event): void => {
+ currentQueryString = event.queryString;
+ },
+ },
+ );
+
+ const { result } = renderHook(() => useGlobalTime(), { wrapper });
+
+ await waitFor(() => {
+ expect(result.current.selectedTime).toContain('||_||');
+ });
+
+ act(() => {
+ result.current.resetToParentTime();
+ });
+
+ await waitFor(() => {
+ expect(currentQueryString).not.toContain('startTime');
+ expect(currentQueryString).not.toContain('endTime');
+ });
+
+ expect(result.current.selectedTime).toBe('6h');
+ });
+ });
});
diff --git a/frontend/src/store/globalTime/globalTimeStore.ts b/frontend/src/store/globalTime/globalTimeStore.ts
index 86acdebc01..e281e8b385 100644
--- a/frontend/src/store/globalTime/globalTimeStore.ts
+++ b/frontend/src/store/globalTime/globalTimeStore.ts
@@ -5,6 +5,7 @@ import { DEFAULT_TIME_RANGE } from 'container/TopNav/DateTimeSelectionV2/constan
import {
GlobalTimeSelectedTime,
GlobalTimeState,
+ GlobalTimeStoreApiRef,
GlobalTimeStore,
ParsedTimeRange,
} from './types';
@@ -18,6 +19,10 @@ import {
export type GlobalTimeStoreApi = StoreApi<GlobalTimeStore>;
export type IGlobalTimeStore = GlobalTimeStore;
+export interface CreateGlobalTimeStoreOptions extends Partial<GlobalTimeState> {
+ parentStore?: GlobalTimeStoreApiRef;
+}
+
function computeIsRefreshEnabled(
selectedTime: GlobalTimeSelectedTime,
refreshInterval: number,
@@ -29,11 +34,12 @@ function computeIsRefreshEnabled(
}
export function createGlobalTimeStore(
- initialState?: Partial<GlobalTimeState>,
+ options?: CreateGlobalTimeStoreOptions,
): GlobalTimeStoreApi {
- const selectedTime = initialState?.selectedTime ?? DEFAULT_TIME_RANGE;
- const refreshInterval = initialState?.refreshInterval ?? 0;
- const name = initialState?.name;
+ const selectedTime = options?.selectedTime ?? DEFAULT_TIME_RANGE;
+ const refreshInterval = options?.refreshInterval ?? 0;
+ const name = options?.name;
+ const parentStore = options?.parentStore;
return createStore<GlobalTimeStore>((set, get) => ({
name,
@@ -42,6 +48,8 @@ export function createGlobalTimeStore(
isRefreshEnabled: computeIsRefreshEnabled(selectedTime, refreshInterval),
lastRefreshTimestamp: 0,
lastComputedMinMax: safeParseSelectedTime(selectedTime),
+ parentStore,
+ shouldClearUrlParams: false,
setSelectedTime: (
time: GlobalTimeSelectedTime,
@@ -130,6 +138,29 @@ export function createGlobalTimeStore(
}
return [REACT_QUERY_KEY.AUTO_REFRESH_QUERY, ...queryParts, selectedTime];
},
+
+ resetToParentTime: (): boolean => {
+ const state = get();
+ if (!state.parentStore) {
+ return false;
+ }
+
+ const parentSelectedTime = state.parentStore.getState().selectedTime;
+ const computedMinMax = parseSelectedTime(parentSelectedTime);
+
+ set({
+ selectedTime: parentSelectedTime,
+ lastComputedMinMax: computedMinMax,
+ lastRefreshTimestamp: Date.now(),
+ shouldClearUrlParams: true,
+ });
+
+ return true;
+ },
+
+ clearUrlParamsFlag: (): void => {
+ set({ shouldClearUrlParams: false });
+ },
}));
}
diff --git a/frontend/src/store/globalTime/index.ts b/frontend/src/store/globalTime/index.ts
index f1e02e9935..e4c0102192 100644
--- a/frontend/src/store/globalTime/index.ts
+++ b/frontend/src/store/globalTime/index.ts
@@ -206,6 +206,7 @@
* | `getMinMaxTime(time?)` | Get min/max (fresh if auto-refresh enabled, cached otherwise) |
* | `computeAndStoreMinMax()` | Compute fresh values and cache them |
* | `getAutoRefreshQueryKey(time, ...parts)` | Build scoped query key for this store instance |
+ * | `resetToParentTime()` | Reset to parent store's time (only if `inheritGlobalTime` was used). Clears URL params. Returns `true` on success |
*
* ### Utilities
*
@@ -235,7 +236,7 @@
* | Option | Type | Description |
* |--------|------|-------------|
* | `name` | `string` | Scope query keys to this store (enables isolated invalidation) |
- * | `inheritGlobalTime` | `boolean` | Initialize with parent/global time value |
+ * | `inheritGlobalTime` | `boolean` | Initialize with parent/global time value. Enables `resetToParentTime()` |
* | `initialTime` | `string` | Initial time if not inheriting |
* | `enableUrlParams` | `boolean \| object` | Sync time to URL query params |
* | `removeQueryParamsOnUnmount` | `boolean` | Clean URL params on unmount |
@@ -341,7 +342,39 @@
* }
* ```
*
- * ### Example 3: Nested Contexts
+ * ### Example 3: Reset to Parent Time
+ *
+ * When using `inheritGlobalTime`, you can reset the child store back to the parent's time:
+ *
+ * ```tsx
+ * function DrawerHeader({ onClose }) {
+ * const selectedTime = useGlobalTime((s) => s.selectedTime);
+ * const resetToParentTime = useGlobalTime((s) => s.resetToParentTime);
+ * const parentStore = useGlobalTime((s) => s.parentStore);
+ *
+ * const handleReset = () => {
+ * // Returns true if reset succeeded, false if no parent store
+ * const success = resetToParentTime();
+ * if (success) {
+ * // URL params are automatically cleared
+ * console.log('Reset to parent time');
+ * }
+ * };
+ *
+ * return (
+ * <div>
+ * <DateTimeSelectionV3 />
+ * {parentStore && (
+ * <Button onClick={handleReset}>
+ * Reset to Global Time
+ * </Button>
+ * )}
+ * </div>
+ * );
+ * }
+ * ```
+ *
+ * ### Example 4: Nested Contexts
*
* Contexts can be nested - each level creates isolation:
*
@@ -379,9 +412,10 @@
* }
* ```
*
- * ### Example 4: URL Sync for Shareable Links
+ * ### Example 5: URL Sync for Shareable Links
*
- * Persist time selection to URL for shareable links:
+ * Persist time selection to URL for shareable links. When using `resetToParentTime()`,
+ * URL params are automatically cleared:
*
* ```tsx
* function TracesExplorer() {
@@ -400,7 +434,7 @@
* }
* ```
*
- * ### Example 5: localStorage Persistence
+ * ### Example 6: localStorage Persistence
*
* Remember user's last selected time across sessions:
*
diff --git a/frontend/src/store/globalTime/types.ts b/frontend/src/store/globalTime/types.ts
index 29dfa060b0..cfcdb0b69a 100644
--- a/frontend/src/store/globalTime/types.ts
+++ b/frontend/src/store/globalTime/types.ts
@@ -1,9 +1,13 @@
+import { StoreApi } from 'zustand';
import { Time } from 'container/TopNav/DateTimeSelectionV2/types';
export type CustomTimeRangeSeparator = '||_||';
export type CustomTimeRange = `${number}${CustomTimeRangeSeparator}${number}`;
export type GlobalTimeSelectedTime = Time | CustomTimeRange;
+// Forward declaration to avoid circular dependency
+export type GlobalTimeStoreApiRef = StoreApi<GlobalTimeStore>;
+
export interface IGlobalTimeStoreState {
/**
* The selected time range, can be:
@@ -88,6 +92,16 @@ export interface GlobalTimeState {
isRefreshEnabled: boolean;
lastRefreshTimestamp: number;
lastComputedMinMax: ParsedTimeRange;
+ /**
+ * Reference to parent store when inheritGlobalTime was used.
+ * Enables resetToParentTime() functionality.
+ */
+ parentStore?: GlobalTimeStoreApiRef;
+ /**
+ * Flag indicating URL params should be cleared (not synced).
+ * Set by resetToParentTime(), consumed by useUrlSync.
+ */
+ shouldClearUrlParams: boolean;
}
export interface GlobalTimeActions {
@@ -118,6 +132,16 @@ export interface GlobalTimeActions {
selectedTime: GlobalTimeSelectedTime,
...queryParts: unknown[]
) => unknown[];
+ /**
+ * Reset to parent store's time. Only works if inheritGlobalTime was used.
+ * Clears URL params and sets selectedTime to parent's value.
+ * @returns true if reset succeeded, false if no parent store
+ */
+ resetToParentTime: () => boolean;
+ /**
+ * Clear the shouldClearUrlParams flag after URL sync has processed it.
+ */
+ clearUrlParamsFlag: () => void;
}
export type GlobalTimeStore = GlobalTimeState & GlobalTimeActions;
diff --git a/frontend/src/store/globalTime/useUrlSync.ts b/frontend/src/store/globalTime/useUrlSync.ts
index 3f77557d00..76617d9e1d 100644
--- a/frontend/src/store/globalTime/useUrlSync.ts
+++ b/frontend/src/store/globalTime/useUrlSync.ts
@@ -91,6 +91,17 @@ export function useUrlSync(
let previousSelectedTime = store.getState().selectedTime;
return store.subscribe((state) => {
+ if (state.shouldClearUrlParams) {
+ previousSelectedTime = state.selectedTime;
+ void setUrlState({
+ [keys.relativeTimeKey]: null,
+ [keys.startTimeKey]: null,
+ [keys.endTimeKey]: null,
+ });
+ store.getState().clearUrlParamsFlag();
+ return;
+ }
+
if (state.selectedTime === previousSelectedTime) {
return;
}