PanelModel.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Libraries
  2. import _ from 'lodash';
  3. // Types
  4. import { Emitter } from 'app/core/utils/emitter';
  5. import { DataQuery, TimeSeries } 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. 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?: TimeSeries[] | [TableData];
  83. timeFrom?: any;
  84. timeShift?: any;
  85. hideTimeOverride?: any;
  86. options: object;
  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. }
  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.options || {}, panelDefaults);
  121. }
  122. updateOptions(options: object) {
  123. this.options = options;
  124. this.render();
  125. }
  126. getSaveModel() {
  127. const model: any = {};
  128. for (const property in this) {
  129. if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
  130. continue;
  131. }
  132. if (_.isEqual(this[property], defaults[property])) {
  133. continue;
  134. }
  135. model[property] = _.cloneDeep(this[property]);
  136. }
  137. return model;
  138. }
  139. setViewMode(fullscreen: boolean, isEditing: boolean) {
  140. this.fullscreen = fullscreen;
  141. this.isEditing = isEditing;
  142. this.events.emit('view-mode-changed');
  143. }
  144. updateGridPos(newPos: GridPos) {
  145. let sizeChanged = false;
  146. if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
  147. sizeChanged = true;
  148. }
  149. this.gridPos.x = newPos.x;
  150. this.gridPos.y = newPos.y;
  151. this.gridPos.w = newPos.w;
  152. this.gridPos.h = newPos.h;
  153. if (sizeChanged) {
  154. this.events.emit('panel-size-changed');
  155. }
  156. }
  157. resizeDone() {
  158. this.events.emit('panel-size-changed');
  159. }
  160. refresh() {
  161. this.hasRefreshed = true;
  162. this.events.emit('refresh');
  163. }
  164. render() {
  165. if (!this.hasRefreshed) {
  166. this.refresh();
  167. } else {
  168. this.events.emit('render');
  169. }
  170. }
  171. initialized() {
  172. this.events.emit('panel-initialized');
  173. }
  174. private getOptionsToRemember() {
  175. return Object.keys(this).reduce((acc, property) => {
  176. if (notPersistedProperties[property] || mustKeepProps[property]) {
  177. return acc;
  178. }
  179. return {
  180. ...acc,
  181. [property]: this[property],
  182. };
  183. }, {});
  184. }
  185. private saveCurrentPanelOptions() {
  186. this.cachedPluginOptions[this.type] = this.getOptionsToRemember();
  187. }
  188. private restorePanelOptions(pluginId: string) {
  189. const prevOptions = this.cachedPluginOptions[pluginId] || {};
  190. Object.keys(prevOptions).map(property => {
  191. this[property] = prevOptions[property];
  192. });
  193. }
  194. changeType(pluginId: string, fromAngularPanel: boolean) {
  195. this.saveCurrentPanelOptions();
  196. this.type = pluginId;
  197. // for angular panels only we need to remove all events and let angular panels do some cleanup
  198. if (fromAngularPanel) {
  199. this.destroy();
  200. for (const key of _.keys(this)) {
  201. if (mustKeepProps[key]) {
  202. continue;
  203. }
  204. delete this[key];
  205. }
  206. }
  207. this.restorePanelOptions(pluginId);
  208. }
  209. addQuery(query?: Partial<DataQuery>) {
  210. query = query || { refId: 'A' };
  211. query.refId = this.getNextQueryLetter();
  212. this.targets.push(query as DataQuery);
  213. }
  214. getNextQueryLetter(): string {
  215. const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  216. return _.find(letters, refId => {
  217. return _.every(this.targets, other => {
  218. return other.refId !== refId;
  219. });
  220. });
  221. }
  222. changeQuery(query: DataQuery, index: number) {
  223. // ensure refId is maintained
  224. query.refId = this.targets[index].refId;
  225. // update query in array
  226. this.targets = this.targets.map((item, itemIndex) => {
  227. if (itemIndex === index) {
  228. return query;
  229. }
  230. return item;
  231. });
  232. }
  233. destroy() {
  234. this.events.emit('panel-teardown');
  235. this.events.removeAllListeners();
  236. }
  237. }