PanelModel.ts 7.9 KB

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