DashboardModel.ts 24 KB

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