dashboard_model.ts 21 KB

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