dashboard_srv_specs.ts 11 KB

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