Loki: improve backend-compatibility (#47579)

* loki: annotation queries: make them ready for the backend-mode

* removed unused code
This commit is contained in:
Gábor Farkas 2022-04-12 18:23:19 +02:00 committed by GitHub
parent 712b239d5a
commit a15c2021fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,7 @@ import Prism from 'prismjs';
import {
AnnotationEvent,
AnnotationQueryRequest,
CoreApp,
DataFrame,
DataFrameView,
DataQueryError,
@ -31,6 +32,7 @@ import {
QueryResultMeta,
ScopedVars,
TimeRange,
rangeUtil,
} from '@grafana/data';
import { BackendSrvRequest, FetchError, getBackendSrv, config, DataSourceWithBackend } from '@grafana/runtime';
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
@ -78,6 +80,21 @@ const DEFAULT_QUERY_PARAMS: Partial<LokiRangeQueryRequest> = {
query: '',
};
function makeRequest(query: LokiQuery, range: TimeRange, app: CoreApp, requestId: string): DataQueryRequest<LokiQuery> {
const intervalInfo = rangeUtil.calculateInterval(range, 1);
return {
targets: [query],
requestId,
interval: intervalInfo.interval,
intervalMs: intervalInfo.intervalMs,
range: range,
scopedVars: {},
timezone: 'UTC',
app,
startTime: Date.now(),
};
}
export class LokiDatasource
extends DataSourceWithBackend<LokiQuery, LokiOptions>
implements
@ -652,32 +669,25 @@ export class LokiDatasource
}
async annotationQuery(options: any): Promise<AnnotationEvent[]> {
const {
expr,
maxLines,
instant,
stepInterval,
tagKeys = '',
titleFormat = '',
textFormat = '',
} = options.annotation;
const { expr, maxLines, instant, tagKeys = '', titleFormat = '', textFormat = '' } = options.annotation;
if (!expr) {
return [];
}
const interpolatedExpr = this.templateSrv.replace(expr, {}, this.interpolateQueryExpr);
const query = {
refId: `annotation-${options.annotation.name}`,
expr: interpolatedExpr,
const id = `annotation-${options.annotation.name}`;
const query: LokiQuery = {
refId: id,
expr,
maxLines,
instant,
stepInterval,
queryType: instant ? LokiQueryType.Instant : LokiQueryType.Range,
};
const { data } = instant
? await lastValueFrom(this.runInstantQuery(query, options as any))
: await lastValueFrom(this.runRangeQuery(query, options as any));
const request = makeRequest(query, options.range, CoreApp.Dashboard, id);
const { data } = await lastValueFrom(this.query(request));
const annotations: AnnotationEvent[] = [];
const splitKeys: string[] = tagKeys.split(',').filter((v: string) => v !== '');