2019-01-18 00:59:47 +08:00
|
|
|
// Libraries
|
2021-04-21 15:38:00 +08:00
|
|
|
import { cloneDeep, defaultsDeep, isArray, isEqual, keys } from 'lodash';
|
2019-03-18 17:44:00 +08:00
|
|
|
// Utils
|
2020-10-02 01:51:23 +08:00
|
|
|
import { getTemplateSrv } from '@grafana/runtime';
|
2019-03-18 18:17:58 +08:00
|
|
|
import { getNextRefIdChar } from 'app/core/utils/query';
|
2019-03-18 17:44:00 +08:00
|
|
|
// Types
|
2019-10-31 17:48:05 +08:00
|
|
|
import {
|
2020-03-16 21:26:03 +08:00
|
|
|
DataConfigSource,
|
2021-03-02 23:37:36 +08:00
|
|
|
DataFrameDTO,
|
2020-03-10 15:53:41 +08:00
|
|
|
DataLink,
|
2021-03-02 23:37:36 +08:00
|
|
|
DataLinkBuiltInVars,
|
2019-10-31 17:48:05 +08:00
|
|
|
DataQuery,
|
|
|
|
|
DataTransformerConfig,
|
2021-03-02 23:37:36 +08:00
|
|
|
EventBus,
|
|
|
|
|
EventBusSrv,
|
2020-04-28 02:50:33 +08:00
|
|
|
FieldConfigSource,
|
2020-03-10 15:53:41 +08:00
|
|
|
PanelPlugin,
|
2021-04-27 13:39:02 +08:00
|
|
|
PanelPluginDataSupport,
|
2019-10-31 17:48:05 +08:00
|
|
|
ScopedVars,
|
2020-12-15 20:29:37 +08:00
|
|
|
urlUtil,
|
2019-10-31 17:48:05 +08:00
|
|
|
} from '@grafana/data';
|
2020-02-23 04:08:42 +08:00
|
|
|
import { EDIT_PANEL_ID } from 'app/core/constants';
|
2019-03-25 21:10:23 +08:00
|
|
|
import config from 'app/core/config';
|
2020-11-27 01:12:02 +08:00
|
|
|
import { PanelQueryRunner } from '../../query/state/PanelQueryRunner';
|
2020-12-23 17:45:31 +08:00
|
|
|
import {
|
|
|
|
|
PanelOptionsChangedEvent,
|
|
|
|
|
PanelQueriesChangedEvent,
|
|
|
|
|
PanelTransformationsChangedEvent,
|
|
|
|
|
RefreshEvent,
|
|
|
|
|
RenderEvent,
|
|
|
|
|
} from 'app/types/events';
|
2020-12-15 20:29:37 +08:00
|
|
|
import { getTimeSrv } from '../services/TimeSrv';
|
2021-04-01 12:33:11 +08:00
|
|
|
import { getVariablesUrlParams } from '../../variables/getAllVariableValuesForUrl';
|
2021-01-20 23:06:29 +08:00
|
|
|
import {
|
|
|
|
|
filterFieldConfigOverrides,
|
|
|
|
|
getPanelOptionsWithDefaults,
|
|
|
|
|
isStandardFieldProp,
|
|
|
|
|
restoreCustomOverrideRules,
|
|
|
|
|
} from './getPanelOptionsWithDefaults';
|
2021-03-03 22:16:54 +08:00
|
|
|
import { QueryGroupOptions } from 'app/types';
|
2021-03-02 23:37:36 +08:00
|
|
|
import { PanelModelLibraryPanel } from '../../library-panels/types';
|
2021-04-12 18:47:17 +08:00
|
|
|
import { isDeprecatedPanel } from '../utils/panel';
|
2019-04-18 14:22:14 +08:00
|
|
|
|
2017-10-10 20:20:53 +08:00
|
|
|
export interface GridPos {
|
2017-10-10 15:34:14 +08:00
|
|
|
x: number;
|
|
|
|
|
y: number;
|
2017-10-10 20:20:53 +08:00
|
|
|
w: number;
|
|
|
|
|
h: number;
|
2017-10-16 15:55:55 +08:00
|
|
|
static?: boolean;
|
2017-10-10 20:20:53 +08:00
|
|
|
}
|
|
|
|
|
|
2017-12-19 23:06:54 +08:00
|
|
|
const notPersistedProperties: { [str: string]: boolean } = {
|
|
|
|
|
events: true,
|
2020-04-10 22:37:26 +08:00
|
|
|
isViewing: true,
|
2017-12-20 19:33:33 +08:00
|
|
|
isEditing: true,
|
2019-05-04 03:55:22 +08:00
|
|
|
isInView: true,
|
2018-08-25 23:49:39 +08:00
|
|
|
hasRefreshed: true,
|
2018-12-04 21:19:55 +08:00
|
|
|
cachedPluginOptions: true,
|
2019-03-24 22:56:32 +08:00
|
|
|
plugin: true,
|
2019-04-18 14:22:14 +08:00
|
|
|
queryRunner: true,
|
2020-03-16 21:26:03 +08:00
|
|
|
replaceVariables: true,
|
2020-03-27 13:40:14 +08:00
|
|
|
editSourceId: true,
|
2021-04-20 01:24:39 +08:00
|
|
|
configRev: true,
|
2021-03-15 15:44:13 +08:00
|
|
|
getDisplayTitle: true,
|
2021-04-27 13:39:02 +08:00
|
|
|
dataSupport: true,
|
2017-10-10 20:20:53 +08:00
|
|
|
};
|
|
|
|
|
|
2018-11-16 17:00:13 +08: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 21:19:55 +08:00
|
|
|
cachedPluginOptions: true,
|
2018-12-06 17:34:27 +08:00
|
|
|
transparent: true,
|
2019-03-23 04:45:09 +08:00
|
|
|
pluginVersion: true,
|
2019-05-03 16:29:22 +08:00
|
|
|
queryRunner: true,
|
2019-09-09 14:58:57 +08:00
|
|
|
transformations: true,
|
2020-03-19 18:50:31 +08:00
|
|
|
fieldConfig: true,
|
2020-04-27 23:32:35 +08:00
|
|
|
editSourceId: true,
|
2020-05-12 04:03:43 +08:00
|
|
|
maxDataPoints: true,
|
|
|
|
|
interval: true,
|
2021-01-20 23:06:29 +08:00
|
|
|
replaceVariables: true,
|
2021-02-25 18:26:28 +08:00
|
|
|
libraryPanel: true,
|
2021-03-15 15:44:13 +08:00
|
|
|
getDisplayTitle: true,
|
2021-04-21 03:22:58 +08:00
|
|
|
configRev: true,
|
2018-11-16 17:00:13 +08:00
|
|
|
};
|
|
|
|
|
|
2018-10-15 03:14:11 +08:00
|
|
|
const defaults: any = {
|
|
|
|
|
gridPos: { x: 0, y: 0, h: 3, w: 6 },
|
2019-01-22 03:35:24 +08:00
|
|
|
targets: [{ refId: 'A' }],
|
2018-12-04 21:19:55 +08:00
|
|
|
cachedPluginOptions: {},
|
2018-12-06 17:34:27 +08:00
|
|
|
transparent: false,
|
2020-02-13 23:06:45 +08:00
|
|
|
options: {},
|
2020-05-12 04:03:43 +08:00
|
|
|
datasource: null,
|
2021-02-22 17:06:07 +08:00
|
|
|
title: '',
|
2018-10-15 03:14:11 +08:00
|
|
|
};
|
|
|
|
|
|
2020-03-16 21:26:03 +08:00
|
|
|
export class PanelModel implements DataConfigSource {
|
2020-02-10 21:23:54 +08:00
|
|
|
/* persisted id, used in URL to identify a panel */
|
2021-02-22 17:06:07 +08:00
|
|
|
id!: number;
|
|
|
|
|
editSourceId?: number;
|
|
|
|
|
gridPos!: GridPos;
|
|
|
|
|
type!: string;
|
|
|
|
|
title!: string;
|
2017-10-11 22:08:56 +08:00
|
|
|
alert?: any;
|
2019-03-04 17:42:59 +08:00
|
|
|
scopedVars?: ScopedVars;
|
2017-10-13 20:50:16 +08:00
|
|
|
repeat?: string;
|
|
|
|
|
repeatIteration?: number;
|
|
|
|
|
repeatPanelId?: number;
|
|
|
|
|
repeatDirection?: string;
|
2018-01-10 21:30:43 +08:00
|
|
|
repeatedByRow?: boolean;
|
2018-08-20 21:33:49 +08:00
|
|
|
maxPerRow?: number;
|
2017-10-17 20:53:52 +08:00
|
|
|
collapsed?: boolean;
|
2020-08-04 00:50:36 +08:00
|
|
|
|
2017-10-17 18:04:18 +08:00
|
|
|
panels?: any;
|
2021-04-15 20:21:06 +08:00
|
|
|
declare targets: DataQuery[];
|
2019-09-09 14:58:57 +08:00
|
|
|
transformations?: DataTransformerConfig[];
|
2021-04-15 20:21:06 +08:00
|
|
|
datasource: string | null = null;
|
2018-08-26 03:22:50 +08:00
|
|
|
thresholds?: any;
|
2019-03-23 04:45:09 +08:00
|
|
|
pluginVersion?: string;
|
2017-10-11 17:42:49 +08:00
|
|
|
|
2020-11-17 17:25:36 +08:00
|
|
|
snapshotData?: DataFrameDTO[];
|
2018-11-08 21:44:12 +08:00
|
|
|
timeFrom?: any;
|
|
|
|
|
timeShift?: any;
|
|
|
|
|
hideTimeOverride?: any;
|
2021-04-15 20:21:06 +08:00
|
|
|
declare options: {
|
2019-02-18 18:40:25 +08:00
|
|
|
[key: string]: any;
|
|
|
|
|
};
|
2021-04-15 20:21:06 +08:00
|
|
|
declare fieldConfig: FieldConfigSource;
|
2018-11-08 21:44:12 +08:00
|
|
|
|
2020-12-02 22:42:54 +08:00
|
|
|
maxDataPoints?: number | null;
|
|
|
|
|
interval?: string | null;
|
2018-11-14 15:07:16 +08:00
|
|
|
description?: string;
|
2019-06-25 17:38:51 +08:00
|
|
|
links?: DataLink[];
|
2021-04-15 20:21:06 +08:00
|
|
|
declare transparent: boolean;
|
2018-11-12 16:32:55 +08:00
|
|
|
|
2021-03-02 23:37:36 +08:00
|
|
|
libraryPanel?: { uid: undefined; name: string } | PanelModelLibraryPanel;
|
2021-02-25 18:26:28 +08:00
|
|
|
|
2017-10-11 17:42:49 +08:00
|
|
|
// non persisted
|
2021-04-15 20:21:06 +08:00
|
|
|
isViewing = false;
|
|
|
|
|
isEditing = false;
|
|
|
|
|
isInView = false;
|
2021-04-20 01:24:39 +08:00
|
|
|
configRev = 0; // increments when configs change
|
2020-09-17 13:35:24 +08:00
|
|
|
|
2021-04-15 20:21:06 +08:00
|
|
|
hasRefreshed?: boolean;
|
2021-02-04 16:15:01 +08:00
|
|
|
events: EventBus;
|
2018-11-20 23:33:26 +08:00
|
|
|
cacheTimeout?: any;
|
2021-04-15 20:21:06 +08:00
|
|
|
declare cachedPluginOptions: Record<string, PanelOptionsCache>;
|
2020-03-10 15:53:41 +08:00
|
|
|
legend?: { show: boolean; sort?: string; sortDesc?: boolean };
|
2019-05-01 13:36:46 +08:00
|
|
|
plugin?: PanelPlugin;
|
2021-04-27 13:39:02 +08:00
|
|
|
dataSupport?: PanelPluginDataSupport;
|
2020-02-09 17:53:34 +08:00
|
|
|
|
2019-04-19 05:10:18 +08:00
|
|
|
private queryRunner?: PanelQueryRunner;
|
2018-12-04 21:19:55 +08:00
|
|
|
|
2019-03-20 01:24:09 +08:00
|
|
|
constructor(model: any) {
|
2020-11-03 20:08:54 +08:00
|
|
|
this.events = new EventBusSrv();
|
2020-02-07 21:59:04 +08:00
|
|
|
this.restoreModel(model);
|
2020-03-16 21:26:03 +08:00
|
|
|
this.replaceVariables = this.replaceVariables.bind(this);
|
2020-02-07 21:59:04 +08:00
|
|
|
}
|
2019-09-24 18:15:35 +08:00
|
|
|
|
2020-02-07 21:59:04 +08:00
|
|
|
/** Given a persistened PanelModel restores property values */
|
2020-02-28 18:04:40 +08:00
|
|
|
restoreModel(model: any) {
|
2020-04-28 02:50:33 +08:00
|
|
|
// Start with clean-up
|
2020-05-12 04:03:43 +08:00
|
|
|
for (const property in this) {
|
|
|
|
|
if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
|
2020-04-28 02:50:33 +08: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 20:20:53 +08:00
|
|
|
// copy properties from persisted model
|
2018-08-29 20:26:50 +08:00
|
|
|
for (const property in model) {
|
2019-03-20 01:24:09 +08:00
|
|
|
(this as any)[property] = model[property];
|
2017-10-10 20:20:53 +08:00
|
|
|
}
|
2017-10-13 03:37:27 +08:00
|
|
|
|
2018-06-26 22:32:01 +08:00
|
|
|
// defaults
|
2021-04-21 15:38:00 +08:00
|
|
|
defaultsDeep(this, cloneDeep(defaults));
|
2019-05-07 12:07:33 +08:00
|
|
|
|
2019-01-22 03:35:24 +08:00
|
|
|
// queries must have refId
|
|
|
|
|
this.ensureQueryIds();
|
2020-02-28 18:04:40 +08:00
|
|
|
}
|
2019-01-22 03:35:24 +08:00
|
|
|
|
|
|
|
|
ensureQueryIds() {
|
2021-04-21 15:38:00 +08:00
|
|
|
if (this.targets && isArray(this.targets)) {
|
2019-01-22 03:35:24 +08:00
|
|
|
for (const query of this.targets) {
|
|
|
|
|
if (!query.refId) {
|
2019-03-18 18:17:58 +08:00
|
|
|
query.refId = getNextRefIdChar(this.targets);
|
2019-01-22 03:35:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-10 20:20:53 +08:00
|
|
|
}
|
|
|
|
|
|
2019-05-21 19:19:19 +08:00
|
|
|
getOptions() {
|
|
|
|
|
return this.options;
|
2018-11-06 00:46:09 +08:00
|
|
|
}
|
2020-08-31 23:53:21 +08:00
|
|
|
|
2020-03-19 18:50:31 +08:00
|
|
|
getFieldConfig() {
|
|
|
|
|
return this.fieldConfig;
|
|
|
|
|
}
|
2018-11-06 00:46:09 +08:00
|
|
|
|
2021-04-20 01:24:39 +08:00
|
|
|
get hasChanged(): boolean {
|
|
|
|
|
return this.configRev > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 00:46:09 +08:00
|
|
|
updateOptions(options: object) {
|
2019-02-18 18:41:14 +08:00
|
|
|
this.options = options;
|
2021-04-20 01:24:39 +08:00
|
|
|
this.configRev++;
|
2020-11-26 22:14:57 +08:00
|
|
|
this.events.publish(new PanelOptionsChangedEvent());
|
2020-03-19 18:50:31 +08:00
|
|
|
this.render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateFieldConfig(config: FieldConfigSource) {
|
|
|
|
|
this.fieldConfig = config;
|
2021-04-20 01:24:39 +08:00
|
|
|
this.configRev++;
|
2020-12-17 20:30:55 +08:00
|
|
|
this.events.publish(new PanelOptionsChangedEvent());
|
2020-03-19 18:50:31 +08:00
|
|
|
|
2020-03-16 21:26:03 +08:00
|
|
|
this.resendLastResult();
|
2018-11-06 00:46:09 +08:00
|
|
|
this.render();
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 20:20:53 +08:00
|
|
|
getSaveModel() {
|
2017-10-10 23:57:53 +08:00
|
|
|
const model: any = {};
|
2020-05-12 04:03:43 +08:00
|
|
|
|
2018-08-29 20:26:50 +08:00
|
|
|
for (const property in this) {
|
2017-10-10 20:20:53 +08:00
|
|
|
if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 15:38:00 +08:00
|
|
|
if (isEqual(this[property], defaults[property])) {
|
2018-10-15 03:14:11 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 15:38:00 +08:00
|
|
|
model[property] = cloneDeep(this[property]);
|
2017-10-10 20:20:53 +08:00
|
|
|
}
|
2020-05-12 04:03:43 +08: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 23:57:53 +08:00
|
|
|
return model;
|
2017-10-10 20:20:53 +08:00
|
|
|
}
|
|
|
|
|
|
2020-04-10 22:37:26 +08:00
|
|
|
setIsViewing(isViewing: boolean) {
|
|
|
|
|
this.isViewing = isViewing;
|
2017-10-11 17:42:49 +08:00
|
|
|
}
|
|
|
|
|
|
2017-10-10 20:20:53 +08: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-12 03:36:03 +08:00
|
|
|
}
|
2017-10-16 22:09:23 +08:00
|
|
|
|
2018-08-25 23:49:39 +08:00
|
|
|
refresh() {
|
|
|
|
|
this.hasRefreshed = true;
|
2020-12-23 17:45:31 +08:00
|
|
|
this.events.publish(new RefreshEvent());
|
2018-08-25 23:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
if (!this.hasRefreshed) {
|
|
|
|
|
this.refresh();
|
|
|
|
|
} else {
|
2020-12-23 17:45:31 +08:00
|
|
|
this.events.publish(new RenderEvent());
|
2018-08-25 23:49:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 14:33:21 +08:00
|
|
|
private getOptionsToRemember() {
|
2018-12-04 21:19:55 +08:00
|
|
|
return Object.keys(this).reduce((acc, property) => {
|
2018-12-05 14:33:21 +08:00
|
|
|
if (notPersistedProperties[property] || mustKeepProps[property]) {
|
2018-12-04 21:19:55 +08:00
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...acc,
|
2019-03-20 01:24:09 +08:00
|
|
|
[property]: (this as any)[property],
|
2018-12-04 21:19:55 +08:00
|
|
|
};
|
|
|
|
|
}, {});
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 14:33:21 +08:00
|
|
|
private restorePanelOptions(pluginId: string) {
|
2021-01-20 23:06:29 +08:00
|
|
|
if (!this.cachedPluginOptions) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-12-04 21:19:55 +08:00
|
|
|
|
2021-01-20 23:06:29 +08:00
|
|
|
const prevOptions = this.cachedPluginOptions[pluginId];
|
2018-12-04 21:19:55 +08:00
|
|
|
|
2021-01-20 23:06:29 +08:00
|
|
|
if (!prevOptions) {
|
2019-05-21 19:19:19 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2020-08-31 23:53:21 +08:00
|
|
|
|
2021-01-20 23:06:29 +08:00
|
|
|
Object.keys(prevOptions.properties).map((property) => {
|
|
|
|
|
(this as any)[property] = prevOptions.properties[property];
|
2019-11-19 21:59:39 +08:00
|
|
|
});
|
2020-03-19 18:50:31 +08:00
|
|
|
|
2021-01-20 23:06:29 +08:00
|
|
|
this.fieldConfig = restoreCustomOverrideRules(this.fieldConfig, prevOptions.fieldConfig);
|
2020-11-09 20:11:09 +08:00
|
|
|
}
|
|
|
|
|
|
2021-02-13 18:35:39 +08:00
|
|
|
applyPluginOptionDefaults(plugin: PanelPlugin, isAfterPluginChange: boolean) {
|
2021-01-20 23:06:29 +08:00
|
|
|
const options = getPanelOptionsWithDefaults({
|
|
|
|
|
plugin,
|
|
|
|
|
currentOptions: this.options,
|
|
|
|
|
currentFieldConfig: this.fieldConfig,
|
2021-02-13 18:35:39 +08:00
|
|
|
isAfterPluginChange: isAfterPluginChange,
|
2021-01-20 23:06:29 +08:00
|
|
|
});
|
2020-11-09 20:11:09 +08:00
|
|
|
|
2021-01-20 23:06:29 +08:00
|
|
|
this.fieldConfig = options.fieldConfig;
|
|
|
|
|
this.options = options.options;
|
2019-05-21 19:19:19 +08:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 13:36:46 +08:00
|
|
|
pluginLoaded(plugin: PanelPlugin) {
|
2019-03-24 22:56:32 +08:00
|
|
|
this.plugin = plugin;
|
2021-04-12 18:47:17 +08:00
|
|
|
const version = getPluginVersion(plugin);
|
2019-03-24 22:56:32 +08:00
|
|
|
|
2020-07-01 15:39:06 +08:00
|
|
|
if (plugin.onPanelMigration) {
|
2019-03-25 21:10:23 +08:00
|
|
|
if (version !== this.pluginVersion) {
|
2019-05-01 13:36:46 +08:00
|
|
|
this.options = plugin.onPanelMigration(this);
|
2019-03-25 21:10:23 +08:00
|
|
|
this.pluginVersion = version;
|
|
|
|
|
}
|
2019-03-24 22:56:32 +08:00
|
|
|
}
|
2019-08-06 14:26:11 +08:00
|
|
|
|
2021-04-27 13:39:02 +08:00
|
|
|
this.dataSupport = plugin.dataSupport;
|
2021-02-13 18:35:39 +08:00
|
|
|
this.applyPluginOptionDefaults(plugin, false);
|
2020-03-16 21:26:03 +08:00
|
|
|
this.resendLastResult();
|
2019-03-24 22:56:32 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-20 23:06:29 +08:00
|
|
|
clearPropertiesBeforePluginChange() {
|
2019-02-18 18:41:14 +08:00
|
|
|
// remove panel type specific options
|
2021-04-21 15:38:00 +08:00
|
|
|
for (const key of keys(this)) {
|
2019-02-18 18:41:14 +08:00
|
|
|
if (mustKeepProps[key]) {
|
|
|
|
|
continue;
|
2018-11-16 17:00:13 +08:00
|
|
|
}
|
2019-03-20 01:24:09 +08:00
|
|
|
delete (this as any)[key];
|
2018-11-16 17:00:13 +08:00
|
|
|
}
|
2018-12-04 21:19:55 +08:00
|
|
|
|
2021-01-20 23:06:29 +08: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-21 01:28:27 +08:00
|
|
|
const prevFieldConfig = this.fieldConfig;
|
2021-01-20 23:06:29 +08:00
|
|
|
const oldPluginId = this.type;
|
2021-04-12 18:47:17 +08:00
|
|
|
const wasAngular = this.isAngularPlugin() || isDeprecatedPanel(this.type);
|
2021-01-20 23:06:29 +08:00
|
|
|
this.cachedPluginOptions[oldPluginId] = {
|
|
|
|
|
properties: oldOptions,
|
2021-01-21 01:28:27 +08:00
|
|
|
fieldConfig: prevFieldConfig,
|
2021-01-20 23:06:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.clearPropertiesBeforePluginChange();
|
2018-12-04 21:19:55 +08:00
|
|
|
this.restorePanelOptions(pluginId);
|
2019-02-21 20:43:36 +08:00
|
|
|
|
2019-03-24 23:39:55 +08:00
|
|
|
// Let panel plugins inspect options from previous panel and keep any that it can use
|
2019-05-01 13:36:46 +08:00
|
|
|
if (newPlugin.onPanelTypeChanged) {
|
2021-01-21 01:28:27 +08:00
|
|
|
const prevOptions = wasAngular ? { angular: oldOptions } : oldOptions.options;
|
|
|
|
|
Object.assign(this.options, newPlugin.onPanelTypeChanged(this, oldPluginId, prevOptions, prevFieldConfig));
|
2019-05-01 13:36:46 +08:00
|
|
|
}
|
2019-04-05 00:30:15 +08:00
|
|
|
|
2019-08-19 06:01:07 +08:00
|
|
|
// switch
|
|
|
|
|
this.type = pluginId;
|
|
|
|
|
this.plugin = newPlugin;
|
2021-04-20 01:24:39 +08:00
|
|
|
this.configRev++;
|
2020-03-16 21:26:03 +08: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 18:35:39 +08:00
|
|
|
this.applyPluginOptionDefaults(newPlugin, true);
|
2019-08-19 06:01:07 +08:00
|
|
|
|
2019-05-01 13:36:46 +08:00
|
|
|
if (newPlugin.onPanelMigration) {
|
|
|
|
|
this.pluginVersion = getPluginVersion(newPlugin);
|
2019-02-21 20:43:36 +08:00
|
|
|
}
|
2018-07-10 00:17:51 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-03 22:16:54 +08:00
|
|
|
updateQueries(options: QueryGroupOptions) {
|
|
|
|
|
this.datasource = options.dataSource.default ? null : options.dataSource.name!;
|
|
|
|
|
this.timeFrom = options.timeRange?.from;
|
|
|
|
|
this.timeShift = options.timeRange?.shift;
|
|
|
|
|
this.hideTimeOverride = options.timeRange?.hide;
|
|
|
|
|
this.interval = options.minInterval;
|
|
|
|
|
this.maxDataPoints = options.maxDataPoints;
|
|
|
|
|
this.targets = options.queries;
|
2021-04-20 01:24:39 +08:00
|
|
|
this.configRev++;
|
2021-03-03 22:16:54 +08:00
|
|
|
|
2020-11-26 22:14:57 +08:00
|
|
|
this.events.publish(new PanelQueriesChangedEvent());
|
2020-09-28 15:06:46 +08:00
|
|
|
}
|
|
|
|
|
|
2018-12-12 15:54:12 +08:00
|
|
|
addQuery(query?: Partial<DataQuery>) {
|
2018-12-11 20:36:44 +08:00
|
|
|
query = query || { refId: 'A' };
|
2019-03-18 18:17:58 +08:00
|
|
|
query.refId = getNextRefIdChar(this.targets);
|
2019-01-22 03:35:24 +08:00
|
|
|
this.targets.push(query as DataQuery);
|
2021-04-20 01:24:39 +08:00
|
|
|
this.configRev++;
|
2018-12-11 20:36:44 +08:00
|
|
|
}
|
|
|
|
|
|
2019-01-29 16:39:23 +08:00
|
|
|
changeQuery(query: DataQuery, index: number) {
|
|
|
|
|
// ensure refId is maintained
|
|
|
|
|
query.refId = this.targets[index].refId;
|
2021-04-20 01:24:39 +08:00
|
|
|
this.configRev++;
|
2019-01-29 16:39:23 +08:00
|
|
|
|
|
|
|
|
// update query in array
|
|
|
|
|
this.targets = this.targets.map((item, itemIndex) => {
|
|
|
|
|
if (itemIndex === index) {
|
|
|
|
|
return query;
|
|
|
|
|
}
|
|
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-07 21:59:04 +08:00
|
|
|
getEditClone() {
|
2020-02-13 23:06:45 +08:00
|
|
|
const sourceModel = this.getSaveModel();
|
|
|
|
|
|
|
|
|
|
// Temporary id for the clone, restored later in redux action when changes are saved
|
2020-02-23 04:08:42 +08:00
|
|
|
sourceModel.id = EDIT_PANEL_ID;
|
2020-03-27 13:40:14 +08:00
|
|
|
sourceModel.editSourceId = this.id;
|
2020-02-13 23:06:45 +08:00
|
|
|
|
|
|
|
|
const clone = new PanelModel(sourceModel);
|
2020-04-10 22:37:26 +08:00
|
|
|
clone.isEditing = true;
|
2020-02-11 21:57:16 +08:00
|
|
|
const sourceQueryRunner = this.getQueryRunner();
|
2020-02-08 20:23:16 +08:00
|
|
|
|
2020-05-06 22:06:21 +08:00
|
|
|
// Copy last query result
|
|
|
|
|
clone.getQueryRunner().useLastResultFrom(sourceQueryRunner);
|
2020-02-08 20:23:16 +08:00
|
|
|
|
2020-02-07 21:59:04 +08:00
|
|
|
return clone;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 21:26:03 +08:00
|
|
|
getTransformations() {
|
|
|
|
|
return this.transformations;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getFieldOverrideOptions() {
|
|
|
|
|
if (!this.plugin) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2020-04-06 22:24:41 +08:00
|
|
|
fieldConfig: this.fieldConfig,
|
2020-03-16 21:26:03 +08:00
|
|
|
replaceVariables: this.replaceVariables,
|
2020-04-06 22:24:41 +08:00
|
|
|
fieldConfigRegistry: this.plugin.fieldConfigRegistry,
|
2020-03-16 21:26:03 +08:00
|
|
|
theme: config.theme,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-19 05:10:18 +08:00
|
|
|
getQueryRunner(): PanelQueryRunner {
|
|
|
|
|
if (!this.queryRunner) {
|
2020-03-16 21:26:03 +08:00
|
|
|
this.queryRunner = new PanelQueryRunner(this);
|
2019-04-19 05:10:18 +08:00
|
|
|
}
|
|
|
|
|
return this.queryRunner;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 21:26:09 +08:00
|
|
|
hasTitle() {
|
2019-05-08 23:21:46 +08:00
|
|
|
return this.title && this.title.length > 0;
|
2019-05-06 21:26:09 +08:00
|
|
|
}
|
|
|
|
|
|
2019-09-12 23:28:46 +08:00
|
|
|
isAngularPlugin(): boolean {
|
2020-07-09 21:16:35 +08:00
|
|
|
return (this.plugin && this.plugin.angularPanelCtrl) !== undefined;
|
2019-09-12 23:28:46 +08:00
|
|
|
}
|
|
|
|
|
|
2017-10-16 22:09:23 +08:00
|
|
|
destroy() {
|
|
|
|
|
this.events.removeAllListeners();
|
2019-04-19 12:56:27 +08:00
|
|
|
|
|
|
|
|
if (this.queryRunner) {
|
|
|
|
|
this.queryRunner.destroy();
|
|
|
|
|
}
|
2017-10-16 22:09:23 +08:00
|
|
|
}
|
2019-09-09 14:58:57 +08:00
|
|
|
|
|
|
|
|
setTransformations(transformations: DataTransformerConfig[]) {
|
|
|
|
|
this.transformations = transformations;
|
2020-04-08 01:54:06 +08:00
|
|
|
this.resendLastResult();
|
2021-04-20 01:24:39 +08:00
|
|
|
this.configRev++;
|
2020-11-26 22:14:57 +08:00
|
|
|
this.events.publish(new PanelTransformationsChangedEvent());
|
2020-03-16 21:26:03 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-03 22:16:54 +08:00
|
|
|
setProperty(key: keyof this, value: any) {
|
|
|
|
|
this[key] = value;
|
2021-04-20 01:24:39 +08:00
|
|
|
this.configRev++;
|
2021-03-25 15:33:13 +08:00
|
|
|
|
|
|
|
|
// Custom handling of repeat dependent options, handled here as PanelEditor can
|
|
|
|
|
// update one key at a time right now
|
|
|
|
|
if (key === 'repeat') {
|
|
|
|
|
if (this.repeat && !this.repeatDirection) {
|
|
|
|
|
this.repeatDirection = 'h';
|
|
|
|
|
} else if (!this.repeat) {
|
|
|
|
|
delete this.repeatDirection;
|
|
|
|
|
delete this.maxPerRow;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-03 22:16:54 +08:00
|
|
|
}
|
|
|
|
|
|
2020-12-15 20:29:37 +08:00
|
|
|
replaceVariables(value: string, extraVars: ScopedVars | undefined, format?: string | Function) {
|
2020-03-16 21:26:03 +08:00
|
|
|
let vars = this.scopedVars;
|
2020-12-15 20:29:37 +08:00
|
|
|
|
2020-03-16 21:26:03 +08:00
|
|
|
if (extraVars) {
|
|
|
|
|
vars = vars ? { ...vars, ...extraVars } : extraVars;
|
|
|
|
|
}
|
2021-04-01 12:33:11 +08:00
|
|
|
|
|
|
|
|
const allVariablesParams = getVariablesUrlParams(vars);
|
2020-12-15 20:29:37 +08:00
|
|
|
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-02 01:51:23 +08:00
|
|
|
return getTemplateSrv().replace(value, vars, format);
|
2020-03-16 21:26:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resendLastResult() {
|
|
|
|
|
if (!this.plugin) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.getQueryRunner().resendLastResult();
|
2019-09-09 14:58:57 +08:00
|
|
|
}
|
2020-04-10 22:37:26 +08: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;
|
|
|
|
|
}
|
2021-03-15 15:44:13 +08:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This is the title used when displaying the title in the UI so it will include any interpolated variables.
|
|
|
|
|
* If you need the raw title without interpolation use title property instead.
|
|
|
|
|
* */
|
|
|
|
|
getDisplayTitle(): string {
|
|
|
|
|
return this.replaceVariables(this.title, {}, 'text');
|
|
|
|
|
}
|
2017-10-10 15:34:14 +08:00
|
|
|
}
|
2019-05-01 13:36:46 +08:00
|
|
|
|
|
|
|
|
function getPluginVersion(plugin: PanelPlugin): string {
|
|
|
|
|
return plugin && plugin.meta.info.version ? plugin.meta.info.version : config.buildInfo.version;
|
|
|
|
|
}
|
2021-01-20 23:06:29 +08:00
|
|
|
|
|
|
|
|
interface PanelOptionsCache {
|
|
|
|
|
properties: any;
|
|
|
|
|
fieldConfig: FieldConfigSource;
|
|
|
|
|
}
|