mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 00:25:46 -06:00
* Add and configure eslint-plugin-import * Fix the lint:ts npm command * Autofix + prettier all the files * Manually fix remaining files * Move jquery code in jest-setup to external file to safely reorder imports * Resolve issue caused by circular dependencies within Prometheus * Update .betterer.results * Fix missing // @ts-ignore * ignore iconBundle.ts * Fix missing // @ts-ignore
77 lines
2.1 KiB
TypeScript
77 lines
2.1 KiB
TypeScript
import { Observable, of } from 'rxjs';
|
|
import { map, mergeMap } from 'rxjs/operators';
|
|
|
|
import { CoreApp, DataQueryRequest, DataSourceApi, rangeUtil, ScopedVars } from '@grafana/data';
|
|
|
|
import { runRequest } from '../query/state/runRequest';
|
|
|
|
import { standardAnnotationSupport } from './standardAnnotationSupport';
|
|
import { AnnotationQueryOptions, AnnotationQueryResponse } from './types';
|
|
|
|
let counter = 100;
|
|
function getNextRequestId() {
|
|
return 'AQ' + counter++;
|
|
}
|
|
|
|
export function executeAnnotationQuery(
|
|
options: AnnotationQueryOptions,
|
|
datasource: DataSourceApi,
|
|
savedJsonAnno: any
|
|
): Observable<AnnotationQueryResponse> {
|
|
const processor = {
|
|
...standardAnnotationSupport,
|
|
...datasource.annotations,
|
|
};
|
|
|
|
const annotation = processor.prepareAnnotation!(savedJsonAnno);
|
|
if (!annotation) {
|
|
return of({});
|
|
}
|
|
|
|
const query = processor.prepareQuery!(annotation);
|
|
if (!query) {
|
|
return of({});
|
|
}
|
|
|
|
// No more points than pixels
|
|
const maxDataPoints = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
|
|
|
|
// Add interval to annotation queries
|
|
const interval = rangeUtil.calculateInterval(options.range, maxDataPoints, datasource.interval);
|
|
|
|
const scopedVars: ScopedVars = {
|
|
__interval: { text: interval.interval, value: interval.interval },
|
|
__interval_ms: { text: interval.intervalMs.toString(), value: interval.intervalMs },
|
|
__annotation: { text: annotation.name, value: annotation },
|
|
};
|
|
|
|
const queryRequest: DataQueryRequest = {
|
|
startTime: Date.now(),
|
|
requestId: getNextRequestId(),
|
|
range: options.range,
|
|
maxDataPoints,
|
|
scopedVars,
|
|
...interval,
|
|
app: CoreApp.Dashboard,
|
|
|
|
timezone: options.dashboard.timezone,
|
|
|
|
targets: [
|
|
{
|
|
...query,
|
|
refId: 'Anno',
|
|
},
|
|
],
|
|
};
|
|
|
|
return runRequest(datasource, queryRequest).pipe(
|
|
mergeMap((panelData) => {
|
|
if (!panelData.series) {
|
|
return of({ panelData, events: [] });
|
|
}
|
|
|
|
return processor.processEvents!(annotation, panelData.series).pipe(map((events) => ({ panelData, events })));
|
|
})
|
|
);
|
|
}
|