Commit 78cb3239 by Peter Holmberg

adding from and to built in variables

parent ab322beb
...@@ -9,6 +9,7 @@ import sortByKeys from 'app/core/utils/sort_by_keys'; ...@@ -9,6 +9,7 @@ import sortByKeys from 'app/core/utils/sort_by_keys';
import { PanelModel } from './panel_model'; import { PanelModel } from './panel_model';
import { DashboardMigrator } from './dashboard_migration'; import { DashboardMigrator } from './dashboard_migration';
import { TimeRange } from '@grafana/ui/src';
export class DashboardModel { export class DashboardModel {
id: any; id: any;
...@@ -200,8 +201,8 @@ export class DashboardModel { ...@@ -200,8 +201,8 @@ export class DashboardModel {
this.events.emit('view-mode-changed', panel); this.events.emit('view-mode-changed', panel);
} }
timeRangeUpdated() { timeRangeUpdated(timeRange: TimeRange) {
this.events.emit('time-range-updated'); this.events.emit('time-range-updated', timeRange);
} }
startRefresh() { startRefresh() {
......
...@@ -147,7 +147,7 @@ export class TimeSrv { ...@@ -147,7 +147,7 @@ export class TimeSrv {
} }
refreshDashboard() { refreshDashboard() {
this.dashboard.timeRangeUpdated(); this.dashboard.timeRangeUpdated(this.timeRange());
} }
private startNextRefreshTimer(afterMs) { private startNextRefreshTimer(afterMs) {
......
...@@ -8,7 +8,9 @@ describe('VariableSrv', function(this: any) { ...@@ -8,7 +8,9 @@ describe('VariableSrv', function(this: any) {
const ctx = { const ctx = {
datasourceSrv: {}, datasourceSrv: {},
timeSrv: { timeSrv: {
timeRange: () => {}, timeRange: () => {
return { from: '2018-01-29', to: '2019-01-29' };
},
}, },
$rootScope: { $rootScope: {
$on: () => {}, $on: () => {},
...@@ -45,7 +47,14 @@ describe('VariableSrv', function(this: any) { ...@@ -45,7 +47,14 @@ describe('VariableSrv', function(this: any) {
const ds: any = {}; const ds: any = {};
ds.metricFindQuery = () => Promise.resolve(scenario.queryResult); ds.metricFindQuery = () => Promise.resolve(scenario.queryResult);
ctx.variableSrv = new VariableSrv(ctx.$rootScope, $q, ctx.$location, ctx.$injector, ctx.templateSrv); ctx.variableSrv = new VariableSrv(
ctx.$rootScope,
$q,
ctx.$location,
ctx.$injector,
ctx.templateSrv,
ctx.timeSrv
);
ctx.variableSrv.timeSrv = ctx.timeSrv; ctx.variableSrv.timeSrv = ctx.timeSrv;
ctx.datasourceSrv = { ctx.datasourceSrv = {
......
...@@ -18,6 +18,12 @@ describe('VariableSrv init', function(this: any) { ...@@ -18,6 +18,12 @@ describe('VariableSrv init', function(this: any) {
}), }),
}; };
const timeSrv = {
timeRange: () => {
return { from: '2018-01-29', to: '2019-01-29' };
},
};
const $injector = {} as any; const $injector = {} as any;
const $rootscope = { const $rootscope = {
$on: () => {}, $on: () => {},
...@@ -47,7 +53,7 @@ describe('VariableSrv init', function(this: any) { ...@@ -47,7 +53,7 @@ describe('VariableSrv init', function(this: any) {
templateSrv, templateSrv,
}; };
ctx.variableSrv = new VariableSrv($rootscope, $q, {}, $injector, templateSrv); ctx.variableSrv = new VariableSrv($rootscope, $q, {}, $injector, templateSrv, timeSrv);
$injector.instantiate = (variable, model) => { $injector.instantiate = (variable, model) => {
return getVarMockConstructor(variable, model, ctx); return getVarMockConstructor(variable, model, ctx);
......
import kbn from 'app/core/utils/kbn'; import kbn from 'app/core/utils/kbn';
import _ from 'lodash'; import _ from 'lodash';
import { variableRegex } from 'app/features/templating/variable'; import { variableRegex } from 'app/features/templating/variable';
import { TimeRange } from '@grafana/ui/src';
function luceneEscape(value) { function luceneEscape(value) {
return value.replace(/([\!\*\+\-\=<>\s\&\|\(\)\[\]\{\}\^\~\?\:\\/"])/g, '\\$1'); return value.replace(/([\!\*\+\-\=<>\s\&\|\(\)\[\]\{\}\^\~\?\:\\/"])/g, '\\$1');
...@@ -13,6 +14,7 @@ export class TemplateSrv { ...@@ -13,6 +14,7 @@ export class TemplateSrv {
private index = {}; private index = {};
private grafanaVariables = {}; private grafanaVariables = {};
private builtIns = {}; private builtIns = {};
private timeRange: TimeRange = null;
constructor() { constructor() {
this.builtIns['__interval'] = { text: '1s', value: '1s' }; this.builtIns['__interval'] = { text: '1s', value: '1s' };
...@@ -20,8 +22,9 @@ export class TemplateSrv { ...@@ -20,8 +22,9 @@ export class TemplateSrv {
this.variables = []; this.variables = [];
} }
init(variables) { init(variables, timeRange?: TimeRange) {
this.variables = variables; this.variables = variables;
this.timeRange = timeRange;
this.updateTemplateData(); this.updateTemplateData();
} }
...@@ -34,6 +37,26 @@ export class TemplateSrv { ...@@ -34,6 +37,26 @@ export class TemplateSrv {
} }
return acc; return acc;
}, {}); }, {});
if (this.timeRange) {
const from = this.timeRange.from.valueOf().toString();
const to = this.timeRange.to.valueOf().toString();
this.index = {
...this.index,
['__from']: {
current: { value: from, text: from },
},
['__to']: {
current: { value: to, text: to },
},
};
}
}
updateTimeVariables(timeRange: TimeRange) {
this.timeRange = timeRange;
this.updateTemplateData();
} }
variableInitialized(variable) { variableInitialized(variable) {
...@@ -81,8 +104,14 @@ export class TemplateSrv { ...@@ -81,8 +104,14 @@ export class TemplateSrv {
// also the sub-delims "!", "'", "(", ")" and "*" are encoded; // also the sub-delims "!", "'", "(", ")" and "*" are encoded;
// unicode handling uses UTF-8 as in ECMA-262. // unicode handling uses UTF-8 as in ECMA-262.
encodeURIComponentStrict(str) { encodeURIComponentStrict(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, (c) => { return encodeURIComponent(str).replace(/[!'()*]/g, c => {
return '%' + c.charCodeAt(0).toString(16).toUpperCase(); return (
'%' +
c
.charCodeAt(0)
.toString(16)
.toUpperCase()
);
}); });
} }
...@@ -256,7 +285,7 @@ export class TemplateSrv { ...@@ -256,7 +285,7 @@ export class TemplateSrv {
const value = this.grafanaVariables[variable.current.value]; const value = this.grafanaVariables[variable.current.value];
return typeof(value) === 'string' ? value : variable.current.text; return typeof value === 'string' ? value : variable.current.text;
}); });
} }
......
...@@ -6,13 +6,14 @@ import _ from 'lodash'; ...@@ -6,13 +6,14 @@ import _ from 'lodash';
import coreModule from 'app/core/core_module'; import coreModule from 'app/core/core_module';
import { variableTypes } from './variable'; import { variableTypes } from './variable';
import { Graph } from 'app/core/utils/dag'; import { Graph } from 'app/core/utils/dag';
import { TimeRange } from '@grafana/ui/src';
export class VariableSrv { export class VariableSrv {
dashboard: any; dashboard: any;
variables: any; variables: any;
/** @ngInject */ /** @ngInject */
constructor(private $rootScope, private $q, private $location, private $injector, private templateSrv) { constructor(private $rootScope, private $q, private $location, private $injector, private templateSrv, private timeSrv) {
$rootScope.$on('template-variable-value-updated', this.updateUrlParamsWithCurrentVariables.bind(this), $rootScope); $rootScope.$on('template-variable-value-updated', this.updateUrlParamsWithCurrentVariables.bind(this), $rootScope);
} }
...@@ -22,7 +23,7 @@ export class VariableSrv { ...@@ -22,7 +23,7 @@ export class VariableSrv {
// create working class models representing variables // create working class models representing variables
this.variables = dashboard.templating.list = dashboard.templating.list.map(this.createVariableFromModel.bind(this)); this.variables = dashboard.templating.list = dashboard.templating.list.map(this.createVariableFromModel.bind(this));
this.templateSrv.init(this.variables); this.templateSrv.init(this.variables, this.timeSrv.timeRange());
// init variables // init variables
for (const variable of this.variables) { for (const variable of this.variables) {
...@@ -41,7 +42,8 @@ export class VariableSrv { ...@@ -41,7 +42,8 @@ export class VariableSrv {
}); });
} }
onTimeRangeUpdated() { onTimeRangeUpdated(timeRange: TimeRange) {
this.templateSrv.updateTimeVariables(timeRange);
const promises = this.variables.filter(variable => variable.refresh === 2).map(variable => { const promises = this.variables.filter(variable => variable.refresh === 2).map(variable => {
const previousOptions = variable.options.slice(); const previousOptions = variable.options.slice();
......
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