PanelModel.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. 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: {
  87. [key: string]: any;
  88. };
  89. maxDataPoints?: number;
  90. interval?: string;
  91. description?: string;
  92. links?: [];
  93. transparent: boolean;
  94. // non persisted
  95. fullscreen: boolean;
  96. isEditing: boolean;
  97. hasRefreshed: boolean;
  98. events: Emitter;
  99. cacheTimeout?: any;
  100. cachedPluginOptions?: any;
  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 = this.getNextQueryLetter();
  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 saveCurrentPanelOptions() {
  200. this.cachedPluginOptions[this.type] = this.getOptionsToRemember();
  201. }
  202. private restorePanelOptions(pluginId: string) {
  203. const prevOptions = this.cachedPluginOptions[pluginId] || {};
  204. Object.keys(prevOptions).map(property => {
  205. this[property] = prevOptions[property];
  206. });
  207. }
  208. changeType(pluginId: string, fromAngularPanel: boolean) {
  209. this.saveCurrentPanelOptions();
  210. this.type = pluginId;
  211. // for angular panels only we need to remove all events and let angular panels do some cleanup
  212. if (fromAngularPanel) {
  213. this.destroy();
  214. }
  215. // remove panel type specific options
  216. for (const key of _.keys(this)) {
  217. if (mustKeepProps[key]) {
  218. continue;
  219. }
  220. delete this[key];
  221. }
  222. this.restorePanelOptions(pluginId);
  223. }
  224. addQuery(query?: Partial<DataQuery>) {
  225. query = query || { refId: 'A' };
  226. query.refId = this.getNextQueryLetter();
  227. this.targets.push(query as DataQuery);
  228. }
  229. getNextQueryLetter(): string {
  230. const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  231. return _.find(letters, refId => {
  232. return _.every(this.targets, other => {
  233. return other.refId !== refId;
  234. });
  235. });
  236. }
  237. changeQuery(query: DataQuery, index: number) {
  238. // ensure refId is maintained
  239. query.refId = this.targets[index].refId;
  240. // update query in array
  241. this.targets = this.targets.map((item, itemIndex) => {
  242. if (itemIndex === index) {
  243. return query;
  244. }
  245. return item;
  246. });
  247. }
  248. destroy() {
  249. this.events.emit('panel-teardown');
  250. this.events.removeAllListeners();
  251. }
  252. }