dashboard_srv.ts 16 KB

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