Skip to content

Commit

Permalink
feat(LogsPanelScene): issue queries in forward or backward direction …
Browse files Browse the repository at this point in the history
…depending on sort
  • Loading branch information
matyax committed Jan 6, 2025
1 parent 9cea79c commit bfc0b7d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
12 changes: 11 additions & 1 deletion src/Components/ServiceScene/LogsPanelScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
sceneGraph,
SceneObjectBase,
SceneObjectState,
SceneQueryRunner,
VizPanel,
} from '@grafana/scenes';
import { DataFrame, getValueFormat, LogRowModel } from '@grafana/data';
Expand All @@ -22,7 +23,7 @@ import { CopyLinkButton } from './CopyLinkButton';
import { getLogsPanelSortOrder, LogOptionsScene } from './LogOptionsScene';
import { LogsVolumePanel, logsVolumePanelKey } from './LogsVolumePanel';
import { getPanelWrapperStyles, PanelMenu } from '../Panels/PanelMenu';
import { ServiceScene } from './ServiceScene';
import { LOGS_PANEL_QUERY_REFID, ServiceScene } from './ServiceScene';

interface LogsPanelSceneState extends SceneObjectState {
body?: VizPanel;
Expand Down Expand Up @@ -104,6 +105,15 @@ export class LogsPanelScene extends SceneObjectBase<LogsPanelSceneState> {
if (!this.state.body) {
return;
}
if ('sortOrder' in options) {
const serviceScene = sceneGraph.getAncestor(this, ServiceScene);
const runners = sceneGraph.findDescendents(serviceScene, SceneQueryRunner);
runners.forEach((runner) => {
if (runner.isActive && runner.state.queries[0]?.refId === LOGS_PANEL_QUERY_REFID) {
runner.runQueries();
}
});
}
this.state.body.onOptionsChange(options);
}

Expand Down
30 changes: 23 additions & 7 deletions src/services/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@grafana/data';
import { config, DataSourceWithBackend, getDataSourceSrv } from '@grafana/runtime';
import { RuntimeDataSource, sceneUtils } from '@grafana/scenes';
import { DataQuery } from '@grafana/schema';
import { DataQuery, LogsSortOrder } from '@grafana/schema';
import { Observable, Subscriber } from 'rxjs';
import { getDataSource } from './scenes';
import { getPrimaryLabelFromUrl } from './routing';
Expand All @@ -19,11 +19,13 @@ import { FIELDS_TO_REMOVE, LABELS_TO_REMOVE, sortLabelsByCardinality } from './f
import { SERVICE_NAME } from './variables';
import { runShardSplitQuery } from './shardQuerySplitting';
import { requestSupportsSharding } from './logql';
import { LokiDatasource, LokiQuery } from './lokiQuery';
import { LokiDatasource, LokiQuery, LokiQueryDirection } from './lokiQuery';
import { SceneDataQueryRequest, SceneDataQueryResourceRequest, VolumeRequestProps } from './datasourceTypes';
import { logger } from './logger';
import { PLUGIN_ID } from './plugin';
import { sanitizeStreamSelector } from './query';
import { LOGS_PANEL_QUERY_REFID } from 'Components/ServiceScene/ServiceScene';
import { getLogsPanelSortOrder } from 'Components/ServiceScene/LogOptionsScene';

export const WRAPPED_LOKI_DS_UID = 'wrapped-loki-ds-uid';

Expand Down Expand Up @@ -141,11 +143,13 @@ export class WrappedLokiDatasource extends RuntimeDataSource<DataQuery> {

const updatedRequest = {
...request,
targets: ds.interpolateVariablesInQueries(request.targets, request.scopedVars).map((target) => ({
...target,
resource: undefined,
expr: sanitizeStreamSelector(target.expr),
})),
targets: this.applyQueryDirection(
ds.interpolateVariablesInQueries(request.targets, request.scopedVars).map((target) => ({
...target,
resource: undefined,
expr: sanitizeStreamSelector(target.expr),
}))
),
};

// Query the datasource and return either observable or promise
Expand Down Expand Up @@ -266,6 +270,18 @@ export class WrappedLokiDatasource extends RuntimeDataSource<DataQuery> {
return { interpolatedTarget, expression };
}

private applyQueryDirection(targets: LokiQuery[]) {
const sortOrder = getLogsPanelSortOrder();
return targets.map((target) => {
if (target.refId !== LOGS_PANEL_QUERY_REFID) {
return target;
}
target.direction =
sortOrder === LogsSortOrder.Descending ? LokiQueryDirection.Backward : LokiQueryDirection.Forward;
return target;
});
}

private async getDetectedLabels(
request: DataQueryRequest<LokiQuery & SceneDataQueryResourceRequest>,
ds: LokiDatasource,
Expand Down
7 changes: 7 additions & 0 deletions src/services/lokiQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { DataSourceRef } from '@grafana/schema';
import { DataSourceWithBackend } from '@grafana/runtime';
import { DataSourceJsonData } from '@grafana/data';

export enum LokiQueryDirection {
Backward = 'backward',
Forward = 'forward',
Scan = 'scan',
}

export type LokiQuery = {
refId: string;
queryType?: LokiQueryType;
Expand All @@ -13,6 +19,7 @@ export type LokiQuery = {
splitDuration?: string;
datasource?: DataSourceRef;
maxLines?: number;
direction?: LokiQueryDirection;
};

export type LokiQueryType = 'instant' | 'range' | 'stream' | string;
Expand Down

0 comments on commit bfc0b7d

Please sign in to comment.