Commit d891cc59 by Ryan McKinley Committed by GitHub

TemplateSrv: expose the replace function (#24353)

parent fa260fec
import { VariableModel } from '@grafana/data'; import { VariableModel, ScopedVars } from '@grafana/data';
/** /**
* Via the TemplateSrv consumers get access to all the available template variables * Via the TemplateSrv consumers get access to all the available template variables
...@@ -8,7 +8,15 @@ import { VariableModel } from '@grafana/data'; ...@@ -8,7 +8,15 @@ import { VariableModel } from '@grafana/data';
* @public * @public
*/ */
export interface TemplateSrv { export interface TemplateSrv {
/**
* List the dashboard variables
*/
getVariables(): VariableModel[]; 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; let singletonInstance: TemplateSrv;
......
...@@ -317,7 +317,7 @@ export class TemplateSrv implements BaseTemplateSrv { ...@@ -317,7 +317,7 @@ export class TemplateSrv implements BaseTemplateSrv {
return scopedVar.value; return scopedVar.value;
} }
replace(target: string, scopedVars?: ScopedVars, format?: string | Function): any { replace(target: string, scopedVars?: ScopedVars, format?: string | Function): string {
if (!target) { if (!target) {
return target; return target;
} }
......
...@@ -300,7 +300,7 @@ export class CloudWatchDatasource extends DataSourceApi<CloudWatchQuery, CloudWa ...@@ -300,7 +300,7 @@ export class CloudWatchDatasource extends DataSourceApi<CloudWatchQuery, CloudWa
} }
getPeriod(target: CloudWatchMetricsQuery, options: any) { 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 (period && period.toLowerCase() !== 'auto') {
if (/^\d+$/.test(period)) { if (/^\d+$/.test(period)) {
period = parseInt(period, 10); period = parseInt(period, 10);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment