PanelModel.ts 8.4 KB

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