2019-01-17 10:59:47 -06:00
|
|
|
// Libraries
|
2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
2019-03-18 04:44:00 -05:00
|
|
|
// Utils
|
2020-10-01 12:51:23 -05:00
|
|
|
import { getTemplateSrv } from '@grafana/runtime';
|
2019-03-18 05:17:58 -05:00
|
|
|
import { getNextRefIdChar } from 'app/core/utils/query';
|
2019-03-18 04:44:00 -05:00
|
|
|
// Types
|
2019-10-31 04:48:05 -05:00
|
|
|
import {
|
2020-03-16 08:26:03 -05:00
|
|
|
DataConfigSource,
|
2020-03-10 02:53:41 -05:00
|
|
|
DataLink,
|
2019-10-31 04:48:05 -05:00
|
|
|
DataQuery,
|
|
|
|
DataTransformerConfig,
|
2020-04-27 13:50:33 -05:00
|
|
|
FieldConfigSource,
|
2020-03-10 02:53:41 -05:00
|
|
|
PanelPlugin,
|
2019-10-31 04:48:05 -05:00
|
|
|
ScopedVars,
|
2021-02-04 02:15:01 -06:00
|
|
|
EventBus,
|
2020-11-03 06:08:54 -06:00
|
|
|
EventBusSrv,
|
2020-11-17 03:25:36 -06:00
|
|
|
DataFrameDTO,
|
2020-12-15 06:29:37 -06:00
|
|
|
urlUtil,
|
|
|
|
DataLinkBuiltInVars,
|
2019-10-31 04:48:05 -05:00
|
|
|
} from '@grafana/data';
|
2020-02-22 14:08:42 -06:00
|
|
|
import { EDIT_PANEL_ID } from 'app/core/constants';
|
2019-03-25 08:10:23 -05:00
|
|
|
import config from 'app/core/config';
|
2020-11-26 11:12:02 -06:00
|
|
|
import { PanelQueryRunner } from '../../query/state/PanelQueryRunner';
|
2020-12-23 03:45:31 -06:00
|
|
|
import {
|
|
|
|
PanelOptionsChangedEvent,
|
|
|
|
PanelQueriesChangedEvent,
|
|
|
|
PanelTransformationsChangedEvent,
|
|
|
|
RefreshEvent,
|
|
|
|
RenderEvent,
|
|
|
|
} from 'app/types/events';
|
2020-12-15 06:29:37 -06:00
|
|
|
import { getTimeSrv } from '../services/TimeSrv';
|
|
|
|
import { getAllVariableValuesForUrl } from '../../variables/getAllVariableValuesForUrl';
|
2021-01-20 09:06:29 -06:00
|
|
|
import {
|
|
|
|
filterFieldConfigOverrides,
|
|
|
|
getPanelOptionsWithDefaults,
|
|
|
|
isStandardFieldProp,
|
|
|
|
restoreCustomOverrideRules,
|
|
|
|
} from './getPanelOptionsWithDefaults';
|
2021-02-25 04:26:28 -06:00
|
|
|
import { LibraryPanelDTO } from 'app/features/library-panels/state/api';
|
2019-04-18 01:22:14 -05:00
|
|
|
|
2017-10-10 07:20:53 -05:00
|
|
|
export interface GridPos {
|
2017-10-10 02:34:14 -05:00
|
|
|
x: number;
|
|
|
|
y: number;
|
2017-10-10 07:20:53 -05:00
|
|
|
w: number;
|
|
|
|
h: number;
|
2017-10-16 02:55:55 -05:00
|
|
|
static?: boolean;
|
2017-10-10 07:20:53 -05:00
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
const notPersistedProperties: { [str: string]: boolean } = {
|
|
|
|
events: true,
|
2020-04-10 09:37:26 -05:00
|
|
|
isViewing: true,
|
2017-12-20 05:33:33 -06:00
|
|
|
isEditing: true,
|
2019-05-03 14:55:22 -05:00
|
|
|
isInView: true,
|
2018-08-25 10:49:39 -05:00
|
|
|
hasRefreshed: true,
|
2018-12-04 07:19:55 -06:00
|
|
|
cachedPluginOptions: true,
|
2019-03-24 09:56:32 -05:00
|
|
|
plugin: true,
|
2019-04-18 01:22:14 -05:00
|
|
|
queryRunner: true,
|
2020-03-16 08:26:03 -05:00
|
|
|
replaceVariables: true,
|
2020-03-27 00:40:14 -05:00
|
|
|
editSourceId: true,
|
2017-10-10 07:20:53 -05:00
|
|
|
};
|
|
|
|
|
2018-11-16 03:00:13 -06:00
|
|
|
// For angular panels we need to clean up properties when changing type
|
|
|
|
// To make sure the change happens without strange bugs happening when panels use same
|
|
|
|
// named property with different type / value expectations
|
|
|
|
// This is not required for react panels
|
|
|
|
const mustKeepProps: { [str: string]: boolean } = {
|
|
|
|
id: true,
|
|
|
|
gridPos: true,
|
|
|
|
type: true,
|
|
|
|
title: true,
|
|
|
|
scopedVars: true,
|
|
|
|
repeat: true,
|
|
|
|
repeatIteration: true,
|
|
|
|
repeatPanelId: true,
|
|
|
|
repeatDirection: true,
|
|
|
|
repeatedByRow: true,
|
|
|
|
minSpan: true,
|
|
|
|
collapsed: true,
|
|
|
|
panels: true,
|
|
|
|
targets: true,
|
|
|
|
datasource: true,
|
|
|
|
timeFrom: true,
|
|
|
|
timeShift: true,
|
|
|
|
hideTimeOverride: true,
|
|
|
|
description: true,
|
|
|
|
links: true,
|
|
|
|
fullscreen: true,
|
|
|
|
isEditing: true,
|
|
|
|
hasRefreshed: true,
|
|
|
|
events: true,
|
|
|
|
cacheTimeout: true,
|
2018-12-04 07:19:55 -06:00
|
|
|
cachedPluginOptions: true,
|
2018-12-06 03:34:27 -06:00
|
|
|
transparent: true,
|
2019-03-22 15:45:09 -05:00
|
|
|
pluginVersion: true,
|
2019-05-03 03:29:22 -05:00
|
|
|
queryRunner: true,
|
2019-09-09 01:58:57 -05:00
|
|
|
transformations: true,
|
2020-03-19 05:50:31 -05:00
|
|
|
fieldConfig: true,
|
2020-04-27 10:32:35 -05:00
|
|
|
editSourceId: true,
|
2020-05-11 15:03:43 -05:00
|
|
|
maxDataPoints: true,
|
|
|
|
interval: true,
|
2021-01-20 09:06:29 -06:00
|
|
|
replaceVariables: true,
|
2021-02-25 04:26:28 -06:00
|
|
|
libraryPanel: true,
|
2018-11-16 03:00:13 -06:00
|
|
|
};
|
|
|
|
|
2018-10-14 14:14:11 -05:00
|
|
|
const defaults: any = {
|
|
|
|
gridPos: { x: 0, y: 0, h: 3, w: 6 },
|
2019-01-21 13:35:24 -06:00
|
|
|
targets: [{ refId: 'A' }],
|
2018-12-04 07:19:55 -06:00
|
|
|
cachedPluginOptions: {},
|
2018-12-06 03:34:27 -06:00
|
|
|
transparent: false,
|
2020-02-13 09:06:45 -06:00
|
|
|
options: {},
|
2020-05-11 15:03:43 -05:00
|
|
|
datasource: null,
|
2021-02-22 03:06:07 -06:00
|
|
|
title: '',
|
2018-10-14 14:14:11 -05:00
|
|
|
};
|
|
|
|
|
2020-03-16 08:26:03 -05:00
|
|
|
export class PanelModel implements DataConfigSource {
|
2020-02-10 07:23:54 -06:00
|
|
|
/* persisted id, used in URL to identify a panel */
|
2021-02-22 03:06:07 -06:00
|
|
|
id!: number;
|
|
|
|
editSourceId?: number;
|
|
|
|
gridPos!: GridPos;
|
|
|
|
type!: string;
|
|
|
|
title!: string;
|
2017-10-11 09:08:56 -05:00
|
|
|
alert?: any;
|
2019-03-04 03:42:59 -06:00
|
|
|
scopedVars?: ScopedVars;
|
2017-10-13 07:50:16 -05:00
|
|
|
repeat?: string;
|
|
|
|
repeatIteration?: number;
|
|
|
|
repeatPanelId?: number;
|
|
|
|
repeatDirection?: string;
|
2018-01-10 07:30:43 -06:00
|
|
|
repeatedByRow?: boolean;
|
2018-08-20 08:33:49 -05:00
|
|
|
maxPerRow?: number;
|
2017-10-17 07:53:52 -05:00
|
|
|
collapsed?: boolean;
|
2020-08-03 11:50:36 -05:00
|
|
|
|
2017-10-17 05:04:18 -05:00
|
|
|
panels?: any;
|
2019-01-21 13:35:24 -06:00
|
|
|
targets: DataQuery[];
|
2019-09-09 01:58:57 -05:00
|
|
|
transformations?: DataTransformerConfig[];
|
2020-07-09 08:16:35 -05:00
|
|
|
datasource: string | null;
|
2018-08-25 14:22:50 -05:00
|
|
|
thresholds?: any;
|
2019-03-22 15:45:09 -05:00
|
|
|
pluginVersion?: string;
|
2017-10-11 04:42:49 -05:00
|
|
|
|
2020-11-17 03:25:36 -06:00
|
|
|
snapshotData?: DataFrameDTO[];
|
2018-11-08 07:44:12 -06:00
|
|
|
timeFrom?: any;
|
|
|
|
timeShift?: any;
|
|
|
|
hideTimeOverride?: any;
|
2019-02-18 04:40:25 -06:00
|
|
|
options: {
|
|
|
|
[key: string]: any;
|
|
|
|
};
|
2020-03-19 05:50:31 -05:00
|
|
|
fieldConfig: FieldConfigSource;
|
2018-11-08 07:44:12 -06:00
|
|
|
|
2020-12-02 08:42:54 -06:00
|
|
|
maxDataPoints?: number | null;
|
|
|
|
interval?: string | null;
|
2018-11-14 01:07:16 -06:00
|
|
|
description?: string;
|
2019-06-25 04:38:51 -05:00
|
|
|
links?: DataLink[];
|
2018-12-06 03:34:27 -06:00
|
|
|
transparent: boolean;
|
2018-11-12 02:32:55 -06:00
|
|
|
|
2021-02-25 04:26:28 -06:00
|
|
|
libraryPanel?: { uid: undefined; name: string } | Pick<LibraryPanelDTO, 'uid' | 'name' | 'meta'>;
|
|
|
|
|
2017-10-11 04:42:49 -05:00
|
|
|
// non persisted
|
2020-04-10 09:37:26 -05:00
|
|
|
isViewing: boolean;
|
2017-10-11 04:42:49 -05:00
|
|
|
isEditing: boolean;
|
2019-05-03 14:55:22 -05:00
|
|
|
isInView: boolean;
|
2020-09-17 00:35:24 -05:00
|
|
|
|
2018-08-25 10:49:39 -05:00
|
|
|
hasRefreshed: boolean;
|
2021-02-04 02:15:01 -06:00
|
|
|
events: EventBus;
|
2018-11-20 09:33:26 -06:00
|
|
|
cacheTimeout?: any;
|
2021-01-20 09:06:29 -06:00
|
|
|
cachedPluginOptions: Record<string, PanelOptionsCache>;
|
2020-03-10 02:53:41 -05:00
|
|
|
legend?: { show: boolean; sort?: string; sortDesc?: boolean };
|
2019-05-01 00:36:46 -05:00
|
|
|
plugin?: PanelPlugin;
|
2020-02-09 03:53:34 -06:00
|
|
|
|
2019-04-18 16:10:18 -05:00
|
|
|
private queryRunner?: PanelQueryRunner;
|
2018-12-04 07:19:55 -06:00
|
|
|
|
2019-03-19 12:24:09 -05:00
|
|
|
constructor(model: any) {
|
2020-11-03 06:08:54 -06:00
|
|
|
this.events = new EventBusSrv();
|
2020-02-07 07:59:04 -06:00
|
|
|
this.restoreModel(model);
|
2020-03-16 08:26:03 -05:00
|
|
|
this.replaceVariables = this.replaceVariables.bind(this);
|
2020-02-07 07:59:04 -06:00
|
|
|
}
|
2019-09-24 05:15:35 -05:00
|
|
|
|
2020-02-07 07:59:04 -06:00
|
|
|
/** Given a persistened PanelModel restores property values */
|
2020-02-28 04:04:40 -06:00
|
|
|
restoreModel(model: any) {
|
2020-04-27 13:50:33 -05:00
|
|
|
// Start with clean-up
|
2020-05-11 15:03:43 -05:00
|
|
|
for (const property in this) {
|
|
|
|
if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
|
2020-04-27 13:50:33 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (model[property]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof (this as any)[property] === 'function') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof (this as any)[property] === 'symbol') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete (this as any)[property];
|
|
|
|
}
|
|
|
|
|
2017-10-10 07:20:53 -05:00
|
|
|
// copy properties from persisted model
|
2018-08-29 07:26:50 -05:00
|
|
|
for (const property in model) {
|
2019-03-19 12:24:09 -05:00
|
|
|
(this as any)[property] = model[property];
|
2017-10-10 07:20:53 -05:00
|
|
|
}
|
2017-10-12 14:37:27 -05:00
|
|
|
|
2018-06-26 09:32:01 -05:00
|
|
|
// defaults
|
2018-10-14 14:14:11 -05:00
|
|
|
_.defaultsDeep(this, _.cloneDeep(defaults));
|
2019-05-06 23:07:33 -05:00
|
|
|
|
2019-01-21 13:35:24 -06:00
|
|
|
// queries must have refId
|
|
|
|
this.ensureQueryIds();
|
2020-02-28 04:04:40 -06:00
|
|
|
}
|
2019-01-21 13:35:24 -06:00
|
|
|
|
|
|
|
ensureQueryIds() {
|
2019-03-15 02:26:56 -05:00
|
|
|
if (this.targets && _.isArray(this.targets)) {
|
2019-01-21 13:35:24 -06:00
|
|
|
for (const query of this.targets) {
|
|
|
|
if (!query.refId) {
|
2019-03-18 05:17:58 -05:00
|
|
|
query.refId = getNextRefIdChar(this.targets);
|
2019-01-21 13:35:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 07:20:53 -05:00
|
|
|
}
|
|
|
|
|
2019-05-21 06:19:19 -05:00
|
|
|
getOptions() {
|
|
|
|
return this.options;
|
2018-11-05 10:46:09 -06:00
|
|
|
}
|
2020-08-31 10:53:21 -05:00
|
|
|
|
2020-03-19 05:50:31 -05:00
|
|
|
getFieldConfig() {
|
|
|
|
return this.fieldConfig;
|
|
|
|
}
|
2018-11-05 10:46:09 -06:00
|
|
|
|
|
|
|
updateOptions(options: object) {
|
2019-02-18 04:41:14 -06:00
|
|
|
this.options = options;
|
2020-11-26 08:14:57 -06:00
|
|
|
this.events.publish(new PanelOptionsChangedEvent());
|
2020-03-19 05:50:31 -05:00
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFieldConfig(config: FieldConfigSource) {
|
|
|
|
this.fieldConfig = config;
|
2020-12-17 06:30:55 -06:00
|
|
|
this.events.publish(new PanelOptionsChangedEvent());
|
2020-03-19 05:50:31 -05:00
|
|
|
|
2020-03-16 08:26:03 -05:00
|
|
|
this.resendLastResult();
|
2018-11-05 10:46:09 -06:00
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
2017-10-10 07:20:53 -05:00
|
|
|
getSaveModel() {
|
2017-10-10 10:57:53 -05:00
|
|
|
const model: any = {};
|
2020-05-11 15:03:43 -05:00
|
|
|
|
2018-08-29 07:26:50 -05:00
|
|
|
for (const property in this) {
|
2017-10-10 07:20:53 -05:00
|
|
|
if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-14 14:14:11 -05:00
|
|
|
if (_.isEqual(this[property], defaults[property])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-12 14:37:27 -05:00
|
|
|
model[property] = _.cloneDeep(this[property]);
|
2017-10-10 07:20:53 -05:00
|
|
|
}
|
2020-05-11 15:03:43 -05:00
|
|
|
|
|
|
|
if (model.datasource === undefined) {
|
|
|
|
// This is part of defaults as defaults are removed in save model and
|
|
|
|
// this should not be removed in save model as exporter needs to templatize it
|
|
|
|
model.datasource = null;
|
|
|
|
}
|
|
|
|
|
2017-10-10 10:57:53 -05:00
|
|
|
return model;
|
2017-10-10 07:20:53 -05:00
|
|
|
}
|
|
|
|
|
2020-04-10 09:37:26 -05:00
|
|
|
setIsViewing(isViewing: boolean) {
|
|
|
|
this.isViewing = isViewing;
|
2017-10-11 04:42:49 -05:00
|
|
|
}
|
|
|
|
|
2017-10-10 07:20:53 -05:00
|
|
|
updateGridPos(newPos: GridPos) {
|
|
|
|
this.gridPos.x = newPos.x;
|
|
|
|
this.gridPos.y = newPos.y;
|
|
|
|
this.gridPos.w = newPos.w;
|
|
|
|
this.gridPos.h = newPos.h;
|
2017-10-11 14:36:03 -05:00
|
|
|
}
|
2017-10-16 09:09:23 -05:00
|
|
|
|
2018-08-25 10:49:39 -05:00
|
|
|
refresh() {
|
|
|
|
this.hasRefreshed = true;
|
2020-12-23 03:45:31 -06:00
|
|
|
this.events.publish(new RefreshEvent());
|
2018-08-25 10:49:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (!this.hasRefreshed) {
|
|
|
|
this.refresh();
|
|
|
|
} else {
|
2020-12-23 03:45:31 -06:00
|
|
|
this.events.publish(new RenderEvent());
|
2018-08-25 10:49:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:33:21 -06:00
|
|
|
private getOptionsToRemember() {
|
2018-12-04 07:19:55 -06:00
|
|
|
return Object.keys(this).reduce((acc, property) => {
|
2018-12-05 00:33:21 -06:00
|
|
|
if (notPersistedProperties[property] || mustKeepProps[property]) {
|
2018-12-04 07:19:55 -06:00
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...acc,
|
2019-03-19 12:24:09 -05:00
|
|
|
[property]: (this as any)[property],
|
2018-12-04 07:19:55 -06:00
|
|
|
};
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:33:21 -06:00
|
|
|
private restorePanelOptions(pluginId: string) {
|
2021-01-20 09:06:29 -06:00
|
|
|
if (!this.cachedPluginOptions) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-04 07:19:55 -06:00
|
|
|
|
2021-01-20 09:06:29 -06:00
|
|
|
const prevOptions = this.cachedPluginOptions[pluginId];
|
2018-12-04 07:19:55 -06:00
|
|
|
|
2021-01-20 09:06:29 -06:00
|
|
|
if (!prevOptions) {
|
2019-05-21 06:19:19 -05:00
|
|
|
return;
|
|
|
|
}
|
2020-08-31 10:53:21 -05:00
|
|
|
|
2021-01-20 09:06:29 -06:00
|
|
|
Object.keys(prevOptions.properties).map((property) => {
|
|
|
|
(this as any)[property] = prevOptions.properties[property];
|
2019-11-19 07:59:39 -06:00
|
|
|
});
|
2020-03-19 05:50:31 -05:00
|
|
|
|
2021-01-20 09:06:29 -06:00
|
|
|
this.fieldConfig = restoreCustomOverrideRules(this.fieldConfig, prevOptions.fieldConfig);
|
2020-11-09 06:11:09 -06:00
|
|
|
}
|
|
|
|
|
2021-02-13 04:35:39 -06:00
|
|
|
applyPluginOptionDefaults(plugin: PanelPlugin, isAfterPluginChange: boolean) {
|
2021-01-20 09:06:29 -06:00
|
|
|
const options = getPanelOptionsWithDefaults({
|
|
|
|
plugin,
|
|
|
|
currentOptions: this.options,
|
|
|
|
currentFieldConfig: this.fieldConfig,
|
2021-02-13 04:35:39 -06:00
|
|
|
isAfterPluginChange: isAfterPluginChange,
|
2021-01-20 09:06:29 -06:00
|
|
|
});
|
2020-11-09 06:11:09 -06:00
|
|
|
|
2021-01-20 09:06:29 -06:00
|
|
|
this.fieldConfig = options.fieldConfig;
|
|
|
|
this.options = options.options;
|
2019-05-21 06:19:19 -05:00
|
|
|
}
|
|
|
|
|
2019-05-01 00:36:46 -05:00
|
|
|
pluginLoaded(plugin: PanelPlugin) {
|
2019-03-24 09:56:32 -05:00
|
|
|
this.plugin = plugin;
|
|
|
|
|
2020-07-01 02:39:06 -05:00
|
|
|
if (plugin.onPanelMigration) {
|
2019-05-01 00:36:46 -05:00
|
|
|
const version = getPluginVersion(plugin);
|
2020-02-10 07:23:54 -06:00
|
|
|
|
2019-03-25 08:10:23 -05:00
|
|
|
if (version !== this.pluginVersion) {
|
2019-05-01 00:36:46 -05:00
|
|
|
this.options = plugin.onPanelMigration(this);
|
2019-03-25 08:10:23 -05:00
|
|
|
this.pluginVersion = version;
|
|
|
|
}
|
2019-03-24 09:56:32 -05:00
|
|
|
}
|
2019-08-06 01:26:11 -05:00
|
|
|
|
2021-02-13 04:35:39 -06:00
|
|
|
this.applyPluginOptionDefaults(plugin, false);
|
2020-03-16 08:26:03 -05:00
|
|
|
this.resendLastResult();
|
2019-03-24 09:56:32 -05:00
|
|
|
}
|
|
|
|
|
2021-01-20 09:06:29 -06:00
|
|
|
clearPropertiesBeforePluginChange() {
|
2019-02-18 04:41:14 -06:00
|
|
|
// remove panel type specific options
|
|
|
|
for (const key of _.keys(this)) {
|
|
|
|
if (mustKeepProps[key]) {
|
|
|
|
continue;
|
2018-11-16 03:00:13 -06:00
|
|
|
}
|
2019-03-19 12:24:09 -05:00
|
|
|
delete (this as any)[key];
|
2018-11-16 03:00:13 -06:00
|
|
|
}
|
2018-12-04 07:19:55 -06:00
|
|
|
|
2021-01-20 09:06:29 -06:00
|
|
|
this.options = {};
|
|
|
|
|
|
|
|
// clear custom options
|
|
|
|
this.fieldConfig = {
|
|
|
|
defaults: {
|
|
|
|
...this.fieldConfig.defaults,
|
|
|
|
custom: {},
|
|
|
|
},
|
|
|
|
// filter out custom overrides
|
|
|
|
overrides: filterFieldConfigOverrides(this.fieldConfig.overrides, isStandardFieldProp),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
changePlugin(newPlugin: PanelPlugin) {
|
|
|
|
const pluginId = newPlugin.meta.id;
|
|
|
|
const oldOptions: any = this.getOptionsToRemember();
|
2021-01-20 11:28:27 -06:00
|
|
|
const prevFieldConfig = this.fieldConfig;
|
2021-01-20 09:06:29 -06:00
|
|
|
const oldPluginId = this.type;
|
|
|
|
const wasAngular = this.isAngularPlugin();
|
|
|
|
|
|
|
|
this.cachedPluginOptions[oldPluginId] = {
|
|
|
|
properties: oldOptions,
|
2021-01-20 11:28:27 -06:00
|
|
|
fieldConfig: prevFieldConfig,
|
2021-01-20 09:06:29 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
this.clearPropertiesBeforePluginChange();
|
2018-12-04 07:19:55 -06:00
|
|
|
this.restorePanelOptions(pluginId);
|
2019-02-21 06:43:36 -06:00
|
|
|
|
2019-03-24 10:39:55 -05:00
|
|
|
// Let panel plugins inspect options from previous panel and keep any that it can use
|
2019-05-01 00:36:46 -05:00
|
|
|
if (newPlugin.onPanelTypeChanged) {
|
2021-01-20 11:28:27 -06:00
|
|
|
const prevOptions = wasAngular ? { angular: oldOptions } : oldOptions.options;
|
|
|
|
Object.assign(this.options, newPlugin.onPanelTypeChanged(this, oldPluginId, prevOptions, prevFieldConfig));
|
2019-05-01 00:36:46 -05:00
|
|
|
}
|
2019-04-04 11:30:15 -05:00
|
|
|
|
2019-08-18 17:01:07 -05:00
|
|
|
// switch
|
|
|
|
this.type = pluginId;
|
|
|
|
this.plugin = newPlugin;
|
2020-03-16 08:26:03 -05:00
|
|
|
|
|
|
|
// For some reason I need to rebind replace variables here, otherwise the viz repeater does not work
|
|
|
|
this.replaceVariables = this.replaceVariables.bind(this);
|
2021-02-13 04:35:39 -06:00
|
|
|
this.applyPluginOptionDefaults(newPlugin, true);
|
2019-08-18 17:01:07 -05:00
|
|
|
|
2019-05-01 00:36:46 -05:00
|
|
|
if (newPlugin.onPanelMigration) {
|
|
|
|
this.pluginVersion = getPluginVersion(newPlugin);
|
2019-02-21 06:43:36 -06:00
|
|
|
}
|
2018-07-09 11:17:51 -05:00
|
|
|
}
|
|
|
|
|
2020-09-28 02:06:46 -05:00
|
|
|
updateQueries(queries: DataQuery[]) {
|
2020-11-26 08:14:57 -06:00
|
|
|
this.events.publish(new PanelQueriesChangedEvent());
|
2020-09-28 02:06:46 -05:00
|
|
|
this.targets = queries;
|
|
|
|
}
|
|
|
|
|
2018-12-12 01:54:12 -06:00
|
|
|
addQuery(query?: Partial<DataQuery>) {
|
2018-12-11 06:36:44 -06:00
|
|
|
query = query || { refId: 'A' };
|
2019-03-18 05:17:58 -05:00
|
|
|
query.refId = getNextRefIdChar(this.targets);
|
2019-01-21 13:35:24 -06:00
|
|
|
this.targets.push(query as DataQuery);
|
2018-12-11 06:36:44 -06:00
|
|
|
}
|
|
|
|
|
2019-01-29 02:39:23 -06:00
|
|
|
changeQuery(query: DataQuery, index: number) {
|
|
|
|
// ensure refId is maintained
|
|
|
|
query.refId = this.targets[index].refId;
|
|
|
|
|
|
|
|
// update query in array
|
|
|
|
this.targets = this.targets.map((item, itemIndex) => {
|
|
|
|
if (itemIndex === index) {
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-07 07:59:04 -06:00
|
|
|
getEditClone() {
|
2020-02-13 09:06:45 -06:00
|
|
|
const sourceModel = this.getSaveModel();
|
|
|
|
|
|
|
|
// Temporary id for the clone, restored later in redux action when changes are saved
|
2020-02-22 14:08:42 -06:00
|
|
|
sourceModel.id = EDIT_PANEL_ID;
|
2020-03-27 00:40:14 -05:00
|
|
|
sourceModel.editSourceId = this.id;
|
2020-02-13 09:06:45 -06:00
|
|
|
|
|
|
|
const clone = new PanelModel(sourceModel);
|
2020-04-10 09:37:26 -05:00
|
|
|
clone.isEditing = true;
|
2020-02-11 07:57:16 -06:00
|
|
|
const sourceQueryRunner = this.getQueryRunner();
|
2020-02-08 06:23:16 -06:00
|
|
|
|
2020-05-06 09:06:21 -05:00
|
|
|
// Copy last query result
|
|
|
|
clone.getQueryRunner().useLastResultFrom(sourceQueryRunner);
|
2020-02-08 06:23:16 -06:00
|
|
|
|
2020-02-07 07:59:04 -06:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2020-03-16 08:26:03 -05:00
|
|
|
getTransformations() {
|
|
|
|
return this.transformations;
|
|
|
|
}
|
|
|
|
|
|
|
|
getFieldOverrideOptions() {
|
|
|
|
if (!this.plugin) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-04-06 09:24:41 -05:00
|
|
|
fieldConfig: this.fieldConfig,
|
2020-03-16 08:26:03 -05:00
|
|
|
replaceVariables: this.replaceVariables,
|
2020-04-06 09:24:41 -05:00
|
|
|
fieldConfigRegistry: this.plugin.fieldConfigRegistry,
|
2020-03-16 08:26:03 -05:00
|
|
|
theme: config.theme,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-18 16:10:18 -05:00
|
|
|
getQueryRunner(): PanelQueryRunner {
|
|
|
|
if (!this.queryRunner) {
|
2020-03-16 08:26:03 -05:00
|
|
|
this.queryRunner = new PanelQueryRunner(this);
|
2019-04-18 16:10:18 -05:00
|
|
|
}
|
|
|
|
return this.queryRunner;
|
|
|
|
}
|
|
|
|
|
2019-05-06 08:26:09 -05:00
|
|
|
hasTitle() {
|
2019-05-08 10:21:46 -05:00
|
|
|
return this.title && this.title.length > 0;
|
2019-05-06 08:26:09 -05:00
|
|
|
}
|
|
|
|
|
2019-09-12 10:28:46 -05:00
|
|
|
isAngularPlugin(): boolean {
|
2020-07-09 08:16:35 -05:00
|
|
|
return (this.plugin && this.plugin.angularPanelCtrl) !== undefined;
|
2019-09-12 10:28:46 -05:00
|
|
|
}
|
|
|
|
|
2017-10-16 09:09:23 -05:00
|
|
|
destroy() {
|
|
|
|
this.events.removeAllListeners();
|
2019-04-18 23:56:27 -05:00
|
|
|
|
|
|
|
if (this.queryRunner) {
|
|
|
|
this.queryRunner.destroy();
|
|
|
|
}
|
2017-10-16 09:09:23 -05:00
|
|
|
}
|
2019-09-09 01:58:57 -05:00
|
|
|
|
|
|
|
setTransformations(transformations: DataTransformerConfig[]) {
|
|
|
|
this.transformations = transformations;
|
2020-04-07 12:54:06 -05:00
|
|
|
this.resendLastResult();
|
2020-11-26 08:14:57 -06:00
|
|
|
this.events.publish(new PanelTransformationsChangedEvent());
|
2020-03-16 08:26:03 -05:00
|
|
|
}
|
|
|
|
|
2020-12-15 06:29:37 -06:00
|
|
|
replaceVariables(value: string, extraVars: ScopedVars | undefined, format?: string | Function) {
|
2020-03-16 08:26:03 -05:00
|
|
|
let vars = this.scopedVars;
|
2020-12-15 06:29:37 -06:00
|
|
|
|
2020-03-16 08:26:03 -05:00
|
|
|
if (extraVars) {
|
|
|
|
vars = vars ? { ...vars, ...extraVars } : extraVars;
|
|
|
|
}
|
2020-12-15 06:29:37 -06:00
|
|
|
const allVariablesParams = getAllVariableValuesForUrl(vars);
|
|
|
|
const variablesQuery = urlUtil.toUrlParams(allVariablesParams);
|
|
|
|
const timeRangeUrl = urlUtil.toUrlParams(getTimeSrv().timeRangeForUrl());
|
|
|
|
|
|
|
|
vars = {
|
|
|
|
...vars,
|
|
|
|
[DataLinkBuiltInVars.keepTime]: {
|
|
|
|
text: timeRangeUrl,
|
|
|
|
value: timeRangeUrl,
|
|
|
|
},
|
|
|
|
[DataLinkBuiltInVars.includeVars]: {
|
|
|
|
text: variablesQuery,
|
|
|
|
value: variablesQuery,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-10-01 12:51:23 -05:00
|
|
|
return getTemplateSrv().replace(value, vars, format);
|
2020-03-16 08:26:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
resendLastResult() {
|
|
|
|
if (!this.plugin) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getQueryRunner().resendLastResult();
|
2019-09-09 01:58:57 -05:00
|
|
|
}
|
2020-04-10 09:37:26 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Panel have a different id while in edit mode (to more easily be able to discard changes)
|
|
|
|
* Use this to always get the underlying source id
|
|
|
|
* */
|
|
|
|
getSavedId(): number {
|
|
|
|
return this.editSourceId ?? this.id;
|
|
|
|
}
|
2017-10-10 02:34:14 -05:00
|
|
|
}
|
2019-05-01 00:36:46 -05:00
|
|
|
|
|
|
|
function getPluginVersion(plugin: PanelPlugin): string {
|
|
|
|
return plugin && plugin.meta.info.version ? plugin.meta.info.version : config.buildInfo.version;
|
|
|
|
}
|
2021-01-20 09:06:29 -06:00
|
|
|
|
2021-02-25 04:26:28 -06:00
|
|
|
export function isLibraryPanel(panel: PanelModel): panel is PanelModel & Required<Pick<PanelModel, 'libraryPanel'>> {
|
|
|
|
return panel.libraryPanel !== undefined;
|
|
|
|
}
|
|
|
|
|
2021-01-20 09:06:29 -06:00
|
|
|
interface PanelOptionsCache {
|
|
|
|
properties: any;
|
|
|
|
fieldConfig: FieldConfigSource;
|
|
|
|
}
|