From 8b68ba5cbbbb6601b9ac71842dc16123c1badac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 29 Jan 2019 20:49:54 +0100 Subject: [PATCH] Minor refactoring and adding some typing --- public/app/features/templating/editor_ctrl.ts | 2 +- .../templating/specs/template_srv.test.ts | 4 +-- .../templating/specs/variable_srv.test.ts | 2 +- .../specs/variable_srv_init.test.ts | 3 ++- .../app/features/templating/template_srv.ts | 10 +++---- .../app/features/templating/variable_srv.ts | 26 +++++++++++++------ public/test/specs/helpers.ts | 2 +- 7 files changed, 30 insertions(+), 19 deletions(-) diff --git a/public/app/features/templating/editor_ctrl.ts b/public/app/features/templating/editor_ctrl.ts index fdab5587b42..c47c42a4aad 100644 --- a/public/app/features/templating/editor_ctrl.ts +++ b/public/app/features/templating/editor_ctrl.ts @@ -135,7 +135,7 @@ export class VariableEditorCtrl { $scope.runQuery().then(() => { $scope.reset(); $scope.mode = 'list'; - templateSrv.updateTemplateData(); + templateSrv.updateIndex(); }); } }; diff --git a/public/app/features/templating/specs/template_srv.test.ts b/public/app/features/templating/specs/template_srv.test.ts index 30faffea3be..9a041f42cd3 100644 --- a/public/app/features/templating/specs/template_srv.test.ts +++ b/public/app/features/templating/specs/template_srv.test.ts @@ -348,7 +348,7 @@ describe('templateSrv', () => { }); }); - describe('updateTemplateData with simple value', () => { + describe('updateIndex with simple value', () => { beforeEach(() => { initTemplateSrv([{ type: 'query', name: 'test', current: { value: 'muuuu' } }]); }); @@ -476,7 +476,7 @@ describe('templateSrv', () => { } ]); _templateSrv.setGrafanaVariable('$__auto_interval_interval', '13m'); - _templateSrv.updateTemplateData(); + _templateSrv.updateIndex(); }); it('should replace with text except for grafanaVariables', () => { diff --git a/public/app/features/templating/specs/variable_srv.test.ts b/public/app/features/templating/specs/variable_srv.test.ts index 5c9d623fcb2..7e5d0ff98c7 100644 --- a/public/app/features/templating/specs/variable_srv.test.ts +++ b/public/app/features/templating/specs/variable_srv.test.ts @@ -23,7 +23,7 @@ describe('VariableSrv', function(this: any) { init: vars => { this.variables = vars; }, - updateTemplateData: () => {}, + updateIndex: () => {}, replace: str => str.replace(this.regex, match => { 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 6976f6fadda..4e5e025522f 100644 --- a/public/app/features/templating/specs/variable_srv_init.test.ts +++ b/public/app/features/templating/specs/variable_srv_init.test.ts @@ -11,7 +11,7 @@ describe('VariableSrv init', function(this: any) { this.variables = vars; }, variableInitialized: () => {}, - updateTemplateData: () => {}, + updateIndex: () => {}, replace: str => str.replace(this.regex, match => { return match; @@ -53,6 +53,7 @@ describe('VariableSrv init', function(this: any) { templateSrv, }; + // @ts-ignore ctx.variableSrv = new VariableSrv($rootscope, $q, {}, $injector, templateSrv, timeSrv); $injector.instantiate = (variable, model) => { diff --git a/public/app/features/templating/template_srv.ts b/public/app/features/templating/template_srv.ts index 10637fa6f86..de5efabcf23 100644 --- a/public/app/features/templating/template_srv.ts +++ b/public/app/features/templating/template_srv.ts @@ -25,10 +25,10 @@ export class TemplateSrv { init(variables, timeRange?: TimeRange) { this.variables = variables; this.timeRange = timeRange; - this.updateTemplateData(); + this.updateIndex(); } - updateTemplateData() { + updateIndex() { const existsOrEmpty = value => value || value === ''; this.index = this.variables.reduce((acc, currentValue) => { @@ -54,9 +54,9 @@ export class TemplateSrv { } } - updateTimeVariables(timeRange: TimeRange) { + updateTimeRange(timeRange: TimeRange) { this.timeRange = timeRange; - this.updateTemplateData(); + this.updateIndex(); } variableInitialized(variable) { @@ -289,7 +289,7 @@ export class TemplateSrv { }); } - fillVariableValuesForUrl(params, scopedVars) { + fillVariableValuesForUrl(params, scopedVars?) { _.each(this.variables, variable => { if (scopedVars && scopedVars[variable.name] !== void 0) { if (scopedVars[variable.name].skipUrlSync) { diff --git a/public/app/features/templating/variable_srv.ts b/public/app/features/templating/variable_srv.ts index 700ccc4cce6..dff798ace29 100644 --- a/public/app/features/templating/variable_srv.ts +++ b/public/app/features/templating/variable_srv.ts @@ -6,18 +6,28 @@ import _ from 'lodash'; import coreModule from 'app/core/core_module'; import { variableTypes } from './variable'; import { Graph } from 'app/core/utils/dag'; +import { TemplateSrv } from 'app/features/templating/template_srv'; +import { TimeSrv } from 'app/features/dashboard/time_srv'; +import { DashboardModel } from 'app/features/dashboard/dashboard_model'; + +// Types import { TimeRange } from '@grafana/ui/src'; export class VariableSrv { - dashboard: any; - variables: any; + dashboard: DashboardModel; + variables: any[]; /** @ngInject */ - constructor(private $rootScope, private $q, private $location, private $injector, private templateSrv, private timeSrv) { + constructor(private $rootScope, + private $q, + private $location, + private $injector, + private templateSrv: TemplateSrv, + private timeSrv: TimeSrv) { $rootScope.$on('template-variable-value-updated', this.updateUrlParamsWithCurrentVariables.bind(this), $rootScope); } - init(dashboard) { + init(dashboard: DashboardModel) { this.dashboard = dashboard; this.dashboard.events.on('time-range-updated', this.onTimeRangeUpdated.bind(this)); @@ -38,12 +48,12 @@ export class VariableSrv { }) ) .then(() => { - this.templateSrv.updateTemplateData(); + this.templateSrv.updateIndex(); }); } onTimeRangeUpdated(timeRange: TimeRange) { - this.templateSrv.updateTimeVariables(timeRange); + this.templateSrv.updateTimeRange(timeRange); const promises = this.variables.filter(variable => variable.refresh === 2).map(variable => { const previousOptions = variable.options.slice(); @@ -102,14 +112,14 @@ export class VariableSrv { addVariable(variable) { this.variables.push(variable); - this.templateSrv.updateTemplateData(); + this.templateSrv.updateIndex(); this.dashboard.updateSubmenuVisibility(); } removeVariable(variable) { const index = _.indexOf(this.variables, variable); this.variables.splice(index, 1); - this.templateSrv.updateTemplateData(); + this.templateSrv.updateIndex(); this.dashboard.updateSubmenuVisibility(); } diff --git a/public/test/specs/helpers.ts b/public/test/specs/helpers.ts index 0345576d96c..536b277eec3 100644 --- a/public/test/specs/helpers.ts +++ b/public/test/specs/helpers.ts @@ -182,7 +182,7 @@ export function TemplateSrvStub(this: any) { return []; }; this.fillVariableValuesForUrl = () => {}; - this.updateTemplateData = () => {}; + this.updateIndex = () => {}; this.variableExists = () => { return false; };