dashboardSrv-specs.js 12 KB

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