From 6fa7c6b20623b53c412aeac49e093cb311cb28e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 1 Apr 2021 06:33:11 +0200 Subject: [PATCH] TimeSrv: Refactor service to have no dependency on angular (#32562) * TimeSrv: Refactor service to have no dependency on angular * Fixing reference to function that does not exist * fixing tests * Worked around the strange error --- .../src/field/fieldOverrides.test.ts | 10 ++-- .../grafana-data/src/utils/location.test.ts | 6 +- packages/grafana-data/src/utils/location.ts | 19 +++---- public/app/angular/AngularApp.ts | 7 ++- public/app/app.ts | 9 +++ public/app/features/all.ts | 1 - .../dashboard/services/TimeSrv.test.ts | 28 ++++----- .../features/dashboard/services/TimeSrv.ts | 57 ++++++++----------- .../features/dashboard/state/PanelModel.ts | 5 +- .../app/features/dashboard/state/actions.ts | 3 + .../app/features/panel/panellinks/link_srv.ts | 4 +- .../panel/panellinks/specs/link_srv.test.ts | 14 +---- public/app/features/templating/all.ts | 4 -- .../app/features/templating/template_srv.ts | 2 + .../getAllVariableValuesForUrl.test.ts | 12 ++-- .../variables/getAllVariableValuesForUrl.ts | 6 +- .../app/plugins/panel/heatmap/heatmap_ctrl.ts | 5 +- public/app/routes/GrafanaCtrl.ts | 13 +---- 18 files changed, 93 insertions(+), 112 deletions(-) delete mode 100644 public/app/features/templating/all.ts diff --git a/packages/grafana-data/src/field/fieldOverrides.test.ts b/packages/grafana-data/src/field/fieldOverrides.test.ts index d5ab4750b11..a94037f42d0 100644 --- a/packages/grafana-data/src/field/fieldOverrides.test.ts +++ b/packages/grafana-data/src/field/fieldOverrides.test.ts @@ -67,10 +67,8 @@ export const customFieldRegistry: FieldConfigOptionsRegistry = new Registry {}, - // @ts-ignore - getTimeRangeForUrl: () => {}, + getVariablesUrlParams: (() => {}) as any, + getTimeRangeForUrl: (() => {}) as any, }); describe('Global MinMax', () => { @@ -528,7 +526,7 @@ describe('getLinksSupplier', () => { it('will replace variables in url and title of the data link', () => { locationUtil.initialize({ config: {} as any, - buildParamsFromVariables: (() => {}) as any, + getVariablesUrlParams: (() => {}) as any, getTimeRangeForUrl: (() => {}) as any, }); @@ -572,7 +570,7 @@ describe('getLinksSupplier', () => { it('handles internal links', () => { locationUtil.initialize({ config: { appSubUrl: '' } as any, - buildParamsFromVariables: (() => {}) as any, + getVariablesUrlParams: (() => {}) as any, getTimeRangeForUrl: (() => {}) as any, }); diff --git a/packages/grafana-data/src/utils/location.test.ts b/packages/grafana-data/src/utils/location.test.ts index 25a5caf1cc9..05d33e333ef 100644 --- a/packages/grafana-data/src/utils/location.test.ts +++ b/packages/grafana-data/src/utils/location.test.ts @@ -4,10 +4,8 @@ describe('locationUtil', () => { beforeAll(() => { locationUtil.initialize({ config: { appSubUrl: '/subUrl' } as any, - // @ts-ignore - buildParamsFromVariables: () => {}, - // @ts-ignore - getTimeRangeForUrl: () => {}, + getVariablesUrlParams: (() => {}) as any, + getTimeRangeForUrl: (() => {}) as any, }); }); diff --git a/packages/grafana-data/src/utils/location.ts b/packages/grafana-data/src/utils/location.ts index 62ba723fc2a..29e982646e8 100644 --- a/packages/grafana-data/src/utils/location.ts +++ b/packages/grafana-data/src/utils/location.ts @@ -1,10 +1,10 @@ import { GrafanaConfig, RawTimeRange, ScopedVars } from '../types'; -import { urlUtil } from './url'; +import { UrlQueryMap, urlUtil } from './url'; import { textUtil } from '../text'; let grafanaConfig: GrafanaConfig = { appSubUrl: '' } as any; let getTimeRangeUrlParams: () => RawTimeRange; -let getVariablesUrlParams: (params?: Record, scopedVars?: ScopedVars) => string; +let getVariablesUrlParams: (scopedVars?: ScopedVars) => UrlQueryMap; /** * @@ -35,21 +35,21 @@ const assureBaseUrl = (url: string): string => { interface LocationUtilDependencies { config: GrafanaConfig; getTimeRangeForUrl: () => RawTimeRange; - buildParamsFromVariables: (params: any, scopedVars?: ScopedVars) => string; + getVariablesUrlParams: (scopedVars?: ScopedVars) => UrlQueryMap; } export const locationUtil = { /** * * @param getConfig - * @param buildParamsFromVariables + * @param getAllVariableValuesForUrl * @param getTimeRangeForUrl * @internal */ - initialize: ({ config, buildParamsFromVariables, getTimeRangeForUrl }: LocationUtilDependencies) => { - grafanaConfig = config; - getTimeRangeUrlParams = getTimeRangeForUrl; - getVariablesUrlParams = buildParamsFromVariables; + initialize: (dependencies: LocationUtilDependencies) => { + grafanaConfig = dependencies.config; + getTimeRangeUrlParams = dependencies.getTimeRangeForUrl; + getVariablesUrlParams = dependencies.getVariablesUrlParams; }, stripBaseFromUrl, assureBaseUrl, @@ -63,8 +63,7 @@ export const locationUtil = { if (!getVariablesUrlParams) { return null; } - const params = {}; - getVariablesUrlParams(params, scopedVars); + const params = getVariablesUrlParams(scopedVars); return urlUtil.toUrlParams(params); }, processUrl: (url: string) => { diff --git a/public/app/angular/AngularApp.ts b/public/app/angular/AngularApp.ts index 91b851678e7..ae938be493e 100644 --- a/public/app/angular/AngularApp.ts +++ b/public/app/angular/AngularApp.ts @@ -5,12 +5,14 @@ import 'angular-bindonce'; import 'vendor/bootstrap/bootstrap'; import 'vendor/angular-other/angular-strap'; import { config } from 'app/core/config'; -import { angularModules } from 'app/core/core_module'; +import coreModule, { angularModules } from 'app/core/core_module'; import { DashboardLoaderSrv } from 'app/features/dashboard/services/DashboardLoaderSrv'; import { registerAngularDirectives } from 'app/core/core'; import { initAngularRoutingBridge } from 'app/angular/bridgeReactAngularRouting'; import { monkeyPatchInjectorWithPreAssignedBindings } from 'app/core/injectorMonkeyPatch'; import { extend } from 'lodash'; +import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv'; +import { getTemplateSrv } from '@grafana/runtime'; export class AngularApp { ngModuleDependencies: any[]; @@ -83,6 +85,9 @@ export class AngularApp { // register react angular wrappers angular.module('grafana.services').service('dashboardLoaderSrv', DashboardLoaderSrv); + coreModule.factory('timeSrv', () => getTimeSrv()); + coreModule.factory('templateSrv', () => getTemplateSrv()); + registerAngularDirectives(); initAngularRoutingBridge(); } diff --git a/public/app/app.ts b/public/app/app.ts index 7d531beebc5..a030cadad65 100644 --- a/public/app/app.ts +++ b/public/app/app.ts @@ -13,6 +13,7 @@ import React from 'react'; import config from 'app/core/config'; // @ts-ignore ignoring this for now, otherwise we would have to extend _ interface with move import { + locationUtil, setLocale, setTimeZoneResolver, standardEditorsRegistry, @@ -39,6 +40,8 @@ import { interceptLinkClicks } from './core/navigation/patch/interceptLinkClicks import { AngularApp } from './angular/AngularApp'; import { PanelRenderer } from './features/panel/PanelRenderer'; import { QueryRunner } from './features/query/state/QueryRunner'; +import { getTimeSrv } from './features/dashboard/services/TimeSrv'; +import { getVariablesUrlParams } from './features/variables/getAllVariableValuesForUrl'; // add move to lodash for backward compatabilty with plugins // @ts-ignore @@ -79,6 +82,12 @@ export class GrafanaApp { setQueryRunnerFactory(() => new QueryRunner()); setVariableQueryRunner(new VariableQueryRunner()); + locationUtil.initialize({ + config, + getTimeRangeForUrl: getTimeSrv().timeRangeForUrl, + getVariablesUrlParams: getVariablesUrlParams, + }); + // intercept anchor clicks and forward it to custom history instead of relying on browser's history document.addEventListener('click', interceptLinkClicks); diff --git a/public/app/features/all.ts b/public/app/features/all.ts index 378c4da057a..3553964ca0d 100644 --- a/public/app/features/all.ts +++ b/public/app/features/all.ts @@ -1,5 +1,4 @@ import './annotations/all'; -import './templating/all'; import './plugins/all'; import './dashboard'; import './panel/all'; diff --git a/public/app/features/dashboard/services/TimeSrv.test.ts b/public/app/features/dashboard/services/TimeSrv.test.ts index fc6fb5ea8df..163a7c59f37 100644 --- a/public/app/features/dashboard/services/TimeSrv.test.ts +++ b/public/app/features/dashboard/services/TimeSrv.test.ts @@ -10,12 +10,6 @@ jest.mock('app/core/core', () => ({ })); describe('timeSrv', () => { - const timer = { - register: jest.fn(), - cancel: jest.fn(), - cancelAll: jest.fn(), - }; - let timeSrv: TimeSrv; const _dashboard: any = { @@ -25,7 +19,7 @@ describe('timeSrv', () => { }; beforeEach(() => { - timeSrv = new TimeSrv(jest.fn() as any, timer, new ContextSrvStub() as any); + timeSrv = new TimeSrv(new ContextSrvStub() as any); timeSrv.init(_dashboard); _dashboard.refresh = false; }); @@ -50,7 +44,7 @@ describe('timeSrv', () => { it('should handle relative times', () => { locationService.push('/d/id?from=now-2d&to=now'); - timeSrv = new TimeSrv(jest.fn() as any, timer, new ContextSrvStub() as any); + timeSrv = new TimeSrv(new ContextSrvStub() as any); timeSrv.init(_dashboard); const time = timeSrv.timeRange(); @@ -61,7 +55,7 @@ describe('timeSrv', () => { it('should handle formatted dates', () => { locationService.push('/d/id?from=20140410T052010&to=20140520T031022'); - timeSrv = new TimeSrv(jest.fn() as any, timer, new ContextSrvStub() as any); + timeSrv = new TimeSrv(new ContextSrvStub() as any); timeSrv.init(_dashboard); const time = timeSrv.timeRange(); @@ -72,7 +66,7 @@ describe('timeSrv', () => { it('should ignore refresh if time absolute', () => { locationService.push('/d/id?from=20140410T052010&to=20140520T031022'); - timeSrv = new TimeSrv(jest.fn() as any, timer, new ContextSrvStub() as any); + timeSrv = new TimeSrv(new ContextSrvStub() as any); // dashboard saved with refresh on _dashboard.refresh = true; @@ -84,7 +78,7 @@ describe('timeSrv', () => { it('should handle formatted dates without time', () => { locationService.push('/d/id?from=20140410&to=20140520'); - timeSrv = new TimeSrv(jest.fn() as any, timer, new ContextSrvStub() as any); + timeSrv = new TimeSrv(new ContextSrvStub() as any); timeSrv.init(_dashboard); const time = timeSrv.timeRange(); @@ -95,7 +89,7 @@ describe('timeSrv', () => { it('should handle epochs', () => { locationService.push('/d/id?from=1410337646373&to=1410337665699'); - timeSrv = new TimeSrv(jest.fn() as any, timer, new ContextSrvStub() as any); + timeSrv = new TimeSrv(new ContextSrvStub() as any); timeSrv.init(_dashboard); const time = timeSrv.timeRange(); @@ -106,7 +100,7 @@ describe('timeSrv', () => { it('should handle epochs that look like formatted date without time', () => { locationService.push('/d/id?from=20149999&to=20159999'); - timeSrv = new TimeSrv(jest.fn() as any, timer, new ContextSrvStub() as any); + timeSrv = new TimeSrv(new ContextSrvStub() as any); timeSrv.init(_dashboard); const time = timeSrv.timeRange(); @@ -117,7 +111,7 @@ describe('timeSrv', () => { it('should handle epochs that look like formatted date', () => { locationService.push('/d/id?from=201499991234567&to=201599991234567'); - timeSrv = new TimeSrv(jest.fn() as any, timer, new ContextSrvStub() as any); + timeSrv = new TimeSrv(new ContextSrvStub() as any); timeSrv.init(_dashboard); const time = timeSrv.timeRange(); @@ -128,7 +122,7 @@ describe('timeSrv', () => { it('should handle bad dates', () => { locationService.push('/d/id?from=20151126T00010%3C%2Fp%3E%3Cspan%20class&to=now'); - timeSrv = new TimeSrv(jest.fn() as any, timer, new ContextSrvStub() as any); + timeSrv = new TimeSrv(new ContextSrvStub() as any); _dashboard.time.from = 'now-6h'; timeSrv.init(_dashboard); @@ -140,7 +134,7 @@ describe('timeSrv', () => { it('handles time window specfied as interval string', () => { locationService.push('/d/id?time=1410337645000&time.window=10s'); - timeSrv = new TimeSrv(jest.fn() as any, timer, new ContextSrvStub() as any); + timeSrv = new TimeSrv(new ContextSrvStub() as any); timeSrv.init(_dashboard); const time = timeSrv.timeRange(); @@ -151,7 +145,7 @@ describe('timeSrv', () => { it('handles time window specified in ms', () => { locationService.push('/d/id?time=1410337645000&time.window=10000'); - timeSrv = new TimeSrv(jest.fn() as any, timer, new ContextSrvStub() as any); + timeSrv = new TimeSrv(new ContextSrvStub() as any); timeSrv.init(_dashboard); const time = timeSrv.timeRange(); diff --git a/public/app/features/dashboard/services/TimeSrv.ts b/public/app/features/dashboard/services/TimeSrv.ts index d4ea7973724..e0c450bb9a2 100644 --- a/public/app/features/dashboard/services/TimeSrv.ts +++ b/public/app/features/dashboard/services/TimeSrv.ts @@ -1,5 +1,4 @@ import _ from 'lodash'; -import { ITimeoutService } from 'angular'; import { dateMath, dateTime, @@ -10,16 +9,14 @@ import { TimeRange, toUtc, } from '@grafana/data'; - -import coreModule from 'app/core/core_module'; -import { ContextSrv } from 'app/core/services/context_srv'; import { DashboardModel } from '../state/DashboardModel'; import { getShiftedTimeRange, getZoomedTimeRange } from 'app/core/utils/timePicker'; -import { appEvents } from '../../../core/core'; import { config } from 'app/core/config'; import { getRefreshFromUrl } from '../utils/getRefreshFromUrl'; import { locationService } from '@grafana/runtime'; import { ShiftTimeEvent, ShiftTimeEventPayload, ZoomOutEvent } from '../../../types/events'; +import { contextSrv, ContextSrv } from 'app/core/services/context_srv'; +import appEvents from 'app/core/app_events'; export class TimeSrv { time: any; @@ -30,14 +27,15 @@ export class TimeSrv { timeAtLoad: any; private autoRefreshBlocked: boolean; - /** @ngInject */ - constructor(private $timeout: ITimeoutService, private timer: any, private contextSrv: ContextSrv) { + constructor(private contextSrv: ContextSrv) { // default time this.time = getDefaultTimeRange().raw; this.refreshDashboard = this.refreshDashboard.bind(this); + appEvents.subscribe(ZoomOutEvent, (e) => { this.zoomOut(e.payload); }); + appEvents.subscribe(ShiftTimeEvent, (e) => { this.shiftTime(e.payload); }); @@ -51,8 +49,6 @@ export class TimeSrv { } init(dashboard: DashboardModel) { - this.timer.cancelAll(); - this.dashboard = dashboard; this.time = dashboard.time; this.refresh = dashboard.refresh; @@ -193,18 +189,16 @@ export class TimeSrv { setAutoRefresh(interval: any) { this.dashboard.refresh = interval; - this.cancelNextRefresh(); + this.stopAutoRefresh(); if (interval) { const validInterval = this.contextSrv.getValidInterval(interval); const intervalMs = rangeUtil.intervalToMs(validInterval); - this.refreshTimer = this.timer.register( - this.$timeout(() => { - this.startNextRefreshTimer(intervalMs); - this.refreshDashboard(); - }, intervalMs) - ); + this.refreshTimer = setTimeout(() => { + this.startNextRefreshTimer(intervalMs); + this.refreshDashboard(); + }, intervalMs); } if (interval) { @@ -220,21 +214,18 @@ export class TimeSrv { } private startNextRefreshTimer(afterMs: number) { - this.cancelNextRefresh(); - this.refreshTimer = this.timer.register( - this.$timeout(() => { - this.startNextRefreshTimer(afterMs); - if (this.contextSrv.isGrafanaVisible()) { - this.refreshDashboard(); - } else { - this.autoRefreshBlocked = true; - } - }, afterMs) - ); + this.refreshTimer = setTimeout(() => { + this.startNextRefreshTimer(afterMs); + if (this.contextSrv.isGrafanaVisible()) { + this.refreshDashboard(); + } else { + this.autoRefreshBlocked = true; + } + }, afterMs); } - private cancelNextRefresh() { - this.timer.cancel(this.refreshTimer); + stopAutoRefresh() { + clearTimeout(this.refreshTimer); } setTime(time: RawTimeRange, fromRouteUpdate?: boolean) { @@ -313,14 +304,16 @@ export class TimeSrv { } } -let singleton: TimeSrv; +let singleton: TimeSrv | undefined; export function setTimeSrv(srv: TimeSrv) { singleton = srv; } export function getTimeSrv(): TimeSrv { + if (!singleton) { + singleton = new TimeSrv(contextSrv); + } + return singleton; } - -coreModule.service('timeSrv', TimeSrv); diff --git a/public/app/features/dashboard/state/PanelModel.ts b/public/app/features/dashboard/state/PanelModel.ts index 810987460c4..aa9b9b983de 100644 --- a/public/app/features/dashboard/state/PanelModel.ts +++ b/public/app/features/dashboard/state/PanelModel.ts @@ -29,7 +29,7 @@ import { RenderEvent, } from 'app/types/events'; import { getTimeSrv } from '../services/TimeSrv'; -import { getAllVariableValuesForUrl } from '../../variables/getAllVariableValuesForUrl'; +import { getVariablesUrlParams } from '../../variables/getAllVariableValuesForUrl'; import { filterFieldConfigOverrides, getPanelOptionsWithDefaults, @@ -530,7 +530,8 @@ export class PanelModel implements DataConfigSource { if (extraVars) { vars = vars ? { ...vars, ...extraVars } : extraVars; } - const allVariablesParams = getAllVariableValuesForUrl(vars); + + const allVariablesParams = getVariablesUrlParams(vars); const variablesQuery = urlUtil.toUrlParams(allVariablesParams); const timeRangeUrl = urlUtil.toUrlParams(getTimeSrv().timeRangeForUrl()); diff --git a/public/app/features/dashboard/state/actions.ts b/public/app/features/dashboard/state/actions.ts index 7a350049605..a4b76783cac 100644 --- a/public/app/features/dashboard/state/actions.ts +++ b/public/app/features/dashboard/state/actions.ts @@ -15,6 +15,7 @@ import { loadPanelPlugin } from 'app/features/plugins/state/actions'; import { DashboardAcl, DashboardAclUpdateDTO, NewDashboardAclItem, PermissionLevel, ThunkResult } from 'app/types'; import { PanelModel } from './PanelModel'; import { cancelVariables } from '../../variables/state/actions'; +import { getTimeSrv } from '../services/TimeSrv'; export function getDashboardPermissions(id: number): ThunkResult { return async (dispatch) => { @@ -168,6 +169,8 @@ export const cleanUpDashboardAndVariables = (): ThunkResult => (dispatch, dashboard.destroy(); } + getTimeSrv().stopAutoRefresh(); + dispatch(cleanUpDashboard()); dispatch(cancelVariables()); }; diff --git a/public/app/features/panel/panellinks/link_srv.ts b/public/app/features/panel/panellinks/link_srv.ts index b2ba10e70bc..d1499ce6690 100644 --- a/public/app/features/panel/panellinks/link_srv.ts +++ b/public/app/features/panel/panellinks/link_srv.ts @@ -24,7 +24,7 @@ import { VariableSuggestion, VariableSuggestionsScope, } from '@grafana/data'; -import { getAllVariableValuesForUrl } from '../../variables/getAllVariableValuesForUrl'; +import { getVariablesUrlParams } from '../../variables/getAllVariableValuesForUrl'; const timeRangeVars = [ { @@ -280,7 +280,7 @@ export class LinkSrv implements LinkService { if (link.includeVars) { params = { ...params, - ...getAllVariableValuesForUrl(), + ...getVariablesUrlParams(), }; } diff --git a/public/app/features/panel/panellinks/specs/link_srv.test.ts b/public/app/features/panel/panellinks/specs/link_srv.test.ts index 5154b587112..ba970c9bfd6 100644 --- a/public/app/features/panel/panellinks/specs/link_srv.test.ts +++ b/public/app/features/panel/panellinks/specs/link_srv.test.ts @@ -19,19 +19,13 @@ describe('linkSrv', () => { let templateSrv: TemplateSrv; function initLinkSrv() { - const timer = { - register: jest.fn(), - cancel: jest.fn(), - cancelAll: jest.fn(), - }; - const _dashboard: any = { time: { from: 'now-6h', to: 'now' }, getTimezone: jest.fn(() => 'browser'), timeRangeUpdated: () => {}, }; - const timeSrv = new TimeSrv(jest.fn() as any, timer, {} as any); + const timeSrv = new TimeSrv({} as any); timeSrv.init(_dashboard); timeSrv.setTime({ from: 'now-1h', to: 'now' }); _dashboard.refresh = false; @@ -126,10 +120,8 @@ describe('linkSrv', () => { ({ url, appSubUrl, expected }) => { locationUtil.initialize({ config: { appSubUrl } as any, - // @ts-ignore - buildParamsFromVariables: () => {}, - // @ts-ignore - getTimeRangeForUrl: () => {}, + getVariablesUrlParams: (() => {}) as any, + getTimeRangeForUrl: (() => {}) as any, }); const link = linkSrv.getDataLinkUIModel( diff --git a/public/app/features/templating/all.ts b/public/app/features/templating/all.ts deleted file mode 100644 index b132805355d..00000000000 --- a/public/app/features/templating/all.ts +++ /dev/null @@ -1,4 +0,0 @@ -import coreModule from 'app/core/core_module'; -import { getTemplateSrv } from './template_srv'; - -coreModule.factory('templateSrv', () => getTemplateSrv()); diff --git a/public/app/features/templating/template_srv.ts b/public/app/features/templating/template_srv.ts index 6693b3bc76a..c07d64181e2 100644 --- a/public/app/features/templating/template_srv.ts +++ b/public/app/features/templating/template_srv.ts @@ -329,5 +329,7 @@ export class TemplateSrv implements BaseTemplateSrv { // Expose the template srv const srv = new TemplateSrv(); + setTemplateSrv(srv); + export const getTemplateSrv = () => srv; diff --git a/public/app/features/variables/getAllVariableValuesForUrl.test.ts b/public/app/features/variables/getAllVariableValuesForUrl.test.ts index 9778bd5ee2b..7eb44ac7be0 100644 --- a/public/app/features/variables/getAllVariableValuesForUrl.test.ts +++ b/public/app/features/variables/getAllVariableValuesForUrl.test.ts @@ -1,7 +1,7 @@ import { setTemplateSrv } from '@grafana/runtime'; import { variableAdapters } from './adapters'; import { createQueryVariableAdapter } from './query/adapter'; -import { getAllVariableValuesForUrl } from './getAllVariableValuesForUrl'; +import { getVariablesUrlParams } from './getAllVariableValuesForUrl'; import { initTemplateSrv } from '../../../test/helpers/initTemplateSrv'; describe('getAllVariableValuesForUrl', () => { @@ -26,7 +26,7 @@ describe('getAllVariableValuesForUrl', () => { }); it('should set multiple url params', () => { - let params: any = getAllVariableValuesForUrl(); + let params: any = getVariablesUrlParams(); expect(params['var-test']).toMatchObject(['val1', 'val2']); }); }); @@ -48,7 +48,7 @@ describe('getAllVariableValuesForUrl', () => { }); it('should not include template variable value in url', () => { - const params = getAllVariableValuesForUrl(); + const params = getVariablesUrlParams(); expect(params['var-test']).toBe(undefined); }); }); @@ -71,7 +71,7 @@ describe('getAllVariableValuesForUrl', () => { }); it('should not include template variable value in url', () => { - const params = getAllVariableValuesForUrl(); + const params = getVariablesUrlParams(); expect(params['var-test']).toBe(undefined); }); }); @@ -82,7 +82,7 @@ describe('getAllVariableValuesForUrl', () => { }); it('should set scoped value as url params', () => { - const params = getAllVariableValuesForUrl({ + const params = getVariablesUrlParams({ test: { value: 'val1', text: 'val1text' }, }); expect(params['var-test']).toBe('val1'); @@ -95,7 +95,7 @@ describe('getAllVariableValuesForUrl', () => { }); it('should not set scoped value as url params', () => { - const params = getAllVariableValuesForUrl({ + const params = getVariablesUrlParams({ test: { name: 'test', value: 'val1', text: 'val1text', skipUrlSync: true }, }); expect(params['var-test']).toBe(undefined); diff --git a/public/app/features/variables/getAllVariableValuesForUrl.ts b/public/app/features/variables/getAllVariableValuesForUrl.ts index 8abb33b9ffc..4cfbfd55d33 100644 --- a/public/app/features/variables/getAllVariableValuesForUrl.ts +++ b/public/app/features/variables/getAllVariableValuesForUrl.ts @@ -1,9 +1,9 @@ -import { ScopedVars } from '@grafana/data'; +import { ScopedVars, UrlQueryMap } from '@grafana/data'; import { getTemplateSrv } from '@grafana/runtime'; import { variableAdapters } from './adapters'; -export function getAllVariableValuesForUrl(scopedVars?: ScopedVars) { - const params: Record = {}; +export function getVariablesUrlParams(scopedVars?: ScopedVars): UrlQueryMap { + const params: UrlQueryMap = {}; const variables = getTemplateSrv().getVariables(); // console.log(variables) diff --git a/public/app/plugins/panel/heatmap/heatmap_ctrl.ts b/public/app/plugins/panel/heatmap/heatmap_ctrl.ts index 3dc460890aa..24b98810d13 100644 --- a/public/app/plugins/panel/heatmap/heatmap_ctrl.ts +++ b/public/app/plugins/panel/heatmap/heatmap_ctrl.ts @@ -17,6 +17,8 @@ import { getProcessedDataFrames } from 'app/features/query/state/runRequest'; import { DataProcessor } from '../graph/data_processor'; import { LegacyResponseData, PanelEvents, DataFrame, rangeUtil } from '@grafana/data'; import { CoreEvents } from 'app/types'; +import { TemplateSrv } from 'app/features/templating/template_srv'; +import { TimeSrv } from 'app/features/dashboard/services/TimeSrv'; const X_BUCKET_NUMBER_DEFAULT = 30; const Y_BUCKET_NUMBER_DEFAULT = 10; @@ -125,8 +127,9 @@ export class HeatmapCtrl extends MetricsPanelCtrl { processor: DataProcessor; // Shared with graph panel /** @ngInject */ - constructor($scope: any, $injector: auto.IInjectorService) { + constructor($scope: any, $injector: auto.IInjectorService, templateSrv: TemplateSrv, timeSrv: TimeSrv) { super($scope, $injector); + this.selectionActivated = false; _.defaultsDeep(this.panel, panelDefaults); diff --git a/public/app/routes/GrafanaCtrl.ts b/public/app/routes/GrafanaCtrl.ts index fbe3428ec5f..76100e2beef 100644 --- a/public/app/routes/GrafanaCtrl.ts +++ b/public/app/routes/GrafanaCtrl.ts @@ -5,7 +5,6 @@ import $ from 'jquery'; // Utils and servies import { colors } from '@grafana/ui'; import { - getTemplateSrv, setBackendSrv, setDataSourceSrv, setLegacyAngularInjector, @@ -16,7 +15,6 @@ import config from 'app/core/config'; import coreModule from 'app/core/core_module'; import { profiler } from 'app/core/profiler'; import appEvents from 'app/core/app_events'; -import { TimeSrv, setTimeSrv, getTimeSrv } from 'app/features/dashboard/services/TimeSrv'; import { DatasourceSrv } from 'app/features/plugins/datasource_srv'; import { AngularLoader, setAngularLoader } from 'app/core/services/AngularLoader'; @@ -27,7 +25,7 @@ import { UtilSrv } from 'app/core/services/util_srv'; import { ContextSrv } from 'app/core/services/context_srv'; import { DashboardSrv, setDashboardSrv } from 'app/features/dashboard/services/DashboardSrv'; import { IRootScopeService, IAngularEvent, auto } from 'angular'; -import { AppEvent, locationUtil } from '@grafana/data'; +import { AppEvent } from '@grafana/data'; import { backendSrv } from 'app/core/services/backend_srv'; import { initGrafanaLive } from 'app/features/live/live'; @@ -40,7 +38,6 @@ export class GrafanaCtrl { utilSrv: UtilSrv, $rootScope: GrafanaRootScope, contextSrv: ContextSrv, - timeSrv: TimeSrv, linkSrv: LinkSrv, datasourceSrv: DatasourceSrv, dashboardSrv: DashboardSrv, @@ -51,20 +48,12 @@ export class GrafanaCtrl { setAngularLoader(angularLoader); setBackendSrv(backendSrv); setDataSourceSrv(datasourceSrv); - setTimeSrv(timeSrv); setLinkSrv(linkSrv); setDashboardSrv(dashboardSrv); setLegacyAngularInjector($injector); datasourceSrv.init(config.datasources, config.defaultDatasource); - locationUtil.initialize({ - config, - getTimeRangeForUrl: getTimeSrv().timeRangeForUrl, - // @ts-ignore - buildParamsFromVariables: getTemplateSrv().fillVariableValuesForUrl, - }); - setLocationSrv(locationService); // Initialize websocket event streaming