diff --git a/docs/sources/reference/templating.md b/docs/sources/reference/templating.md index 8bf8c9bf2b6..0caf4cc5682 100644 --- a/docs/sources/reference/templating.md +++ b/docs/sources/reference/templating.md @@ -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 diff --git a/public/app/features/templating/specs/variable_srv.test.ts b/public/app/features/templating/specs/variable_srv.test.ts index c0a5b5bc6c7..c8fdbc54151 100644 --- a/public/app/features/templating/specs/variable_srv.test.ts +++ b/public/app/features/templating/specs/variable_srv.test.ts @@ -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; diff --git a/public/app/features/templating/specs/variable_srv_init.test.ts b/public/app/features/templating/specs/variable_srv_init.test.ts index af73dd43ea9..bb0dee1e131 100644 --- a/public/app/features/templating/specs/variable_srv_init.test.ts +++ b/public/app/features/templating/specs/variable_srv_init.test.ts @@ -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; diff --git a/public/app/features/templating/variable_srv.ts b/public/app/features/templating/variable_srv.ts index cc38359e4de..611b0901236 100644 --- a/public/app/features/templating/variable_srv.ts +++ b/public/app/features/templating/variable_srv.ts @@ -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; + }, + }, + }); }); }