PanelModel.ts 7.8 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 { DataQuery, ScopedVars, DataQueryResponseData, PanelPlugin } 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. isInView: 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. queryRunner: true,
  61. };
  62. const defaults: any = {
  63. gridPos: { x: 0, y: 0, h: 3, w: 6 },
  64. datasource: null,
  65. targets: [{ refId: 'A' }],
  66. cachedPluginOptions: {},
  67. transparent: false,
  68. };
  69. export class PanelModel {
  70. id: number;
  71. gridPos: GridPos;
  72. type: string;
  73. title: string;
  74. alert?: any;
  75. scopedVars?: ScopedVars;
  76. repeat?: string;
  77. repeatIteration?: number;
  78. repeatPanelId?: number;
  79. repeatDirection?: string;
  80. repeatedByRow?: boolean;
  81. maxPerRow?: number;
  82. collapsed?: boolean;
  83. panels?: any;
  84. soloMode?: boolean;
  85. targets: DataQuery[];
  86. datasource: string;
  87. thresholds?: any;
  88. pluginVersion?: string;
  89. snapshotData?: DataQueryResponseData[];
  90. timeFrom?: any;
  91. timeShift?: any;
  92. hideTimeOverride?: any;
  93. options: {
  94. [key: string]: any;
  95. };
  96. maxDataPoints?: number;
  97. interval?: string;
  98. description?: string;
  99. links?: [];
  100. transparent: boolean;
  101. // non persisted
  102. fullscreen: boolean;
  103. isEditing: boolean;
  104. isInView: boolean;
  105. hasRefreshed: boolean;
  106. events: Emitter;
  107. cacheTimeout?: any;
  108. cachedPluginOptions?: any;
  109. legend?: { show: boolean };
  110. plugin?: PanelPlugin;
  111. private queryRunner?: PanelQueryRunner;
  112. constructor(model: any) {
  113. this.events = new Emitter();
  114. // copy properties from persisted model
  115. for (const property in model) {
  116. (this as any)[property] = model[property];
  117. }
  118. // defaults
  119. _.defaultsDeep(this, _.cloneDeep(defaults));
  120. // queries must have refId
  121. this.ensureQueryIds();
  122. this.restoreInfintyForThresholds();
  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. restoreInfintyForThresholds() {
  134. if (this.options && this.options.fieldOptions) {
  135. for (const threshold of this.options.fieldOptions.thresholds) {
  136. if (threshold.value === null) {
  137. threshold.value = -Infinity;
  138. }
  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. pluginLoaded(plugin: PanelPlugin) {
  215. this.plugin = plugin;
  216. if (plugin.panel && plugin.onPanelMigration) {
  217. const version = getPluginVersion(plugin);
  218. if (version !== this.pluginVersion) {
  219. this.options = plugin.onPanelMigration(this);
  220. this.pluginVersion = version;
  221. }
  222. }
  223. }
  224. changePlugin(newPlugin: PanelPlugin) {
  225. const pluginId = newPlugin.meta.id;
  226. const oldOptions: any = this.getOptionsToRemember();
  227. const oldPluginId = this.type;
  228. // for angular panels we must remove all events and let angular panels do some cleanup
  229. if (this.plugin.angularPanelCtrl) {
  230. this.destroy();
  231. }
  232. // remove panel type specific options
  233. for (const key of _.keys(this)) {
  234. if (mustKeepProps[key]) {
  235. continue;
  236. }
  237. delete (this as any)[key];
  238. }
  239. this.cachedPluginOptions[oldPluginId] = oldOptions;
  240. this.restorePanelOptions(pluginId);
  241. // switch
  242. this.type = pluginId;
  243. this.plugin = newPlugin;
  244. // Let panel plugins inspect options from previous panel and keep any that it can use
  245. if (newPlugin.onPanelTypeChanged) {
  246. this.options = this.options || {};
  247. const old = oldOptions && oldOptions.options ? oldOptions.options : {};
  248. Object.assign(this.options, newPlugin.onPanelTypeChanged(this.options, oldPluginId, old));
  249. }
  250. if (newPlugin.onPanelMigration) {
  251. this.pluginVersion = getPluginVersion(newPlugin);
  252. }
  253. }
  254. addQuery(query?: Partial<DataQuery>) {
  255. query = query || { refId: 'A' };
  256. query.refId = getNextRefIdChar(this.targets);
  257. this.targets.push(query as DataQuery);
  258. }
  259. changeQuery(query: DataQuery, index: number) {
  260. // ensure refId is maintained
  261. query.refId = this.targets[index].refId;
  262. // update query in array
  263. this.targets = this.targets.map((item, itemIndex) => {
  264. if (itemIndex === index) {
  265. return query;
  266. }
  267. return item;
  268. });
  269. }
  270. getQueryRunner(): PanelQueryRunner {
  271. if (!this.queryRunner) {
  272. this.queryRunner = new PanelQueryRunner();
  273. }
  274. return this.queryRunner;
  275. }
  276. hasTitle() {
  277. return !!this.title.length;
  278. }
  279. destroy() {
  280. this.events.emit('panel-teardown');
  281. this.events.removeAllListeners();
  282. if (this.queryRunner) {
  283. this.queryRunner.destroy();
  284. this.queryRunner = null;
  285. }
  286. }
  287. }
  288. function getPluginVersion(plugin: PanelPlugin): string {
  289. return plugin && plugin.meta.info.version ? plugin.meta.info.version : config.buildInfo.version;
  290. }