Skip to content

Commit

Permalink
Logs: Re-run Loki queries in Explore when direction and sort order ar…
Browse files Browse the repository at this point in the history
…e changed (grafana#99994)
  • Loading branch information
matyax authored Feb 25, 2025
1 parent 1302ee4 commit c525031
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions public/app/features/explore/Logs/Logs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ jest.mock('../state/explorePane', () => ({
changePanelState: (exploreId: string, panel: 'logs', panelState: {} | ExploreLogsPanelState) => {
return fakeChangePanelState(exploreId, panel, panelState);
},
changeQueries: (args: { queries: DataQuery[]; exploreId: string | undefined }) => {
return fakeChangeQueries(args);
},
}));

const fakeChangeQueries = jest.fn().mockReturnValue({ type: 'fakeChangeQueries' });
const fakeRunQueries = jest.fn().mockReturnValue({ type: 'fakeRunQueries' });
jest.mock('../state/query', () => ({
...jest.requireActual('../state/query'),
changeQueries: (args: { queries: DataQuery[]; exploreId: string | undefined }) => {
return fakeChangeQueries(args);
},
runQueries: (args: { queries: DataQuery[]; exploreId: string | undefined }) => {
return fakeRunQueries(args);
},
}));

describe('Logs', () => {
Expand Down Expand Up @@ -388,6 +389,7 @@ describe('Logs', () => {
expect(logRows.length).toBe(3);
expect(logRows[0].textContent).toContain('log message 1');
expect(logRows[2].textContent).toContain('log message 3');
expect(fakeRunQueries).not.toHaveBeenCalled();
});

it('should sync the query direction when changing the order of loki queries', async () => {
Expand All @@ -399,6 +401,7 @@ describe('Logs', () => {
exploreId: 'left',
queries: [{ ...query, direction: LokiQueryDirection.Forward }],
});
expect(fakeRunQueries).toHaveBeenCalledWith({ exploreId: 'left' });
});

it('should not change the query direction when changing the order of non-loki queries', async () => {
Expand Down
6 changes: 3 additions & 3 deletions public/app/features/explore/Logs/Logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ import {
import { useContentOutlineContext } from '../ContentOutline/ContentOutlineContext';
import { getUrlStateFromPaneState } from '../hooks/useStateSync';
import { changePanelState } from '../state/explorePane';
import { changeQueries } from '../state/query';
import { changeQueries, runQueries } from '../state/query';

import { LogsFeedback } from './LogsFeedback';
import { LogsMetaRow } from './LogsMetaRow';
Expand Down Expand Up @@ -478,12 +478,11 @@ const UnthemedLogs: React.FunctionComponent<Props> = (props: Props) => {
if (query.datasource?.type !== 'loki' || !isLokiQuery(query)) {
return query;
}
hasLokiQueries = true;

if (query.direction === LokiQueryDirection.Scan) {
// Don't override Scan. When the direction is Scan it means that the user specifically assigned this direction to the query.
return query;
}
hasLokiQueries = true;
const newDirection =
newSortOrder === LogsSortOrder.Ascending ? LokiQueryDirection.Forward : LokiQueryDirection.Backward;
if (newDirection !== query.direction) {
Expand All @@ -494,6 +493,7 @@ const UnthemedLogs: React.FunctionComponent<Props> = (props: Props) => {

if (hasLokiQueries) {
dispatch(changeQueries({ exploreId, queries: newQueries }));
dispatch(runQueries({ exploreId }));
}
}

Expand Down

0 comments on commit c525031

Please sign in to comment.