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.

fix: recalculate the query_range start and end timestamps for logs explorer chart and list queries (#8277)

Shaheer Kochai35e816546318
diff --git a/frontend/src/container/LogsExplorerViews/index.tsx b/frontend/src/container/LogsExplorerViews/index.tsx
index 5b5afdb7d6..df27831299 100644
--- a/frontend/src/container/LogsExplorerViews/index.tsx
+++ b/frontend/src/container/LogsExplorerViews/index.tsx
@@ -58,7 +58,8 @@ import {
useRef,
useState,
} from 'react';
-import { useSelector } from 'react-redux';
+import { useDispatch, useSelector } from 'react-redux';
+import { UpdateTimeInterval } from 'store/actions';
import { AppState } from 'store/reducers';
import { Dashboard } from 'types/api/dashboard/getAll';
import { ILog } from 'types/api/logs/log';
@@ -95,6 +96,7 @@ function LogsExplorerViews({
chartQueryKeyRef: MutableRefObject<any>;
}): JSX.Element {
const { safeNavigate } = useSafeNavigate();
+ const dispatch = useDispatch();
// this is to respect the panel type present in the URL rather than defaulting it to list always.
const panelTypes = useGetPanelTypesQueryParam(PANEL_TYPES.LIST);
@@ -106,9 +108,10 @@ function LogsExplorerViews({
DEFAULT_PER_PAGE_VALUE,
);
- const { minTime, maxTime } = useSelector<AppState, GlobalReducer>(
- (state) => state.globalTime,
- );
+ const { minTime, maxTime, selectedTime } = useSelector<
+ AppState,
+ GlobalReducer
+ >((state) => state.globalTime);
const currentMinTimeRef = useRef<number>(minTime);
@@ -134,6 +137,7 @@ function LogsExplorerViews({
const [showFormatMenuItems, setShowFormatMenuItems] = useState(false);
const [queryId, setQueryId] = useState<string>(v4());
const [queryStats, setQueryStats] = useState<WsDataEvent>();
+ const [listChartQuery, setListChartQuery] = useState<Query | null>(null);
const listQuery = useMemo(() => {
if (!stagedQuery || stagedQuery.builder.queryData.length < 1) return null;
@@ -170,8 +174,11 @@ function LogsExplorerViews({
return logs.length >= listQuery.limit;
}, [logs.length, listQuery]);
- const listChartQuery = useMemo(() => {
- if (!stagedQuery || !listQuery) return null;
+ useEffect(() => {
+ if (!stagedQuery || !listQuery) {
+ setListChartQuery(null);
+ return;
+ }
const modifiedQueryData: IBuilderQuery = {
...listQuery,
@@ -220,7 +227,7 @@ function LogsExplorerViews({
},
};
- return modifiedQuery;
+ setListChartQuery(modifiedQuery);
}, [stagedQuery, listQuery, activeLogId]);
const exportDefaultQuery = useMemo(
@@ -256,6 +263,8 @@ function LogsExplorerViews({
{},
undefined,
chartQueryKeyRef,
+ undefined,
+ 'custom',
);
const {
@@ -502,6 +511,16 @@ function LogsExplorerViews({
requestData?.id !== stagedQuery?.id ||
currentMinTimeRef.current !== minTime
) {
+ // Recalculate global time when query changes i.e. stage and run query clicked
+ if (
+ !!requestData?.id &&
+ stagedQuery?.id &&
+ requestData?.id !== stagedQuery?.id &&
+ selectedTime !== 'custom'
+ ) {
+ dispatch(UpdateTimeInterval(selectedTime));
+ }
+
const newRequestData = getRequestData(stagedQuery, {
filters: listQuery?.filters || initialFilters,
page: 1,
@@ -523,6 +542,9 @@ function LogsExplorerViews({
activeLogId,
panelType,
selectedView,
+ dispatch,
+ selectedTime,
+ maxTime,
]);
const chartData = useMemo(() => {
diff --git a/frontend/src/container/LogsExplorerViews/tests/LogsExplorerViews.test.tsx b/frontend/src/container/LogsExplorerViews/tests/LogsExplorerViews.test.tsx
index e29f598a6a..408be24b31 100644
--- a/frontend/src/container/LogsExplorerViews/tests/LogsExplorerViews.test.tsx
+++ b/frontend/src/container/LogsExplorerViews/tests/LogsExplorerViews.test.tsx
@@ -8,7 +8,7 @@ import { SELECTED_VIEWS } from 'pages/LogsExplorer/utils';
import { PreferenceContextProvider } from 'providers/preferences/context/PreferenceContextProvider';
import { QueryBuilderContext } from 'providers/QueryBuilder';
import { VirtuosoMockContext } from 'react-virtuoso';
-import { fireEvent, render, RenderResult } from 'tests/test-utils';
+import { fireEvent, render, RenderResult, waitFor } from 'tests/test-utils';
import { TagFilterItem } from 'types/api/queryBuilder/queryBuilderData';
import LogsExplorerViews from '..';
@@ -197,12 +197,16 @@ describe('LogsExplorerViews -', () => {
).toBeInTheDocument();
});
- it('should add activeLogId filter when present in URL', () => {
+ it('should add activeLogId filter when present in URL', async () => {
// Mock useCopyLogLink to return an activeLogId
(useCopyLogLink as jest.Mock).mockReturnValue({
activeLogId: ACTIVE_LOG_ID,
});
+ const originalFiltersLength =
+ mockQueryBuilderContextValue.currentQuery.builder.queryData[0].filters?.items
+ .length || 0;
+
lodsQueryServerRequest();
render(
<QueryBuilderContext.Provider value={mockQueryBuilderContextValue}>
@@ -218,27 +222,36 @@ describe('LogsExplorerViews -', () => {
</QueryBuilderContext.Provider>,
);
- // Get the query data from the first call to useGetExplorerQueryRange
- const {
- queryData,
- } = (useGetExplorerQueryRange as jest.Mock).mock.calls[0][0].builder;
- const firstQuery = queryData[0];
+ await waitFor(() => {
+ const listCall = (useGetExplorerQueryRange as jest.Mock).mock.calls.find(
+ (call) =>
+ call[0] &&
+ call[0].builder.queryData[0].filters.items.length ===
+ originalFiltersLength + 1,
+ );
- // Get the original number of filters from mock data
- const originalFiltersLength =
- mockQueryBuilderContextValue.currentQuery.builder.queryData[0].filters?.items
- .length || 0;
- const expectedFiltersLength = originalFiltersLength + 1; // +1 for activeLogId filter
+ expect(listCall).toBeDefined();
- // Verify that the activeLogId filter is present
- expect(
- firstQuery.filters?.items.some(
- (item: TagFilterItem) =>
- item.key?.key === 'id' && item.op === '<=' && item.value === ACTIVE_LOG_ID,
- ),
- ).toBe(true);
+ if (listCall) {
+ const { queryData } = listCall[0].builder;
+
+ const firstQuery = queryData[0];
- // Verify the total number of filters (original + 1 new activeLogId filter)
- expect(firstQuery.filters?.items.length).toBe(expectedFiltersLength);
+ const expectedFiltersLength = originalFiltersLength + 1; // +1 for activeLogId filter
+
+ // Verify that the activeLogId filter is present
+ expect(
+ firstQuery.filters?.items.some(
+ (item: TagFilterItem) =>
+ item.key?.key === 'id' &&
+ item.op === '<=' &&
+ item.value === ACTIVE_LOG_ID,
+ ),
+ ).toBe(true);
+
+ // Verify the total number of filters (original + 1 new activeLogId filter)
+ expect(firstQuery.filters?.items.length).toBe(expectedFiltersLength);
+ }
+ });
});
});