mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Templating: Add new global built-in variables (#21790)
* Add new global built-in variables #20523 new branch from master * #20523 Revert change on __from and __to like requested * #20523 simplify contextSrv access * #20523 simplify contextSrv access * #20523 repair jest tests
This commit is contained in:
parent
93195facba
commit
7720bfab95
@ -347,6 +347,18 @@ This variable is only available in the Singlestat panel and can be used in the p
|
||||
|
||||
Currently only supported for Prometheus data sources. This variable represents the range for the current dashboard. It is calculated by `to - from`. It has a millisecond and a second representation called `$__range_ms` and `$__range_s`.
|
||||
|
||||
### The $__dashboard Variable
|
||||
> Only available in Grafana v6.6+
|
||||
|
||||
This variable is the UID of the current dashboard.
|
||||
`${__dashboard.name}` is the name of the current dashboard.
|
||||
|
||||
### The $__org Variable
|
||||
> Only available in Grafana v6.6+
|
||||
|
||||
This variable is the ID of the current organization.
|
||||
`${__org.name}` is the name of the current organization.
|
||||
|
||||
## Repeating Panels
|
||||
|
||||
Template variables can be very useful to dynamically change your queries across a whole dashboard. If you want
|
||||
|
@ -6,6 +6,12 @@ import $q from 'q';
|
||||
import { dateTime } from '@grafana/data';
|
||||
import { CustomVariable } from '../custom_variable';
|
||||
|
||||
jest.mock('app/core/core', () => ({
|
||||
contextSrv: {
|
||||
user: { orgId: 1, orgName: 'TestOrg' },
|
||||
},
|
||||
}));
|
||||
|
||||
describe('VariableSrv', function(this: any) {
|
||||
const ctx = {
|
||||
datasourceSrv: {},
|
||||
@ -26,6 +32,7 @@ describe('VariableSrv', function(this: any) {
|
||||
this.variables = vars;
|
||||
},
|
||||
updateIndex: () => {},
|
||||
setGlobalVariable: (name: string, variable: any) => {},
|
||||
replace: (str: any) =>
|
||||
str.replace(this.regex, (match: string) => {
|
||||
return match;
|
||||
|
@ -6,6 +6,12 @@ import { DashboardModel } from '../../dashboard/state/DashboardModel';
|
||||
// @ts-ignore
|
||||
import $q from 'q';
|
||||
|
||||
jest.mock('app/core/core', () => ({
|
||||
contextSrv: {
|
||||
user: { orgId: 1, orgName: 'TestOrg' },
|
||||
},
|
||||
}));
|
||||
|
||||
describe('VariableSrv init', function(this: any) {
|
||||
const templateSrv = {
|
||||
init: (vars: any) => {
|
||||
@ -13,6 +19,7 @@ describe('VariableSrv init', function(this: any) {
|
||||
},
|
||||
variableInitialized: () => {},
|
||||
updateIndex: () => {},
|
||||
setGlobalVariable: (name: string, variable: any) => {},
|
||||
replace: (str: string) =>
|
||||
str.replace(this.regex, match => {
|
||||
return match;
|
||||
|
@ -14,7 +14,7 @@ import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
|
||||
import { TimeRange, AppEvents } from '@grafana/data';
|
||||
import { CoreEvents } from 'app/types';
|
||||
import { UrlQueryMap } from '@grafana/runtime';
|
||||
import { appEvents } from 'app/core/core';
|
||||
import { appEvents, contextSrv } from 'app/core/core';
|
||||
|
||||
export class VariableSrv {
|
||||
dashboard: DashboardModel;
|
||||
@ -55,6 +55,24 @@ export class VariableSrv {
|
||||
)
|
||||
.then(() => {
|
||||
this.templateSrv.updateIndex();
|
||||
this.templateSrv.setGlobalVariable('__dashboard', {
|
||||
value: {
|
||||
name: dashboard.title,
|
||||
uid: dashboard.uid,
|
||||
toString: function() {
|
||||
return this.uid;
|
||||
},
|
||||
},
|
||||
});
|
||||
this.templateSrv.setGlobalVariable('__org', {
|
||||
value: {
|
||||
name: contextSrv.user.orgName,
|
||||
id: contextSrv.user.id,
|
||||
toString: function() {
|
||||
return this.id;
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user