PanelModel.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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, TimeSeries, Threshold, ScopedVars, PanelTypeChangedHook } from '@grafana/ui';
  8. import { TableData } from '@grafana/ui/src';
  9. export interface GridPos {
  10. x: number;
  11. y: number;
  12. w: number;
  13. h: number;
  14. static?: boolean;
  15. }
  16. const notPersistedProperties: { [str: string]: boolean } = {
  17. events: true,
  18. fullscreen: true,
  19. isEditing: true,
  20. hasRefreshed: true,
  21. cachedPluginOptions: true,
  22. };
  23. // For angular panels we need to clean up properties when changing type
  24. // To make sure the change happens without strange bugs happening when panels use same
  25. // named property with different type / value expectations
  26. // This is not required for react panels
  27. const mustKeepProps: { [str: string]: boolean } = {
  28. id: true,
  29. gridPos: true,
  30. type: true,
  31. title: true,
  32. scopedVars: true,
  33. repeat: true,
  34. repeatIteration: true,
  35. repeatPanelId: true,
  36. repeatDirection: true,
  37. repeatedByRow: true,
  38. minSpan: true,
  39. collapsed: true,
  40. panels: true,
  41. targets: true,
  42. datasource: true,
  43. timeFrom: true,
  44. timeShift: true,
  45. hideTimeOverride: true,
  46. description: true,
  47. links: true,
  48. fullscreen: true,
  49. isEditing: true,
  50. hasRefreshed: true,
  51. events: true,
  52. cacheTimeout: true,
  53. cachedPluginOptions: true,
  54. transparent: true,
  55. pluginVersion: true,
  56. };
  57. const defaults: any = {
  58. gridPos: { x: 0, y: 0, h: 3, w: 6 },
  59. datasource: null,
  60. targets: [{ refId: 'A' }],
  61. cachedPluginOptions: {},
  62. transparent: false,
  63. };
  64. export class PanelModel {
  65. id: number;
  66. gridPos: GridPos;
  67. type: string;
  68. title: string;
  69. alert?: any;
  70. scopedVars?: ScopedVars;
  71. repeat?: string;
  72. repeatIteration?: number;
  73. repeatPanelId?: number;
  74. repeatDirection?: string;
  75. repeatedByRow?: boolean;
  76. maxPerRow?: number;
  77. collapsed?: boolean;
  78. panels?: any;
  79. soloMode?: boolean;
  80. targets: DataQuery[];
  81. datasource: string;
  82. thresholds?: any;
  83. pluginVersion?: string;
  84. snapshotData?: TimeSeries[] | [TableData];
  85. timeFrom?: any;
  86. timeShift?: any;
  87. hideTimeOverride?: any;
  88. options: {
  89. [key: string]: any;
  90. };
  91. maxDataPoints?: number;
  92. interval?: string;
  93. description?: string;
  94. links?: [];
  95. transparent: boolean;
  96. // non persisted
  97. fullscreen: boolean;
  98. isEditing: boolean;
  99. hasRefreshed: boolean;
  100. events: Emitter;
  101. cacheTimeout?: any;
  102. cachedPluginOptions?: any;
  103. legend?: { show: boolean };
  104. constructor(model: any) {
  105. this.events = new Emitter();
  106. // copy properties from persisted model
  107. for (const property in model) {
  108. (this as any)[property] = model[property];
  109. }
  110. // defaults
  111. _.defaultsDeep(this, _.cloneDeep(defaults));
  112. // queries must have refId
  113. this.ensureQueryIds();
  114. this.restoreInfintyForThresholds();
  115. }
  116. ensureQueryIds() {
  117. if (this.targets && _.isArray(this.targets)) {
  118. for (const query of this.targets) {
  119. if (!query.refId) {
  120. query.refId = getNextRefIdChar(this.targets);
  121. }
  122. }
  123. }
  124. }
  125. restoreInfintyForThresholds() {
  126. if (this.options && this.options.thresholds) {
  127. this.options.thresholds = this.options.thresholds.map((threshold: Threshold) => {
  128. // JSON serialization of -Infinity is 'null' so lets convert it back to -Infinity
  129. if (threshold.index === 0 && threshold.value === null) {
  130. return { ...threshold, value: -Infinity };
  131. }
  132. return threshold;
  133. });
  134. }
  135. }
  136. getOptions(panelDefaults: any) {
  137. return _.defaultsDeep(this.options || {}, panelDefaults);
  138. }
  139. updateOptions(options: object) {
  140. this.options = options;
  141. this.render();
  142. }
  143. getSaveModel() {
  144. const model: any = {};
  145. for (const property in this) {
  146. if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
  147. continue;
  148. }
  149. if (_.isEqual(this[property], defaults[property])) {
  150. continue;
  151. }
  152. model[property] = _.cloneDeep(this[property]);
  153. }
  154. return model;
  155. }
  156. setViewMode(fullscreen: boolean, isEditing: boolean) {
  157. this.fullscreen = fullscreen;
  158. this.isEditing = isEditing;
  159. this.events.emit('view-mode-changed');
  160. }
  161. updateGridPos(newPos: GridPos) {
  162. let sizeChanged = false;
  163. if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
  164. sizeChanged = true;
  165. }
  166. this.gridPos.x = newPos.x;
  167. this.gridPos.y = newPos.y;
  168. this.gridPos.w = newPos.w;
  169. this.gridPos.h = newPos.h;
  170. if (sizeChanged) {
  171. this.events.emit('panel-size-changed');
  172. }
  173. }
  174. resizeDone() {
  175. this.events.emit('panel-size-changed');
  176. }
  177. refresh() {
  178. this.hasRefreshed = true;
  179. this.events.emit('refresh');
  180. }
  181. render() {
  182. if (!this.hasRefreshed) {
  183. this.refresh();
  184. } else {
  185. this.events.emit('render');
  186. }
  187. }
  188. initialized() {
  189. this.events.emit('panel-initialized');
  190. }
  191. private getOptionsToRemember() {
  192. return Object.keys(this).reduce((acc, property) => {
  193. if (notPersistedProperties[property] || mustKeepProps[property]) {
  194. return acc;
  195. }
  196. return {
  197. ...acc,
  198. [property]: (this as any)[property],
  199. };
  200. }, {});
  201. }
  202. private restorePanelOptions(pluginId: string) {
  203. const prevOptions = this.cachedPluginOptions[pluginId] || {};
  204. Object.keys(prevOptions).map(property => {
  205. (this as any)[property] = prevOptions[property];
  206. });
  207. }
  208. changeType(pluginId: string, hook?: PanelTypeChangedHook) {
  209. const oldOptions: any = this.getOptionsToRemember();
  210. const oldPluginId = this.type;
  211. this.type = pluginId;
  212. // remove panel type specific options
  213. for (const key of _.keys(this)) {
  214. if (mustKeepProps[key]) {
  215. continue;
  216. }
  217. delete (this as any)[key];
  218. }
  219. this.cachedPluginOptions[oldPluginId] = oldOptions;
  220. this.restorePanelOptions(pluginId);
  221. // Callback that can validate and migrate any existing settings
  222. if (hook) {
  223. this.options = this.options || {};
  224. const old = oldOptions ? oldOptions.options : null;
  225. Object.assign(this.options, hook(this.options, oldPluginId, old));
  226. }
  227. }
  228. addQuery(query?: Partial<DataQuery>) {
  229. query = query || { refId: 'A' };
  230. query.refId = getNextRefIdChar(this.targets);
  231. this.targets.push(query as DataQuery);
  232. }
  233. changeQuery(query: DataQuery, index: number) {
  234. // ensure refId is maintained
  235. query.refId = this.targets[index].refId;
  236. // update query in array
  237. this.targets = this.targets.map((item, itemIndex) => {
  238. if (itemIndex === index) {
  239. return query;
  240. }
  241. return item;
  242. });
  243. }
  244. destroy() {
  245. this.events.emit('panel-teardown');
  246. this.events.removeAllListeners();
  247. }
  248. }