dashboard_srv_specs.ts 11 KB

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