PanelModel.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Libraries
  2. import _ from 'lodash';
  3. // Types
  4. import { Emitter } from 'app/core/utils/emitter';
  5. import { DataQuery, TimeSeries, Threshold, ScopedVars, PanelTypeChangedHook } from '@grafana/ui';
  6. import { TableData } from '@grafana/ui/src';
  7. import { getNextQueryLetter } from '../../../core/utils/query';
  8. export interface GridPos {
  9. x: number;
  10. y: number;
  11. w: number;
  12. h: number;
  13. static?: boolean;
  14. }
  15. const notPersistedProperties: { [str: string]: boolean } = {
  16. events: true,
  17. fullscreen: true,
  18. isEditing: true,
  19. hasRefreshed: true,
  20. cachedPluginOptions: true,
  21. };
  22. // For angular panels we need to clean up properties when changing type
  23. // To make sure the change happens without strange bugs happening when panels use same
  24. // named property with different type / value expectations
  25. // This is not required for react panels
  26. const mustKeepProps: { [str: string]: boolean } = {
  27. id: true,
  28. gridPos: true,
  29. type: true,
  30. title: true,
  31. scopedVars: true,
  32. repeat: true,
  33. repeatIteration: true,
  34. repeatPanelId: true,
  35. repeatDirection: true,
  36. repeatedByRow: true,
  37. minSpan: true,
  38. collapsed: true,
  39. panels: true,
  40. targets: true,
  41. datasource: true,
  42. timeFrom: true,
  43. timeShift: true,
  44. hideTimeOverride: true,
  45. description: true,
  46. links: true,
  47. fullscreen: true,
  48. isEditing: true,
  49. hasRefreshed: true,
  50. events: true,
  51. cacheTimeout: true,
  52. cachedPluginOptions: true,
  53. transparent: true,
  54. };
  55. const defaults: any = {
  56. gridPos: { x: 0, y: 0, h: 3, w: 6 },
  57. datasource: null,
  58. targets: [{ refId: 'A' }],
  59. cachedPluginOptions: {},
  60. transparent: false,
  61. };
  62. export class PanelModel {
  63. id: number;
  64. gridPos: GridPos;
  65. type: string;
  66. title: string;
  67. alert?: any;
  68. scopedVars?: ScopedVars;
  69. repeat?: string;
  70. repeatIteration?: number;
  71. repeatPanelId?: number;
  72. repeatDirection?: string;
  73. repeatedByRow?: boolean;
  74. maxPerRow?: number;
  75. collapsed?: boolean;
  76. panels?: any;
  77. soloMode?: boolean;
  78. targets: DataQuery[];
  79. datasource: string;
  80. thresholds?: any;
  81. snapshotData?: TimeSeries[] | [TableData];
  82. timeFrom?: any;
  83. timeShift?: any;
  84. hideTimeOverride?: any;
  85. options: {
  86. [key: string]: any;
  87. };
  88. maxDataPoints?: number;
  89. interval?: string;
  90. description?: string;
  91. links?: [];
  92. transparent: boolean;
  93. // non persisted
  94. fullscreen: boolean;
  95. isEditing: boolean;
  96. hasRefreshed: boolean;
  97. events: Emitter;
  98. cacheTimeout?: any;
  99. cachedPluginOptions?: any;
  100. legend?: { show: boolean };
  101. constructor(model) {
  102. this.events = new Emitter();
  103. // copy properties from persisted model
  104. for (const property in model) {
  105. this[property] = model[property];
  106. }
  107. // defaults
  108. _.defaultsDeep(this, _.cloneDeep(defaults));
  109. // queries must have refId
  110. this.ensureQueryIds();
  111. this.restoreInfintyForThresholds();
  112. }
  113. ensureQueryIds() {
  114. if (this.targets) {
  115. for (const query of this.targets) {
  116. if (!query.refId) {
  117. query.refId = getNextQueryLetter(this.targets);
  118. }
  119. }
  120. }
  121. }
  122. restoreInfintyForThresholds() {
  123. if (this.options && this.options.thresholds) {
  124. this.options.thresholds = this.options.thresholds.map((threshold: Threshold) => {
  125. // JSON serialization of -Infinity is 'null' so lets convert it back to -Infinity
  126. if (threshold.index === 0 && threshold.value === null) {
  127. return { ...threshold, value: -Infinity };
  128. }
  129. return threshold;
  130. });
  131. }
  132. }
  133. getOptions(panelDefaults) {
  134. return _.defaultsDeep(this.options || {}, panelDefaults);
  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[property],
  196. };
  197. }, {});
  198. }
  199. private restorePanelOptions(pluginId: string) {
  200. const prevOptions = this.cachedPluginOptions[pluginId] || {};
  201. Object.keys(prevOptions).map(property => {
  202. this[property] = prevOptions[property];
  203. });
  204. }
  205. changeType(pluginId: string, hook?: PanelTypeChangedHook) {
  206. const oldOptions: any = this.getOptionsToRemember();
  207. const oldPluginId = this.type;
  208. this.type = pluginId;
  209. // remove panel type specific options
  210. for (const key of _.keys(this)) {
  211. if (mustKeepProps[key]) {
  212. continue;
  213. }
  214. delete this[key];
  215. }
  216. this.cachedPluginOptions[oldPluginId] = oldOptions;
  217. this.restorePanelOptions(pluginId);
  218. // Callback that can validate and migrate any existing settings
  219. if (hook) {
  220. this.options = this.options || {};
  221. const old = oldOptions ? oldOptions.options : null;
  222. Object.assign(this.options, hook(this.options, oldPluginId, old));
  223. }
  224. }
  225. addQuery(query?: Partial<DataQuery>) {
  226. query = query || { refId: 'A' };
  227. query.refId = getNextQueryLetter(this.targets);
  228. this.targets.push(query as DataQuery);
  229. }
  230. changeQuery(query: DataQuery, index: number) {
  231. // ensure refId is maintained
  232. query.refId = this.targets[index].refId;
  233. // update query in array
  234. this.targets = this.targets.map((item, itemIndex) => {
  235. if (itemIndex === index) {
  236. return query;
  237. }
  238. return item;
  239. });
  240. }
  241. destroy() {
  242. this.events.emit('panel-teardown');
  243. this.events.removeAllListeners();
  244. }
  245. }