dashboard_model_specs.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. import {describe, beforeEach, it, expect} from 'test/lib/common';
  2. import _ from 'lodash';
  3. import {DashboardModel} from '../model';
  4. describe('DashboardModel', function() {
  5. describe('when creating new dashboard model defaults only', function() {
  6. var model;
  7. beforeEach(function() {
  8. model = new DashboardModel({}, {});
  9. });
  10. it('should have title', function() {
  11. expect(model.title).to.be('No Title');
  12. });
  13. it('should have meta', function() {
  14. expect(model.meta.canSave).to.be(true);
  15. expect(model.meta.canShare).to.be(true);
  16. });
  17. it('should have default properties', function() {
  18. expect(model.rows.length).to.be(0);
  19. });
  20. });
  21. describe('when getting next panel id', function() {
  22. var model;
  23. beforeEach(function() {
  24. model = new DashboardModel({
  25. rows: [{ panels: [{ id: 5 }]}]
  26. });
  27. });
  28. it('should return max id + 1', function() {
  29. expect(model.getNextPanelId()).to.be(6);
  30. });
  31. });
  32. describe('getSaveModelClone', function() {
  33. it('should sort keys', () => {
  34. var model = new DashboardModel({});
  35. var saveModel = model.getSaveModelClone();
  36. var keys = _.keys(saveModel);
  37. expect(keys[0]).to.be('addEmptyRow');
  38. expect(keys[1]).to.be('addPanel');
  39. });
  40. });
  41. describe('row and panel manipulation', function() {
  42. var dashboard;
  43. beforeEach(function() {
  44. dashboard = new DashboardModel({});
  45. });
  46. it('adding default should split span in half', function() {
  47. dashboard.addEmptyRow();
  48. dashboard.rows[0].addPanel({span: 12});
  49. dashboard.rows[0].addPanel({span: 12});
  50. expect(dashboard.rows[0].panels[0].span).to.be(6);
  51. expect(dashboard.rows[0].panels[1].span).to.be(6);
  52. });
  53. it('duplicate panel should try to add it to same row', function() {
  54. var panel = { span: 4, attr: '123', id: 10 };
  55. dashboard.addEmptyRow();
  56. dashboard.rows[0].addPanel(panel);
  57. dashboard.duplicatePanel(panel, dashboard.rows[0]);
  58. expect(dashboard.rows[0].panels[0].span).to.be(4);
  59. expect(dashboard.rows[0].panels[1].span).to.be(4);
  60. expect(dashboard.rows[0].panels[1].attr).to.be('123');
  61. expect(dashboard.rows[0].panels[1].id).to.be(11);
  62. });
  63. it('duplicate panel should remove repeat data', function() {
  64. var panel = { span: 4, attr: '123', id: 10, repeat: 'asd', scopedVars: { test: 'asd' }};
  65. dashboard.addEmptyRow();
  66. dashboard.rows[0].addPanel(panel);
  67. dashboard.duplicatePanel(panel, dashboard.rows[0]);
  68. expect(dashboard.rows[0].panels[1].repeat).to.be(undefined);
  69. expect(dashboard.rows[0].panels[1].scopedVars).to.be(undefined);
  70. });
  71. });
  72. describe('when creating dashboard with old schema', function() {
  73. var model;
  74. var graph;
  75. var singlestat;
  76. var table;
  77. beforeEach(function() {
  78. model = new DashboardModel({
  79. services: { filter: { time: { from: 'now-1d', to: 'now'}, list: [{}] }},
  80. pulldowns: [
  81. {type: 'filtering', enable: true},
  82. {type: 'annotations', enable: true, annotations: [{name: 'old'}]}
  83. ],
  84. rows: [
  85. {
  86. panels: [
  87. {
  88. type: 'graph', legend: true, aliasYAxis: { test: 2 },
  89. y_formats: ['kbyte', 'ms'],
  90. grid: {
  91. min: 1,
  92. max: 10,
  93. rightMin: 5,
  94. rightMax: 15,
  95. leftLogBase: 1,
  96. rightLogBase: 2,
  97. threshold1: 200,
  98. threshold2: 400,
  99. threshold1Color: 'yellow',
  100. threshold2Color: 'red',
  101. },
  102. leftYAxisLabel: 'left label',
  103. targets: [{refId: 'A'}, {}],
  104. },
  105. {
  106. type: 'singlestat', legend: true, thresholds: '10,20,30', aliasYAxis: { test: 2 }, grid: { min: 1, max: 10 },
  107. targets: [{refId: 'A'}, {}],
  108. },
  109. {
  110. type: 'table', legend: true, styles: [{ thresholds: ["10", "20", "30"]}, { thresholds: ["100", "200", "300"]}],
  111. targets: [{refId: 'A'}, {}],
  112. }
  113. ]
  114. }
  115. ]
  116. });
  117. graph = model.rows[0].panels[0];
  118. singlestat = model.rows[0].panels[1];
  119. table = model.rows[0].panels[2];
  120. });
  121. it('should have title', function() {
  122. expect(model.title).to.be('No Title');
  123. });
  124. it('should have panel id', function() {
  125. expect(graph.id).to.be(1);
  126. });
  127. it('should move time and filtering list', function() {
  128. expect(model.time.from).to.be('now-1d');
  129. expect(model.templating.list[0].allFormat).to.be('glob');
  130. });
  131. it('graphite panel should change name too graph', function() {
  132. expect(graph.type).to.be('graph');
  133. });
  134. it('single stat panel should have two thresholds', function() {
  135. expect(singlestat.thresholds).to.be('20,30');
  136. });
  137. it('queries without refId should get it', function() {
  138. expect(graph.targets[1].refId).to.be('B');
  139. });
  140. it('update legend setting', function() {
  141. expect(graph.legend.show).to.be(true);
  142. });
  143. it('move aliasYAxis to series override', function() {
  144. expect(graph.seriesOverrides[0].alias).to.be("test");
  145. expect(graph.seriesOverrides[0].yaxis).to.be(2);
  146. });
  147. it('should move pulldowns to new schema', function() {
  148. expect(model.annotations.list[0].name).to.be('old');
  149. });
  150. it('table panel should only have two thresholds values', function() {
  151. expect(table.styles[0].thresholds[0]).to.be("20");
  152. expect(table.styles[0].thresholds[1]).to.be("30");
  153. expect(table.styles[1].thresholds[0]).to.be("200");
  154. expect(table.styles[1].thresholds[1]).to.be("300");
  155. });
  156. it('graph grid to yaxes options', function() {
  157. expect(graph.yaxes[0].min).to.be(1);
  158. expect(graph.yaxes[0].max).to.be(10);
  159. expect(graph.yaxes[0].format).to.be('kbyte');
  160. expect(graph.yaxes[0].label).to.be('left label');
  161. expect(graph.yaxes[0].logBase).to.be(1);
  162. expect(graph.yaxes[1].min).to.be(5);
  163. expect(graph.yaxes[1].max).to.be(15);
  164. expect(graph.yaxes[1].format).to.be('ms');
  165. expect(graph.yaxes[1].logBase).to.be(2);
  166. expect(graph.grid.rightMax).to.be(undefined);
  167. expect(graph.grid.rightLogBase).to.be(undefined);
  168. expect(graph.y_formats).to.be(undefined);
  169. });
  170. it('dashboard schema version should be set to latest', function() {
  171. expect(model.schemaVersion).to.be(14);
  172. });
  173. it('graph thresholds should be migrated', function() {
  174. expect(graph.thresholds.length).to.be(2);
  175. expect(graph.thresholds[0].op).to.be('gt');
  176. expect(graph.thresholds[0].value).to.be(200);
  177. expect(graph.thresholds[0].fillColor).to.be('yellow');
  178. expect(graph.thresholds[1].value).to.be(400);
  179. expect(graph.thresholds[1].fillColor).to.be('red');
  180. });
  181. });
  182. describe('when creating dashboard model with missing list for annoations or templating', function() {
  183. var model;
  184. beforeEach(function() {
  185. model = new DashboardModel({
  186. annotations: {
  187. enable: true,
  188. },
  189. templating: {
  190. enable: true
  191. }
  192. });
  193. });
  194. it('should add empty list', function() {
  195. expect(model.annotations.list.length).to.be(0);
  196. expect(model.templating.list.length).to.be(0);
  197. });
  198. });
  199. describe('Given editable false dashboard', function() {
  200. var model;
  201. beforeEach(function() {
  202. model = new DashboardModel({editable: false});
  203. });
  204. it('Should set meta canEdit and canSave to false', function() {
  205. expect(model.meta.canSave).to.be(false);
  206. expect(model.meta.canEdit).to.be(false);
  207. });
  208. it('getSaveModelClone should remove meta', function() {
  209. var clone = model.getSaveModelClone();
  210. expect(clone.meta).to.be(undefined);
  211. });
  212. });
  213. describe('when loading dashboard with old influxdb query schema', function() {
  214. var model;
  215. var target;
  216. beforeEach(function() {
  217. model = new DashboardModel({
  218. rows: [{
  219. panels: [{
  220. type: 'graph',
  221. grid: {},
  222. yaxes: [{}, {}],
  223. targets: [{
  224. "alias": "$tag_datacenter $tag_source $col",
  225. "column": "value",
  226. "measurement": "logins.count",
  227. "fields": [
  228. {
  229. "func": "mean",
  230. "name": "value",
  231. "mathExpr": "*2",
  232. "asExpr": "value"
  233. },
  234. {
  235. "name": "one-minute",
  236. "func": "mean",
  237. "mathExpr": "*3",
  238. "asExpr": "one-minute"
  239. }
  240. ],
  241. "tags": [],
  242. "fill": "previous",
  243. "function": "mean",
  244. "groupBy": [
  245. {
  246. "interval": "auto",
  247. "type": "time"
  248. },
  249. {
  250. "key": "source",
  251. "type": "tag"
  252. },
  253. {
  254. "type": "tag",
  255. "key": "datacenter"
  256. }
  257. ],
  258. }]
  259. }]
  260. }]
  261. });
  262. target = model.rows[0].panels[0].targets[0];
  263. });
  264. it('should update query schema', function() {
  265. expect(target.fields).to.be(undefined);
  266. expect(target.select.length).to.be(2);
  267. expect(target.select[0].length).to.be(4);
  268. expect(target.select[0][0].type).to.be('field');
  269. expect(target.select[0][1].type).to.be('mean');
  270. expect(target.select[0][2].type).to.be('math');
  271. expect(target.select[0][3].type).to.be('alias');
  272. });
  273. });
  274. describe('when creating dashboard model with missing list for annoations or templating', function() {
  275. var model;
  276. beforeEach(function() {
  277. model = new DashboardModel({
  278. annotations: {
  279. enable: true,
  280. },
  281. templating: {
  282. enable: true
  283. }
  284. });
  285. });
  286. it('should add empty list', function() {
  287. expect(model.annotations.list.length).to.be(0);
  288. expect(model.templating.list.length).to.be(0);
  289. });
  290. });
  291. describe('Formatting epoch timestamp when timezone is set as utc', function() {
  292. var dashboard;
  293. beforeEach(function() {
  294. dashboard = new DashboardModel({timezone: 'utc'});
  295. });
  296. it('Should format timestamp with second resolution by default', function() {
  297. expect(dashboard.formatDate(1234567890000)).to.be('2009-02-13 23:31:30');
  298. });
  299. it('Should format timestamp with second resolution even if second format is passed as parameter', function() {
  300. expect(dashboard.formatDate(1234567890007,'YYYY-MM-DD HH:mm:ss')).to.be('2009-02-13 23:31:30');
  301. });
  302. it('Should format timestamp with millisecond resolution if format is passed as parameter', function() {
  303. expect(dashboard.formatDate(1234567890007,'YYYY-MM-DD HH:mm:ss.SSS')).to.be('2009-02-13 23:31:30.007');
  304. });
  305. });
  306. describe('updateSubmenuVisibility with empty lists', function() {
  307. var model;
  308. beforeEach(function() {
  309. model = new DashboardModel({});
  310. model.updateSubmenuVisibility();
  311. });
  312. it('should not enable submmenu', function() {
  313. expect(model.meta.submenuEnabled).to.be(false);
  314. });
  315. });
  316. describe('updateSubmenuVisibility with annotation', function() {
  317. var model;
  318. beforeEach(function() {
  319. model = new DashboardModel({
  320. annotations: {
  321. list: [{}]
  322. }
  323. });
  324. model.updateSubmenuVisibility();
  325. });
  326. it('should enable submmenu', function() {
  327. expect(model.meta.submenuEnabled).to.be(true);
  328. });
  329. });
  330. describe('updateSubmenuVisibility with template var', function() {
  331. var model;
  332. beforeEach(function() {
  333. model = new DashboardModel({
  334. templating: {
  335. list: [{}]
  336. }
  337. });
  338. model.updateSubmenuVisibility();
  339. });
  340. it('should enable submmenu', function() {
  341. expect(model.meta.submenuEnabled).to.be(true);
  342. });
  343. });
  344. describe('updateSubmenuVisibility with hidden template var', function() {
  345. var model;
  346. beforeEach(function() {
  347. model = new DashboardModel({
  348. templating: {
  349. list: [{hide: 2}]
  350. }
  351. });
  352. model.updateSubmenuVisibility();
  353. });
  354. it('should not enable submmenu', function() {
  355. expect(model.meta.submenuEnabled).to.be(false);
  356. });
  357. });
  358. describe('updateSubmenuVisibility with hidden annotation toggle', function() {
  359. var model;
  360. beforeEach(function() {
  361. model = new DashboardModel({
  362. annotations: {
  363. list: [{hide: true}]
  364. }
  365. });
  366. model.updateSubmenuVisibility();
  367. });
  368. it('should not enable submmenu', function() {
  369. expect(model.meta.submenuEnabled).to.be(false);
  370. });
  371. });
  372. });