dashboard_model_specs.ts 12 KB

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