PanelModel.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. }
  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. getOptions() {
  134. return this.options;
  135. }
  136. updateOptions(options: object) {
  137. this.options = options;
  138. this.render();
  139. }
  140. getSaveModel() {
  141. const model: any = {};
  142. for (const property in this) {
  143. if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
  144. continue;
  145. }
  146. if (_.isEqual(this[property], defaults[property])) {
  147. continue;
  148. }
  149. model[property] = _.cloneDeep(this[property]);
  150. }
  151. return model;
  152. }
  153. setViewMode(fullscreen: boolean, isEditing: boolean) {
  154. this.fullscreen = fullscreen;
  155. this.isEditing = isEditing;
  156. this.events.emit('view-mode-changed');
  157. }
  158. updateGridPos(newPos: GridPos) {
  159. let sizeChanged = false;
  160. if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
  161. sizeChanged = true;
  162. }
  163. this.gridPos.x = newPos.x;
  164. this.gridPos.y = newPos.y;
  165. this.gridPos.w = newPos.w;
  166. this.gridPos.h = newPos.h;
  167. if (sizeChanged) {
  168. this.events.emit('panel-size-changed');
  169. }
  170. }
  171. resizeDone() {
  172. this.events.emit('panel-size-changed');
  173. }
  174. refresh() {
  175. this.hasRefreshed = true;
  176. this.events.emit('refresh');
  177. }
  178. render() {
  179. if (!this.hasRefreshed) {
  180. this.refresh();
  181. } else {
  182. this.events.emit('render');
  183. }
  184. }
  185. initialized() {
  186. this.events.emit('panel-initialized');
  187. }
  188. private getOptionsToRemember() {
  189. return Object.keys(this).reduce((acc, property) => {
  190. if (notPersistedProperties[property] || mustKeepProps[property]) {
  191. return acc;
  192. }
  193. return {
  194. ...acc,
  195. [property]: (this as any)[property],
  196. };
  197. }, {});
  198. }
  199. private restorePanelOptions(pluginId: string) {
  200. const prevOptions = this.cachedPluginOptions[pluginId] || {};
  201. Object.keys(prevOptions).map(property => {
  202. (this as any)[property] = prevOptions[property];
  203. });
  204. }
  205. private applyPluginOptionDefaults(plugin: PanelPlugin) {
  206. if (plugin.angularConfigCtrl) {
  207. return;
  208. }
  209. this.options = _.defaultsDeep({}, this.options || {}, plugin.defaults);
  210. }
  211. pluginLoaded(plugin: PanelPlugin) {
  212. this.plugin = plugin;
  213. if (plugin.panel && plugin.onPanelMigration) {
  214. const version = getPluginVersion(plugin);
  215. if (version !== this.pluginVersion) {
  216. this.options = plugin.onPanelMigration(this);
  217. this.pluginVersion = version;
  218. }
  219. }
  220. this.applyPluginOptionDefaults(plugin);
  221. }
  222. changePlugin(newPlugin: PanelPlugin) {
  223. const pluginId = newPlugin.meta.id;
  224. const oldOptions: any = this.getOptionsToRemember();
  225. const oldPluginId = this.type;
  226. const wasAngular = !!this.plugin.angularPanelCtrl;
  227. // for angular panels we must remove all events and let angular panels do some cleanup
  228. if (wasAngular) {
  229. this.destroy();
  230. }
  231. // remove panel type specific options
  232. for (const key of _.keys(this)) {
  233. if (mustKeepProps[key]) {
  234. continue;
  235. }
  236. delete (this as any)[key];
  237. }
  238. this.cachedPluginOptions[oldPluginId] = oldOptions;
  239. this.restorePanelOptions(pluginId);
  240. // Let panel plugins inspect options from previous panel and keep any that it can use
  241. if (newPlugin.onPanelTypeChanged) {
  242. let old: any = {};
  243. if (wasAngular) {
  244. old = { angular: oldOptions };
  245. } else if (oldOptions && oldOptions.options) {
  246. old = oldOptions.options;
  247. }
  248. this.options = this.options || {};
  249. Object.assign(this.options, newPlugin.onPanelTypeChanged(this.options, oldPluginId, old));
  250. }
  251. // switch
  252. this.type = pluginId;
  253. this.plugin = newPlugin;
  254. this.applyPluginOptionDefaults(newPlugin);
  255. if (newPlugin.onPanelMigration) {
  256. this.pluginVersion = getPluginVersion(newPlugin);
  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(this.id);
  278. }
  279. return this.queryRunner;
  280. }
  281. hasTitle() {
  282. return this.title && this.title.length > 0;
  283. }
  284. destroy() {
  285. this.events.emit('panel-teardown');
  286. this.events.removeAllListeners();
  287. if (this.queryRunner) {
  288. this.queryRunner.destroy();
  289. this.queryRunner = null;
  290. }
  291. }
  292. }
  293. function getPluginVersion(plugin: PanelPlugin): string {
  294. return plugin && plugin.meta.info.version ? plugin.meta.info.version : config.buildInfo.version;
  295. }