PanelModel.ts 7.9 KB

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