dashboardSrv-specs.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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: {min: 1, max: 10, rightMin: 5, rightMax: 15, leftLogBase: 1, rightLogBase: 2},
  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(12);
  175. });
  176. });
  177. describe('when creating dashboard model with missing list for annoations or templating', function() {
  178. var model;
  179. beforeEach(function() {
  180. model = _dashboardSrv.create({
  181. annotations: {
  182. enable: true,
  183. },
  184. templating: {
  185. enable: true
  186. }
  187. });
  188. });
  189. it('should add empty list', function() {
  190. expect(model.annotations.list.length).to.be(0);
  191. expect(model.templating.list.length).to.be(0);
  192. });
  193. });
  194. describe('Given editable false dashboard', function() {
  195. var model;
  196. beforeEach(function() {
  197. model = _dashboardSrv.create({
  198. editable: false,
  199. });
  200. });
  201. it('Should set meta canEdit and canSave to false', function() {
  202. expect(model.meta.canSave).to.be(false);
  203. expect(model.meta.canEdit).to.be(false);
  204. });
  205. it('getSaveModelClone should remove meta', function() {
  206. var clone = model.getSaveModelClone();
  207. expect(clone.meta).to.be(undefined);
  208. });
  209. });
  210. describe('when loading dashboard with old influxdb query schema', function() {
  211. var model;
  212. var target;
  213. beforeEach(function() {
  214. model = _dashboardSrv.create({
  215. rows: [{
  216. panels: [{
  217. type: 'graph',
  218. grid: {},
  219. yaxes: [{}, {}],
  220. targets: [{
  221. "alias": "$tag_datacenter $tag_source $col",
  222. "column": "value",
  223. "measurement": "logins.count",
  224. "fields": [
  225. {
  226. "func": "mean",
  227. "name": "value",
  228. "mathExpr": "*2",
  229. "asExpr": "value"
  230. },
  231. {
  232. "name": "one-minute",
  233. "func": "mean",
  234. "mathExpr": "*3",
  235. "asExpr": "one-minute"
  236. }
  237. ],
  238. "tags": [],
  239. "fill": "previous",
  240. "function": "mean",
  241. "groupBy": [
  242. {
  243. "interval": "auto",
  244. "type": "time"
  245. },
  246. {
  247. "key": "source",
  248. "type": "tag"
  249. },
  250. {
  251. "type": "tag",
  252. "key": "datacenter"
  253. }
  254. ],
  255. }]
  256. }]
  257. }]
  258. });
  259. target = model.rows[0].panels[0].targets[0];
  260. });
  261. it('should update query schema', function() {
  262. expect(target.fields).to.be(undefined);
  263. expect(target.select.length).to.be(2);
  264. expect(target.select[0].length).to.be(4);
  265. expect(target.select[0][0].type).to.be('field');
  266. expect(target.select[0][1].type).to.be('mean');
  267. expect(target.select[0][2].type).to.be('math');
  268. expect(target.select[0][3].type).to.be('alias');
  269. });
  270. });
  271. describe('when creating dashboard model with missing list for annoations or templating', function() {
  272. var model;
  273. beforeEach(function() {
  274. model = _dashboardSrv.create({
  275. annotations: {
  276. enable: true,
  277. },
  278. templating: {
  279. enable: true
  280. }
  281. });
  282. });
  283. it('should add empty list', function() {
  284. expect(model.annotations.list.length).to.be(0);
  285. expect(model.templating.list.length).to.be(0);
  286. });
  287. });
  288. describe('Formatting epoch timestamp when timezone is set as utc', function() {
  289. var dashboard;
  290. beforeEach(function() {
  291. dashboard = _dashboardSrv.create({
  292. timezone: 'utc',
  293. });
  294. });
  295. it('Should format timestamp with second resolution by default', function() {
  296. expect(dashboard.formatDate(1234567890000)).to.be('2009-02-13 23:31:30');
  297. });
  298. it('Should format timestamp with second resolution even if second format is passed as parameter', function() {
  299. expect(dashboard.formatDate(1234567890007,'YYYY-MM-DD HH:mm:ss')).to.be('2009-02-13 23:31:30');
  300. });
  301. it('Should format timestamp with millisecond resolution if format is passed as parameter', function() {
  302. expect(dashboard.formatDate(1234567890007,'YYYY-MM-DD HH:mm:ss.SSS')).to.be('2009-02-13 23:31:30.007');
  303. });
  304. });
  305. });
  306. });