PanelModel.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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, TimeSeries } from '@grafana/ui';
  7. import { TableData } from '@grafana/ui/src';
  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. maxDataPoints: true,
  46. interval: true,
  47. description: true,
  48. links: true,
  49. fullscreen: true,
  50. isEditing: true,
  51. hasRefreshed: true,
  52. events: true,
  53. cacheTimeout: true,
  54. cachedPluginOptions: true,
  55. transparent: 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?: any;
  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. snapshotData?: TimeSeries[] | [TableData];
  84. timeFrom?: any;
  85. timeShift?: any;
  86. hideTimeOverride?: any;
  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. // cache props between plugins
  99. cachedPluginOptions?: any;
  100. constructor(model) {
  101. this.events = new Emitter();
  102. // copy properties from persisted model
  103. for (const property in model) {
  104. this[property] = model[property];
  105. }
  106. // defaults
  107. _.defaultsDeep(this, _.cloneDeep(defaults));
  108. // queries must have refId
  109. this.ensureQueryIds();
  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. getOptions(panelDefaults) {
  121. return _.defaultsDeep(this[this.getOptionsKey()] || {}, panelDefaults);
  122. }
  123. updateOptions(options: object) {
  124. const update: any = {};
  125. update[this.getOptionsKey()] = options;
  126. Object.assign(this, update);
  127. this.render();
  128. }
  129. private getOptionsKey() {
  130. return PANEL_OPTIONS_KEY_PREFIX + this.type;
  131. }
  132. getSaveModel() {
  133. const model: any = {};
  134. for (const property in this) {
  135. if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
  136. continue;
  137. }
  138. if (_.isEqual(this[property], defaults[property])) {
  139. continue;
  140. }
  141. model[property] = _.cloneDeep(this[property]);
  142. }
  143. return model;
  144. }
  145. setViewMode(fullscreen: boolean, isEditing: boolean) {
  146. this.fullscreen = fullscreen;
  147. this.isEditing = isEditing;
  148. this.events.emit('view-mode-changed');
  149. }
  150. updateGridPos(newPos: GridPos) {
  151. let sizeChanged = false;
  152. if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
  153. sizeChanged = true;
  154. }
  155. this.gridPos.x = newPos.x;
  156. this.gridPos.y = newPos.y;
  157. this.gridPos.w = newPos.w;
  158. this.gridPos.h = newPos.h;
  159. if (sizeChanged) {
  160. this.events.emit('panel-size-changed');
  161. }
  162. }
  163. resizeDone() {
  164. this.events.emit('panel-size-changed');
  165. }
  166. refresh() {
  167. this.hasRefreshed = true;
  168. this.events.emit('refresh');
  169. }
  170. render() {
  171. if (!this.hasRefreshed) {
  172. this.refresh();
  173. } else {
  174. this.events.emit('render');
  175. }
  176. }
  177. initialized() {
  178. this.events.emit('panel-initialized');
  179. }
  180. private getOptionsToRemember() {
  181. return Object.keys(this).reduce((acc, property) => {
  182. if (notPersistedProperties[property] || mustKeepProps[property]) {
  183. return acc;
  184. }
  185. return {
  186. ...acc,
  187. [property]: this[property],
  188. };
  189. }, {});
  190. }
  191. private saveCurrentPanelOptions() {
  192. this.cachedPluginOptions[this.type] = this.getOptionsToRemember();
  193. }
  194. private restorePanelOptions(pluginId: string) {
  195. const prevOptions = this.cachedPluginOptions[pluginId] || {};
  196. Object.keys(prevOptions).map(property => {
  197. this[property] = prevOptions[property];
  198. });
  199. }
  200. changeType(pluginId: string, fromAngularPanel: boolean) {
  201. this.saveCurrentPanelOptions();
  202. this.type = pluginId;
  203. // for angular panels only we need to remove all events and let angular panels do some cleanup
  204. if (fromAngularPanel) {
  205. this.destroy();
  206. for (const key of _.keys(this)) {
  207. if (mustKeepProps[key]) {
  208. continue;
  209. }
  210. delete this[key];
  211. }
  212. }
  213. this.restorePanelOptions(pluginId);
  214. }
  215. addQuery(query?: Partial<DataQuery>) {
  216. query = query || { refId: 'A' };
  217. query.refId = this.getNextQueryLetter();
  218. this.targets.push(query as DataQuery);
  219. }
  220. getNextQueryLetter(): string {
  221. const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  222. return _.find(letters, refId => {
  223. return _.every(this.targets, other => {
  224. return other.refId !== refId;
  225. });
  226. });
  227. }
  228. destroy() {
  229. this.events.emit('panel-teardown');
  230. this.events.removeAllListeners();
  231. }
  232. }