PanelModel.ts 6.5 KB

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