2021-04-21 08:38:00 +01:00
|
|
|
import { cloneDeep, defaultsDeep, isArray, isEqual, keys } from 'lodash';
|
2021-10-13 08:53:36 +02:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2019-10-31 10:48:05 +01:00
|
|
|
import {
|
2020-03-16 14:26:03 +01:00
|
|
|
DataConfigSource,
|
2021-03-02 16:37:36 +01:00
|
|
|
DataFrameDTO,
|
2020-03-10 08:53:41 +01:00
|
|
|
DataLink,
|
2021-03-02 16:37:36 +01:00
|
|
|
DataLinkBuiltInVars,
|
2019-10-31 10:48:05 +01:00
|
|
|
DataQuery,
|
|
|
|
|
DataTransformerConfig,
|
2021-03-02 16:37:36 +01:00
|
|
|
EventBusSrv,
|
2020-04-27 20:50:33 +02:00
|
|
|
FieldConfigSource,
|
2020-03-10 08:53:41 +01:00
|
|
|
PanelPlugin,
|
2021-04-27 07:39:02 +02:00
|
|
|
PanelPluginDataSupport,
|
2019-10-31 10:48:05 +01:00
|
|
|
ScopedVars,
|
2020-12-15 13:29:37 +01:00
|
|
|
urlUtil,
|
2021-08-11 00:23:41 -07:00
|
|
|
PanelModel as IPanelModel,
|
2021-10-29 10:57:24 -07:00
|
|
|
DataSourceRef,
|
2019-10-31 10:48:05 +01:00
|
|
|
} from '@grafana/data';
|
2022-04-22 14:33:13 +01:00
|
|
|
import { getTemplateSrv, RefreshEvent } from '@grafana/runtime';
|
2019-03-25 06:10:23 -07:00
|
|
|
import config from 'app/core/config';
|
2022-07-22 16:10:10 +01:00
|
|
|
import { safeStringifyValue } from 'app/core/utils/explore';
|
2022-04-22 14:33:13 +01:00
|
|
|
import { getNextRefIdChar } from 'app/core/utils/query';
|
|
|
|
|
import { QueryGroupOptions } from 'app/types';
|
2020-12-23 10:45:31 +01:00
|
|
|
import {
|
|
|
|
|
PanelOptionsChangedEvent,
|
|
|
|
|
PanelQueriesChangedEvent,
|
|
|
|
|
PanelTransformationsChangedEvent,
|
|
|
|
|
RenderEvent,
|
|
|
|
|
} from 'app/types/events';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
|
|
|
|
import { PanelModelLibraryPanel } from '../../library-panels/types';
|
|
|
|
|
import { PanelQueryRunner } from '../../query/state/PanelQueryRunner';
|
2021-04-01 06:33:11 +02:00
|
|
|
import { getVariablesUrlParams } from '../../variables/getAllVariableValuesForUrl';
|
2022-04-22 14:33:13 +01:00
|
|
|
import { getTimeSrv } from '../services/TimeSrv';
|
|
|
|
|
import { TimeOverrideResult } from '../utils/panel';
|
|
|
|
|
|
2021-01-20 16:06:29 +01:00
|
|
|
import {
|
|
|
|
|
filterFieldConfigOverrides,
|
|
|
|
|
getPanelOptionsWithDefaults,
|
|
|
|
|
isStandardFieldProp,
|
|
|
|
|
restoreCustomOverrideRules,
|
|
|
|
|
} from './getPanelOptionsWithDefaults';
|
2021-06-22 14:44:18 +02:00
|
|
|
|
2017-10-10 14:20:53 +02:00
|
|
|
export interface GridPos {
|
2017-10-10 09:34:14 +02:00
|
|
|
x: number;
|
|
|
|
|
y: number;
|
2017-10-10 14:20:53 +02:00
|
|
|
w: number;
|
|
|
|
|
h: number;
|
2017-10-16 09:55:55 +02:00
|
|
|
static?: boolean;
|
2017-10-10 14:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-10 10:36:19 +02:00
|
|
|
type RunPanelQueryOptions = {
|
|
|
|
|
/** @deprecate */
|
|
|
|
|
dashboardId: number;
|
|
|
|
|
dashboardUID: string;
|
|
|
|
|
dashboardTimezone: string;
|
|
|
|
|
timeData: TimeOverrideResult;
|
|
|
|
|
width: number;
|
|
|
|
|
publicDashboardAccessToken?: string;
|
|
|
|
|
};
|
2017-12-19 16:06:54 +01:00
|
|
|
const notPersistedProperties: { [str: string]: boolean } = {
|
|
|
|
|
events: true,
|
2020-04-10 16:37:26 +02:00
|
|
|
isViewing: true,
|
2017-12-20 12:33:33 +01:00
|
|
|
isEditing: true,
|
2019-05-03 12:55:22 -07:00
|
|
|
isInView: true,
|
2018-08-25 08:49:39 -07:00
|
|
|
hasRefreshed: true,
|
2018-12-04 14:19:55 +01:00
|
|
|
cachedPluginOptions: true,
|
2019-03-24 15:56:32 +01:00
|
|
|
plugin: true,
|
2019-04-17 23:22:14 -07:00
|
|
|
queryRunner: true,
|
2020-03-16 14:26:03 +01:00
|
|
|
replaceVariables: true,
|
2021-04-19 10:24:39 -07:00
|
|
|
configRev: true,
|
2022-09-06 19:00:32 +02:00
|
|
|
hasSavedPanelEditChange: true,
|
2021-03-15 08:44:13 +01:00
|
|
|
getDisplayTitle: true,
|
2021-04-27 07:39:02 +02:00
|
|
|
dataSupport: true,
|
2021-09-01 08:03:56 -07:00
|
|
|
key: true,
|
2017-10-10 14:20:53 +02:00
|
|
|
};
|
|
|
|
|
|
2018-11-16 10:00:13 +01: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,
|
|
|
|
|
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 14:19:55 +01:00
|
|
|
cachedPluginOptions: true,
|
2018-12-06 10:34:27 +01:00
|
|
|
transparent: true,
|
2019-03-22 13:45:09 -07:00
|
|
|
pluginVersion: true,
|
2019-05-03 10:29:22 +02:00
|
|
|
queryRunner: true,
|
2019-09-08 23:58:57 -07:00
|
|
|
transformations: true,
|
2020-03-19 11:50:31 +01:00
|
|
|
fieldConfig: true,
|
2020-05-11 22:03:43 +02:00
|
|
|
maxDataPoints: true,
|
|
|
|
|
interval: true,
|
2021-01-20 16:06:29 +01:00
|
|
|
replaceVariables: true,
|
2021-02-25 10:26:28 +00:00
|
|
|
libraryPanel: true,
|
2021-03-15 08:44:13 +01:00
|
|
|
getDisplayTitle: true,
|
2021-04-20 21:22:58 +02:00
|
|
|
configRev: true,
|
2021-10-13 08:53:36 +02:00
|
|
|
key: true,
|
2018-11-16 10:00:13 +01:00
|
|
|
};
|
|
|
|
|
|
2018-10-14 21:14:11 +02:00
|
|
|
const defaults: any = {
|
|
|
|
|
gridPos: { x: 0, y: 0, h: 3, w: 6 },
|
2019-01-21 20:35:24 +01:00
|
|
|
targets: [{ refId: 'A' }],
|
2018-12-04 14:19:55 +01:00
|
|
|
cachedPluginOptions: {},
|
2018-12-06 10:34:27 +01:00
|
|
|
transparent: false,
|
2020-02-13 16:06:45 +01:00
|
|
|
options: {},
|
2021-04-27 10:09:37 +02:00
|
|
|
fieldConfig: {
|
|
|
|
|
defaults: {},
|
|
|
|
|
overrides: [],
|
|
|
|
|
},
|
2021-02-22 10:06:07 +01:00
|
|
|
title: '',
|
2018-10-14 21:14:11 +02:00
|
|
|
};
|
|
|
|
|
|
2021-08-11 00:23:41 -07:00
|
|
|
export class PanelModel implements DataConfigSource, IPanelModel {
|
2020-02-10 14:23:54 +01:00
|
|
|
/* persisted id, used in URL to identify a panel */
|
2021-02-22 10:06:07 +01:00
|
|
|
id!: number;
|
|
|
|
|
gridPos!: GridPos;
|
|
|
|
|
type!: string;
|
|
|
|
|
title!: string;
|
2017-10-11 16:08:56 +02:00
|
|
|
alert?: any;
|
2019-03-04 10:42:59 +01:00
|
|
|
scopedVars?: ScopedVars;
|
2017-10-13 14:50:16 +02:00
|
|
|
repeat?: string;
|
|
|
|
|
repeatIteration?: number;
|
|
|
|
|
repeatPanelId?: number;
|
|
|
|
|
repeatDirection?: string;
|
2018-01-10 16:30:43 +03:00
|
|
|
repeatedByRow?: boolean;
|
2018-08-20 15:33:49 +02:00
|
|
|
maxPerRow?: number;
|
2017-10-17 14:53:52 +02:00
|
|
|
collapsed?: boolean;
|
2020-08-03 09:50:36 -07:00
|
|
|
|
2022-05-04 09:39:41 +01:00
|
|
|
panels?: PanelModel[];
|
2021-04-15 14:21:06 +02:00
|
|
|
declare targets: DataQuery[];
|
2019-09-08 23:58:57 -07:00
|
|
|
transformations?: DataTransformerConfig[];
|
2021-10-29 10:57:24 -07:00
|
|
|
datasource: DataSourceRef | null = null;
|
2018-08-25 12:22:50 -07:00
|
|
|
thresholds?: any;
|
2019-03-22 13:45:09 -07:00
|
|
|
pluginVersion?: string;
|
2017-10-11 11:42:49 +02:00
|
|
|
|
2020-11-17 10:25:36 +01:00
|
|
|
snapshotData?: DataFrameDTO[];
|
2018-11-08 14:44:12 +01:00
|
|
|
timeFrom?: any;
|
|
|
|
|
timeShift?: any;
|
|
|
|
|
hideTimeOverride?: any;
|
2021-04-15 14:21:06 +02:00
|
|
|
declare options: {
|
2019-02-18 11:40:25 +01:00
|
|
|
[key: string]: any;
|
|
|
|
|
};
|
2021-04-15 14:21:06 +02:00
|
|
|
declare fieldConfig: FieldConfigSource;
|
2018-11-08 14:44:12 +01:00
|
|
|
|
2020-12-02 15:42:54 +01:00
|
|
|
maxDataPoints?: number | null;
|
|
|
|
|
interval?: string | null;
|
2018-11-14 08:07:16 +01:00
|
|
|
description?: string;
|
2019-06-25 11:38:51 +02:00
|
|
|
links?: DataLink[];
|
2021-04-15 14:21:06 +02:00
|
|
|
declare transparent: boolean;
|
2018-11-12 09:32:55 +01:00
|
|
|
|
2022-09-06 19:00:32 +02:00
|
|
|
libraryPanel?: { uid: undefined; name: string; version?: number } | PanelModelLibraryPanel;
|
2021-02-25 10:26:28 +00:00
|
|
|
|
2022-06-10 17:12:56 -07:00
|
|
|
autoMigrateFrom?: string;
|
|
|
|
|
|
2017-10-11 11:42:49 +02:00
|
|
|
// non persisted
|
2021-04-15 14:21:06 +02:00
|
|
|
isViewing = false;
|
|
|
|
|
isEditing = false;
|
|
|
|
|
isInView = false;
|
2021-04-19 10:24:39 -07:00
|
|
|
configRev = 0; // increments when configs change
|
2022-09-06 19:00:32 +02:00
|
|
|
hasSavedPanelEditChange?: boolean;
|
2021-04-15 14:21:06 +02:00
|
|
|
hasRefreshed?: boolean;
|
2021-11-24 14:24:33 +01:00
|
|
|
cacheTimeout?: string | null;
|
2021-04-27 10:09:37 +02:00
|
|
|
cachedPluginOptions: Record<string, PanelOptionsCache> = {};
|
2020-03-10 08:53:41 +01:00
|
|
|
legend?: { show: boolean; sort?: string; sortDesc?: boolean };
|
2019-04-30 22:36:46 -07:00
|
|
|
plugin?: PanelPlugin;
|
2021-10-13 08:53:36 +02:00
|
|
|
/**
|
|
|
|
|
* Unique in application state, this is used as redux key for panel and for redux panel state
|
|
|
|
|
* Change will cause unmount and re-init of panel
|
|
|
|
|
*/
|
|
|
|
|
key: string;
|
2020-02-09 10:53:34 +01:00
|
|
|
|
2021-04-30 13:33:29 -07:00
|
|
|
/**
|
|
|
|
|
* The PanelModel event bus only used for internal and legacy angular support.
|
|
|
|
|
* The EventBus passed to panels is based on the dashboard event model.
|
|
|
|
|
*/
|
|
|
|
|
events: EventBusSrv;
|
|
|
|
|
|
2019-04-18 14:10:18 -07:00
|
|
|
private queryRunner?: PanelQueryRunner;
|
2018-12-04 14:19:55 +01:00
|
|
|
|
2019-03-19 18:24:09 +01:00
|
|
|
constructor(model: any) {
|
2020-11-03 13:08:54 +01:00
|
|
|
this.events = new EventBusSrv();
|
2020-02-07 14:59:04 +01:00
|
|
|
this.restoreModel(model);
|
2020-03-16 14:26:03 +01:00
|
|
|
this.replaceVariables = this.replaceVariables.bind(this);
|
2021-10-13 08:53:36 +02:00
|
|
|
this.key = uuidv4();
|
2020-02-07 14:59:04 +01:00
|
|
|
}
|
2019-09-24 12:15:35 +02:00
|
|
|
|
2020-02-07 14:59:04 +01:00
|
|
|
/** Given a persistened PanelModel restores property values */
|
2020-02-28 11:04:40 +01:00
|
|
|
restoreModel(model: any) {
|
2020-04-27 20:50:33 +02:00
|
|
|
// Start with clean-up
|
2020-05-11 22:03:43 +02:00
|
|
|
for (const property in this) {
|
|
|
|
|
if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
|
2020-04-27 20:50:33 +02: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 14:20:53 +02:00
|
|
|
// copy properties from persisted model
|
2018-08-29 14:26:50 +02:00
|
|
|
for (const property in model) {
|
2019-03-19 18:24:09 +01:00
|
|
|
(this as any)[property] = model[property];
|
2017-10-10 14:20:53 +02:00
|
|
|
}
|
2017-10-12 21:37:27 +02:00
|
|
|
|
2022-06-23 11:48:28 -07:00
|
|
|
switch (this.type) {
|
|
|
|
|
case 'graph':
|
|
|
|
|
if (config?.featureToggles?.autoMigrateGraphPanels) {
|
|
|
|
|
this.autoMigrateFrom = this.type;
|
|
|
|
|
this.type = 'timeseries';
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'heatmap-new':
|
|
|
|
|
this.autoMigrateFrom = this.type;
|
|
|
|
|
this.type = 'heatmap';
|
|
|
|
|
break;
|
2022-06-10 17:12:56 -07:00
|
|
|
}
|
|
|
|
|
|
2018-06-26 16:32:01 +02:00
|
|
|
// defaults
|
2021-04-21 08:38:00 +01:00
|
|
|
defaultsDeep(this, cloneDeep(defaults));
|
2019-05-07 06:07:33 +02:00
|
|
|
|
2019-01-21 20:35:24 +01:00
|
|
|
// queries must have refId
|
|
|
|
|
this.ensureQueryIds();
|
2020-02-28 11:04:40 +01:00
|
|
|
}
|
2019-01-21 20:35:24 +01:00
|
|
|
|
2021-10-13 08:53:36 +02:00
|
|
|
generateNewKey() {
|
|
|
|
|
this.key = uuidv4();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-21 20:35:24 +01:00
|
|
|
ensureQueryIds() {
|
2021-04-21 08:38:00 +01:00
|
|
|
if (this.targets && isArray(this.targets)) {
|
2019-01-21 20:35:24 +01:00
|
|
|
for (const query of this.targets) {
|
|
|
|
|
if (!query.refId) {
|
2019-03-18 11:17:58 +01:00
|
|
|
query.refId = getNextRefIdChar(this.targets);
|
2019-01-21 20:35:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-10 14:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-21 13:19:19 +02:00
|
|
|
getOptions() {
|
|
|
|
|
return this.options;
|
2018-11-05 17:46:09 +01:00
|
|
|
}
|
2020-08-31 17:53:21 +02:00
|
|
|
|
2021-04-19 10:24:39 -07:00
|
|
|
get hasChanged(): boolean {
|
|
|
|
|
return this.configRev > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 17:46:09 +01:00
|
|
|
updateOptions(options: object) {
|
2019-02-18 11:41:14 +01:00
|
|
|
this.options = options;
|
2021-04-19 10:24:39 -07:00
|
|
|
this.configRev++;
|
2020-11-26 15:14:57 +01:00
|
|
|
this.events.publish(new PanelOptionsChangedEvent());
|
2020-03-19 11:50:31 +01:00
|
|
|
this.render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateFieldConfig(config: FieldConfigSource) {
|
|
|
|
|
this.fieldConfig = config;
|
2021-04-19 10:24:39 -07:00
|
|
|
this.configRev++;
|
2020-12-17 13:30:55 +01:00
|
|
|
this.events.publish(new PanelOptionsChangedEvent());
|
2020-03-19 11:50:31 +01:00
|
|
|
|
2020-03-16 14:26:03 +01:00
|
|
|
this.resendLastResult();
|
2018-11-05 17:46:09 +01:00
|
|
|
this.render();
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 14:20:53 +02:00
|
|
|
getSaveModel() {
|
2017-10-10 17:57:53 +02:00
|
|
|
const model: any = {};
|
2020-05-11 22:03:43 +02:00
|
|
|
|
2018-08-29 14:26:50 +02:00
|
|
|
for (const property in this) {
|
2017-10-10 14:20:53 +02:00
|
|
|
if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 08:38:00 +01:00
|
|
|
if (isEqual(this[property], defaults[property])) {
|
2018-10-14 21:14:11 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 08:38:00 +01:00
|
|
|
model[property] = cloneDeep(this[property]);
|
2017-10-10 14:20:53 +02:00
|
|
|
}
|
2020-05-11 22:03:43 +02:00
|
|
|
|
2017-10-10 17:57:53 +02:00
|
|
|
return model;
|
2017-10-10 14:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-10 16:37:26 +02:00
|
|
|
setIsViewing(isViewing: boolean) {
|
|
|
|
|
this.isViewing = isViewing;
|
2017-10-11 11:42:49 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-27 12:30:59 +01:00
|
|
|
updateGridPos(newPos: GridPos, manuallyUpdated = true) {
|
2022-06-17 08:58:53 +02:00
|
|
|
if (
|
|
|
|
|
newPos.x === this.gridPos.x &&
|
|
|
|
|
newPos.y === this.gridPos.y &&
|
|
|
|
|
newPos.h === this.gridPos.h &&
|
|
|
|
|
newPos.w === this.gridPos.w
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 14:20:53 +02:00
|
|
|
this.gridPos.x = newPos.x;
|
|
|
|
|
this.gridPos.y = newPos.y;
|
|
|
|
|
this.gridPos.w = newPos.w;
|
|
|
|
|
this.gridPos.h = newPos.h;
|
2022-06-27 12:30:59 +01:00
|
|
|
if (manuallyUpdated) {
|
|
|
|
|
this.configRev++;
|
|
|
|
|
}
|
2017-10-11 21:36:03 +02:00
|
|
|
}
|
2017-10-16 16:09:23 +02:00
|
|
|
|
2022-08-10 10:36:19 +02:00
|
|
|
runAllPanelQueries({
|
|
|
|
|
dashboardId,
|
|
|
|
|
dashboardUID,
|
|
|
|
|
dashboardTimezone,
|
|
|
|
|
timeData,
|
|
|
|
|
width,
|
|
|
|
|
publicDashboardAccessToken,
|
|
|
|
|
}: RunPanelQueryOptions) {
|
2021-06-01 13:52:08 +02:00
|
|
|
this.getQueryRunner().run({
|
|
|
|
|
datasource: this.datasource,
|
|
|
|
|
queries: this.targets,
|
2021-10-13 08:53:36 +02:00
|
|
|
panelId: this.id,
|
2021-06-01 13:52:08 +02:00
|
|
|
dashboardId: dashboardId,
|
2022-08-10 10:36:19 +02:00
|
|
|
dashboardUID: dashboardUID,
|
2022-06-22 13:58:52 -08:00
|
|
|
publicDashboardAccessToken,
|
2021-06-01 13:52:08 +02:00
|
|
|
timezone: dashboardTimezone,
|
|
|
|
|
timeRange: timeData.timeRange,
|
|
|
|
|
timeInfo: timeData.timeInfo,
|
2021-11-29 11:49:53 +01:00
|
|
|
maxDataPoints: this.maxDataPoints || Math.floor(width),
|
2021-06-01 13:52:08 +02:00
|
|
|
minInterval: this.interval,
|
|
|
|
|
scopedVars: this.scopedVars,
|
|
|
|
|
cacheTimeout: this.cacheTimeout,
|
|
|
|
|
transformations: this.transformations,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-25 08:49:39 -07:00
|
|
|
refresh() {
|
|
|
|
|
this.hasRefreshed = true;
|
2020-12-23 10:45:31 +01:00
|
|
|
this.events.publish(new RefreshEvent());
|
2018-08-25 08:49:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
if (!this.hasRefreshed) {
|
|
|
|
|
this.refresh();
|
|
|
|
|
} else {
|
2020-12-23 10:45:31 +01:00
|
|
|
this.events.publish(new RenderEvent());
|
2018-08-25 08:49:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 07:33:21 +01:00
|
|
|
private getOptionsToRemember() {
|
2018-12-04 14:19:55 +01:00
|
|
|
return Object.keys(this).reduce((acc, property) => {
|
2018-12-05 07:33:21 +01:00
|
|
|
if (notPersistedProperties[property] || mustKeepProps[property]) {
|
2018-12-04 14:19:55 +01:00
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...acc,
|
2019-03-19 18:24:09 +01:00
|
|
|
[property]: (this as any)[property],
|
2018-12-04 14:19:55 +01:00
|
|
|
};
|
|
|
|
|
}, {});
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 07:33:21 +01:00
|
|
|
private restorePanelOptions(pluginId: string) {
|
2021-01-20 16:06:29 +01:00
|
|
|
const prevOptions = this.cachedPluginOptions[pluginId];
|
2018-12-04 14:19:55 +01:00
|
|
|
|
2021-01-20 16:06:29 +01:00
|
|
|
if (!prevOptions) {
|
2019-05-21 13:19:19 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2020-08-31 17:53:21 +02:00
|
|
|
|
2021-01-20 16:06:29 +01:00
|
|
|
Object.keys(prevOptions.properties).map((property) => {
|
|
|
|
|
(this as any)[property] = prevOptions.properties[property];
|
2019-11-19 13:59:39 +00:00
|
|
|
});
|
2020-03-19 11:50:31 +01:00
|
|
|
|
2021-01-20 16:06:29 +01:00
|
|
|
this.fieldConfig = restoreCustomOverrideRules(this.fieldConfig, prevOptions.fieldConfig);
|
2020-11-09 13:11:09 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-13 11:35:39 +01:00
|
|
|
applyPluginOptionDefaults(plugin: PanelPlugin, isAfterPluginChange: boolean) {
|
2021-01-20 16:06:29 +01:00
|
|
|
const options = getPanelOptionsWithDefaults({
|
|
|
|
|
plugin,
|
|
|
|
|
currentOptions: this.options,
|
|
|
|
|
currentFieldConfig: this.fieldConfig,
|
2021-02-13 11:35:39 +01:00
|
|
|
isAfterPluginChange: isAfterPluginChange,
|
2021-01-20 16:06:29 +01:00
|
|
|
});
|
2020-11-09 13:11:09 +01:00
|
|
|
|
2021-01-20 16:06:29 +01:00
|
|
|
this.fieldConfig = options.fieldConfig;
|
|
|
|
|
this.options = options.options;
|
2019-05-21 13:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-30 22:36:46 -07:00
|
|
|
pluginLoaded(plugin: PanelPlugin) {
|
2019-03-24 15:56:32 +01:00
|
|
|
this.plugin = plugin;
|
2021-04-12 12:47:17 +02:00
|
|
|
const version = getPluginVersion(plugin);
|
2019-03-24 15:56:32 +01:00
|
|
|
|
2022-06-10 17:12:56 -07:00
|
|
|
if (this.autoMigrateFrom) {
|
|
|
|
|
const wasAngular = this.autoMigrateFrom === 'graph';
|
|
|
|
|
this.callPanelTypeChangeHandler(
|
|
|
|
|
plugin,
|
|
|
|
|
this.autoMigrateFrom,
|
|
|
|
|
this.getOptionsToRemember(), // old options
|
|
|
|
|
wasAngular
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
delete this.autoMigrateFrom;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-01 00:39:06 -07:00
|
|
|
if (plugin.onPanelMigration) {
|
2019-03-25 06:10:23 -07:00
|
|
|
if (version !== this.pluginVersion) {
|
2019-04-30 22:36:46 -07:00
|
|
|
this.options = plugin.onPanelMigration(this);
|
2019-03-25 06:10:23 -07:00
|
|
|
this.pluginVersion = version;
|
|
|
|
|
}
|
2019-03-24 15:56:32 +01:00
|
|
|
}
|
2019-08-06 08:26:11 +02:00
|
|
|
|
2021-02-13 11:35:39 +01:00
|
|
|
this.applyPluginOptionDefaults(plugin, false);
|
2020-03-16 14:26:03 +01:00
|
|
|
this.resendLastResult();
|
2019-03-24 15:56:32 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-20 16:06:29 +01:00
|
|
|
clearPropertiesBeforePluginChange() {
|
2019-02-18 11:41:14 +01:00
|
|
|
// remove panel type specific options
|
2021-04-21 08:38:00 +01:00
|
|
|
for (const key of keys(this)) {
|
2019-02-18 11:41:14 +01:00
|
|
|
if (mustKeepProps[key]) {
|
|
|
|
|
continue;
|
2018-11-16 10:00:13 +01:00
|
|
|
}
|
2019-03-19 18:24:09 +01:00
|
|
|
delete (this as any)[key];
|
2018-11-16 10:00:13 +01:00
|
|
|
}
|
2018-12-04 14:19:55 +01:00
|
|
|
|
2021-01-20 16:06:29 +01:00
|
|
|
this.options = {};
|
|
|
|
|
|
|
|
|
|
// clear custom options
|
|
|
|
|
this.fieldConfig = {
|
|
|
|
|
defaults: {
|
|
|
|
|
...this.fieldConfig.defaults,
|
|
|
|
|
custom: {},
|
|
|
|
|
},
|
|
|
|
|
// filter out custom overrides
|
|
|
|
|
overrides: filterFieldConfigOverrides(this.fieldConfig.overrides, isStandardFieldProp),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-10 17:12:56 -07:00
|
|
|
// Let panel plugins inspect options from previous panel and keep any that it can use
|
|
|
|
|
private callPanelTypeChangeHandler(
|
|
|
|
|
newPlugin: PanelPlugin,
|
|
|
|
|
oldPluginId: string,
|
|
|
|
|
oldOptions: any,
|
|
|
|
|
wasAngular: boolean
|
|
|
|
|
) {
|
|
|
|
|
if (newPlugin.onPanelTypeChanged) {
|
|
|
|
|
const prevOptions = wasAngular ? { angular: oldOptions } : oldOptions.options;
|
|
|
|
|
Object.assign(this.options, newPlugin.onPanelTypeChanged(this, oldPluginId, prevOptions, this.fieldConfig));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-20 16:06:29 +01:00
|
|
|
changePlugin(newPlugin: PanelPlugin) {
|
|
|
|
|
const pluginId = newPlugin.meta.id;
|
|
|
|
|
const oldOptions: any = this.getOptionsToRemember();
|
2021-01-20 18:28:27 +01:00
|
|
|
const prevFieldConfig = this.fieldConfig;
|
2021-01-20 16:06:29 +01:00
|
|
|
const oldPluginId = this.type;
|
2021-04-27 10:09:37 +02:00
|
|
|
const wasAngular = this.isAngularPlugin();
|
2021-01-20 16:06:29 +01:00
|
|
|
this.cachedPluginOptions[oldPluginId] = {
|
|
|
|
|
properties: oldOptions,
|
2021-01-20 18:28:27 +01:00
|
|
|
fieldConfig: prevFieldConfig,
|
2021-01-20 16:06:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.clearPropertiesBeforePluginChange();
|
2018-12-04 14:19:55 +01:00
|
|
|
this.restorePanelOptions(pluginId);
|
2019-02-21 13:43:36 +01:00
|
|
|
|
2022-06-10 17:12:56 -07:00
|
|
|
// Potentially modify current options
|
|
|
|
|
this.callPanelTypeChangeHandler(newPlugin, oldPluginId, oldOptions, wasAngular);
|
2019-04-04 18:30:15 +02:00
|
|
|
|
2019-08-18 15:01:07 -07:00
|
|
|
// switch
|
|
|
|
|
this.type = pluginId;
|
|
|
|
|
this.plugin = newPlugin;
|
2021-04-19 10:24:39 -07:00
|
|
|
this.configRev++;
|
2020-03-16 14:26:03 +01:00
|
|
|
|
2021-02-13 11:35:39 +01:00
|
|
|
this.applyPluginOptionDefaults(newPlugin, true);
|
2019-08-18 15:01:07 -07:00
|
|
|
|
2019-04-30 22:36:46 -07:00
|
|
|
if (newPlugin.onPanelMigration) {
|
|
|
|
|
this.pluginVersion = getPluginVersion(newPlugin);
|
2019-02-21 13:43:36 +01:00
|
|
|
}
|
2018-07-09 18:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-03 14:16:54 +00:00
|
|
|
updateQueries(options: QueryGroupOptions) {
|
2021-10-29 10:57:24 -07:00
|
|
|
const { dataSource } = options;
|
2022-03-08 08:56:12 +01:00
|
|
|
this.datasource = {
|
|
|
|
|
uid: dataSource.uid,
|
|
|
|
|
type: dataSource.type,
|
|
|
|
|
};
|
2021-11-24 14:24:33 +01:00
|
|
|
this.cacheTimeout = options.cacheTimeout;
|
2021-03-03 14:16:54 +00:00
|
|
|
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-19 10:24:39 -07:00
|
|
|
this.configRev++;
|
2021-03-03 14:16:54 +00:00
|
|
|
|
2020-11-26 15:14:57 +01:00
|
|
|
this.events.publish(new PanelQueriesChangedEvent());
|
2020-09-28 09:06:46 +02:00
|
|
|
}
|
|
|
|
|
|
2018-12-12 08:54:12 +01:00
|
|
|
addQuery(query?: Partial<DataQuery>) {
|
2018-12-11 13:36:44 +01:00
|
|
|
query = query || { refId: 'A' };
|
2019-03-18 11:17:58 +01:00
|
|
|
query.refId = getNextRefIdChar(this.targets);
|
2019-01-21 20:35:24 +01:00
|
|
|
this.targets.push(query as DataQuery);
|
2021-04-19 10:24:39 -07:00
|
|
|
this.configRev++;
|
2018-12-11 13:36:44 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-29 09:39:23 +01:00
|
|
|
changeQuery(query: DataQuery, index: number) {
|
|
|
|
|
// ensure refId is maintained
|
|
|
|
|
query.refId = this.targets[index].refId;
|
2021-04-19 10:24:39 -07:00
|
|
|
this.configRev++;
|
2019-01-29 09:39:23 +01:00
|
|
|
|
|
|
|
|
// update query in array
|
|
|
|
|
this.targets = this.targets.map((item, itemIndex) => {
|
|
|
|
|
if (itemIndex === index) {
|
|
|
|
|
return query;
|
|
|
|
|
}
|
|
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-07 14:59:04 +01:00
|
|
|
getEditClone() {
|
2020-02-13 16:06:45 +01:00
|
|
|
const sourceModel = this.getSaveModel();
|
|
|
|
|
|
|
|
|
|
const clone = new PanelModel(sourceModel);
|
2020-04-10 16:37:26 +02:00
|
|
|
clone.isEditing = true;
|
2022-08-24 08:35:44 +02:00
|
|
|
clone.plugin = this.plugin;
|
2021-10-13 08:53:36 +02:00
|
|
|
|
2020-02-11 14:57:16 +01:00
|
|
|
const sourceQueryRunner = this.getQueryRunner();
|
2020-02-08 13:23:16 +01:00
|
|
|
|
2020-05-06 16:06:21 +02:00
|
|
|
// Copy last query result
|
|
|
|
|
clone.getQueryRunner().useLastResultFrom(sourceQueryRunner);
|
2020-02-08 13:23:16 +01:00
|
|
|
|
2020-02-07 14:59:04 +01:00
|
|
|
return clone;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 14:26:03 +01:00
|
|
|
getTransformations() {
|
|
|
|
|
return this.transformations;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getFieldOverrideOptions() {
|
|
|
|
|
if (!this.plugin) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2020-04-06 16:24:41 +02:00
|
|
|
fieldConfig: this.fieldConfig,
|
2020-03-16 14:26:03 +01:00
|
|
|
replaceVariables: this.replaceVariables,
|
2020-04-06 16:24:41 +02:00
|
|
|
fieldConfigRegistry: this.plugin.fieldConfigRegistry,
|
2021-04-29 12:44:06 +02:00
|
|
|
theme: config.theme2,
|
2020-03-16 14:26:03 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 08:52:05 +02:00
|
|
|
getDataSupport(): PanelPluginDataSupport {
|
|
|
|
|
return this.plugin?.dataSupport ?? { annotations: false, alertStates: false };
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-18 14:10:18 -07:00
|
|
|
getQueryRunner(): PanelQueryRunner {
|
|
|
|
|
if (!this.queryRunner) {
|
2020-03-16 14:26:03 +01:00
|
|
|
this.queryRunner = new PanelQueryRunner(this);
|
2019-04-18 14:10:18 -07:00
|
|
|
}
|
|
|
|
|
return this.queryRunner;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 15:26:09 +02:00
|
|
|
hasTitle() {
|
2019-05-08 17:21:46 +02:00
|
|
|
return this.title && this.title.length > 0;
|
2019-05-06 15:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-12 17:28:46 +02:00
|
|
|
isAngularPlugin(): boolean {
|
2020-07-09 15:16:35 +02:00
|
|
|
return (this.plugin && this.plugin.angularPanelCtrl) !== undefined;
|
2019-09-12 17:28:46 +02:00
|
|
|
}
|
|
|
|
|
|
2017-10-16 16:09:23 +02:00
|
|
|
destroy() {
|
|
|
|
|
this.events.removeAllListeners();
|
2019-04-18 21:56:27 -07:00
|
|
|
|
|
|
|
|
if (this.queryRunner) {
|
|
|
|
|
this.queryRunner.destroy();
|
|
|
|
|
}
|
2017-10-16 16:09:23 +02:00
|
|
|
}
|
2019-09-08 23:58:57 -07:00
|
|
|
|
|
|
|
|
setTransformations(transformations: DataTransformerConfig[]) {
|
|
|
|
|
this.transformations = transformations;
|
2020-04-07 19:54:06 +02:00
|
|
|
this.resendLastResult();
|
2021-04-19 10:24:39 -07:00
|
|
|
this.configRev++;
|
2020-11-26 15:14:57 +01:00
|
|
|
this.events.publish(new PanelTransformationsChangedEvent());
|
2020-03-16 14:26:03 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-03 14:16:54 +00:00
|
|
|
setProperty(key: keyof this, value: any) {
|
|
|
|
|
this[key] = value;
|
2021-04-19 10:24:39 -07:00
|
|
|
this.configRev++;
|
2021-03-25 08:33:13 +01: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 14:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-15 13:29:37 +01:00
|
|
|
replaceVariables(value: string, extraVars: ScopedVars | undefined, format?: string | Function) {
|
2021-12-16 12:05:00 +01:00
|
|
|
const lastRequest = this.getQueryRunner().getLastRequest();
|
|
|
|
|
const vars: ScopedVars = Object.assign({}, this.scopedVars, lastRequest?.scopedVars, extraVars);
|
2021-04-01 06:33:11 +02:00
|
|
|
|
|
|
|
|
const allVariablesParams = getVariablesUrlParams(vars);
|
2020-12-15 13:29:37 +01:00
|
|
|
const variablesQuery = urlUtil.toUrlParams(allVariablesParams);
|
|
|
|
|
const timeRangeUrl = urlUtil.toUrlParams(getTimeSrv().timeRangeForUrl());
|
|
|
|
|
|
2021-12-16 12:05:00 +01:00
|
|
|
vars[DataLinkBuiltInVars.keepTime] = {
|
|
|
|
|
text: timeRangeUrl,
|
|
|
|
|
value: timeRangeUrl,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
vars[DataLinkBuiltInVars.includeVars] = {
|
|
|
|
|
text: variablesQuery,
|
|
|
|
|
value: variablesQuery,
|
2020-12-15 13:29:37 +01:00
|
|
|
};
|
|
|
|
|
|
2020-10-01 18:51:23 +01:00
|
|
|
return getTemplateSrv().replace(value, vars, format);
|
2020-03-16 14:26:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resendLastResult() {
|
|
|
|
|
if (!this.plugin) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.getQueryRunner().resendLastResult();
|
2019-09-08 23:58:57 -07:00
|
|
|
}
|
2020-04-10 16:37:26 +02:00
|
|
|
|
2021-03-15 08:44:13 +01: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 {
|
2021-12-16 12:05:00 +01:00
|
|
|
return this.replaceVariables(this.title, undefined, 'text');
|
2021-03-15 08:44:13 +01:00
|
|
|
}
|
2017-10-10 09:34:14 +02:00
|
|
|
}
|
2019-04-30 22:36:46 -07:00
|
|
|
|
|
|
|
|
function getPluginVersion(plugin: PanelPlugin): string {
|
|
|
|
|
return plugin && plugin.meta.info.version ? plugin.meta.info.version : config.buildInfo.version;
|
|
|
|
|
}
|
2021-01-20 16:06:29 +01:00
|
|
|
|
|
|
|
|
interface PanelOptionsCache {
|
|
|
|
|
properties: any;
|
|
|
|
|
fieldConfig: FieldConfigSource;
|
|
|
|
|
}
|
2022-07-22 16:10:10 +01:00
|
|
|
|
|
|
|
|
// For cases where we immediately want to stringify the panel model without cloning each property
|
|
|
|
|
export function stringifyPanelModel(panel: PanelModel) {
|
|
|
|
|
const model: any = {};
|
|
|
|
|
|
|
|
|
|
Object.entries(panel)
|
|
|
|
|
.filter(
|
|
|
|
|
([prop, val]) => !notPersistedProperties[prop] && panel.hasOwnProperty(prop) && !isEqual(val, defaults[prop])
|
|
|
|
|
)
|
|
|
|
|
.forEach(([k, v]) => {
|
|
|
|
|
model[k] = v;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return safeStringifyValue(model);
|
|
|
|
|
}
|