dashboard_srv_specs.ts 11 KB

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