dashboard_model.ts 23 KB

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