-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { getDefaultTimeRange, MetricFindValue, TimeRange } from '@grafana/data'; | ||
|
||
import { DataSource } from './DataSource'; | ||
import { lastValueFrom } from 'rxjs'; | ||
import { doFetch } from './doFetch'; | ||
import { BackendDataSourceResponse } from '@grafana/runtime'; | ||
import { map } from 'rxjs/operators'; | ||
import { ResponseParser } from './response_parser'; | ||
|
||
export class MetricFindQuery { | ||
range: TimeRange; | ||
responseParser: ResponseParser; | ||
|
||
constructor( | ||
private datasource: DataSource, | ||
private query: string | ||
) { | ||
this.datasource = datasource; | ||
this.query = query; | ||
this.range = getDefaultTimeRange(); | ||
this.responseParser = new ResponseParser(); | ||
} | ||
|
||
process(timeRange: TimeRange | undefined = getDefaultTimeRange()): Promise<MetricFindValue[]> { | ||
return lastValueFrom( | ||
doFetch<BackendDataSourceResponse>(this.datasource, { | ||
url: `${this.datasource.url}/variable`, | ||
data: { | ||
payload: this.query, | ||
range: timeRange, | ||
}, | ||
method: 'POST', | ||
}).pipe(map((response) => this.responseParser.transformMetricFindResponse(response))) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Core Grafana history https://github.com/grafana/grafana/blob/v11.0.0-preview/public/app/plugins/datasource/prometheus/variables.ts | ||
import { from, Observable, of } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
import { CustomVariableSupport, DataQueryRequest, DataQueryResponse, rangeUtil } from '@grafana/data'; | ||
import { getTemplateSrv, TemplateSrv } from '@grafana/runtime'; | ||
|
||
import { DataSource } from '../DataSource'; | ||
import { VariableQueryEditor } from '../Component/VariableQueryEditor'; | ||
import { VariableQuery } from '../types'; | ||
import { MetricFindQuery } from '../MetricFindQuery'; | ||
|
||
export class VariableSupport extends CustomVariableSupport<DataSource> { | ||
// export class VariableSupport extends DataSourceVariableSupport<DataSource> { | ||
constructor( | ||
private readonly datasource: DataSource, | ||
private readonly templateSrv: TemplateSrv = getTemplateSrv() | ||
) { | ||
super(); | ||
} | ||
|
||
editor = VariableQueryEditor; | ||
|
||
query(request: DataQueryRequest<VariableQuery>): Observable<DataQueryResponse> { | ||
const query = typeof request.targets[0] === 'string' ? request.targets[0] : request.targets[0]; | ||
|
||
if (!query) { | ||
return of({ data: [] }); | ||
} | ||
|
||
const scopedVars = { | ||
...request.scopedVars, | ||
__interval: { text: this.datasource.interval, value: this.datasource.interval }, | ||
__interval_ms: | ||
this.datasource.interval === undefined | ||
? undefined | ||
: { | ||
text: rangeUtil.intervalToMs(this.datasource.interval), | ||
value: rangeUtil.intervalToMs(this.datasource.interval), | ||
}, | ||
}; | ||
|
||
const interpolated: string = | ||
query.format === 'json' | ||
? JSON.parse(this.templateSrv.replace(query.query, scopedVars, 'json')) | ||
: { | ||
target: this.templateSrv.replace(query.query, scopedVars, 'regex'), | ||
}; | ||
|
||
const metricFindQuery = new MetricFindQuery(this.datasource, interpolated); | ||
const metricFindStream = from(metricFindQuery.process(request.range)); | ||
|
||
return metricFindStream.pipe(map((results) => ({ data: results }))); | ||
} | ||
} |