panel_model.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Libraries
  2. import _ from 'lodash';
  3. // Types
  4. import { Emitter } from 'app/core/utils/emitter';
  5. import { PANEL_OPTIONS_KEY_PREFIX } from 'app/core/constants';
  6. import { DataQuery } from '@grafana/ui/src/types';
  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. maxDataPoints: true,
  45. interval: 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?: any;
  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?: any;
  83. timeFrom?: any;
  84. timeShift?: any;
  85. hideTimeOverride?: any;
  86. maxDataPoints?: number;
  87. interval?: string;
  88. description?: string;
  89. links?: [];
  90. transparent: boolean;
  91. // non persisted
  92. fullscreen: boolean;
  93. isEditing: boolean;
  94. hasRefreshed: boolean;
  95. events: Emitter;
  96. cacheTimeout?: any;
  97. // cache props between plugins
  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. }
  110. ensureQueryIds() {
  111. if (this.targets) {
  112. for (const query of this.targets) {
  113. if (!query.refId) {
  114. query.refId = this.getNextQueryLetter();
  115. }
  116. }
  117. }
  118. }
  119. getOptions(panelDefaults) {
  120. return _.defaultsDeep(this[this.getOptionsKey()] || {}, panelDefaults);
  121. }
  122. updateOptions(options: object) {
  123. const update: any = {};
  124. update[this.getOptionsKey()] = options;
  125. Object.assign(this, update);
  126. this.render();
  127. }
  128. private getOptionsKey() {
  129. return PANEL_OPTIONS_KEY_PREFIX + this.type;
  130. }
  131. getSaveModel() {
  132. const model: any = {};
  133. for (const property in this) {
  134. if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
  135. continue;
  136. }
  137. if (_.isEqual(this[property], defaults[property])) {
  138. continue;
  139. }
  140. model[property] = _.cloneDeep(this[property]);
  141. }
  142. return model;
  143. }
  144. setViewMode(fullscreen: boolean, isEditing: boolean) {
  145. this.fullscreen = fullscreen;
  146. this.isEditing = isEditing;
  147. this.events.emit('view-mode-changed');
  148. }
  149. updateGridPos(newPos: GridPos) {
  150. let sizeChanged = false;
  151. if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
  152. sizeChanged = true;
  153. }
  154. this.gridPos.x = newPos.x;
  155. this.gridPos.y = newPos.y;
  156. this.gridPos.w = newPos.w;
  157. this.gridPos.h = newPos.h;
  158. if (sizeChanged) {
  159. this.events.emit('panel-size-changed');
  160. }
  161. }
  162. resizeDone() {
  163. this.events.emit('panel-size-changed');
  164. }
  165. refresh() {
  166. this.hasRefreshed = true;
  167. this.events.emit('refresh');
  168. }
  169. render() {
  170. if (!this.hasRefreshed) {
  171. this.refresh();
  172. } else {
  173. this.events.emit('render');
  174. }
  175. }
  176. initialized() {
  177. this.events.emit('panel-initialized');
  178. }
  179. private getOptionsToRemember() {
  180. return Object.keys(this).reduce((acc, property) => {
  181. if (notPersistedProperties[property] || mustKeepProps[property]) {
  182. return acc;
  183. }
  184. return {
  185. ...acc,
  186. [property]: this[property],
  187. };
  188. }, {});
  189. }
  190. private saveCurrentPanelOptions() {
  191. this.cachedPluginOptions[this.type] = this.getOptionsToRemember();
  192. }
  193. private restorePanelOptions(pluginId: string) {
  194. const prevOptions = this.cachedPluginOptions[pluginId] || {};
  195. Object.keys(prevOptions).map(property => {
  196. this[property] = prevOptions[property];
  197. });
  198. }
  199. changeType(pluginId: string, fromAngularPanel: boolean) {
  200. this.saveCurrentPanelOptions();
  201. this.type = pluginId;
  202. // for angular panels only we need to remove all events and let angular panels do some cleanup
  203. if (fromAngularPanel) {
  204. this.destroy();
  205. for (const key of _.keys(this)) {
  206. if (mustKeepProps[key]) {
  207. continue;
  208. }
  209. delete this[key];
  210. }
  211. }
  212. this.restorePanelOptions(pluginId);
  213. }
  214. addQuery(query?: Partial<DataQuery>) {
  215. query = query || { refId: 'A' };
  216. query.refId = this.getNextQueryLetter();
  217. this.targets.push(query as DataQuery);
  218. }
  219. getNextQueryLetter(): string {
  220. const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  221. return _.find(letters, refId => {
  222. return _.every(this.targets, other => {
  223. return other.refId !== refId;
  224. });
  225. });
  226. }
  227. destroy() {
  228. this.events.emit('panel-teardown');
  229. this.events.removeAllListeners();
  230. }
  231. }