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
|
2019-01-17 10:59:47 -06:00
|
|
|
import { Emitter } from 'app/core/utils/emitter';
|
2019-03-18 05:17:58 -05:00
|
|
|
import { getNextRefIdChar } from 'app/core/utils/query';
|
2020-03-16 08:26:03 -05:00
|
|
|
import templateSrv from 'app/features/templating/template_srv';
|
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,
|
|
|
|
DataQueryResponseData,
|
|
|
|
DataTransformerConfig,
|
2020-03-10 02:53:41 -05:00
|
|
|
eventFactory,
|
|
|
|
PanelEvents,
|
|
|
|
PanelPlugin,
|
2019-10-31 04:48:05 -05:00
|
|
|
ScopedVars,
|
2020-03-19 05:50:31 -05:00
|
|
|
FieldConfigSource,
|
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-07-06 11:18:23 -05:00
|
|
|
|
2019-03-25 08:10:23 -05:00
|
|
|
import config from 'app/core/config';
|
2017-10-10 02:34:14 -05:00
|
|
|
|
2019-04-18 01:22:14 -05:00
|
|
|
import { PanelQueryRunner } from './PanelQueryRunner';
|
2020-02-08 06:23:16 -06:00
|
|
|
import { take } from 'rxjs/operators';
|
2019-10-14 03:27:47 -05:00
|
|
|
|
|
|
|
export const panelAdded = eventFactory<PanelModel | undefined>('panel-added');
|
|
|
|
export const panelRemoved = eventFactory<PanelModel | undefined>('panel-removed');
|
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,
|
|
|
|
fullscreen: 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,
|
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: {},
|
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 */
|
2017-10-10 07:20:53 -05:00
|
|
|
id: number;
|
2020-03-27 00:40:14 -05:00
|
|
|
editSourceId: number;
|
2017-12-19 09:06:54 -06:00
|
|
|
gridPos: GridPos;
|
2017-10-10 02:34:14 -05:00
|
|
|
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;
|
2017-10-17 05:04:18 -05:00
|
|
|
panels?: any;
|
2017-11-28 02:38:28 -06:00
|
|
|
soloMode?: boolean;
|
2019-01-21 13:35:24 -06:00
|
|
|
targets: DataQuery[];
|
2019-09-09 01:58:57 -05:00
|
|
|
transformations?: DataTransformerConfig[];
|
2018-06-26 09:32:01 -05:00
|
|
|
datasource: string;
|
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
|
|
|
|
2019-03-28 14:59:59 -05:00
|
|
|
snapshotData?: DataQueryResponseData[];
|
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
|
|
|
|
2018-11-12 03:35:46 -06:00
|
|
|
maxDataPoints?: number;
|
|
|
|
interval?: string;
|
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
|
|
|
|
2017-10-11 04:42:49 -05:00
|
|
|
// non persisted
|
|
|
|
fullscreen: boolean;
|
|
|
|
isEditing: boolean;
|
2019-05-03 14:55:22 -05:00
|
|
|
isInView: boolean;
|
2018-08-25 10:49:39 -05:00
|
|
|
hasRefreshed: boolean;
|
2017-10-10 07:20:53 -05:00
|
|
|
events: Emitter;
|
2018-11-20 09:33:26 -06:00
|
|
|
cacheTimeout?: any;
|
2018-12-04 07:19:55 -06:00
|
|
|
cachedPluginOptions?: any;
|
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) {
|
2017-10-10 10:57:53 -05:00
|
|
|
this.events = new Emitter();
|
2019-09-24 05:15:35 -05:00
|
|
|
// should not be 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
|
|
|
|
this.datasource = null;
|
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) {
|
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-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-03-19 05:50:31 -05:00
|
|
|
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFieldConfig(config: FieldConfigSource) {
|
|
|
|
this.fieldConfig = config;
|
|
|
|
|
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 = {};
|
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
|
|
|
}
|
2017-10-10 10:57:53 -05:00
|
|
|
return model;
|
2017-10-10 07:20:53 -05:00
|
|
|
}
|
|
|
|
|
2017-10-11 04:42:49 -05:00
|
|
|
setViewMode(fullscreen: boolean, isEditing: boolean) {
|
|
|
|
this.fullscreen = fullscreen;
|
|
|
|
this.isEditing = isEditing;
|
2019-10-14 03:27:47 -05:00
|
|
|
this.events.emit(PanelEvents.viewModeChanged);
|
2017-10-11 04:42:49 -05:00
|
|
|
}
|
|
|
|
|
2017-10-10 07:20:53 -05:00
|
|
|
updateGridPos(newPos: GridPos) {
|
|
|
|
let sizeChanged = false;
|
|
|
|
|
|
|
|
if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
|
|
|
|
sizeChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.gridPos.x = newPos.x;
|
|
|
|
this.gridPos.y = newPos.y;
|
|
|
|
this.gridPos.w = newPos.w;
|
|
|
|
this.gridPos.h = newPos.h;
|
|
|
|
|
|
|
|
if (sizeChanged) {
|
2019-10-14 03:27:47 -05:00
|
|
|
this.events.emit(PanelEvents.panelSizeChanged);
|
2017-10-10 07:20:53 -05:00
|
|
|
}
|
|
|
|
}
|
2017-10-11 14:36:03 -05:00
|
|
|
|
|
|
|
resizeDone() {
|
2019-10-14 03:27:47 -05:00
|
|
|
this.events.emit(PanelEvents.panelSizeChanged);
|
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;
|
2019-10-14 03:27:47 -05:00
|
|
|
this.events.emit(PanelEvents.refresh);
|
2018-08-25 10:49:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (!this.hasRefreshed) {
|
|
|
|
this.refresh();
|
|
|
|
} else {
|
2019-10-14 03:27:47 -05:00
|
|
|
this.events.emit(PanelEvents.render);
|
2018-08-25 10:49:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 02:23:47 -06:00
|
|
|
initialized() {
|
2019-10-14 03:27:47 -05:00
|
|
|
this.events.emit(PanelEvents.panelInitialized);
|
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) {
|
2018-12-04 07:39:56 -06:00
|
|
|
const prevOptions = this.cachedPluginOptions[pluginId] || {};
|
2018-12-04 07:19:55 -06:00
|
|
|
|
2018-12-05 00:33:21 -06:00
|
|
|
Object.keys(prevOptions).map(property => {
|
2019-03-19 12:24:09 -05:00
|
|
|
(this as any)[property] = prevOptions[property];
|
2018-12-04 07:19:55 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-21 06:19:19 -05:00
|
|
|
private applyPluginOptionDefaults(plugin: PanelPlugin) {
|
|
|
|
if (plugin.angularConfigCtrl) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-19 07:59:39 -06:00
|
|
|
this.options = _.mergeWith({}, plugin.defaults, this.options || {}, (objValue: any, srcValue: any): any => {
|
|
|
|
if (_.isArray(srcValue)) {
|
|
|
|
return srcValue;
|
2019-11-08 02:46:53 -06:00
|
|
|
}
|
2019-11-19 07:59:39 -06:00
|
|
|
});
|
2020-03-19 05:50:31 -05:00
|
|
|
|
|
|
|
this.fieldConfig = {
|
|
|
|
defaults: _.mergeWith(
|
|
|
|
{},
|
|
|
|
plugin.fieldConfigDefaults.defaults,
|
|
|
|
this.fieldConfig ? this.fieldConfig.defaults : {},
|
|
|
|
(objValue: any, srcValue: any): any => {
|
|
|
|
if (_.isArray(srcValue)) {
|
|
|
|
return srcValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
),
|
|
|
|
overrides: [
|
|
|
|
...plugin.fieldConfigDefaults.overrides,
|
|
|
|
...(this.fieldConfig && this.fieldConfig.overrides ? this.fieldConfig.overrides : []),
|
|
|
|
],
|
|
|
|
};
|
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;
|
|
|
|
|
2019-05-01 00:36:46 -05:00
|
|
|
if (plugin.panel && plugin.onPanelMigration) {
|
|
|
|
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
|
|
|
|
|
|
|
this.applyPluginOptionDefaults(plugin);
|
2020-03-16 08:26:03 -05:00
|
|
|
this.resendLastResult();
|
2019-03-24 09:56:32 -05:00
|
|
|
}
|
|
|
|
|
2019-05-01 00:36:46 -05:00
|
|
|
changePlugin(newPlugin: PanelPlugin) {
|
|
|
|
const pluginId = newPlugin.meta.id;
|
2019-02-21 06:43:36 -06:00
|
|
|
const oldOptions: any = this.getOptionsToRemember();
|
|
|
|
const oldPluginId = this.type;
|
2019-08-18 17:01:07 -05:00
|
|
|
const wasAngular = !!this.plugin.angularPanelCtrl;
|
2018-08-25 14:22:50 -05:00
|
|
|
|
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-02-18 04:41:14 -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
|
|
|
|
2019-02-21 06:43:36 -06:00
|
|
|
this.cachedPluginOptions[oldPluginId] = oldOptions;
|
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) {
|
2019-08-18 17:01:07 -05:00
|
|
|
let old: any = {};
|
|
|
|
|
|
|
|
if (wasAngular) {
|
|
|
|
old = { angular: oldOptions };
|
|
|
|
} else if (oldOptions && oldOptions.options) {
|
|
|
|
old = oldOptions.options;
|
|
|
|
}
|
2019-05-01 00:36:46 -05:00
|
|
|
this.options = this.options || {};
|
2020-03-13 02:27:16 -05:00
|
|
|
Object.assign(this.options, newPlugin.onPanelTypeChanged(this, oldPluginId, old));
|
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);
|
2019-08-18 17:01:07 -05:00
|
|
|
this.applyPluginOptionDefaults(newPlugin);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-02-11 07:57:16 -06:00
|
|
|
const sourceQueryRunner = this.getQueryRunner();
|
2020-02-08 06:23:16 -06:00
|
|
|
|
2020-02-11 07:57:16 -06:00
|
|
|
// pipe last result to new clone query runner
|
|
|
|
sourceQueryRunner
|
2020-02-08 06:23:16 -06:00
|
|
|
.getData()
|
|
|
|
.pipe(take(1))
|
2020-02-11 07:57:16 -06:00
|
|
|
.subscribe(val => clone.getQueryRunner().pipeDataToSubject(val));
|
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 {
|
|
|
|
return this.plugin && !!this.plugin.angularPanelCtrl;
|
|
|
|
}
|
|
|
|
|
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();
|
2019-05-06 12:35:10 -05:00
|
|
|
this.queryRunner = null;
|
2019-04-18 23:56:27 -05:00
|
|
|
}
|
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-03-16 08:26:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
replaceVariables(value: string, extraVars?: ScopedVars, format?: string) {
|
|
|
|
let vars = this.scopedVars;
|
|
|
|
if (extraVars) {
|
|
|
|
vars = vars ? { ...vars, ...extraVars } : extraVars;
|
|
|
|
}
|
|
|
|
return templateSrv.replace(value, vars, format);
|
|
|
|
}
|
|
|
|
|
|
|
|
resendLastResult() {
|
|
|
|
if (!this.plugin) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getQueryRunner().resendLastResult();
|
2019-09-09 01:58:57 -05:00
|
|
|
}
|
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;
|
|
|
|
}
|