dashboard_model_specs.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. import {describe, beforeEach, it, expect} from 'test/lib/common';
  2. import _ from 'lodash';
  3. import {DashboardModel} from '../dashboard_model';
  4. import {PanelModel} from '../panel_model';
  5. describe('DashboardModel', function() {
  6. describe('when creating new dashboard model defaults only', function() {
  7. var model;
  8. beforeEach(function() {
  9. model = new DashboardModel({}, {});
  10. });
  11. it('should have title', function() {
  12. expect(model.title).to.be('No Title');
  13. });
  14. it('should have meta', function() {
  15. expect(model.meta.canSave).to.be(true);
  16. expect(model.meta.canShare).to.be(true);
  17. });
  18. it('should have default properties', function() {
  19. expect(model.panels.length).to.be(0);
  20. });
  21. });
  22. describe('when getting next panel id', function() {
  23. var model;
  24. beforeEach(function() {
  25. model = new DashboardModel({
  26. panels: [{ id: 5 }]
  27. });
  28. });
  29. it('should return max id + 1', function() {
  30. expect(model.getNextPanelId()).to.be(6);
  31. });
  32. });
  33. describe('getSaveModelClone', function() {
  34. it('should sort keys', () => {
  35. var model = new DashboardModel({});
  36. var saveModel = model.getSaveModelClone();
  37. var keys = _.keys(saveModel);
  38. expect(keys[0]).to.be('annotations');
  39. expect(keys[1]).to.be('autoUpdate');
  40. });
  41. });
  42. describe('row and panel manipulation', function() {
  43. var dashboard;
  44. beforeEach(function() {
  45. dashboard = new DashboardModel({});
  46. });
  47. it('adding panel should new up panel model', function() {
  48. dashboard.addPanel({type: 'test', title: 'test'});
  49. expect(dashboard.panels[0] instanceof PanelModel).to.be(true);
  50. });
  51. it('duplicate panel should try to add to the right if there is space', function() {
  52. var panel = {id: 10, gridPos: {x: 0, y: 0, w: 6, h: 2}};
  53. dashboard.addPanel(panel);
  54. dashboard.duplicatePanel(dashboard.panels[0]);
  55. expect(dashboard.panels[1].gridPos).to.eql({x: 6, y: 0, h: 2, w: 6});
  56. });
  57. it('duplicate panel should remove repeat data', function() {
  58. var panel = {id: 10, gridPos: {x: 0, y: 0, w: 6, h: 2}, repeat: 'asd', scopedVars: {test: 'asd'}};
  59. dashboard.addPanel(panel);
  60. dashboard.duplicatePanel(dashboard.panels[0]);
  61. expect(dashboard.panels[1].repeat).to.be(undefined);
  62. expect(dashboard.panels[1].scopedVars).to.be(undefined);
  63. });
  64. });
  65. describe('when creating dashboard with old schema', function() {
  66. var model;
  67. var graph;
  68. var singlestat;
  69. var table;
  70. beforeEach(function() {
  71. model = new DashboardModel({
  72. services: { filter: { time: { from: 'now-1d', to: 'now'}, list: [{}] }},
  73. pulldowns: [
  74. {type: 'filtering', enable: true},
  75. {type: 'annotations', enable: true, annotations: [{name: 'old'}]}
  76. ],
  77. panels: [
  78. {
  79. type: 'graph', legend: true, aliasYAxis: { test: 2 },
  80. y_formats: ['kbyte', 'ms'],
  81. grid: {
  82. min: 1,
  83. max: 10,
  84. rightMin: 5,
  85. rightMax: 15,
  86. leftLogBase: 1,
  87. rightLogBase: 2,
  88. threshold1: 200,
  89. threshold2: 400,
  90. threshold1Color: 'yellow',
  91. threshold2Color: 'red',
  92. },
  93. leftYAxisLabel: 'left label',
  94. targets: [{refId: 'A'}, {}],
  95. },
  96. {
  97. type: 'singlestat', legend: true, thresholds: '10,20,30', aliasYAxis: { test: 2 }, grid: { min: 1, max: 10 },
  98. targets: [{refId: 'A'}, {}],
  99. },
  100. {
  101. type: 'table', legend: true, styles: [{ thresholds: ["10", "20", "30"]}, { thresholds: ["100", "200", "300"]}],
  102. targets: [{refId: 'A'}, {}],
  103. }
  104. ]
  105. });
  106. graph = model.panels[0];
  107. singlestat = model.panels[1];
  108. table = model.panels[2];
  109. });
  110. it('should have title', function() {
  111. expect(model.title).to.be('No Title');
  112. });
  113. it('should have panel id', function() {
  114. expect(graph.id).to.be(1);
  115. });
  116. it('should move time and filtering list', function() {
  117. expect(model.time.from).to.be('now-1d');
  118. expect(model.templating.list[0].allFormat).to.be('glob');
  119. });
  120. it('graphite panel should change name too graph', function() {
  121. expect(graph.type).to.be('graph');
  122. });
  123. it('single stat panel should have two thresholds', function() {
  124. expect(singlestat.thresholds).to.be('20,30');
  125. });
  126. it('queries without refId should get it', function() {
  127. expect(graph.targets[1].refId).to.be('B');
  128. });
  129. it('update legend setting', function() {
  130. expect(graph.legend.show).to.be(true);
  131. });
  132. it('move aliasYAxis to series override', function() {
  133. expect(graph.seriesOverrides[0].alias).to.be("test");
  134. expect(graph.seriesOverrides[0].yaxis).to.be(2);
  135. });
  136. it('should move pulldowns to new schema', function() {
  137. expect(model.annotations.list[0].name).to.be('old');
  138. });
  139. it('table panel should only have two thresholds values', function() {
  140. expect(table.styles[0].thresholds[0]).to.be("20");
  141. expect(table.styles[0].thresholds[1]).to.be("30");
  142. expect(table.styles[1].thresholds[0]).to.be("200");
  143. expect(table.styles[1].thresholds[1]).to.be("300");
  144. });
  145. it('graph grid to yaxes options', function() {
  146. expect(graph.yaxes[0].min).to.be(1);
  147. expect(graph.yaxes[0].max).to.be(10);
  148. expect(graph.yaxes[0].format).to.be('kbyte');
  149. expect(graph.yaxes[0].label).to.be('left label');
  150. expect(graph.yaxes[0].logBase).to.be(1);
  151. expect(graph.yaxes[1].min).to.be(5);
  152. expect(graph.yaxes[1].max).to.be(15);
  153. expect(graph.yaxes[1].format).to.be('ms');
  154. expect(graph.yaxes[1].logBase).to.be(2);
  155. expect(graph.grid.rightMax).to.be(undefined);
  156. expect(graph.grid.rightLogBase).to.be(undefined);
  157. expect(graph.y_formats).to.be(undefined);
  158. });
  159. it('dashboard schema version should be set to latest', function() {
  160. expect(model.schemaVersion).to.be(16);
  161. });
  162. it('graph thresholds should be migrated', function() {
  163. expect(graph.thresholds.length).to.be(2);
  164. expect(graph.thresholds[0].op).to.be('gt');
  165. expect(graph.thresholds[0].value).to.be(200);
  166. expect(graph.thresholds[0].fillColor).to.be('yellow');
  167. expect(graph.thresholds[1].value).to.be(400);
  168. expect(graph.thresholds[1].fillColor).to.be('red');
  169. });
  170. });
  171. describe('Given editable false dashboard', function() {
  172. var model;
  173. beforeEach(function() {
  174. model = new DashboardModel({editable: false});
  175. });
  176. it('Should set meta canEdit and canSave to false', function() {
  177. expect(model.meta.canSave).to.be(false);
  178. expect(model.meta.canEdit).to.be(false);
  179. });
  180. it('getSaveModelClone should remove meta', function() {
  181. var clone = model.getSaveModelClone();
  182. expect(clone.meta).to.be(undefined);
  183. });
  184. });
  185. describe('when loading dashboard with old influxdb query schema', function() {
  186. var model;
  187. var target;
  188. beforeEach(function() {
  189. model = new DashboardModel({
  190. panels: [{
  191. type: 'graph',
  192. grid: {},
  193. yaxes: [{}, {}],
  194. targets: [{
  195. "alias": "$tag_datacenter $tag_source $col",
  196. "column": "value",
  197. "measurement": "logins.count",
  198. "fields": [
  199. {
  200. "func": "mean",
  201. "name": "value",
  202. "mathExpr": "*2",
  203. "asExpr": "value"
  204. },
  205. {
  206. "name": "one-minute",
  207. "func": "mean",
  208. "mathExpr": "*3",
  209. "asExpr": "one-minute"
  210. }
  211. ],
  212. "tags": [],
  213. "fill": "previous",
  214. "function": "mean",
  215. "groupBy": [
  216. {
  217. "interval": "auto",
  218. "type": "time"
  219. },
  220. {
  221. "key": "source",
  222. "type": "tag"
  223. },
  224. {
  225. "type": "tag",
  226. "key": "datacenter"
  227. }
  228. ],
  229. }]
  230. }]
  231. });
  232. target = model.panels[0].targets[0];
  233. });
  234. it('should update query schema', function() {
  235. expect(target.fields).to.be(undefined);
  236. expect(target.select.length).to.be(2);
  237. expect(target.select[0].length).to.be(4);
  238. expect(target.select[0][0].type).to.be('field');
  239. expect(target.select[0][1].type).to.be('mean');
  240. expect(target.select[0][2].type).to.be('math');
  241. expect(target.select[0][3].type).to.be('alias');
  242. });
  243. });
  244. describe('when creating dashboard model with missing list for annoations or templating', function() {
  245. var model;
  246. beforeEach(function() {
  247. model = new DashboardModel({
  248. annotations: {
  249. enable: true,
  250. },
  251. templating: {
  252. enable: true
  253. }
  254. });
  255. });
  256. it('should add empty list', function() {
  257. expect(model.annotations.list.length).to.be(1);
  258. expect(model.templating.list.length).to.be(0);
  259. });
  260. it('should add builtin annotation query', function() {
  261. expect(model.annotations.list[0].builtIn).to.be(1);
  262. expect(model.templating.list.length).to.be(0);
  263. });
  264. });
  265. describe('Formatting epoch timestamp when timezone is set as utc', function() {
  266. var dashboard;
  267. beforeEach(function() {
  268. dashboard = new DashboardModel({timezone: 'utc'});
  269. });
  270. it('Should format timestamp with second resolution by default', function() {
  271. expect(dashboard.formatDate(1234567890000)).to.be('2009-02-13 23:31:30');
  272. });
  273. it('Should format timestamp with second resolution even if second format is passed as parameter', function() {
  274. expect(dashboard.formatDate(1234567890007,'YYYY-MM-DD HH:mm:ss')).to.be('2009-02-13 23:31:30');
  275. });
  276. it('Should format timestamp with millisecond resolution if format is passed as parameter', function() {
  277. expect(dashboard.formatDate(1234567890007,'YYYY-MM-DD HH:mm:ss.SSS')).to.be('2009-02-13 23:31:30.007');
  278. });
  279. });
  280. describe('updateSubmenuVisibility with empty lists', function() {
  281. var model;
  282. beforeEach(function() {
  283. model = new DashboardModel({});
  284. model.updateSubmenuVisibility();
  285. });
  286. it('should not enable submmenu', function() {
  287. expect(model.meta.submenuEnabled).to.be(false);
  288. });
  289. });
  290. describe('updateSubmenuVisibility with annotation', function() {
  291. var model;
  292. beforeEach(function() {
  293. model = new DashboardModel({
  294. annotations: {
  295. list: [{}]
  296. }
  297. });
  298. model.updateSubmenuVisibility();
  299. });
  300. it('should enable submmenu', function() {
  301. expect(model.meta.submenuEnabled).to.be(true);
  302. });
  303. });
  304. describe('updateSubmenuVisibility with template var', function() {
  305. var model;
  306. beforeEach(function() {
  307. model = new DashboardModel({
  308. templating: {
  309. list: [{}]
  310. }
  311. });
  312. model.updateSubmenuVisibility();
  313. });
  314. it('should enable submmenu', function() {
  315. expect(model.meta.submenuEnabled).to.be(true);
  316. });
  317. });
  318. describe('updateSubmenuVisibility with hidden template var', function() {
  319. var model;
  320. beforeEach(function() {
  321. model = new DashboardModel({
  322. templating: {
  323. list: [{hide: 2}]
  324. }
  325. });
  326. model.updateSubmenuVisibility();
  327. });
  328. it('should not enable submmenu', function() {
  329. expect(model.meta.submenuEnabled).to.be(false);
  330. });
  331. });
  332. describe('updateSubmenuVisibility with hidden annotation toggle', function() {
  333. var dashboard;
  334. beforeEach(function() {
  335. dashboard = new DashboardModel({
  336. annotations: {
  337. list: [{hide: true}]
  338. }
  339. });
  340. dashboard.updateSubmenuVisibility();
  341. });
  342. it('should not enable submmenu', function() {
  343. expect(dashboard.meta.submenuEnabled).to.be(false);
  344. });
  345. });
  346. describe('given dashboard with panel repeat in horizontal direction', function(ctx) {
  347. var dashboard;
  348. beforeEach(function() {
  349. dashboard = new DashboardModel({
  350. panels: [{id: 2, repeat: 'apps', repeatDirection: 'h', gridPos: {x: 0, y: 0, h: 2, w: 24}}],
  351. templating: {
  352. list: [{
  353. name: 'apps',
  354. current: {
  355. text: 'se1, se2, se3',
  356. value: ['se1', 'se2', 'se3']
  357. },
  358. options: [
  359. {text: 'se1', value: 'se1', selected: true},
  360. {text: 'se2', value: 'se2', selected: true},
  361. {text: 'se3', value: 'se3', selected: true},
  362. {text: 'se4', value: 'se4', selected: false}
  363. ]
  364. }]
  365. }
  366. });
  367. dashboard.processRepeats();
  368. });
  369. it('should repeat panel 3 times', function() {
  370. expect(dashboard.panels.length).to.be(3);
  371. });
  372. it('should mark panel repeated', function() {
  373. expect(dashboard.panels[0].repeat).to.be('apps');
  374. expect(dashboard.panels[1].repeatPanelId).to.be(2);
  375. });
  376. it('should set scopedVars on panels', function() {
  377. expect(dashboard.panels[0].scopedVars.apps.value).to.be('se1');
  378. expect(dashboard.panels[1].scopedVars.apps.value).to.be('se2');
  379. expect(dashboard.panels[2].scopedVars.apps.value).to.be('se3');
  380. });
  381. it('should place on first row and adjust width so all fit', function() {
  382. expect(dashboard.panels[0].gridPos).to.eql({x: 0, y: 0, h: 2, w: 8});
  383. expect(dashboard.panels[1].gridPos).to.eql({x: 8, y: 0, h: 2, w: 8});
  384. expect(dashboard.panels[2].gridPos).to.eql({x: 16, y: 0, h: 2, w: 8});
  385. });
  386. describe('After a second iteration', function() {
  387. var repeatedPanelAfterIteration1;
  388. beforeEach(function() {
  389. repeatedPanelAfterIteration1 = dashboard.panels[1];
  390. dashboard.panels[0].fill = 10;
  391. dashboard.processRepeats();
  392. });
  393. it('reused panel should copy properties from source', function() {
  394. expect(dashboard.panels[1].fill).to.be(10);
  395. });
  396. it('should have same panel count', function() {
  397. expect(dashboard.panels.length).to.be(3);
  398. });
  399. });
  400. describe('After a second iteration with different variable', function() {
  401. beforeEach(function() {
  402. dashboard.templating.list.push({
  403. name: 'server',
  404. current: { text: 'se1, se2, se3', value: ['se1']},
  405. options: [{text: 'se1', value: 'se1', selected: true}]
  406. });
  407. dashboard.panels[0].repeat = "server";
  408. dashboard.processRepeats();
  409. });
  410. it('should remove scopedVars value for last variable', function() {
  411. expect(dashboard.panels[0].scopedVars.apps).to.be(undefined);
  412. });
  413. it('should have new variable value in scopedVars', function() {
  414. expect(dashboard.panels[0].scopedVars.server.value).to.be("se1");
  415. });
  416. });
  417. describe('After a second iteration and selected values reduced', function() {
  418. beforeEach(function() {
  419. dashboard.templating.list[0].options[1].selected = false;
  420. dashboard.processRepeats();
  421. });
  422. it('should clean up repeated panel', function() {
  423. expect(dashboard.panels.length).to.be(2);
  424. });
  425. });
  426. describe('After a second iteration and panel repeat is turned off', function() {
  427. beforeEach(function() {
  428. dashboard.panels[0].repeat = null;
  429. dashboard.processRepeats();
  430. });
  431. it('should clean up repeated panel', function() {
  432. expect(dashboard.panels.length).to.be(1);
  433. });
  434. it('should remove scoped vars from reused panel', function() {
  435. expect(dashboard.panels[0].scopedVars).to.be(undefined);
  436. });
  437. });
  438. });
  439. describe('given dashboard with panel repeat in vertical direction', function(ctx) {
  440. var dashboard;
  441. beforeEach(function() {
  442. dashboard = new DashboardModel({
  443. panels: [{id: 2, repeat: 'apps', repeatDirection: 'v', gridPos: {x: 5, y: 0, h: 2, w: 8}}],
  444. templating: {
  445. list: [{
  446. name: 'apps',
  447. current: {
  448. text: 'se1, se2, se3',
  449. value: ['se1', 'se2', 'se3']
  450. },
  451. options: [
  452. {text: 'se1', value: 'se1', selected: true},
  453. {text: 'se2', value: 'se2', selected: true},
  454. {text: 'se3', value: 'se3', selected: true},
  455. {text: 'se4', value: 'se4', selected: false}
  456. ]
  457. }]
  458. }
  459. });
  460. dashboard.processRepeats();
  461. });
  462. it('should place on items on top of each other and keep witdh', function() {
  463. expect(dashboard.panels[0].gridPos).to.eql({x: 5, y: 0, h: 2, w: 8});
  464. expect(dashboard.panels[1].gridPos).to.eql({x: 5, y: 2, h: 2, w: 8});
  465. expect(dashboard.panels[2].gridPos).to.eql({x: 5, y: 4, h: 2, w: 8});
  466. });
  467. });
  468. });