PanelModel.ts 6.6 KB

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