TemplateSrv: expose the replace function (#24353)

This commit is contained in:
Ryan McKinley 2020-05-06 11:15:31 -07:00 committed by GitHub
parent fa260fec87
commit d891cc5949
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 3 deletions

View File

@ -1,4 +1,4 @@
import { VariableModel } from '@grafana/data';
import { VariableModel, ScopedVars } from '@grafana/data';
/**
* Via the TemplateSrv consumers get access to all the available template variables
@ -8,7 +8,15 @@ import { VariableModel } from '@grafana/data';
* @public
*/
export interface TemplateSrv {
/**
* List the dashboard variables
*/
getVariables(): VariableModel[];
/**
* Replace the values within the target string. See also {@link InterpolateFunction}
*/
replace(target: string, scopedVars?: ScopedVars, format?: string | Function): string;
}
let singletonInstance: TemplateSrv;

View File

@ -317,7 +317,7 @@ export class TemplateSrv implements BaseTemplateSrv {
return scopedVar.value;
}
replace(target: string, scopedVars?: ScopedVars, format?: string | Function): any {
replace(target: string, scopedVars?: ScopedVars, format?: string | Function): string {
if (!target) {
return target;
}

View File

@ -300,7 +300,7 @@ export class CloudWatchDatasource extends DataSourceApi<CloudWatchQuery, CloudWa
}
getPeriod(target: CloudWatchMetricsQuery, options: any) {
let period = this.templateSrv.replace(target.period, options.scopedVars);
let period = this.templateSrv.replace(target.period, options.scopedVars) as any;
if (period && period.toLowerCase() !== 'auto') {
if (/^\d+$/.test(period)) {
period = parseInt(period, 10);