dashboard_model.ts 23 KB

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