PanelModel.ts 7.4 KB

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