PanelModel.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 } from '@grafana/ui';
  8. import { PanelPluginMeta } from 'app/types';
  9. import config from 'app/core/config';
  10. import { PanelQueryRunner } from './PanelQueryRunner';
  11. export interface GridPos {
  12. x: number;
  13. y: number;
  14. w: number;
  15. h: number;
  16. static?: boolean;
  17. }
  18. const notPersistedProperties: { [str: string]: boolean } = {
  19. events: true,
  20. fullscreen: true,
  21. isEditing: 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. };
  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?: PanelPluginMeta;
  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. private getPluginVersion(plugin: PanelPluginMeta): string {
  215. return this.plugin && this.plugin.info.version ? this.plugin.info.version : config.buildInfo.version;
  216. }
  217. pluginLoaded(plugin: PanelPluginMeta) {
  218. this.plugin = plugin;
  219. if (plugin.vizPlugin && plugin.vizPlugin.onPanelMigration) {
  220. const version = this.getPluginVersion(plugin);
  221. if (version !== this.pluginVersion) {
  222. this.options = plugin.vizPlugin.onPanelMigration(this);
  223. this.pluginVersion = version;
  224. }
  225. }
  226. }
  227. changePlugin(newPlugin: PanelPluginMeta) {
  228. const pluginId = newPlugin.id;
  229. const oldOptions: any = this.getOptionsToRemember();
  230. const oldPluginId = this.type;
  231. // for angular panels we must remove all events and let angular panels do some cleanup
  232. if (this.plugin.angularPlugin) {
  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. const reactPanel = newPlugin.vizPlugin;
  249. if (reactPanel) {
  250. if (reactPanel.onPanelTypeChanged) {
  251. this.options = this.options || {};
  252. const old = oldOptions && oldOptions.options ? oldOptions.options : {};
  253. Object.assign(this.options, reactPanel.onPanelTypeChanged(this.options, oldPluginId, old));
  254. }
  255. if (reactPanel.onPanelMigration) {
  256. this.pluginVersion = this.getPluginVersion(newPlugin);
  257. }
  258. }
  259. }
  260. addQuery(query?: Partial<DataQuery>) {
  261. query = query || { refId: 'A' };
  262. query.refId = getNextRefIdChar(this.targets);
  263. this.targets.push(query as DataQuery);
  264. }
  265. changeQuery(query: DataQuery, index: number) {
  266. // ensure refId is maintained
  267. query.refId = this.targets[index].refId;
  268. // update query in array
  269. this.targets = this.targets.map((item, itemIndex) => {
  270. if (itemIndex === index) {
  271. return query;
  272. }
  273. return item;
  274. });
  275. }
  276. getQueryRunner(): PanelQueryRunner {
  277. if (!this.queryRunner) {
  278. this.queryRunner = new PanelQueryRunner();
  279. }
  280. return this.queryRunner;
  281. }
  282. destroy() {
  283. this.events.emit('panel-teardown');
  284. this.events.removeAllListeners();
  285. if (this.queryRunner) {
  286. this.queryRunner.destroy();
  287. }
  288. }
  289. }