dashboard_model.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. timeRangeUpdated() {
  171. this.events.emit('time-range-updated');
  172. }
  173. startRefresh() {
  174. this.events.emit('refresh');
  175. for (const panel of this.panels) {
  176. if (!this.otherPanelInFullscreen(panel)) {
  177. panel.refresh();
  178. }
  179. }
  180. }
  181. render() {
  182. this.events.emit('render');
  183. for (const panel of this.panels) {
  184. panel.render();
  185. }
  186. }
  187. panelInitialized(panel: PanelModel) {
  188. panel.initialized();
  189. if (!this.otherPanelInFullscreen(panel)) {
  190. panel.refresh();
  191. }
  192. }
  193. otherPanelInFullscreen(panel: PanelModel) {
  194. return this.meta.fullscreen && !panel.fullscreen;
  195. }
  196. private ensureListExist(data) {
  197. if (!data) {
  198. data = {};
  199. }
  200. if (!data.list) {
  201. data.list = [];
  202. }
  203. return data;
  204. }
  205. getNextPanelId() {
  206. let max = 0;
  207. for (const panel of this.panels) {
  208. if (panel.id > max) {
  209. max = panel.id;
  210. }
  211. if (panel.collapsed) {
  212. for (const rowPanel of panel.panels) {
  213. if (rowPanel.id > max) {
  214. max = rowPanel.id;
  215. }
  216. }
  217. }
  218. }
  219. return max + 1;
  220. }
  221. forEachPanel(callback) {
  222. for (let i = 0; i < this.panels.length; i++) {
  223. callback(this.panels[i], i);
  224. }
  225. }
  226. getPanelById(id) {
  227. for (const panel of this.panels) {
  228. if (panel.id === id) {
  229. return panel;
  230. }
  231. }
  232. return null;
  233. }
  234. addPanel(panelData) {
  235. panelData.id = this.getNextPanelId();
  236. const panel = new PanelModel(panelData);
  237. this.panels.unshift(panel);
  238. this.sortPanelsByGridPos();
  239. this.events.emit('panel-added', panel);
  240. }
  241. sortPanelsByGridPos() {
  242. this.panels.sort((panelA, panelB) => {
  243. if (panelA.gridPos.y === panelB.gridPos.y) {
  244. return panelA.gridPos.x - panelB.gridPos.x;
  245. } else {
  246. return panelA.gridPos.y - panelB.gridPos.y;
  247. }
  248. });
  249. }
  250. cleanUpRepeats() {
  251. if (this.snapshot || this.templating.list.length === 0) {
  252. return;
  253. }
  254. this.iteration = (this.iteration || new Date().getTime()) + 1;
  255. const panelsToRemove = [];
  256. // cleanup scopedVars
  257. for (const panel of this.panels) {
  258. delete panel.scopedVars;
  259. }
  260. for (let i = 0; i < this.panels.length; i++) {
  261. const panel = this.panels[i];
  262. if ((!panel.repeat || panel.repeatedByRow) && panel.repeatPanelId && panel.repeatIteration !== this.iteration) {
  263. panelsToRemove.push(panel);
  264. }
  265. }
  266. // remove panels
  267. _.pull(this.panels, ...panelsToRemove);
  268. this.sortPanelsByGridPos();
  269. this.events.emit('repeats-processed');
  270. }
  271. processRepeats() {
  272. if (this.snapshot || this.templating.list.length === 0) {
  273. return;
  274. }
  275. this.cleanUpRepeats();
  276. this.iteration = (this.iteration || new Date().getTime()) + 1;
  277. for (let i = 0; i < this.panels.length; i++) {
  278. const panel = this.panels[i];
  279. if (panel.repeat) {
  280. this.repeatPanel(panel, i);
  281. }
  282. }
  283. this.sortPanelsByGridPos();
  284. this.events.emit('repeats-processed');
  285. }
  286. cleanUpRowRepeats(rowPanels) {
  287. const panelsToRemove = [];
  288. for (let i = 0; i < rowPanels.length; i++) {
  289. const panel = rowPanels[i];
  290. if (!panel.repeat && panel.repeatPanelId) {
  291. panelsToRemove.push(panel);
  292. }
  293. }
  294. _.pull(rowPanels, ...panelsToRemove);
  295. _.pull(this.panels, ...panelsToRemove);
  296. }
  297. processRowRepeats(row: PanelModel) {
  298. if (this.snapshot || this.templating.list.length === 0) {
  299. return;
  300. }
  301. let rowPanels = row.panels;
  302. if (!row.collapsed) {
  303. const rowPanelIndex = _.findIndex(this.panels, p => p.id === row.id);
  304. rowPanels = this.getRowPanels(rowPanelIndex);
  305. }
  306. this.cleanUpRowRepeats(rowPanels);
  307. for (let i = 0; i < rowPanels.length; i++) {
  308. const panel = rowPanels[i];
  309. if (panel.repeat) {
  310. const panelIndex = _.findIndex(this.panels, p => p.id === panel.id);
  311. this.repeatPanel(panel, panelIndex);
  312. }
  313. }
  314. }
  315. getPanelRepeatClone(sourcePanel, valueIndex, sourcePanelIndex) {
  316. // if first clone return source
  317. if (valueIndex === 0) {
  318. return sourcePanel;
  319. }
  320. const clone = new PanelModel(sourcePanel.getSaveModel());
  321. clone.id = this.getNextPanelId();
  322. // insert after source panel + value index
  323. this.panels.splice(sourcePanelIndex + valueIndex, 0, clone);
  324. clone.repeatIteration = this.iteration;
  325. clone.repeatPanelId = sourcePanel.id;
  326. clone.repeat = null;
  327. return clone;
  328. }
  329. getRowRepeatClone(sourceRowPanel, valueIndex, sourcePanelIndex) {
  330. // if first clone return source
  331. if (valueIndex === 0) {
  332. if (!sourceRowPanel.collapsed) {
  333. const rowPanels = this.getRowPanels(sourcePanelIndex);
  334. sourceRowPanel.panels = rowPanels;
  335. }
  336. return sourceRowPanel;
  337. }
  338. const clone = new PanelModel(sourceRowPanel.getSaveModel());
  339. // for row clones we need to figure out panels under row to clone and where to insert clone
  340. let rowPanels, insertPos;
  341. if (sourceRowPanel.collapsed) {
  342. rowPanels = _.cloneDeep(sourceRowPanel.panels);
  343. clone.panels = rowPanels;
  344. // insert copied row after preceding row
  345. insertPos = sourcePanelIndex + valueIndex;
  346. } else {
  347. rowPanels = this.getRowPanels(sourcePanelIndex);
  348. clone.panels = _.map(rowPanels, panel => panel.getSaveModel());
  349. // insert copied row after preceding row's panels
  350. insertPos = sourcePanelIndex + (rowPanels.length + 1) * valueIndex;
  351. }
  352. this.panels.splice(insertPos, 0, clone);
  353. this.updateRepeatedPanelIds(clone);
  354. return clone;
  355. }
  356. repeatPanel(panel: PanelModel, panelIndex: number) {
  357. const variable = _.find(this.templating.list, { name: panel.repeat });
  358. if (!variable) {
  359. return;
  360. }
  361. if (panel.type === 'row') {
  362. this.repeatRow(panel, panelIndex, variable);
  363. return;
  364. }
  365. const selectedOptions = this.getSelectedVariableOptions(variable);
  366. const minWidth = panel.minSpan || 6;
  367. let xPos = 0;
  368. let yPos = panel.gridPos.y;
  369. for (let index = 0; index < selectedOptions.length; index++) {
  370. const option = selectedOptions[index];
  371. let copy;
  372. copy = this.getPanelRepeatClone(panel, index, panelIndex);
  373. copy.scopedVars = copy.scopedVars || {};
  374. copy.scopedVars[variable.name] = option;
  375. if (panel.repeatDirection === REPEAT_DIR_VERTICAL) {
  376. if (index > 0) {
  377. yPos += copy.gridPos.h;
  378. }
  379. copy.gridPos.y = yPos;
  380. } else {
  381. // set width based on how many are selected
  382. // assumed the repeated panels should take up full row width
  383. copy.gridPos.w = Math.max(GRID_COLUMN_COUNT / selectedOptions.length, minWidth);
  384. copy.gridPos.x = xPos;
  385. copy.gridPos.y = yPos;
  386. xPos += copy.gridPos.w;
  387. // handle overflow by pushing down one row
  388. if (xPos + copy.gridPos.w > GRID_COLUMN_COUNT) {
  389. xPos = 0;
  390. yPos += copy.gridPos.h;
  391. }
  392. }
  393. }
  394. // Update gridPos for panels below
  395. const yOffset = yPos - panel.gridPos.y;
  396. if (yOffset > 0) {
  397. const panelBelowIndex = panelIndex + selectedOptions.length;
  398. for (let i = panelBelowIndex; i < this.panels.length; i++) {
  399. this.panels[i].gridPos.y += yOffset;
  400. }
  401. }
  402. }
  403. repeatRow(panel: PanelModel, panelIndex: number, variable) {
  404. const selectedOptions = this.getSelectedVariableOptions(variable);
  405. let yPos = panel.gridPos.y;
  406. function setScopedVars(panel, variableOption) {
  407. panel.scopedVars = panel.scopedVars || {};
  408. panel.scopedVars[variable.name] = variableOption;
  409. }
  410. for (let optionIndex = 0; optionIndex < selectedOptions.length; optionIndex++) {
  411. const option = selectedOptions[optionIndex];
  412. const rowCopy = this.getRowRepeatClone(panel, optionIndex, panelIndex);
  413. setScopedVars(rowCopy, option);
  414. const rowHeight = this.getRowHeight(rowCopy);
  415. const rowPanels = rowCopy.panels || [];
  416. let panelBelowIndex;
  417. if (panel.collapsed) {
  418. // For collapsed row just copy its panels and set scoped vars and proper IDs
  419. _.each(rowPanels, (rowPanel, i) => {
  420. setScopedVars(rowPanel, option);
  421. if (optionIndex > 0) {
  422. this.updateRepeatedPanelIds(rowPanel, true);
  423. }
  424. });
  425. rowCopy.gridPos.y += optionIndex;
  426. yPos += optionIndex;
  427. panelBelowIndex = panelIndex + optionIndex + 1;
  428. } else {
  429. // insert after 'row' panel
  430. const insertPos = panelIndex + (rowPanels.length + 1) * optionIndex + 1;
  431. _.each(rowPanels, (rowPanel, i) => {
  432. setScopedVars(rowPanel, option);
  433. if (optionIndex > 0) {
  434. const cloneRowPanel = new PanelModel(rowPanel);
  435. this.updateRepeatedPanelIds(cloneRowPanel, true);
  436. // For exposed row additionally set proper Y grid position and add it to dashboard panels
  437. cloneRowPanel.gridPos.y += rowHeight * optionIndex;
  438. this.panels.splice(insertPos + i, 0, cloneRowPanel);
  439. }
  440. });
  441. rowCopy.panels = [];
  442. rowCopy.gridPos.y += rowHeight * optionIndex;
  443. yPos += rowHeight;
  444. panelBelowIndex = insertPos + rowPanels.length;
  445. }
  446. // Update gridPos for panels below
  447. for (let i = panelBelowIndex; i < this.panels.length; i++) {
  448. this.panels[i].gridPos.y += yPos;
  449. }
  450. }
  451. }
  452. updateRepeatedPanelIds(panel: PanelModel, repeatedByRow?: boolean) {
  453. panel.repeatPanelId = panel.id;
  454. panel.id = this.getNextPanelId();
  455. panel.repeatIteration = this.iteration;
  456. if (repeatedByRow) {
  457. panel.repeatedByRow = true;
  458. } else {
  459. panel.repeat = null;
  460. }
  461. return panel;
  462. }
  463. getSelectedVariableOptions(variable) {
  464. let selectedOptions;
  465. if (variable.current.text === 'All') {
  466. selectedOptions = variable.options.slice(1, variable.options.length);
  467. } else {
  468. selectedOptions = _.filter(variable.options, { selected: true });
  469. }
  470. return selectedOptions;
  471. }
  472. getRowHeight(rowPanel: PanelModel): number {
  473. if (!rowPanel.panels || rowPanel.panels.length === 0) {
  474. return 0;
  475. }
  476. const rowYPos = rowPanel.gridPos.y;
  477. const positions = _.map(rowPanel.panels, 'gridPos');
  478. const maxPos = _.maxBy(positions, pos => {
  479. return pos.y + pos.h;
  480. });
  481. return maxPos.y + maxPos.h - rowYPos;
  482. }
  483. removePanel(panel: PanelModel) {
  484. const index = _.indexOf(this.panels, panel);
  485. this.panels.splice(index, 1);
  486. this.events.emit('panel-removed', panel);
  487. }
  488. removeRow(row: PanelModel, removePanels: boolean) {
  489. const needToogle = (!removePanels && row.collapsed) || (removePanels && !row.collapsed);
  490. if (needToogle) {
  491. this.toggleRow(row);
  492. }
  493. this.removePanel(row);
  494. }
  495. expandRows() {
  496. for (let i = 0; i < this.panels.length; i++) {
  497. const panel = this.panels[i];
  498. if (panel.type !== 'row') {
  499. continue;
  500. }
  501. if (panel.collapsed) {
  502. this.toggleRow(panel);
  503. }
  504. }
  505. }
  506. collapseRows() {
  507. for (let i = 0; i < this.panels.length; i++) {
  508. const panel = this.panels[i];
  509. if (panel.type !== 'row') {
  510. continue;
  511. }
  512. if (!panel.collapsed) {
  513. this.toggleRow(panel);
  514. }
  515. }
  516. }
  517. setPanelFocus(id) {
  518. this.meta.focusPanelId = id;
  519. }
  520. updateSubmenuVisibility() {
  521. this.meta.submenuEnabled = (() => {
  522. if (this.links.length > 0) {
  523. return true;
  524. }
  525. const visibleVars = _.filter(this.templating.list, variable => variable.hide !== 2);
  526. if (visibleVars.length > 0) {
  527. return true;
  528. }
  529. const visibleAnnotations = _.filter(this.annotations.list, annotation => annotation.hide !== true);
  530. if (visibleAnnotations.length > 0) {
  531. return true;
  532. }
  533. return false;
  534. })();
  535. }
  536. getPanelInfoById(panelId) {
  537. for (let i = 0; i < this.panels.length; i++) {
  538. if (this.panels[i].id === panelId) {
  539. return {
  540. panel: this.panels[i],
  541. index: i,
  542. };
  543. }
  544. }
  545. return null;
  546. }
  547. duplicatePanel(panel) {
  548. const newPanel = panel.getSaveModel();
  549. newPanel.id = this.getNextPanelId();
  550. delete newPanel.repeat;
  551. delete newPanel.repeatIteration;
  552. delete newPanel.repeatPanelId;
  553. delete newPanel.scopedVars;
  554. if (newPanel.alert) {
  555. delete newPanel.thresholds;
  556. }
  557. delete newPanel.alert;
  558. // does it fit to the right?
  559. if (panel.gridPos.x + panel.gridPos.w * 2 <= GRID_COLUMN_COUNT) {
  560. newPanel.gridPos.x += panel.gridPos.w;
  561. } else {
  562. // add below
  563. newPanel.gridPos.y += panel.gridPos.h;
  564. }
  565. this.addPanel(newPanel);
  566. return newPanel;
  567. }
  568. formatDate(date, format?) {
  569. date = moment.isMoment(date) ? date : moment(date);
  570. format = format || 'YYYY-MM-DD HH:mm:ss';
  571. const timezone = this.getTimezone();
  572. return timezone === 'browser' ? moment(date).format(format) : moment.utc(date).format(format);
  573. }
  574. destroy() {
  575. this.events.removeAllListeners();
  576. for (const panel of this.panels) {
  577. panel.destroy();
  578. }
  579. }
  580. toggleRow(row: PanelModel) {
  581. const rowIndex = _.indexOf(this.panels, row);
  582. if (row.collapsed) {
  583. row.collapsed = false;
  584. const hasRepeat = _.some(row.panels, p => p.repeat);
  585. if (row.panels.length > 0) {
  586. // Use first panel to figure out if it was moved or pushed
  587. const firstPanel = row.panels[0];
  588. const yDiff = firstPanel.gridPos.y - (row.gridPos.y + row.gridPos.h);
  589. // start inserting after row
  590. let insertPos = rowIndex + 1;
  591. // y max will represent the bottom y pos after all panels have been added
  592. // needed to know home much panels below should be pushed down
  593. let yMax = row.gridPos.y;
  594. for (const panel of row.panels) {
  595. // make sure y is adjusted (in case row moved while collapsed)
  596. // console.log('yDiff', yDiff);
  597. panel.gridPos.y -= yDiff;
  598. // insert after row
  599. this.panels.splice(insertPos, 0, new PanelModel(panel));
  600. // update insert post and y max
  601. insertPos += 1;
  602. yMax = Math.max(yMax, panel.gridPos.y + panel.gridPos.h);
  603. }
  604. const pushDownAmount = yMax - row.gridPos.y - 1;
  605. // push panels below down
  606. for (let panelIndex = insertPos; panelIndex < this.panels.length; panelIndex++) {
  607. this.panels[panelIndex].gridPos.y += pushDownAmount;
  608. }
  609. row.panels = [];
  610. if (hasRepeat) {
  611. this.processRowRepeats(row);
  612. }
  613. }
  614. // sort panels
  615. this.sortPanelsByGridPos();
  616. // emit change event
  617. this.events.emit('row-expanded');
  618. return;
  619. }
  620. const rowPanels = this.getRowPanels(rowIndex);
  621. // remove panels
  622. _.pull(this.panels, ...rowPanels);
  623. // save panel models inside row panel
  624. row.panels = _.map(rowPanels, panel => panel.getSaveModel());
  625. row.collapsed = true;
  626. // emit change event
  627. this.events.emit('row-collapsed');
  628. }
  629. /**
  630. * Will return all panels after rowIndex until it encounters another row
  631. */
  632. getRowPanels(rowIndex: number): PanelModel[] {
  633. const rowPanels = [];
  634. for (let index = rowIndex + 1; index < this.panels.length; index++) {
  635. const panel = this.panels[index];
  636. // break when encountering another row
  637. if (panel.type === 'row') {
  638. break;
  639. }
  640. // this panel must belong to row
  641. rowPanels.push(panel);
  642. }
  643. return rowPanels;
  644. }
  645. on(eventName, callback) {
  646. this.events.on(eventName, callback);
  647. }
  648. off(eventName, callback?) {
  649. this.events.off(eventName, callback);
  650. }
  651. cycleGraphTooltip() {
  652. this.graphTooltip = (this.graphTooltip + 1) % 3;
  653. }
  654. sharedTooltipModeEnabled() {
  655. return this.graphTooltip > 0;
  656. }
  657. sharedCrosshairModeOnly() {
  658. return this.graphTooltip === 1;
  659. }
  660. getRelativeTime(date) {
  661. date = moment.isMoment(date) ? date : moment(date);
  662. return this.timezone === 'browser' ? moment(date).fromNow() : moment.utc(date).fromNow();
  663. }
  664. getNextQueryLetter(panel) {
  665. const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  666. return _.find(letters, refId => {
  667. return _.every(panel.targets, other => {
  668. return other.refId !== refId;
  669. });
  670. });
  671. }
  672. isTimezoneUtc() {
  673. return this.getTimezone() === 'utc';
  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. }