dashboard_srv.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import angular from 'angular';
  4. import moment from 'moment';
  5. import _ from 'lodash';
  6. import $ from 'jquery';
  7. import {Emitter} from 'app/core/core';
  8. import {contextSrv} from 'app/core/services/context_srv';
  9. import coreModule from 'app/core/core_module';
  10. export class DashboardModel {
  11. id: any;
  12. title: any;
  13. autoUpdate: any;
  14. description: any;
  15. tags: any;
  16. style: any;
  17. timezone: any;
  18. editable: any;
  19. hideControls: any;
  20. sharedCrosshair: any;
  21. rows: any;
  22. time: any;
  23. timepicker: any;
  24. templating: any;
  25. annotations: any;
  26. refresh: any;
  27. snapshot: any;
  28. schemaVersion: number;
  29. version: number;
  30. revision: number;
  31. links: any;
  32. gnetId: any;
  33. meta: any;
  34. events: any;
  35. constructor(data, meta) {
  36. if (!data) {
  37. data = {};
  38. }
  39. this.events = new Emitter();
  40. this.id = data.id || null;
  41. this.revision = data.revision;
  42. this.title = data.title || 'No Title';
  43. this.autoUpdate = data.autoUpdate;
  44. this.description = data.description;
  45. this.tags = data.tags || [];
  46. this.style = data.style || "dark";
  47. this.timezone = data.timezone || '';
  48. this.editable = data.editable !== false;
  49. this.hideControls = data.hideControls || false;
  50. this.sharedCrosshair = data.sharedCrosshair || false;
  51. this.rows = data.rows || [];
  52. this.time = data.time || { from: 'now-6h', to: 'now' };
  53. this.timepicker = data.timepicker || {};
  54. this.templating = this.ensureListExist(data.templating);
  55. this.annotations = this.ensureListExist(data.annotations);
  56. this.refresh = data.refresh;
  57. this.snapshot = data.snapshot;
  58. this.schemaVersion = data.schemaVersion || 0;
  59. this.version = data.version || 0;
  60. this.links = data.links || [];
  61. this.gnetId = data.gnetId || null;
  62. this.updateSchema(data);
  63. this.initMeta(meta);
  64. }
  65. private initMeta(meta) {
  66. meta = meta || {};
  67. meta.canShare = meta.canShare !== false;
  68. meta.canSave = meta.canSave !== false;
  69. meta.canStar = meta.canStar !== false;
  70. meta.canEdit = meta.canEdit !== false;
  71. if (!this.editable) {
  72. meta.canEdit = false;
  73. meta.canDelete = false;
  74. meta.canSave = false;
  75. this.hideControls = true;
  76. }
  77. this.meta = meta;
  78. }
  79. // cleans meta data and other non peristent state
  80. getSaveModelClone() {
  81. // temp remove stuff
  82. var events = this.events;
  83. var meta = this.meta;
  84. delete this.events;
  85. delete this.meta;
  86. events.emit('prepare-save-model');
  87. var copy = $.extend(true, {}, this);
  88. // restore properties
  89. this.events = events;
  90. this.meta = meta;
  91. return copy;
  92. }
  93. private ensureListExist(data) {
  94. if (!data) { data = {}; }
  95. if (!data.list) { data.list = []; }
  96. return data;
  97. }
  98. getNextPanelId() {
  99. var i, j, row, panel, max = 0;
  100. for (i = 0; i < this.rows.length; i++) {
  101. row = this.rows[i];
  102. for (j = 0; j < row.panels.length; j++) {
  103. panel = row.panels[j];
  104. if (panel.id > max) { max = panel.id; }
  105. }
  106. }
  107. return max + 1;
  108. }
  109. forEachPanel(callback) {
  110. var i, j, row;
  111. for (i = 0; i < this.rows.length; i++) {
  112. row = this.rows[i];
  113. for (j = 0; j < row.panels.length; j++) {
  114. callback(row.panels[j], j, row, i);
  115. }
  116. }
  117. }
  118. getPanelById(id) {
  119. for (var i = 0; i < this.rows.length; i++) {
  120. var row = this.rows[i];
  121. for (var j = 0; j < row.panels.length; j++) {
  122. var panel = row.panels[j];
  123. if (panel.id === id) {
  124. return panel;
  125. }
  126. }
  127. }
  128. return null;
  129. }
  130. rowSpan(row) {
  131. return _.reduce(row.panels, function(p,v) {
  132. return p + v.span;
  133. },0);
  134. };
  135. addPanel(panel, row) {
  136. var rowSpan = this.rowSpan(row);
  137. var panelCount = row.panels.length;
  138. var space = (12 - rowSpan) - panel.span;
  139. panel.id = this.getNextPanelId();
  140. // try to make room of there is no space left
  141. if (space <= 0) {
  142. if (panelCount === 1) {
  143. row.panels[0].span = 6;
  144. panel.span = 6;
  145. } else if (panelCount === 2) {
  146. row.panels[0].span = 4;
  147. row.panels[1].span = 4;
  148. panel.span = 4;
  149. }
  150. }
  151. row.panels.push(panel);
  152. }
  153. isSubmenuFeaturesEnabled() {
  154. var visableTemplates = _.filter(this.templating.list, function(template) {
  155. return template.hideVariable === undefined || template.hideVariable === false;
  156. });
  157. return visableTemplates.length > 0 || this.annotations.list.length > 0 || this.links.length > 0;
  158. }
  159. getPanelInfoById(panelId) {
  160. var result: any = {};
  161. _.each(this.rows, function(row) {
  162. _.each(row.panels, function(panel, index) {
  163. if (panel.id === panelId) {
  164. result.panel = panel;
  165. result.row = row;
  166. result.index = index;
  167. }
  168. });
  169. });
  170. if (!result.panel) {
  171. return null;
  172. }
  173. return result;
  174. }
  175. duplicatePanel(panel, row) {
  176. var rowIndex = _.indexOf(this.rows, row);
  177. var newPanel = angular.copy(panel);
  178. newPanel.id = this.getNextPanelId();
  179. delete newPanel.repeat;
  180. delete newPanel.repeatIteration;
  181. delete newPanel.repeatPanelId;
  182. delete newPanel.scopedVars;
  183. var currentRow = this.rows[rowIndex];
  184. currentRow.panels.push(newPanel);
  185. return newPanel;
  186. }
  187. formatDate(date, format) {
  188. date = moment.isMoment(date) ? date : moment(date);
  189. format = format || 'YYYY-MM-DD HH:mm:ss';
  190. this.timezone = this.getTimezone();
  191. return this.timezone === 'browser' ?
  192. moment(date).format(format) :
  193. moment.utc(date).format(format);
  194. }
  195. getRelativeTime(date) {
  196. date = moment.isMoment(date) ? date : moment(date);
  197. return this.timezone === 'browser' ?
  198. moment(date).fromNow() :
  199. moment.utc(date).fromNow();
  200. }
  201. getNextQueryLetter(panel) {
  202. var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  203. return _.find(letters, function(refId) {
  204. return _.every(panel.targets, function(other) {
  205. return other.refId !== refId;
  206. });
  207. });
  208. }
  209. isTimezoneUtc() {
  210. return this.getTimezone() === 'utc';
  211. }
  212. getTimezone() {
  213. return this.timezone ? this.timezone : contextSrv.user.timezone;
  214. }
  215. private updateSchema(old) {
  216. var i, j, k;
  217. var oldVersion = this.schemaVersion;
  218. var panelUpgrades = [];
  219. this.schemaVersion = 13;
  220. if (oldVersion === this.schemaVersion) {
  221. return;
  222. }
  223. // version 2 schema changes
  224. if (oldVersion < 2) {
  225. if (old.services) {
  226. if (old.services.filter) {
  227. this.time = old.services.filter.time;
  228. this.templating.list = old.services.filter.list || [];
  229. }
  230. }
  231. panelUpgrades.push(function(panel) {
  232. // rename panel type
  233. if (panel.type === 'graphite') {
  234. panel.type = 'graph';
  235. }
  236. if (panel.type !== 'graph') {
  237. return;
  238. }
  239. if (_.isBoolean(panel.legend)) { panel.legend = { show: panel.legend }; }
  240. if (panel.grid) {
  241. if (panel.grid.min) {
  242. panel.grid.leftMin = panel.grid.min;
  243. delete panel.grid.min;
  244. }
  245. if (panel.grid.max) {
  246. panel.grid.leftMax = panel.grid.max;
  247. delete panel.grid.max;
  248. }
  249. }
  250. if (panel.y_format) {
  251. panel.y_formats[0] = panel.y_format;
  252. delete panel.y_format;
  253. }
  254. if (panel.y2_format) {
  255. panel.y_formats[1] = panel.y2_format;
  256. delete panel.y2_format;
  257. }
  258. });
  259. }
  260. // schema version 3 changes
  261. if (oldVersion < 3) {
  262. // ensure panel ids
  263. var maxId = this.getNextPanelId();
  264. panelUpgrades.push(function(panel) {
  265. if (!panel.id) {
  266. panel.id = maxId;
  267. maxId += 1;
  268. }
  269. });
  270. }
  271. // schema version 4 changes
  272. if (oldVersion < 4) {
  273. // move aliasYAxis changes
  274. panelUpgrades.push(function(panel) {
  275. if (panel.type !== 'graph') { return; }
  276. _.each(panel.aliasYAxis, function(value, key) {
  277. panel.seriesOverrides = [{ alias: key, yaxis: value }];
  278. });
  279. delete panel.aliasYAxis;
  280. });
  281. }
  282. if (oldVersion < 6) {
  283. // move pulldowns to new schema
  284. var annotations = _.find(old.pulldowns, { type: 'annotations' });
  285. if (annotations) {
  286. this.annotations = {
  287. list: annotations.annotations || [],
  288. };
  289. }
  290. // update template variables
  291. for (i = 0 ; i < this.templating.list.length; i++) {
  292. var variable = this.templating.list[i];
  293. if (variable.datasource === void 0) { variable.datasource = null; }
  294. if (variable.type === 'filter') { variable.type = 'query'; }
  295. if (variable.type === void 0) { variable.type = 'query'; }
  296. if (variable.allFormat === void 0) { variable.allFormat = 'glob'; }
  297. }
  298. }
  299. if (oldVersion < 7) {
  300. if (old.nav && old.nav.length) {
  301. this.timepicker = old.nav[0];
  302. }
  303. // ensure query refIds
  304. panelUpgrades.push(function(panel) {
  305. _.each(panel.targets, function(target) {
  306. if (!target.refId) {
  307. target.refId = this.getNextQueryLetter(panel);
  308. }
  309. }.bind(this));
  310. });
  311. }
  312. if (oldVersion < 8) {
  313. panelUpgrades.push(function(panel) {
  314. _.each(panel.targets, function(target) {
  315. // update old influxdb query schema
  316. if (target.fields && target.tags && target.groupBy) {
  317. if (target.rawQuery) {
  318. delete target.fields;
  319. delete target.fill;
  320. } else {
  321. target.select = _.map(target.fields, function(field) {
  322. var parts = [];
  323. parts.push({type: 'field', params: [field.name]});
  324. parts.push({type: field.func, params: []});
  325. if (field.mathExpr) {
  326. parts.push({type: 'math', params: [field.mathExpr]});
  327. }
  328. if (field.asExpr) {
  329. parts.push({type: 'alias', params: [field.asExpr]});
  330. }
  331. return parts;
  332. });
  333. delete target.fields;
  334. _.each(target.groupBy, function(part) {
  335. if (part.type === 'time' && part.interval) {
  336. part.params = [part.interval];
  337. delete part.interval;
  338. }
  339. if (part.type === 'tag' && part.key) {
  340. part.params = [part.key];
  341. delete part.key;
  342. }
  343. });
  344. if (target.fill) {
  345. target.groupBy.push({type: 'fill', params: [target.fill]});
  346. delete target.fill;
  347. }
  348. }
  349. }
  350. });
  351. });
  352. }
  353. // schema version 9 changes
  354. if (oldVersion < 9) {
  355. // move aliasYAxis changes
  356. panelUpgrades.push(function(panel) {
  357. if (panel.type !== 'singlestat' && panel.thresholds !== "") { return; }
  358. if (panel.thresholds) {
  359. var k = panel.thresholds.split(",");
  360. if (k.length >= 3) {
  361. k.shift();
  362. panel.thresholds = k.join(",");
  363. }
  364. }
  365. });
  366. }
  367. // schema version 10 changes
  368. if (oldVersion < 10) {
  369. // move aliasYAxis changes
  370. panelUpgrades.push(function(panel) {
  371. if (panel.type !== 'table') { return; }
  372. _.each(panel.styles, function(style) {
  373. if (style.thresholds && style.thresholds.length >= 3) {
  374. var k = style.thresholds;
  375. k.shift();
  376. style.thresholds = k;
  377. }
  378. });
  379. });
  380. }
  381. if (oldVersion < 12) {
  382. // update template variables
  383. _.each(this.templating.list, function(templateVariable) {
  384. if (templateVariable.refresh) { templateVariable.refresh = 1; }
  385. if (!templateVariable.refresh) { templateVariable.refresh = 0; }
  386. if (templateVariable.hideVariable) {
  387. templateVariable.hide = 2;
  388. } else if (templateVariable.hideLabel) {
  389. templateVariable.hide = 1;
  390. } else {
  391. templateVariable.hide = 0;
  392. }
  393. });
  394. }
  395. if (oldVersion < 12) {
  396. // update graph yaxes changes
  397. panelUpgrades.push(function(panel) {
  398. if (panel.type !== 'graph') { return; }
  399. if (!panel.grid) { return; }
  400. if (!panel.yaxes) {
  401. panel.yaxes = [
  402. {
  403. show: panel['y-axis'],
  404. min: panel.grid.leftMin,
  405. max: panel.grid.leftMax,
  406. logBase: panel.grid.leftLogBase,
  407. format: panel.y_formats[0],
  408. label: panel.leftYAxisLabel,
  409. },
  410. {
  411. show: panel['y-axis'],
  412. min: panel.grid.rightMin,
  413. max: panel.grid.rightMax,
  414. logBase: panel.grid.rightLogBase,
  415. format: panel.y_formats[1],
  416. label: panel.rightYAxisLabel,
  417. }
  418. ];
  419. panel.xaxis = {
  420. show: panel['x-axis'],
  421. };
  422. delete panel.grid.leftMin;
  423. delete panel.grid.leftMax;
  424. delete panel.grid.leftLogBase;
  425. delete panel.grid.rightMin;
  426. delete panel.grid.rightMax;
  427. delete panel.grid.rightLogBase;
  428. delete panel.y_formats;
  429. delete panel.leftYAxisLabel;
  430. delete panel.rightYAxisLabel;
  431. delete panel['y-axis'];
  432. delete panel['x-axis'];
  433. }
  434. });
  435. }
  436. if (oldVersion < 13) {
  437. // update graph yaxes changes
  438. panelUpgrades.push(function(panel) {
  439. if (panel.type !== 'graph') { return; }
  440. panel.thresholds = [];
  441. var t1: any = {}, t2: any = {};
  442. if (panel.grid.threshold1 !== null) {
  443. t1.value = panel.grid.threshold1;
  444. if (panel.grid.thresholdLine) {
  445. t1.line = true;
  446. t1.lineColor = panel.grid.threshold1Color;
  447. } else {
  448. t1.fill = true;
  449. t1.fillColor = panel.grid.threshold1Color;
  450. }
  451. }
  452. if (panel.grid.threshold2 !== null) {
  453. t2.value = panel.grid.threshold2;
  454. if (panel.grid.thresholdLine) {
  455. t2.line = true;
  456. t2.lineColor = panel.grid.threshold2Color;
  457. } else {
  458. t2.fill = true;
  459. t2.fillColor = panel.grid.threshold2Color;
  460. }
  461. }
  462. if (_.isNumber(t1.value)) {
  463. if (_.isNumber(t2.value)) {
  464. if (t1.value > t2.value) {
  465. t1.op = t2.op = '<';
  466. panel.thresholds.push(t2);
  467. panel.thresholds.push(t1);
  468. } else {
  469. t1.op = t2.op = '>';
  470. panel.thresholds.push(t2);
  471. panel.thresholds.push(t1);
  472. }
  473. } else {
  474. t1.op = '>';
  475. panel.thresholds.push(t1);
  476. }
  477. }
  478. delete panel.grid.threshold1;
  479. delete panel.grid.threshold1Color;
  480. delete panel.grid.threshold2;
  481. delete panel.grid.threshold2Color;
  482. delete panel.grid.thresholdLine;
  483. });
  484. }
  485. if (panelUpgrades.length === 0) {
  486. return;
  487. }
  488. for (i = 0; i < this.rows.length; i++) {
  489. var row = this.rows[i];
  490. for (j = 0; j < row.panels.length; j++) {
  491. for (k = 0; k < panelUpgrades.length; k++) {
  492. panelUpgrades[k].call(this, row.panels[j]);
  493. }
  494. }
  495. }
  496. }
  497. }
  498. export class DashboardSrv {
  499. currentDashboard: any;
  500. create(dashboard, meta) {
  501. return new DashboardModel(dashboard, meta);
  502. }
  503. setCurrent(dashboard) {
  504. this.currentDashboard = dashboard;
  505. }
  506. getCurrent() {
  507. return this.currentDashboard;
  508. }
  509. }
  510. coreModule.service('dashboardSrv', DashboardSrv);