DashboardModel.ts 23 KB

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