SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
feat: enhance logs explorer chart to display full selected time window (#8446)
Shaheer Kochai2141b1b90aac
diff --git a/frontend/src/components/Graph/index.tsx b/frontend/src/components/Graph/index.tsxindex fcf1481e61..23e6d62d51 100644--- a/frontend/src/components/Graph/index.tsx+++ b/frontend/src/components/Graph/index.tsx@@ -94,6 +94,8 @@ const Graph = forwardRef<ToggleGraphProps | undefined, GraphProps>(containerHeight,onDragSelect,dragSelectColor,+ minTime,+ maxTime,},ref,// eslint-disable-next-line sonarjs/cognitive-complexity@@ -105,7 +107,7 @@ const Graph = forwardRef<ToggleGraphProps | undefined, GraphProps>(const { timezone } = useTimezone();const currentTheme = isDarkMode ? 'dark' : 'light';- const xAxisTimeUnit = useXAxisTimeUnit(data); // Computes the relevant time unit for x axis by analyzing the time stamp data+ const xAxisTimeUnit = useXAxisTimeUnit(data, minTime, maxTime); // Computes the relevant time unit for x axis based on data or provided time rangeconst lineChartRef = useRef<Chart>();@@ -167,6 +169,8 @@ const Graph = forwardRef<ToggleGraphProps | undefined, GraphProps>(onClickHandler,data,timezone,+ minTime,+ maxTime,);const chartHasData = hasData(data);@@ -202,6 +206,8 @@ const Graph = forwardRef<ToggleGraphProps | undefined, GraphProps>(onClickHandler,data,timezone,+ minTime,+ maxTime,name,type,]);@@ -236,6 +242,8 @@ Graph.defaultProps = {containerHeight: '90%',onDragSelect: undefined,dragSelectColor: undefined,+ minTime: undefined,+ maxTime: undefined,};Graph.displayName = 'Graph';diff --git a/frontend/src/components/Graph/types.ts b/frontend/src/components/Graph/types.tsindex 4dd1d5bde4..139365671d 100644--- a/frontend/src/components/Graph/types.ts+++ b/frontend/src/components/Graph/types.ts@@ -59,6 +59,8 @@ export interface GraphProps {containerHeight?: string | number;onDragSelect?: (start: number, end: number) => void;dragSelectColor?: string;+ minTime?: number;+ maxTime?: number;ref?: ForwardedRef<ToggleGraphProps | undefined>;}diff --git a/frontend/src/components/Graph/utils.ts b/frontend/src/components/Graph/utils.tsindex 93a0d32f1f..b21f205b72 100644--- a/frontend/src/components/Graph/utils.ts+++ b/frontend/src/components/Graph/utils.ts@@ -53,6 +53,8 @@ export const getGraphOptions = (onClickHandler: GraphOnClickHandler | undefined,data: ChartData,timezone: Timezone,+ minTime?: number,+ maxTime?: number,// eslint-disable-next-line sonarjs/cognitive-complexity): CustomChartOptions => ({animation: {@@ -62,33 +64,35 @@ export const getGraphOptions = (maintainAspectRatio: false,interaction: {mode: 'index',- intersect: false,+ intersect: true,},plugins: {- annotation: staticLine+ ...(staticLine? {- annotations: [- {- type: 'line',- yMin: staticLine.yMin,- yMax: staticLine.yMax,- borderColor: staticLine.borderColor,- borderWidth: staticLine.borderWidth,- label: {- content: staticLine.lineText,- enabled: true,- font: {- size: 10,+ annotation: {+ annotations: [+ {+ type: 'line',+ yMin: staticLine.yMin,+ yMax: staticLine.yMax,+ borderColor: staticLine.borderColor,+ borderWidth: staticLine.borderWidth,+ label: {+ content: staticLine.lineText,+ enabled: true,+ font: {+ size: 10,+ },+ borderWidth: 0,+ position: 'start',+ backgroundColor: 'transparent',+ color: staticLine.textColor,},- borderWidth: 0,- position: 'start',- backgroundColor: 'transparent',- color: staticLine.textColor,},- },- ],+ ],+ },}- : undefined,+ : {}),title: {display: title !== undefined,text: title,@@ -169,6 +173,12 @@ export const getGraphOptions = (},type: 'time',ticks: { color: getAxisLabelColor(currentTheme) },+ ...(minTime && {+ min: dayjs(minTime).tz(timezone.value).format(),+ }),+ ...(maxTime && {+ max: dayjs(maxTime).tz(timezone.value).format(),+ }),},y: {stacked: isStacked,@@ -225,3 +235,9 @@ export const getGraphOptions = (}},});++declare module 'chart.js' {+ interface TooltipPositionerMap {+ custom: TooltipPositionerFunction<ChartType>;+ }+}diff --git a/frontend/src/components/Graph/xAxisConfig.ts b/frontend/src/components/Graph/xAxisConfig.tsindex 3fa0b00e08..01d750001f 100644--- a/frontend/src/components/Graph/xAxisConfig.ts+++ b/frontend/src/components/Graph/xAxisConfig.ts@@ -88,12 +88,16 @@ export const convertTimeRange = (/*** Accepts Chart.js data's data-structure and returns the relevant time unit for the axis based on the range of the data.*/-export const useXAxisTimeUnit = (data: Chart['data']): IAxisTimeConfig => {+export const useXAxisTimeUnit = (+ data: Chart['data'],+ minTime?: number,+ maxTime?: number,+): IAxisTimeConfig => {// Local time is the time range inferred from the input chart data.let localTime: ITimeRange | null;try {- let minTime = Number.POSITIVE_INFINITY;- let maxTime = Number.NEGATIVE_INFINITY;+ let minTimeLocal = Number.POSITIVE_INFINITY;+ let maxTimeLocal = Number.NEGATIVE_INFINITY;data?.labels?.forEach((timeStamp: unknown): void => {const getTimeStamp = (time: Date | number): Date | number | string => {if (time instanceof Date) {@@ -104,13 +108,13 @@ export const useXAxisTimeUnit = (data: Chart['data']): IAxisTimeConfig => {};const time = getTimeStamp(timeStamp as Date | number);- minTime = Math.min(parseInt(time.toString(), 10), minTime);- maxTime = Math.max(parseInt(time.toString(), 10), maxTime);+ minTimeLocal = Math.min(parseInt(time.toString(), 10), minTimeLocal);+ maxTimeLocal = Math.max(parseInt(time.toString(), 10), maxTimeLocal);});localTime = {- minTime: minTime === Number.POSITIVE_INFINITY ? null : minTime,- maxTime: maxTime === Number.NEGATIVE_INFINITY ? null : maxTime,+ minTime: minTimeLocal === Number.POSITIVE_INFINITY ? null : minTimeLocal,+ maxTime: maxTimeLocal === Number.NEGATIVE_INFINITY ? null : maxTimeLocal,};} catch (error) {localTime = null;@@ -122,19 +126,27 @@ export const useXAxisTimeUnit = (data: Chart['data']): IAxisTimeConfig => {(state) => state.globalTime,);- // Use local time if valid else use the global time range- const { maxTime, minTime } = useMemo(() => {+ // Use explicit minTime/maxTime if provided and valid, otherwise use local time if valid, else use global time range+ const { maxTime: finalMaxTime, minTime: finalMinTime } = useMemo(() => {+ // If both minTime and maxTime are explicitly provided and valid, use them+ if (minTime !== undefined && maxTime !== undefined && minTime <= maxTime) {+ return { minTime, maxTime };+ }++ // Otherwise, use local time if validif (localTime && localTime.maxTime && localTime.minTime) {return {minTime: localTime.minTime,maxTime: localTime.maxTime,};}++ // Fall back to global time rangereturn {minTime: globalTime.minTime / 1e6,maxTime: globalTime.maxTime / 1e6,};- }, [globalTime, localTime]);+ }, [globalTime, localTime, minTime, maxTime]);- return convertTimeRange(minTime, maxTime);+ return convertTimeRange(finalMinTime, finalMaxTime);};diff --git a/frontend/src/container/LogsExplorerChart/index.tsx b/frontend/src/container/LogsExplorerChart/index.tsxindex ca15909123..a17b37792d 100644--- a/frontend/src/container/LogsExplorerChart/index.tsx+++ b/frontend/src/container/LogsExplorerChart/index.tsx@@ -8,9 +8,11 @@ import getChartData, { GetChartDataProps } from 'lib/getChartData';import GetMinMax from 'lib/getMinMax';import { colors } from 'lib/getRandomColor';import { memo, useCallback, useMemo } from 'react';-import { useDispatch } from 'react-redux';+import { useDispatch, useSelector } from 'react-redux';import { useLocation } from 'react-router-dom';import { UpdateTimeInterval } from 'store/actions';+import { AppState } from 'store/reducers';+import { GlobalReducer } from 'types/reducer/globalTime';import { LogsExplorerChartProps } from './LogsExplorerChart.interfaces';import { CardStyled } from './LogsExplorerChart.styled';@@ -27,6 +29,11 @@ function LogsExplorerChart({const urlQuery = useUrlQuery();const location = useLocation();const { safeNavigate } = useSafeNavigate();++ // Access global time state for min/max range+ const { minTime, maxTime } = useSelector<AppState, GlobalReducer>(+ (state) => state.globalTime,+ );const handleCreateDatasets: Required<GetChartDataProps>['createDataset'] = useCallback((element, index, allLabels) => ({data: element,@@ -83,6 +90,15 @@ function LogsExplorerChart({[data, handleCreateDatasets],);+ // Convert nanosecond timestamps to milliseconds for Chart.js+ const { chartMinTime, chartMaxTime } = useMemo(+ () => ({+ chartMinTime: minTime ? Math.floor(minTime / 1e6) : undefined,+ chartMaxTime: maxTime ? Math.floor(maxTime / 1e6) : undefined,+ }),+ [minTime, maxTime],+ );+return (<CardStyled className={className}>{isLoading ? (@@ -95,6 +111,8 @@ function LogsExplorerChart({type="bar"animateonDragSelect={onDragSelect}+ minTime={chartMinTime}+ maxTime={chartMaxTime}/>)}</CardStyled>