PanelModel.ts 8.1 KB

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