dashboard_model.ts 23 KB

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