Files
grafana/public/app/plugins/datasource/prometheus/variables.ts
Hugo Häggmark 112a755e18 Variables: Adds new Api that allows proper QueryEditors for Query variables (#28217)
* Initial

* WIP

* wip

* Refactor: fixing types

* Refactor: Fixed more typings

* Feature: Moves TestData to new API

* Feature: Moves CloudMonitoringDatasource to new API

* Feature: Moves PrometheusDatasource to new Variables API

* Refactor: Clean up comments

* Refactor: changes to QueryEditorProps instead

* Refactor: cleans up testdata, prometheus and cloud monitoring variable support

* Refactor: adds variableQueryRunner

* Refactor: adds props to VariableQueryEditor

* Refactor: reverted Loki editor

* Refactor: refactor queryrunner into smaller pieces

* Refactor: adds upgrade query thunk

* Tests: Updates old tests

* Docs: fixes build errors for exported api

* Tests: adds guard tests

* Tests: adds QueryRunner tests

* Tests: fixes broken tests

* Tests: adds variableQueryObserver tests

* Test: adds tests for operator functions

* Test: adds VariableQueryRunner tests

* Refactor: renames dataSource

* Refactor: adds definition for standard variable support

* Refactor: adds cancellation to OptionPicker

* Refactor: changes according to Dominiks suggestion

* Refactor:tt

* Refactor: adds tests for factories

* Refactor: restructuring a bit

* Refactor: renames variableQueryRunner.ts

* Refactor: adds quick exit when runRequest returns errors

* Refactor: using TextArea from grafana/ui

* Refactor: changed from interfaces to classes instead

* Tests: fixes broken test

* Docs: fixes doc issue count

* Docs: fixes doc issue count

* Refactor: Adds check for self referencing queries

* Tests: fixed unused variable

* Refactor: Changes comments
2020-11-18 15:10:32 +01:00

57 lines
1.9 KiB
TypeScript

import { from, Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import {
DataQueryRequest,
DataQueryResponse,
rangeUtil,
StandardVariableQuery,
StandardVariableSupport,
} from '@grafana/data';
import { getTemplateSrv, TemplateSrv } from '@grafana/runtime';
import { PrometheusDatasource } from './datasource';
import { PromQuery } from './types';
import PrometheusMetricFindQuery from './metric_find_query';
import { getTimeSrv, TimeSrv } from '../../../features/dashboard/services/TimeSrv';
export class PrometheusVariableSupport extends StandardVariableSupport<PrometheusDatasource> {
constructor(
private readonly datasource: PrometheusDatasource,
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
private readonly timeSrv: TimeSrv = getTimeSrv()
) {
super();
this.query = this.query.bind(this);
}
query(request: DataQueryRequest<PromQuery>): Observable<DataQueryResponse> {
const query = request.targets[0].expr;
if (!query) {
return of({ data: [] });
}
const scopedVars = {
...request.scopedVars,
__interval: { text: this.datasource.interval, value: this.datasource.interval },
__interval_ms: {
text: rangeUtil.intervalToMs(this.datasource.interval),
value: rangeUtil.intervalToMs(this.datasource.interval),
},
...this.datasource.getRangeScopedVars(this.timeSrv.timeRange()),
};
const interpolated = this.templateSrv.replace(query, scopedVars, this.datasource.interpolateQueryExpr);
const metricFindQuery = new PrometheusMetricFindQuery(this.datasource, interpolated);
const metricFindStream = from(metricFindQuery.process());
return metricFindStream.pipe(map(results => ({ data: results })));
}
toDataQuery(query: StandardVariableQuery): PromQuery {
return {
refId: 'PrometheusDatasource-VariableQuery',
expr: query.query,
};
}
}