From 9d6ac2c3d41a7303b3d3fd4aa9f53ec088909798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Mon, 21 Jan 2019 08:47:41 +0100 Subject: [PATCH 1/9] fix: Viewers can edit means that viewers have acces to Explore #14281 --- pkg/api/frontendsettings.go | 1 + pkg/api/index.go | 2 +- public/app/core/config.ts | 2 ++ public/app/core/services/keybindingSrv.ts | 5 +++-- public/app/features/panel/metrics_panel_ctrl.ts | 2 +- .../panel/specs/metrics_panel_ctrl.test.ts | 15 +++++++++++++++ public/app/routes/routes.ts | 3 ++- 7 files changed, 25 insertions(+), 5 deletions(-) diff --git a/pkg/api/frontendsettings.go b/pkg/api/frontendsettings.go index 8f5457cf271..6d6cc708496 100644 --- a/pkg/api/frontendsettings.go +++ b/pkg/api/frontendsettings.go @@ -165,6 +165,7 @@ func (hs *HTTPServer) getFrontendSettingsMap(c *m.ReqContext) (map[string]interf "externalUserMngInfo": setting.ExternalUserMngInfo, "externalUserMngLinkUrl": setting.ExternalUserMngLinkUrl, "externalUserMngLinkName": setting.ExternalUserMngLinkName, + "viewersCanEdit": setting.ViewersCanEdit, "buildInfo": map[string]interface{}{ "version": setting.BuildVersion, "commit": setting.BuildCommit, diff --git a/pkg/api/index.go b/pkg/api/index.go index 2980d8a5c6b..e90db84016d 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -140,7 +140,7 @@ func (hs *HTTPServer) setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, er Children: dashboardChildNavs, }) - if setting.ExploreEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) { + if setting.ExploreEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR || setting.ViewersCanEdit) { data.NavTree = append(data.NavTree, &dtos.NavLink{ Text: "Explore", Id: "explore", diff --git a/public/app/core/config.ts b/public/app/core/config.ts index 0aa159af84d..26f31ffcf54 100644 --- a/public/app/core/config.ts +++ b/public/app/core/config.ts @@ -34,6 +34,7 @@ export class Settings { disableUserSignUp: boolean; loginHint: any; loginError: any; + viewersCanEdit: boolean; constructor(options) { const defaults = { @@ -50,6 +51,7 @@ export class Settings { env: 'production', isEnterprise: false, }, + viewersCanEdit: false, }; _.extend(this, defaults, options); diff --git a/public/app/core/services/keybindingSrv.ts b/public/app/core/services/keybindingSrv.ts index c02f6850e8b..a85d9b5a500 100644 --- a/public/app/core/services/keybindingSrv.ts +++ b/public/app/core/services/keybindingSrv.ts @@ -8,6 +8,7 @@ import { getExploreUrl } from 'app/core/utils/explore'; import Mousetrap from 'mousetrap'; import 'mousetrap-global-bind'; +import { ContextSrv } from './context_srv'; export class KeybindingSrv { helpModal: boolean; @@ -21,7 +22,7 @@ export class KeybindingSrv { private $timeout, private datasourceSrv, private timeSrv, - private contextSrv + private contextSrv: ContextSrv ) { // clear out all shortcuts on route change $rootScope.$on('$routeChangeSuccess', () => { @@ -196,7 +197,7 @@ export class KeybindingSrv { }); // jump to explore if permissions allow - if (this.contextSrv.isEditor && config.exploreEnabled) { + if ((this.contextSrv.isEditor || config.viewersCanEdit) && config.exploreEnabled) { this.bind('x', async () => { if (dashboard.meta.focusPanelId) { const panel = dashboard.getPanelById(dashboard.meta.focusPanelId); diff --git a/public/app/features/panel/metrics_panel_ctrl.ts b/public/app/features/panel/metrics_panel_ctrl.ts index 5557b477b8f..576bde63114 100644 --- a/public/app/features/panel/metrics_panel_ctrl.ts +++ b/public/app/features/panel/metrics_panel_ctrl.ts @@ -231,7 +231,7 @@ class MetricsPanelCtrl extends PanelCtrl { getAdditionalMenuItems() { const items = []; - if (config.exploreEnabled && this.contextSrv.isEditor && this.datasource) { + if (config.exploreEnabled && (this.contextSrv.isEditor || config.viewersCanEdit) && this.datasource) { items.push({ text: 'Explore', click: 'ctrl.explore();', diff --git a/public/app/features/panel/specs/metrics_panel_ctrl.test.ts b/public/app/features/panel/specs/metrics_panel_ctrl.test.ts index 913a2461fd0..d4ce5cda7e2 100644 --- a/public/app/features/panel/specs/metrics_panel_ctrl.test.ts +++ b/public/app/features/panel/specs/metrics_panel_ctrl.test.ts @@ -2,6 +2,7 @@ jest.mock('app/core/core', () => ({})); jest.mock('app/core/config', () => { return { exploreEnabled: true, + viewersCanEdit: false, panels: { test: { id: 'test', @@ -14,6 +15,7 @@ jest.mock('app/core/config', () => { import q from 'q'; import { PanelModel } from 'app/features/dashboard/panel_model'; import { MetricsPanelCtrl } from '../metrics_panel_ctrl'; +import config from 'app/core/config'; describe('MetricsPanelCtrl', () => { let ctrl; @@ -46,6 +48,19 @@ describe('MetricsPanelCtrl', () => { expect(additionalItems.length).toBe(1); }); }); + + describe('and has datasource set that supports explore and viewersCanEdit is true', () => { + beforeEach(() => { + config.viewersCanEdit = true; + ctrl.contextSrv = { isEditor: false }; + ctrl.datasource = { meta: { explore: true } }; + additionalItems = ctrl.getAdditionalMenuItems(); + }); + + it('should not return any items', () => { + expect(additionalItems.length).toBe(1); + }); + }); }); }); diff --git a/public/app/routes/routes.ts b/public/app/routes/routes.ts index 4b24f69b74e..8552d0510a9 100644 --- a/public/app/routes/routes.ts +++ b/public/app/routes/routes.ts @@ -16,6 +16,7 @@ import UsersListPage from 'app/features/users/UsersListPage'; import DataSourceDashboards from 'app/features/datasources/DataSourceDashboards'; import DataSourceSettingsPage from '../features/datasources/settings/DataSourceSettingsPage'; import OrgDetailsPage from '../features/org/OrgDetailsPage'; +import config from 'app/core/config'; /** @ngInject */ export function setupAngularRoutes($routeProvider, $locationProvider) { @@ -129,7 +130,7 @@ export function setupAngularRoutes($routeProvider, $locationProvider) { template: '', reloadOnSearch: false, resolve: { - roles: () => ['Editor', 'Admin'], + roles: () => (config.viewersCanEdit ? [] : ['Editor', 'Admin']), component: () => import(/* webpackChunkName: "explore" */ 'app/features/explore/Wrapper'), }, }) From b95650d19cc437d72da32c0fe5519e1973ae12ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Mon, 21 Jan 2019 10:50:34 +0100 Subject: [PATCH 2/9] Added function hasAccessToExplore in ContextSrv and refactored tests --- public/app/core/services/context_srv.ts | 4 ++ public/app/core/services/keybindingSrv.ts | 3 +- .../app/features/panel/metrics_panel_ctrl.ts | 6 +-- .../panel/specs/metrics_panel_ctrl.test.ts | 51 +++++++------------ 4 files changed, 25 insertions(+), 39 deletions(-) diff --git a/public/app/core/services/context_srv.ts b/public/app/core/services/context_srv.ts index c4134598175..7bb753e6f71 100644 --- a/public/app/core/services/context_srv.ts +++ b/public/app/core/services/context_srv.ts @@ -59,6 +59,10 @@ export class ContextSrv { this.sidemenu = !this.sidemenu; store.set('grafana.sidemenu', this.sidemenu); } + + hasAccessToExplore() { + return (this.isEditor || config.viewersCanEdit) && config.exploreEnabled; + } } const contextSrv = new ContextSrv(); diff --git a/public/app/core/services/keybindingSrv.ts b/public/app/core/services/keybindingSrv.ts index a85d9b5a500..9e128c449a6 100644 --- a/public/app/core/services/keybindingSrv.ts +++ b/public/app/core/services/keybindingSrv.ts @@ -1,7 +1,6 @@ import $ from 'jquery'; import _ from 'lodash'; -import config from 'app/core/config'; import coreModule from 'app/core/core_module'; import appEvents from 'app/core/app_events'; import { getExploreUrl } from 'app/core/utils/explore'; @@ -197,7 +196,7 @@ export class KeybindingSrv { }); // jump to explore if permissions allow - if ((this.contextSrv.isEditor || config.viewersCanEdit) && config.exploreEnabled) { + if (this.contextSrv.hasAccessToExplore()) { this.bind('x', async () => { if (dashboard.meta.focusPanelId) { const panel = dashboard.getPanelById(dashboard.meta.focusPanelId); diff --git a/public/app/features/panel/metrics_panel_ctrl.ts b/public/app/features/panel/metrics_panel_ctrl.ts index 576bde63114..0b3bbc3080f 100644 --- a/public/app/features/panel/metrics_panel_ctrl.ts +++ b/public/app/features/panel/metrics_panel_ctrl.ts @@ -1,18 +1,18 @@ import _ from 'lodash'; import kbn from 'app/core/utils/kbn'; -import config from 'app/core/config'; import { PanelCtrl } from 'app/features/panel/panel_ctrl'; import { getExploreUrl } from 'app/core/utils/explore'; import { applyPanelTimeOverrides, getResolution } from 'app/features/dashboard/utils/panel'; +import { ContextSrv } from 'app/core/services/context_srv'; class MetricsPanelCtrl extends PanelCtrl { scope: any; datasource: any; $q: any; $timeout: any; - contextSrv: any; + contextSrv: ContextSrv; datasourceSrv: any; timeSrv: any; templateSrv: any; @@ -231,7 +231,7 @@ class MetricsPanelCtrl extends PanelCtrl { getAdditionalMenuItems() { const items = []; - if (config.exploreEnabled && (this.contextSrv.isEditor || config.viewersCanEdit) && this.datasource) { + if (this.contextSrv.hasAccessToExplore() && this.datasource) { items.push({ text: 'Explore', click: 'ctrl.explore();', diff --git a/public/app/features/panel/specs/metrics_panel_ctrl.test.ts b/public/app/features/panel/specs/metrics_panel_ctrl.test.ts index d4ce5cda7e2..8b9607d39ad 100644 --- a/public/app/features/panel/specs/metrics_panel_ctrl.test.ts +++ b/public/app/features/panel/specs/metrics_panel_ctrl.test.ts @@ -1,8 +1,6 @@ jest.mock('app/core/core', () => ({})); jest.mock('app/core/config', () => { return { - exploreEnabled: true, - viewersCanEdit: false, panels: { test: { id: 'test', @@ -15,62 +13,47 @@ jest.mock('app/core/config', () => { import q from 'q'; import { PanelModel } from 'app/features/dashboard/panel_model'; import { MetricsPanelCtrl } from '../metrics_panel_ctrl'; -import config from 'app/core/config'; describe('MetricsPanelCtrl', () => { - let ctrl; - - beforeEach(() => { - ctrl = setupController(); - }); - describe('when getting additional menu items', () => { - let additionalItems; - - describe('and has no datasource set', () => { - beforeEach(() => { - additionalItems = ctrl.getAdditionalMenuItems(); - }); - + describe('and has no datasource set but user has access to explore', () => { it('should not return any items', () => { - expect(additionalItems.length).toBe(0); + const ctrl = setupController({ hasAccessToExplore: true }); + + expect(ctrl.getAdditionalMenuItems().length).toBe(0); }); }); - describe('and has datasource set that supports explore and user has powers', () => { - beforeEach(() => { - ctrl.contextSrv = { isEditor: true }; - ctrl.datasource = { meta: { explore: true } }; - additionalItems = ctrl.getAdditionalMenuItems(); - }); - + describe('and has datasource set that supports explore and user does not have access to explore', () => { it('should not return any items', () => { - expect(additionalItems.length).toBe(1); + const ctrl = setupController({ hasAccessToExplore: false }); + ctrl.datasource = { meta: { explore: true } }; + + expect(ctrl.getAdditionalMenuItems().length).toBe(0); }); }); - describe('and has datasource set that supports explore and viewersCanEdit is true', () => { - beforeEach(() => { - config.viewersCanEdit = true; - ctrl.contextSrv = { isEditor: false }; + describe('and has datasource set that supports explore and user has access to explore', () => { + it('should return one item', () => { + const ctrl = setupController({ hasAccessToExplore: true }); ctrl.datasource = { meta: { explore: true } }; - additionalItems = ctrl.getAdditionalMenuItems(); - }); - it('should not return any items', () => { - expect(additionalItems.length).toBe(1); + expect(ctrl.getAdditionalMenuItems().length).toBe(1); }); }); }); }); -function setupController() { +function setupController({ hasAccessToExplore } = { hasAccessToExplore: false }) { const injectorStub = { get: type => { switch (type) { case '$q': { return q; } + case 'contextSrv': { + return { hasAccessToExplore: () => hasAccessToExplore }; + } default: { return jest.fn(); } From d5e49845f3555b7ef47ebdc1ec17101a8b4b74dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 21 Jan 2019 13:02:30 +0100 Subject: [PATCH 3/9] Moved gometalinter to a script instead of seperate commands in circleci file, removed megacheck and added staticcheck --- .circleci/config.yml | 15 ++------------- scripts/build/gometalinter.sh | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 13 deletions(-) create mode 100755 scripts/build/gometalinter.sh diff --git a/.circleci/config.yml b/.circleci/config.yml index 876db1d4823..008d2faf57c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -81,20 +81,9 @@ jobs: working_directory: /go/src/github.com/grafana/grafana steps: - checkout - - run: 'go get -u github.com/alecthomas/gometalinter' - - run: 'go get -u github.com/tsenart/deadcode' - - run: 'go get -u github.com/jgautheron/goconst/cmd/goconst' - - run: 'go get -u github.com/gordonklaus/ineffassign' - - run: 'go get -u honnef.co/go/tools/cmd/megacheck' - - run: 'go get -u github.com/opennota/check/cmd/structcheck' - - run: 'go get -u github.com/mdempsky/unconvert' - - run: 'go get -u github.com/opennota/check/cmd/varcheck' - run: - name: run linters - command: 'gometalinter --enable-gc --vendor --deadline 10m --disable-all --enable=deadcode --enable=goconst --enable=gofmt --enable=ineffassign --enable=megacheck --enable=structcheck --enable=unconvert --enable=varcheck ./...' - - run: - name: run go vet - command: 'go vet ./pkg/...' + name: frontend tests + command: './scripts/build/gometalinter.sh' test-frontend: docker: diff --git a/scripts/build/gometalinter.sh b/scripts/build/gometalinter.sh new file mode 100755 index 00000000000..9c77220991f --- /dev/null +++ b/scripts/build/gometalinter.sh @@ -0,0 +1,22 @@ +#!/bin/bash -e + +go get -u github.com/alecthomas/gometalinter +go get -u github.com/tsenart/deadcode +go get -u github.com/jgautheron/goconst/cmd/goconst +go get -u github.com/gordonklaus/ineffassign +go get -u github.com/opennota/check/cmd/structcheck +go get -u github.com/mdempsky/unconvert +go get -u github.com/opennota/check/cmd/varcheck +go get -u honnef.co/go/tools/cmd/staticcheck + +gometalinter --enable-gc --vendor --deadline 10m --disable-all \ + --enable=deadcode \ + --enable=goconst \ + --enable=gofmt \ + --enable=ineffassign \ + --enable=structcheck \ + --enable=unconvert \ + --enable=varcheck \ + --enable=staticcheck + +go vet ./pkg/... From 37e917939a217b5623934de0949779a0f6f333f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 21 Jan 2019 13:07:28 +0100 Subject: [PATCH 4/9] moved script and added exit_if_fail --- scripts/{build => }/gometalinter.sh | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) rename scripts/{build => }/gometalinter.sh (63%) diff --git a/scripts/build/gometalinter.sh b/scripts/gometalinter.sh similarity index 63% rename from scripts/build/gometalinter.sh rename to scripts/gometalinter.sh index 9c77220991f..b360b7f1222 100755 --- a/scripts/build/gometalinter.sh +++ b/scripts/gometalinter.sh @@ -1,4 +1,15 @@ -#!/bin/bash -e +#!/bin/bash + +function exit_if_fail { + command=$@ + echo "Executing '$command'" + eval $command + rc=$? + if [ $rc -ne 0 ]; then + echo "'$command' returned $rc." + exit $rc + fi +} go get -u github.com/alecthomas/gometalinter go get -u github.com/tsenart/deadcode @@ -9,7 +20,7 @@ go get -u github.com/mdempsky/unconvert go get -u github.com/opennota/check/cmd/varcheck go get -u honnef.co/go/tools/cmd/staticcheck -gometalinter --enable-gc --vendor --deadline 10m --disable-all \ +exit_if_fail gometalinter --enable-gc --vendor --deadline 10m --disable-all \ --enable=deadcode \ --enable=goconst \ --enable=gofmt \ @@ -19,4 +30,4 @@ gometalinter --enable-gc --vendor --deadline 10m --disable-all \ --enable=varcheck \ --enable=staticcheck -go vet ./pkg/... +exit_if_fail go vet ./pkg/... From f24e6ebf676b2687719e09450a25aa491342bc6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 21 Jan 2019 13:10:00 +0100 Subject: [PATCH 5/9] Fixed circleci name for gometalinter exec step --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 008d2faf57c..a7f601114f5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -82,7 +82,7 @@ jobs: steps: - checkout - run: - name: frontend tests + name: Gometalinter tests command: './scripts/build/gometalinter.sh' test-frontend: From 829e3024ce15fb27eac267940039f650d4ff4362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 21 Jan 2019 13:20:24 +0100 Subject: [PATCH 6/9] fixed circleci script run path for gometalinter --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index a7f601114f5..f6d66daab84 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -83,7 +83,7 @@ jobs: - checkout - run: name: Gometalinter tests - command: './scripts/build/gometalinter.sh' + command: './scripts/gometalinter.sh' test-frontend: docker: From 721e40db90630a645723397e2fae9e81be4475ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 21 Jan 2019 13:51:33 +0100 Subject: [PATCH 7/9] fixed trailing whitespace --- public/app/core/services/context_srv.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/app/core/services/context_srv.ts b/public/app/core/services/context_srv.ts index c52bbdd775d..05985aae999 100644 --- a/public/app/core/services/context_srv.ts +++ b/public/app/core/services/context_srv.ts @@ -64,7 +64,7 @@ export class ContextSrv { hasAccessToExplore() { return (this.isEditor || config.viewersCanEdit) && config.exploreEnabled; } - + getTheme(): ThemeName { return this.user.lightTheme ? ThemeNames.Light : ThemeNames.Dark; } From db2a15daf035d9979bb692bd3fd6f74748f15ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 21 Jan 2019 18:54:57 +0100 Subject: [PATCH 8/9] Fixed loading of default query editor --- .../features/dashboard/panel_editor/QueriesTab.tsx | 2 +- .../dashboard/panel_editor/QueryEditorRow.tsx | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/public/app/features/dashboard/panel_editor/QueriesTab.tsx b/public/app/features/dashboard/panel_editor/QueriesTab.tsx index ca06098debd..28d822e3ad5 100644 --- a/public/app/features/dashboard/panel_editor/QueriesTab.tsx +++ b/public/app/features/dashboard/panel_editor/QueriesTab.tsx @@ -197,7 +197,7 @@ export class QueriesTab extends PureComponent {
{panel.targets.map((query, index) => ( void; onRemoveQuery: (query: DataQuery) => void; onMoveQuery: (query: DataQuery, direction: number) => void; - datasourceName: string | null; + dataSourceValue: string | null; inMixedMode: boolean; } interface State { + loadedDataSourceValue: string | null | undefined; datasource: DataSourceApi | null; isCollapsed: boolean; angularScope: AngularQueryComponentScope | null; @@ -36,6 +37,7 @@ export class QueryEditorRow extends PureComponent { datasource: null, isCollapsed: false, angularScope: null, + loadedDataSourceValue: undefined, }; componentDidMount() { @@ -61,14 +63,14 @@ export class QueryEditorRow extends PureComponent { const dataSourceSrv = getDatasourceSrv(); const datasource = await dataSourceSrv.get(query.datasource || panel.datasource); - this.setState({ datasource }); + this.setState({ datasource, loadedDataSourceValue: this.props.dataSourceValue }); } componentDidUpdate() { - const { datasource } = this.state; + const { loadedDataSourceValue } = this.state; // check if we need to load another datasource - if (datasource && datasource.name !== this.props.datasourceName) { + if (loadedDataSourceValue !== this.props.dataSourceValue) { if (this.angularQueryEditor) { this.angularQueryEditor.destroy(); this.angularQueryEditor = null; @@ -178,7 +180,7 @@ export class QueryEditorRow extends PureComponent { } render() { - const { query, datasourceName, inMixedMode } = this.props; + const { query, inMixedMode } = this.props; const { datasource, isCollapsed } = this.state; const isDisabled = query.hide; @@ -202,7 +204,7 @@ export class QueryEditorRow extends PureComponent { {isCollapsed && } {!isCollapsed && } {query.refId} - {inMixedMode && ({datasourceName})} + {inMixedMode && ({datasource.name})} {isDisabled && Disabled}
From 018fef0400f257b5100e06d19d38dc59853ca575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 21 Jan 2019 20:35:24 +0100 Subject: [PATCH 9/9] Added refId to missing queries on panel model init --- public/app/features/dashboard/panel_model.ts | 18 +++++++++++++++--- .../dashboard/specs/panel_model.test.ts | 8 ++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/public/app/features/dashboard/panel_model.ts b/public/app/features/dashboard/panel_model.ts index b7e8a68c722..6aded0da1d7 100644 --- a/public/app/features/dashboard/panel_model.ts +++ b/public/app/features/dashboard/panel_model.ts @@ -62,7 +62,7 @@ const mustKeepProps: { [str: string]: boolean } = { const defaults: any = { gridPos: { x: 0, y: 0, h: 3, w: 6 }, datasource: null, - targets: [{}], + targets: [{ refId: 'A' }], cachedPluginOptions: {}, transparent: false, }; @@ -83,7 +83,7 @@ export class PanelModel { collapsed?: boolean; panels?: any; soloMode?: boolean; - targets: any[]; + targets: DataQuery[]; datasource: string; thresholds?: any; @@ -118,6 +118,18 @@ export class PanelModel { // defaults _.defaultsDeep(this, _.cloneDeep(defaults)); + // queries must have refId + this.ensureQueryIds(); + } + + ensureQueryIds() { + if (this.targets) { + for (const query of this.targets) { + if (!query.refId) { + query.refId = this.getNextQueryLetter(); + } + } + } } getOptions(panelDefaults) { @@ -243,7 +255,7 @@ export class PanelModel { addQuery(query?: Partial) { query = query || { refId: 'A' }; query.refId = this.getNextQueryLetter(); - this.targets.push(query); + this.targets.push(query as DataQuery); } getNextQueryLetter(): string { diff --git a/public/app/features/dashboard/specs/panel_model.test.ts b/public/app/features/dashboard/specs/panel_model.test.ts index 36bb8d6297e..89976fa275a 100644 --- a/public/app/features/dashboard/specs/panel_model.test.ts +++ b/public/app/features/dashboard/specs/panel_model.test.ts @@ -9,6 +9,10 @@ describe('PanelModel', () => { model = new PanelModel({ type: 'table', showColumns: true, + targets: [ + {refId: 'A'}, + {noRefId: true} + ] }); }); @@ -20,6 +24,10 @@ describe('PanelModel', () => { expect(model.showColumns).toBe(true); }); + it('should add missing refIds', () => { + expect(model.targets[1].refId).toBe('B'); + }); + it('getSaveModel should remove defaults', () => { const saveModel = model.getSaveModel(); expect(saveModel.gridPos).toBe(undefined);