PanelModel.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Libraries
  2. import _ from 'lodash';
  3. // Types
  4. import { Emitter } from 'app/core/utils/emitter';
  5. import { DataQuery, TimeSeries, Threshold, ScopedVars, PanelTypeChangedHook } 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?: ScopedVars;
  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. legend?: { show: boolean };
  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. this.restoreInfintyForThresholds();
  111. }
  112. ensureQueryIds() {
  113. if (this.targets) {
  114. for (const query of this.targets) {
  115. if (!query.refId) {
  116. query.refId = this.getNextQueryLetter();
  117. }
  118. }
  119. }
  120. }
  121. restoreInfintyForThresholds() {
  122. if (this.options && this.options.thresholds) {
  123. this.options.thresholds = this.options.thresholds.map((threshold: Threshold) => {
  124. // JSON serialization of -Infinity is 'null' so lets convert it back to -Infinity
  125. if (threshold.index === 0 && threshold.value === null) {
  126. return { ...threshold, value: -Infinity };
  127. }
  128. return threshold;
  129. });
  130. }
  131. }
  132. getOptions(panelDefaults) {
  133. return _.defaultsDeep(this.options || {}, panelDefaults);
  134. }
  135. updateOptions(options: object) {
  136. this.options = options;
  137. this.render();
  138. }
  139. getSaveModel() {
  140. const model: any = {};
  141. for (const property in this) {
  142. if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
  143. continue;
  144. }
  145. if (_.isEqual(this[property], defaults[property])) {
  146. continue;
  147. }
  148. model[property] = _.cloneDeep(this[property]);
  149. }
  150. return model;
  151. }
  152. setViewMode(fullscreen: boolean, isEditing: boolean) {
  153. this.fullscreen = fullscreen;
  154. this.isEditing = isEditing;
  155. this.events.emit('view-mode-changed');
  156. }
  157. updateGridPos(newPos: GridPos) {
  158. let sizeChanged = false;
  159. if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
  160. sizeChanged = true;
  161. }
  162. this.gridPos.x = newPos.x;
  163. this.gridPos.y = newPos.y;
  164. this.gridPos.w = newPos.w;
  165. this.gridPos.h = newPos.h;
  166. if (sizeChanged) {
  167. this.events.emit('panel-size-changed');
  168. }
  169. }
  170. resizeDone() {
  171. this.events.emit('panel-size-changed');
  172. }
  173. refresh() {
  174. this.hasRefreshed = true;
  175. this.events.emit('refresh');
  176. }
  177. render() {
  178. if (!this.hasRefreshed) {
  179. this.refresh();
  180. } else {
  181. this.events.emit('render');
  182. }
  183. }
  184. initialized() {
  185. this.events.emit('panel-initialized');
  186. }
  187. private getOptionsToRemember() {
  188. return Object.keys(this).reduce((acc, property) => {
  189. if (notPersistedProperties[property] || mustKeepProps[property]) {
  190. return acc;
  191. }
  192. return {
  193. ...acc,
  194. [property]: this[property],
  195. };
  196. }, {});
  197. }
  198. private restorePanelOptions(pluginId: string) {
  199. const prevOptions = this.cachedPluginOptions[pluginId] || {};
  200. Object.keys(prevOptions).map(property => {
  201. this[property] = prevOptions[property];
  202. });
  203. }
  204. changeType(pluginId: string, hook?: PanelTypeChangedHook) {
  205. const oldOptions: any = this.getOptionsToRemember();
  206. const oldPluginId = this.type;
  207. this.type = pluginId;
  208. // remove panel type specific options
  209. for (const key of _.keys(this)) {
  210. if (mustKeepProps[key]) {
  211. continue;
  212. }
  213. delete this[key];
  214. }
  215. this.cachedPluginOptions[oldPluginId] = oldOptions;
  216. this.restorePanelOptions(pluginId);
  217. // Callback that can validate and migrate any existing settings
  218. if (hook) {
  219. this.options = this.options || {};
  220. const old = oldOptions ? oldOptions.options : null;
  221. Object.assign(this.options, hook(this.options, oldPluginId, old));
  222. }
  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. }