PanelModel.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 { PanelPluginMeta, DataQuery, Threshold, ScopedVars, DataQueryResponseData } 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. };
  60. const defaults: any = {
  61. gridPos: { x: 0, y: 0, h: 3, w: 6 },
  62. datasource: null,
  63. targets: [{ refId: 'A' }],
  64. cachedPluginOptions: {},
  65. transparent: false,
  66. };
  67. export class PanelModel {
  68. id: number;
  69. gridPos: GridPos;
  70. type: string;
  71. title: string;
  72. alert?: any;
  73. scopedVars?: ScopedVars;
  74. repeat?: string;
  75. repeatIteration?: number;
  76. repeatPanelId?: number;
  77. repeatDirection?: string;
  78. repeatedByRow?: boolean;
  79. maxPerRow?: number;
  80. collapsed?: boolean;
  81. panels?: any;
  82. soloMode?: boolean;
  83. targets: DataQuery[];
  84. datasource: string;
  85. thresholds?: any;
  86. pluginVersion?: string;
  87. snapshotData?: DataQueryResponseData[];
  88. timeFrom?: any;
  89. timeShift?: any;
  90. hideTimeOverride?: any;
  91. options: {
  92. [key: string]: any;
  93. };
  94. maxDataPoints?: number;
  95. interval?: string;
  96. description?: string;
  97. links?: [];
  98. transparent: boolean;
  99. // non persisted
  100. fullscreen: boolean;
  101. isEditing: boolean;
  102. hasRefreshed: boolean;
  103. events: Emitter;
  104. cacheTimeout?: any;
  105. cachedPluginOptions?: any;
  106. legend?: { show: boolean };
  107. plugin?: PanelPluginMeta;
  108. private queryRunner?: PanelQueryRunner;
  109. constructor(model: any) {
  110. this.events = new Emitter();
  111. // copy properties from persisted model
  112. for (const property in model) {
  113. (this as any)[property] = model[property];
  114. }
  115. // defaults
  116. _.defaultsDeep(this, _.cloneDeep(defaults));
  117. // queries must have refId
  118. this.ensureQueryIds();
  119. this.restoreInfintyForThresholds();
  120. }
  121. ensureQueryIds() {
  122. if (this.targets && _.isArray(this.targets)) {
  123. for (const query of this.targets) {
  124. if (!query.refId) {
  125. query.refId = getNextRefIdChar(this.targets);
  126. }
  127. }
  128. }
  129. }
  130. restoreInfintyForThresholds() {
  131. if (this.options && this.options.thresholds) {
  132. this.options.thresholds = this.options.thresholds.map((threshold: Threshold) => {
  133. // JSON serialization of -Infinity is 'null' so lets convert it back to -Infinity
  134. if (threshold.index === 0 && threshold.value === null) {
  135. return { ...threshold, value: -Infinity };
  136. }
  137. return threshold;
  138. });
  139. }
  140. }
  141. getOptions(panelDefaults: any) {
  142. return _.defaultsDeep(this.options || {}, panelDefaults);
  143. }
  144. updateOptions(options: object) {
  145. this.options = options;
  146. this.render();
  147. }
  148. getSaveModel() {
  149. const model: any = {};
  150. for (const property in this) {
  151. if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
  152. continue;
  153. }
  154. if (_.isEqual(this[property], defaults[property])) {
  155. continue;
  156. }
  157. model[property] = _.cloneDeep(this[property]);
  158. }
  159. return model;
  160. }
  161. setViewMode(fullscreen: boolean, isEditing: boolean) {
  162. this.fullscreen = fullscreen;
  163. this.isEditing = isEditing;
  164. this.events.emit('view-mode-changed');
  165. }
  166. updateGridPos(newPos: GridPos) {
  167. let sizeChanged = false;
  168. if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
  169. sizeChanged = true;
  170. }
  171. this.gridPos.x = newPos.x;
  172. this.gridPos.y = newPos.y;
  173. this.gridPos.w = newPos.w;
  174. this.gridPos.h = newPos.h;
  175. if (sizeChanged) {
  176. this.events.emit('panel-size-changed');
  177. }
  178. }
  179. resizeDone() {
  180. this.events.emit('panel-size-changed');
  181. }
  182. refresh() {
  183. this.hasRefreshed = true;
  184. this.events.emit('refresh');
  185. }
  186. render() {
  187. if (!this.hasRefreshed) {
  188. this.refresh();
  189. } else {
  190. this.events.emit('render');
  191. }
  192. }
  193. initialized() {
  194. this.events.emit('panel-initialized');
  195. }
  196. private getOptionsToRemember() {
  197. return Object.keys(this).reduce((acc, property) => {
  198. if (notPersistedProperties[property] || mustKeepProps[property]) {
  199. return acc;
  200. }
  201. return {
  202. ...acc,
  203. [property]: (this as any)[property],
  204. };
  205. }, {});
  206. }
  207. private restorePanelOptions(pluginId: string) {
  208. const prevOptions = this.cachedPluginOptions[pluginId] || {};
  209. Object.keys(prevOptions).map(property => {
  210. (this as any)[property] = prevOptions[property];
  211. });
  212. }
  213. private getPluginVersion(plugin: PanelPluginMeta): string {
  214. return this.plugin && this.plugin.info.version ? this.plugin.info.version : config.buildInfo.version;
  215. }
  216. pluginLoaded(plugin: PanelPluginMeta) {
  217. this.plugin = plugin;
  218. if (plugin.panelPlugin && plugin.panelPlugin.onPanelMigration) {
  219. const version = this.getPluginVersion(plugin);
  220. if (version !== this.pluginVersion) {
  221. this.options = plugin.panelPlugin.onPanelMigration(this);
  222. this.pluginVersion = version;
  223. }
  224. }
  225. }
  226. changePlugin(newPlugin: PanelPluginMeta) {
  227. const pluginId = newPlugin.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.angularPlugin) {
  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. const reactPanel = newPlugin.panelPlugin;
  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. getQueryRunner(): PanelQueryRunner {
  276. if (!this.queryRunner) {
  277. this.queryRunner = new PanelQueryRunner();
  278. }
  279. return this.queryRunner;
  280. }
  281. destroy() {
  282. this.events.emit('panel-teardown');
  283. this.events.removeAllListeners();
  284. if (this.queryRunner) {
  285. this.queryRunner.destroy();
  286. }
  287. }
  288. }