PanelModel.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Libraries
  2. import _ from 'lodash';
  3. // Utils
  4. import { Emitter } from 'app/core/utils/emitter';
  5. import { getNextRefIdChar } from 'app/core/utils/query';
  6. // Types
  7. import { DataQuery, TimeSeries, Threshold, ScopedVars, TableData } from '@grafana/ui';
  8. import { PanelPlugin } from 'app/types';
  9. import config from 'app/core/config';
  10. export interface GridPos {
  11. x: number;
  12. y: number;
  13. w: number;
  14. h: number;
  15. static?: boolean;
  16. }
  17. const notPersistedProperties: { [str: string]: boolean } = {
  18. events: true,
  19. fullscreen: true,
  20. isEditing: true,
  21. hasRefreshed: true,
  22. cachedPluginOptions: true,
  23. plugin: true,
  24. };
  25. // For angular panels we need to clean up properties when changing type
  26. // To make sure the change happens without strange bugs happening when panels use same
  27. // named property with different type / value expectations
  28. // This is not required for react panels
  29. const mustKeepProps: { [str: string]: boolean } = {
  30. id: true,
  31. gridPos: true,
  32. type: true,
  33. title: true,
  34. scopedVars: true,
  35. repeat: true,
  36. repeatIteration: true,
  37. repeatPanelId: true,
  38. repeatDirection: true,
  39. repeatedByRow: true,
  40. minSpan: true,
  41. collapsed: true,
  42. panels: true,
  43. targets: true,
  44. datasource: true,
  45. timeFrom: true,
  46. timeShift: true,
  47. hideTimeOverride: true,
  48. description: true,
  49. links: true,
  50. fullscreen: true,
  51. isEditing: true,
  52. hasRefreshed: true,
  53. events: true,
  54. cacheTimeout: true,
  55. cachedPluginOptions: true,
  56. transparent: true,
  57. pluginVersion: true,
  58. };
  59. const defaults: any = {
  60. gridPos: { x: 0, y: 0, h: 3, w: 6 },
  61. datasource: null,
  62. targets: [{ refId: 'A' }],
  63. cachedPluginOptions: {},
  64. transparent: false,
  65. };
  66. export class PanelModel {
  67. id: number;
  68. gridPos: GridPos;
  69. type: string;
  70. title: string;
  71. alert?: any;
  72. scopedVars?: ScopedVars;
  73. repeat?: string;
  74. repeatIteration?: number;
  75. repeatPanelId?: number;
  76. repeatDirection?: string;
  77. repeatedByRow?: boolean;
  78. maxPerRow?: number;
  79. collapsed?: boolean;
  80. panels?: any;
  81. soloMode?: boolean;
  82. targets: DataQuery[];
  83. datasource: string;
  84. thresholds?: any;
  85. pluginVersion?: string;
  86. snapshotData?: TimeSeries[] | [TableData];
  87. timeFrom?: any;
  88. timeShift?: any;
  89. hideTimeOverride?: any;
  90. options: {
  91. [key: string]: any;
  92. };
  93. maxDataPoints?: number;
  94. interval?: string;
  95. description?: string;
  96. links?: [];
  97. transparent: boolean;
  98. // non persisted
  99. fullscreen: boolean;
  100. isEditing: boolean;
  101. hasRefreshed: boolean;
  102. events: Emitter;
  103. cacheTimeout?: any;
  104. cachedPluginOptions?: any;
  105. legend?: { show: boolean };
  106. plugin?: PanelPlugin;
  107. constructor(model: any) {
  108. this.events = new Emitter();
  109. // copy properties from persisted model
  110. for (const property in model) {
  111. (this as any)[property] = model[property];
  112. }
  113. // defaults
  114. _.defaultsDeep(this, _.cloneDeep(defaults));
  115. // queries must have refId
  116. this.ensureQueryIds();
  117. this.restoreInfintyForThresholds();
  118. }
  119. ensureQueryIds() {
  120. if (this.targets && _.isArray(this.targets)) {
  121. for (const query of this.targets) {
  122. if (!query.refId) {
  123. query.refId = getNextRefIdChar(this.targets);
  124. }
  125. }
  126. }
  127. }
  128. restoreInfintyForThresholds() {
  129. if (this.options && this.options.thresholds) {
  130. this.options.thresholds = this.options.thresholds.map((threshold: Threshold) => {
  131. // JSON serialization of -Infinity is 'null' so lets convert it back to -Infinity
  132. if (threshold.index === 0 && threshold.value === null) {
  133. return { ...threshold, value: -Infinity };
  134. }
  135. return threshold;
  136. });
  137. }
  138. }
  139. getOptions(panelDefaults: any) {
  140. return _.defaultsDeep(this.options || {}, panelDefaults);
  141. }
  142. updateOptions(options: object) {
  143. this.options = options;
  144. this.render();
  145. }
  146. getSaveModel() {
  147. const model: any = {};
  148. for (const property in this) {
  149. if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
  150. continue;
  151. }
  152. if (_.isEqual(this[property], defaults[property])) {
  153. continue;
  154. }
  155. model[property] = _.cloneDeep(this[property]);
  156. }
  157. return model;
  158. }
  159. setViewMode(fullscreen: boolean, isEditing: boolean) {
  160. this.fullscreen = fullscreen;
  161. this.isEditing = isEditing;
  162. this.events.emit('view-mode-changed');
  163. }
  164. updateGridPos(newPos: GridPos) {
  165. let sizeChanged = false;
  166. if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
  167. sizeChanged = true;
  168. }
  169. this.gridPos.x = newPos.x;
  170. this.gridPos.y = newPos.y;
  171. this.gridPos.w = newPos.w;
  172. this.gridPos.h = newPos.h;
  173. if (sizeChanged) {
  174. this.events.emit('panel-size-changed');
  175. }
  176. }
  177. resizeDone() {
  178. this.events.emit('panel-size-changed');
  179. }
  180. refresh() {
  181. this.hasRefreshed = true;
  182. this.events.emit('refresh');
  183. }
  184. render() {
  185. if (!this.hasRefreshed) {
  186. this.refresh();
  187. } else {
  188. this.events.emit('render');
  189. }
  190. }
  191. initialized() {
  192. this.events.emit('panel-initialized');
  193. }
  194. private getOptionsToRemember() {
  195. return Object.keys(this).reduce((acc, property) => {
  196. if (notPersistedProperties[property] || mustKeepProps[property]) {
  197. return acc;
  198. }
  199. return {
  200. ...acc,
  201. [property]: (this as any)[property],
  202. };
  203. }, {});
  204. }
  205. private restorePanelOptions(pluginId: string) {
  206. const prevOptions = this.cachedPluginOptions[pluginId] || {};
  207. Object.keys(prevOptions).map(property => {
  208. (this as any)[property] = prevOptions[property];
  209. });
  210. }
  211. private getPluginVersion(plugin: PanelPlugin): string {
  212. return this.plugin && this.plugin.info.version ? this.plugin.info.version : config.buildInfo.version;
  213. }
  214. pluginLoaded(plugin: PanelPlugin) {
  215. this.plugin = plugin;
  216. const { reactPanel } = plugin.exports;
  217. // Call PanelMigration Handler if the version has changed
  218. if (reactPanel && reactPanel.onPanelMigration) {
  219. const version = this.getPluginVersion(plugin);
  220. if (version !== this.pluginVersion) {
  221. this.options = reactPanel.onPanelMigration(this);
  222. this.pluginVersion = version;
  223. }
  224. }
  225. }
  226. changePlugin(newPlugin: PanelPlugin) {
  227. const pluginId = newPlugin.id;
  228. const oldOptions: any = this.getOptionsToRemember();
  229. const oldPluginId = this.type;
  230. const reactPanel = newPlugin.exports.reactPanel;
  231. // for angular panels we must remove all events and let angular panels do some cleanup
  232. if (this.plugin.exports.PanelCtrl) {
  233. this.destroy();
  234. }
  235. // remove panel type specific options
  236. for (const key of _.keys(this)) {
  237. if (mustKeepProps[key]) {
  238. continue;
  239. }
  240. delete (this as any)[key];
  241. }
  242. this.cachedPluginOptions[oldPluginId] = oldOptions;
  243. this.restorePanelOptions(pluginId);
  244. // switch
  245. this.type = pluginId;
  246. this.plugin = newPlugin;
  247. // Let panel plugins inspect options from previous panel and keep any that it can use
  248. if (reactPanel) {
  249. if (reactPanel.onPanelTypeChanged) {
  250. this.options = this.options || {};
  251. const old = oldOptions && oldOptions.options ? oldOptions.options : {};
  252. Object.assign(this.options, reactPanel.onPanelTypeChanged(this.options, oldPluginId, old));
  253. }
  254. if (reactPanel.onPanelMigration) {
  255. this.pluginVersion = this.getPluginVersion(newPlugin);
  256. }
  257. }
  258. }
  259. addQuery(query?: Partial<DataQuery>) {
  260. query = query || { refId: 'A' };
  261. query.refId = getNextRefIdChar(this.targets);
  262. this.targets.push(query as DataQuery);
  263. }
  264. changeQuery(query: DataQuery, index: number) {
  265. // ensure refId is maintained
  266. query.refId = this.targets[index].refId;
  267. // update query in array
  268. this.targets = this.targets.map((item, itemIndex) => {
  269. if (itemIndex === index) {
  270. return query;
  271. }
  272. return item;
  273. });
  274. }
  275. destroy() {
  276. this.events.emit('panel-teardown');
  277. this.events.removeAllListeners();
  278. }
  279. }