Panel: Support using $__interval and $__interval_ms in panel title
This commit is contained in:
@@ -233,6 +233,18 @@ describe('PanelModel', () => {
|
||||
const out = model.replaceVariables('hello $aaa and $bbb', extra);
|
||||
expect(out).toBe('hello XXX and BBB');
|
||||
});
|
||||
|
||||
it('Can use request scoped vars', () => {
|
||||
model.getQueryRunner().getLastRequest = () => {
|
||||
return {
|
||||
scopedVars: {
|
||||
__interval: { text: '10m', value: '10m' },
|
||||
},
|
||||
};
|
||||
};
|
||||
const out = model.replaceVariables('hello $__interval');
|
||||
expect(out).toBe('hello 10m');
|
||||
});
|
||||
});
|
||||
|
||||
describe('when changing panel type', () => {
|
||||
|
||||
@@ -564,26 +564,21 @@ export class PanelModel implements DataConfigSource, IPanelModel {
|
||||
}
|
||||
|
||||
replaceVariables(value: string, extraVars: ScopedVars | undefined, format?: string | Function) {
|
||||
let vars = this.scopedVars;
|
||||
|
||||
if (extraVars) {
|
||||
vars = vars ? { ...vars, ...extraVars } : extraVars;
|
||||
}
|
||||
const lastRequest = this.getQueryRunner().getLastRequest();
|
||||
const vars: ScopedVars = Object.assign({}, this.scopedVars, lastRequest?.scopedVars, extraVars);
|
||||
|
||||
const allVariablesParams = getVariablesUrlParams(vars);
|
||||
const variablesQuery = urlUtil.toUrlParams(allVariablesParams);
|
||||
const timeRangeUrl = urlUtil.toUrlParams(getTimeSrv().timeRangeForUrl());
|
||||
|
||||
vars = {
|
||||
...vars,
|
||||
[DataLinkBuiltInVars.keepTime]: {
|
||||
text: timeRangeUrl,
|
||||
value: timeRangeUrl,
|
||||
},
|
||||
[DataLinkBuiltInVars.includeVars]: {
|
||||
text: variablesQuery,
|
||||
value: variablesQuery,
|
||||
},
|
||||
vars[DataLinkBuiltInVars.keepTime] = {
|
||||
text: timeRangeUrl,
|
||||
value: timeRangeUrl,
|
||||
};
|
||||
|
||||
vars[DataLinkBuiltInVars.includeVars] = {
|
||||
text: variablesQuery,
|
||||
value: variablesQuery,
|
||||
};
|
||||
|
||||
return getTemplateSrv().replace(value, vars, format);
|
||||
@@ -602,7 +597,7 @@ export class PanelModel implements DataConfigSource, IPanelModel {
|
||||
* If you need the raw title without interpolation use title property instead.
|
||||
* */
|
||||
getDisplayTitle(): string {
|
||||
return this.replaceVariables(this.title, {}, 'text');
|
||||
return this.replaceVariables(this.title, undefined, 'text');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,7 @@ export class PanelQueryRunner {
|
||||
private subscription?: Unsubscribable;
|
||||
private lastResult?: PanelData;
|
||||
private dataConfigSource: DataConfigSource;
|
||||
private lastRequest?: DataQueryRequest;
|
||||
|
||||
constructor(dataConfigSource: DataConfigSource) {
|
||||
this.subject = new ReplaySubject(1);
|
||||
@@ -251,6 +252,8 @@ export class PanelQueryRunner {
|
||||
request.interval = norm.interval;
|
||||
request.intervalMs = norm.intervalMs;
|
||||
|
||||
this.lastRequest = request;
|
||||
|
||||
this.pipeToSubject(runRequest(ds, request), panelId);
|
||||
} catch (err) {
|
||||
console.error('PanelQueryRunner Error', err);
|
||||
@@ -333,6 +336,10 @@ export class PanelQueryRunner {
|
||||
getLastResult(): PanelData | undefined {
|
||||
return this.lastResult;
|
||||
}
|
||||
|
||||
getLastRequest(): DataQueryRequest | undefined {
|
||||
return this.lastRequest;
|
||||
}
|
||||
}
|
||||
|
||||
async function getDataSource(
|
||||
|
||||
Reference in New Issue
Block a user