DashboardModel.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. // Libaries
  2. import moment from 'moment';
  3. import _ from 'lodash';
  4. // Constants
  5. import { DEFAULT_ANNOTATION_COLOR } from '@grafana/ui';
  6. import { GRID_COLUMN_COUNT, REPEAT_DIR_VERTICAL, GRID_CELL_HEIGHT, GRID_CELL_VMARGIN } from 'app/core/constants';
  7. // Utils & Services
  8. import { Emitter } from 'app/core/utils/emitter';
  9. import { contextSrv } from 'app/core/services/context_srv';
  10. import sortByKeys from 'app/core/utils/sort_by_keys';
  11. // Types
  12. import { PanelModel } from './PanelModel';
  13. import { DashboardMigrator } from './DashboardMigrator';
  14. import { TimeRange } from '@grafana/ui/src';
  15. export class DashboardModel {
  16. id: any;
  17. uid: string;
  18. title: string;
  19. autoUpdate: any;
  20. description: any;
  21. tags: any;
  22. style: any;
  23. timezone: any;
  24. editable: any;
  25. graphTooltip: any;
  26. time: any;
  27. private originalTime: any;
  28. timepicker: any;
  29. templating: any;
  30. private originalTemplating: any;
  31. annotations: any;
  32. refresh: any;
  33. snapshot: any;
  34. schemaVersion: number;
  35. version: number;
  36. revision: number;
  37. links: any;
  38. gnetId: any;
  39. panels: PanelModel[];
  40. // ------------------
  41. // not persisted
  42. // ------------------
  43. // repeat process cycles
  44. iteration: number;
  45. meta: any;
  46. events: Emitter;
  47. static nonPersistedProperties: { [str: string]: boolean } = {
  48. events: true,
  49. meta: true,
  50. panels: true, // needs special handling
  51. templating: true, // needs special handling
  52. originalTime: true,
  53. originalTemplating: true,
  54. };
  55. constructor(data, meta?) {
  56. if (!data) {
  57. data = {};
  58. }
  59. this.events = new Emitter();
  60. this.id = data.id || null;
  61. this.uid = data.uid || null;
  62. this.revision = data.revision;
  63. this.title = data.title || 'No Title';
  64. this.autoUpdate = data.autoUpdate;
  65. this.description = data.description;
  66. this.tags = data.tags || [];
  67. this.style = data.style || 'dark';
  68. this.timezone = data.timezone || '';
  69. this.editable = data.editable !== false;
  70. this.graphTooltip = data.graphTooltip || 0;
  71. this.time = data.time || { from: 'now-6h', to: 'now' };
  72. this.timepicker = data.timepicker || {};
  73. this.templating = this.ensureListExist(data.templating);
  74. this.annotations = this.ensureListExist(data.annotations);
  75. this.refresh = data.refresh;
  76. this.snapshot = data.snapshot;
  77. this.schemaVersion = data.schemaVersion || 0;
  78. this.version = data.version || 0;
  79. this.links = data.links || [];
  80. this.gnetId = data.gnetId || null;
  81. this.panels = _.map(data.panels || [], panelData => new PanelModel(panelData));
  82. this.resetOriginalVariables();
  83. this.resetOriginalTime();
  84. this.initMeta(meta);
  85. this.updateSchema(data);
  86. this.addBuiltInAnnotationQuery();
  87. this.sortPanelsByGridPos();
  88. }
  89. addBuiltInAnnotationQuery() {
  90. let found = false;
  91. for (const item of this.annotations.list) {
  92. if (item.builtIn === 1) {
  93. found = true;
  94. break;
  95. }
  96. }
  97. if (found) {
  98. return;
  99. }
  100. this.annotations.list.unshift({
  101. datasource: '-- Grafana --',
  102. name: 'Annotations & Alerts',
  103. type: 'dashboard',
  104. iconColor: DEFAULT_ANNOTATION_COLOR,
  105. enable: true,
  106. hide: true,
  107. builtIn: 1,
  108. });
  109. }
  110. private initMeta(meta) {
  111. meta = meta || {};
  112. meta.canShare = meta.canShare !== false;
  113. meta.canSave = meta.canSave !== false;
  114. meta.canStar = meta.canStar !== false;
  115. meta.canEdit = meta.canEdit !== false;
  116. meta.showSettings = meta.canEdit;
  117. meta.canMakeEditable = meta.canSave && !this.editable;
  118. if (!this.editable) {
  119. meta.canEdit = false;
  120. meta.canDelete = false;
  121. meta.canSave = false;
  122. }
  123. this.meta = meta;
  124. }
  125. // cleans meta data and other non persistent state
  126. getSaveModelClone(options?) {
  127. const defaults = _.defaults(options || {}, {
  128. saveVariables: true,
  129. saveTimerange: true,
  130. });
  131. // make clone
  132. let copy: any = {};
  133. for (const property in this) {
  134. if (DashboardModel.nonPersistedProperties[property] || !this.hasOwnProperty(property)) {
  135. continue;
  136. }
  137. copy[property] = _.cloneDeep(this[property]);
  138. }
  139. // get variable save models
  140. copy.templating = {
  141. list: _.map(this.templating.list, variable => (variable.getSaveModel ? variable.getSaveModel() : variable)),
  142. };
  143. if (!defaults.saveVariables) {
  144. for (let i = 0; i < copy.templating.list.length; i++) {
  145. const current = copy.templating.list[i];
  146. const original = _.find(this.originalTemplating, { name: current.name, type: current.type });
  147. if (!original) {
  148. continue;
  149. }
  150. if (current.type === 'adhoc') {
  151. copy.templating.list[i].filters = original.filters;
  152. } else {
  153. copy.templating.list[i].current = original.current;
  154. }
  155. }
  156. }
  157. if (!defaults.saveTimerange) {
  158. copy.time = this.originalTime;
  159. }
  160. // get panel save models
  161. copy.panels = _.chain(this.panels)
  162. .filter(panel => panel.type !== 'add-panel')
  163. .map(panel => panel.getSaveModel())
  164. .value();
  165. // sort by keys
  166. copy = sortByKeys(copy);
  167. return copy;
  168. }
  169. setViewMode(panel: PanelModel, fullscreen: boolean, isEditing: boolean) {
  170. this.meta.fullscreen = fullscreen;
  171. this.meta.isEditing = isEditing && this.meta.canEdit;
  172. panel.setViewMode(fullscreen, this.meta.isEditing);
  173. this.events.emit('view-mode-changed', panel);
  174. }
  175. timeRangeUpdated(timeRange: TimeRange) {
  176. this.events.emit('time-range-updated', timeRange);
  177. }
  178. startRefresh() {
  179. this.events.emit('refresh');
  180. for (const panel of this.panels) {
  181. if (!this.otherPanelInFullscreen(panel)) {
  182. panel.refresh();
  183. }
  184. }
  185. }
  186. render() {
  187. this.events.emit('render');
  188. for (const panel of this.panels) {
  189. panel.render();
  190. }
  191. }
  192. panelInitialized(panel: PanelModel) {
  193. panel.initialized();
  194. if (!this.otherPanelInFullscreen(panel)) {
  195. panel.refresh();
  196. }
  197. }
  198. otherPanelInFullscreen(panel: PanelModel) {
  199. return this.meta.fullscreen && !panel.fullscreen;
  200. }
  201. private ensureListExist(data) {
  202. if (!data) {
  203. data = {};
  204. }
  205. if (!data.list) {
  206. data.list = [];
  207. }
  208. return data;
  209. }
  210. getNextPanelId() {
  211. let max = 0;
  212. for (const panel of this.panels) {
  213. if (panel.id > max) {
  214. max = panel.id;
  215. }
  216. if (panel.collapsed) {
  217. for (const rowPanel of panel.panels) {
  218. if (rowPanel.id > max) {
  219. max = rowPanel.id;
  220. }
  221. }
  222. }
  223. }
  224. return max + 1;
  225. }
  226. forEachPanel(callback) {
  227. for (let i = 0; i < this.panels.length; i++) {
  228. callback(this.panels[i], i);
  229. }
  230. }
  231. getPanelById(id): PanelModel {
  232. for (const panel of this.panels) {
  233. if (panel.id === id) {
  234. return panel;
  235. }
  236. }
  237. return null;
  238. }
  239. addPanel(panelData) {
  240. panelData.id = this.getNextPanelId();
  241. const panel = new PanelModel(panelData);
  242. this.panels.unshift(panel);
  243. this.sortPanelsByGridPos();
  244. this.events.emit('panel-added', panel);
  245. }
  246. sortPanelsByGridPos() {
  247. this.panels.sort((panelA, panelB) => {
  248. if (panelA.gridPos.y === panelB.gridPos.y) {
  249. return panelA.gridPos.x - panelB.gridPos.x;
  250. } else {
  251. return panelA.gridPos.y - panelB.gridPos.y;
  252. }
  253. });
  254. }
  255. cleanUpRepeats() {
  256. if (this.snapshot || this.templating.list.length === 0) {
  257. return;
  258. }
  259. this.iteration = (this.iteration || new Date().getTime()) + 1;
  260. const panelsToRemove = [];
  261. // cleanup scopedVars
  262. for (const panel of this.panels) {
  263. delete panel.scopedVars;
  264. }
  265. for (let i = 0; i < this.panels.length; i++) {
  266. const panel = this.panels[i];
  267. if ((!panel.repeat || panel.repeatedByRow) && panel.repeatPanelId && panel.repeatIteration !== this.iteration) {
  268. panelsToRemove.push(panel);
  269. }
  270. }
  271. // remove panels
  272. _.pull(this.panels, ...panelsToRemove);
  273. this.sortPanelsByGridPos();
  274. this.events.emit('repeats-processed');
  275. }
  276. processRepeats() {
  277. if (this.snapshot || this.templating.list.length === 0) {
  278. return;
  279. }
  280. this.cleanUpRepeats();
  281. this.iteration = (this.iteration || new Date().getTime()) + 1;
  282. for (let i = 0; i < this.panels.length; i++) {
  283. const panel = this.panels[i];
  284. if (panel.repeat) {
  285. this.repeatPanel(panel, i);
  286. }
  287. }
  288. this.sortPanelsByGridPos();
  289. this.events.emit('repeats-processed');
  290. }
  291. cleanUpRowRepeats(rowPanels) {
  292. const panelsToRemove = [];
  293. for (let i = 0; i < rowPanels.length; i++) {
  294. const panel = rowPanels[i];
  295. if (!panel.repeat && panel.repeatPanelId) {
  296. panelsToRemove.push(panel);
  297. }
  298. }
  299. _.pull(rowPanels, ...panelsToRemove);
  300. _.pull(this.panels, ...panelsToRemove);
  301. }
  302. processRowRepeats(row: PanelModel) {
  303. if (this.snapshot || this.templating.list.length === 0) {
  304. return;
  305. }
  306. let rowPanels = row.panels;
  307. if (!row.collapsed) {
  308. const rowPanelIndex = _.findIndex(this.panels, p => p.id === row.id);
  309. rowPanels = this.getRowPanels(rowPanelIndex);
  310. }
  311. this.cleanUpRowRepeats(rowPanels);
  312. for (let i = 0; i < rowPanels.length; i++) {
  313. const panel = rowPanels[i];
  314. if (panel.repeat) {
  315. const panelIndex = _.findIndex(this.panels, p => p.id === panel.id);
  316. this.repeatPanel(panel, panelIndex);
  317. }
  318. }
  319. }
  320. getPanelRepeatClone(sourcePanel, valueIndex, sourcePanelIndex) {
  321. // if first clone return source
  322. if (valueIndex === 0) {
  323. return sourcePanel;
  324. }
  325. const clone = new PanelModel(sourcePanel.getSaveModel());
  326. clone.id = this.getNextPanelId();
  327. // insert after source panel + value index
  328. this.panels.splice(sourcePanelIndex + valueIndex, 0, clone);
  329. clone.repeatIteration = this.iteration;
  330. clone.repeatPanelId = sourcePanel.id;
  331. clone.repeat = null;
  332. return clone;
  333. }
  334. getRowRepeatClone(sourceRowPanel, valueIndex, sourcePanelIndex) {
  335. // if first clone return source
  336. if (valueIndex === 0) {
  337. if (!sourceRowPanel.collapsed) {
  338. const rowPanels = this.getRowPanels(sourcePanelIndex);
  339. sourceRowPanel.panels = rowPanels;
  340. }
  341. return sourceRowPanel;
  342. }
  343. const clone = new PanelModel(sourceRowPanel.getSaveModel());
  344. // for row clones we need to figure out panels under row to clone and where to insert clone
  345. let rowPanels, insertPos;
  346. if (sourceRowPanel.collapsed) {
  347. rowPanels = _.cloneDeep(sourceRowPanel.panels);
  348. clone.panels = rowPanels;
  349. // insert copied row after preceding row
  350. insertPos = sourcePanelIndex + valueIndex;
  351. } else {
  352. rowPanels = this.getRowPanels(sourcePanelIndex);
  353. clone.panels = _.map(rowPanels, panel => panel.getSaveModel());
  354. // insert copied row after preceding row's panels
  355. insertPos = sourcePanelIndex + (rowPanels.length + 1) * valueIndex;
  356. }
  357. this.panels.splice(insertPos, 0, clone);
  358. this.updateRepeatedPanelIds(clone);
  359. return clone;
  360. }
  361. repeatPanel(panel: PanelModel, panelIndex: number) {
  362. const variable = _.find(this.templating.list, { name: panel.repeat });
  363. if (!variable) {
  364. return;
  365. }
  366. if (panel.type === 'row') {
  367. this.repeatRow(panel, panelIndex, variable);
  368. return;
  369. }
  370. const selectedOptions = this.getSelectedVariableOptions(variable);
  371. const maxPerRow = panel.maxPerRow || 4;
  372. let xPos = 0;
  373. let yPos = panel.gridPos.y;
  374. for (let index = 0; index < selectedOptions.length; index++) {
  375. const option = selectedOptions[index];
  376. let copy;
  377. copy = this.getPanelRepeatClone(panel, index, panelIndex);
  378. copy.scopedVars = copy.scopedVars || {};
  379. copy.scopedVars[variable.name] = option;
  380. if (panel.repeatDirection === REPEAT_DIR_VERTICAL) {
  381. if (index > 0) {
  382. yPos += copy.gridPos.h;
  383. }
  384. copy.gridPos.y = yPos;
  385. } else {
  386. // set width based on how many are selected
  387. // assumed the repeated panels should take up full row width
  388. copy.gridPos.w = Math.max(GRID_COLUMN_COUNT / selectedOptions.length, GRID_COLUMN_COUNT / maxPerRow);
  389. copy.gridPos.x = xPos;
  390. copy.gridPos.y = yPos;
  391. xPos += copy.gridPos.w;
  392. // handle overflow by pushing down one row
  393. if (xPos + copy.gridPos.w > GRID_COLUMN_COUNT) {
  394. xPos = 0;
  395. yPos += copy.gridPos.h;
  396. }
  397. }
  398. }
  399. // Update gridPos for panels below
  400. const yOffset = yPos - panel.gridPos.y;
  401. if (yOffset > 0) {
  402. const panelBelowIndex = panelIndex + selectedOptions.length;
  403. for (let i = panelBelowIndex; i < this.panels.length; i++) {
  404. this.panels[i].gridPos.y += yOffset;
  405. }
  406. }
  407. }
  408. repeatRow(panel: PanelModel, panelIndex: number, variable) {
  409. const selectedOptions = this.getSelectedVariableOptions(variable);
  410. let yPos = panel.gridPos.y;
  411. function setScopedVars(panel, variableOption) {
  412. panel.scopedVars = panel.scopedVars || {};
  413. panel.scopedVars[variable.name] = variableOption;
  414. }
  415. for (let optionIndex = 0; optionIndex < selectedOptions.length; optionIndex++) {
  416. const option = selectedOptions[optionIndex];
  417. const rowCopy = this.getRowRepeatClone(panel, optionIndex, panelIndex);
  418. setScopedVars(rowCopy, option);
  419. const rowHeight = this.getRowHeight(rowCopy);
  420. const rowPanels = rowCopy.panels || [];
  421. let panelBelowIndex;
  422. if (panel.collapsed) {
  423. // For collapsed row just copy its panels and set scoped vars and proper IDs
  424. _.each(rowPanels, (rowPanel, i) => {
  425. setScopedVars(rowPanel, option);
  426. if (optionIndex > 0) {
  427. this.updateRepeatedPanelIds(rowPanel, true);
  428. }
  429. });
  430. rowCopy.gridPos.y += optionIndex;
  431. yPos += optionIndex;
  432. panelBelowIndex = panelIndex + optionIndex + 1;
  433. } else {
  434. // insert after 'row' panel
  435. const insertPos = panelIndex + (rowPanels.length + 1) * optionIndex + 1;
  436. _.each(rowPanels, (rowPanel, i) => {
  437. setScopedVars(rowPanel, option);
  438. if (optionIndex > 0) {
  439. const cloneRowPanel = new PanelModel(rowPanel);
  440. this.updateRepeatedPanelIds(cloneRowPanel, true);
  441. // For exposed row additionally set proper Y grid position and add it to dashboard panels
  442. cloneRowPanel.gridPos.y += rowHeight * optionIndex;
  443. this.panels.splice(insertPos + i, 0, cloneRowPanel);
  444. }
  445. });
  446. rowCopy.panels = [];
  447. rowCopy.gridPos.y += rowHeight * optionIndex;
  448. yPos += rowHeight;
  449. panelBelowIndex = insertPos + rowPanels.length;
  450. }
  451. // Update gridPos for panels below
  452. for (let i = panelBelowIndex; i < this.panels.length; i++) {
  453. this.panels[i].gridPos.y += yPos;
  454. }
  455. }
  456. }
  457. updateRepeatedPanelIds(panel: PanelModel, repeatedByRow?: boolean) {
  458. panel.repeatPanelId = panel.id;
  459. panel.id = this.getNextPanelId();
  460. panel.repeatIteration = this.iteration;
  461. if (repeatedByRow) {
  462. panel.repeatedByRow = true;
  463. } else {
  464. panel.repeat = null;
  465. }
  466. return panel;
  467. }
  468. getSelectedVariableOptions(variable) {
  469. let selectedOptions;
  470. if (variable.current.text === 'All') {
  471. selectedOptions = variable.options.slice(1, variable.options.length);
  472. } else {
  473. selectedOptions = _.filter(variable.options, { selected: true });
  474. }
  475. return selectedOptions;
  476. }
  477. getRowHeight(rowPanel: PanelModel): number {
  478. if (!rowPanel.panels || rowPanel.panels.length === 0) {
  479. return 0;
  480. }
  481. const rowYPos = rowPanel.gridPos.y;
  482. const positions = _.map(rowPanel.panels, 'gridPos');
  483. const maxPos = _.maxBy(positions, pos => {
  484. return pos.y + pos.h;
  485. });
  486. return maxPos.y + maxPos.h - rowYPos;
  487. }
  488. removePanel(panel: PanelModel) {
  489. const index = _.indexOf(this.panels, panel);
  490. this.panels.splice(index, 1);
  491. this.events.emit('panel-removed', panel);
  492. }
  493. removeRow(row: PanelModel, removePanels: boolean) {
  494. const needToogle = (!removePanels && row.collapsed) || (removePanels && !row.collapsed);
  495. if (needToogle) {
  496. this.toggleRow(row);
  497. }
  498. this.removePanel(row);
  499. }
  500. expandRows() {
  501. for (let i = 0; i < this.panels.length; i++) {
  502. const panel = this.panels[i];
  503. if (panel.type !== 'row') {
  504. continue;
  505. }
  506. if (panel.collapsed) {
  507. this.toggleRow(panel);
  508. }
  509. }
  510. }
  511. collapseRows() {
  512. for (let i = 0; i < this.panels.length; i++) {
  513. const panel = this.panels[i];
  514. if (panel.type !== 'row') {
  515. continue;
  516. }
  517. if (!panel.collapsed) {
  518. this.toggleRow(panel);
  519. }
  520. }
  521. }
  522. setPanelFocus(id) {
  523. this.meta.focusPanelId = id;
  524. }
  525. updateSubmenuVisibility() {
  526. this.meta.submenuEnabled = (() => {
  527. if (this.links.length > 0) {
  528. return true;
  529. }
  530. const visibleVars = _.filter(this.templating.list, variable => variable.hide !== 2);
  531. if (visibleVars.length > 0) {
  532. return true;
  533. }
  534. const visibleAnnotations = _.filter(this.annotations.list, annotation => annotation.hide !== true);
  535. if (visibleAnnotations.length > 0) {
  536. return true;
  537. }
  538. return false;
  539. })();
  540. }
  541. getPanelInfoById(panelId) {
  542. for (let i = 0; i < this.panels.length; i++) {
  543. if (this.panels[i].id === panelId) {
  544. return {
  545. panel: this.panels[i],
  546. index: i,
  547. };
  548. }
  549. }
  550. return null;
  551. }
  552. duplicatePanel(panel) {
  553. const newPanel = panel.getSaveModel();
  554. newPanel.id = this.getNextPanelId();
  555. delete newPanel.repeat;
  556. delete newPanel.repeatIteration;
  557. delete newPanel.repeatPanelId;
  558. delete newPanel.scopedVars;
  559. if (newPanel.alert) {
  560. delete newPanel.thresholds;
  561. }
  562. delete newPanel.alert;
  563. // does it fit to the right?
  564. if (panel.gridPos.x + panel.gridPos.w * 2 <= GRID_COLUMN_COUNT) {
  565. newPanel.gridPos.x += panel.gridPos.w;
  566. } else {
  567. // add below
  568. newPanel.gridPos.y += panel.gridPos.h;
  569. }
  570. this.addPanel(newPanel);
  571. return newPanel;
  572. }
  573. formatDate(date, format?) {
  574. date = moment.isMoment(date) ? date : moment(date);
  575. format = format || 'YYYY-MM-DD HH:mm:ss';
  576. const timezone = this.getTimezone();
  577. return timezone === 'browser' ? moment(date).format(format) : moment.utc(date).format(format);
  578. }
  579. destroy() {
  580. this.events.removeAllListeners();
  581. for (const panel of this.panels) {
  582. panel.destroy();
  583. }
  584. }
  585. toggleRow(row: PanelModel) {
  586. const rowIndex = _.indexOf(this.panels, row);
  587. if (row.collapsed) {
  588. row.collapsed = false;
  589. const hasRepeat = _.some(row.panels, p => p.repeat);
  590. if (row.panels.length > 0) {
  591. // Use first panel to figure out if it was moved or pushed
  592. const firstPanel = row.panels[0];
  593. const yDiff = firstPanel.gridPos.y - (row.gridPos.y + row.gridPos.h);
  594. // start inserting after row
  595. let insertPos = rowIndex + 1;
  596. // y max will represent the bottom y pos after all panels have been added
  597. // needed to know home much panels below should be pushed down
  598. let yMax = row.gridPos.y;
  599. for (const panel of row.panels) {
  600. // make sure y is adjusted (in case row moved while collapsed)
  601. // console.log('yDiff', yDiff);
  602. panel.gridPos.y -= yDiff;
  603. // insert after row
  604. this.panels.splice(insertPos, 0, new PanelModel(panel));
  605. // update insert post and y max
  606. insertPos += 1;
  607. yMax = Math.max(yMax, panel.gridPos.y + panel.gridPos.h);
  608. }
  609. const pushDownAmount = yMax - row.gridPos.y - 1;
  610. // push panels below down
  611. for (let panelIndex = insertPos; panelIndex < this.panels.length; panelIndex++) {
  612. this.panels[panelIndex].gridPos.y += pushDownAmount;
  613. }
  614. row.panels = [];
  615. if (hasRepeat) {
  616. this.processRowRepeats(row);
  617. }
  618. }
  619. // sort panels
  620. this.sortPanelsByGridPos();
  621. // emit change event
  622. this.events.emit('row-expanded');
  623. return;
  624. }
  625. const rowPanels = this.getRowPanels(rowIndex);
  626. // remove panels
  627. _.pull(this.panels, ...rowPanels);
  628. // save panel models inside row panel
  629. row.panels = _.map(rowPanels, panel => panel.getSaveModel());
  630. row.collapsed = true;
  631. // emit change event
  632. this.events.emit('row-collapsed');
  633. }
  634. /**
  635. * Will return all panels after rowIndex until it encounters another row
  636. */
  637. getRowPanels(rowIndex: number): PanelModel[] {
  638. const rowPanels = [];
  639. for (let index = rowIndex + 1; index < this.panels.length; index++) {
  640. const panel = this.panels[index];
  641. // break when encountering another row
  642. if (panel.type === 'row') {
  643. break;
  644. }
  645. // this panel must belong to row
  646. rowPanels.push(panel);
  647. }
  648. return rowPanels;
  649. }
  650. on(eventName, callback) {
  651. this.events.on(eventName, callback);
  652. }
  653. off(eventName, callback?) {
  654. this.events.off(eventName, callback);
  655. }
  656. cycleGraphTooltip() {
  657. this.graphTooltip = (this.graphTooltip + 1) % 3;
  658. }
  659. sharedTooltipModeEnabled() {
  660. return this.graphTooltip > 0;
  661. }
  662. sharedCrosshairModeOnly() {
  663. return this.graphTooltip === 1;
  664. }
  665. getRelativeTime(date) {
  666. date = moment.isMoment(date) ? date : moment(date);
  667. return this.timezone === 'browser' ? moment(date).fromNow() : moment.utc(date).fromNow();
  668. }
  669. isTimezoneUtc() {
  670. return this.getTimezone() === 'utc';
  671. }
  672. isSnapshot() {
  673. return this.snapshot !== undefined;
  674. }
  675. getTimezone() {
  676. return this.timezone ? this.timezone : contextSrv.user.timezone;
  677. }
  678. private updateSchema(old) {
  679. const migrator = new DashboardMigrator(this);
  680. migrator.updateSchema(old);
  681. }
  682. resetOriginalTime() {
  683. this.originalTime = _.cloneDeep(this.time);
  684. }
  685. hasTimeChanged() {
  686. return !_.isEqual(this.time, this.originalTime);
  687. }
  688. resetOriginalVariables() {
  689. this.originalTemplating = _.map(this.templating.list, variable => {
  690. return {
  691. name: variable.name,
  692. type: variable.type,
  693. current: _.cloneDeep(variable.current),
  694. filters: _.cloneDeep(variable.filters),
  695. };
  696. });
  697. }
  698. hasVariableValuesChanged() {
  699. if (this.templating.list.length !== this.originalTemplating.length) {
  700. return false;
  701. }
  702. const updated = _.map(this.templating.list, variable => {
  703. return {
  704. name: variable.name,
  705. type: variable.type,
  706. current: _.cloneDeep(variable.current),
  707. filters: _.cloneDeep(variable.filters),
  708. };
  709. });
  710. return !_.isEqual(updated, this.originalTemplating);
  711. }
  712. autoFitPanels(viewHeight: number) {
  713. if (!this.meta.autofitpanels) {
  714. return;
  715. }
  716. const currentGridHeight = Math.max(
  717. ...this.panels.map(panel => {
  718. return panel.gridPos.h + panel.gridPos.y;
  719. })
  720. );
  721. const navbarHeight = 55;
  722. const margin = 20;
  723. const submenuHeight = 50;
  724. let visibleHeight = viewHeight - navbarHeight - margin;
  725. // Remove submenu height if visible
  726. if (this.meta.submenuEnabled && !this.meta.kiosk) {
  727. visibleHeight -= submenuHeight;
  728. }
  729. // add back navbar height
  730. if (this.meta.kiosk === 'b') {
  731. visibleHeight += 55;
  732. }
  733. const visibleGridHeight = Math.floor(visibleHeight / (GRID_CELL_HEIGHT + GRID_CELL_VMARGIN));
  734. const scaleFactor = currentGridHeight / visibleGridHeight;
  735. this.panels.forEach((panel, i) => {
  736. panel.gridPos.y = Math.round(panel.gridPos.y / scaleFactor) || 1;
  737. panel.gridPos.h = Math.round(panel.gridPos.h / scaleFactor) || 1;
  738. });
  739. }
  740. }