singlestat.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import { SingleStatCtrl } from '../module';
  2. import { dateTime } from '@grafana/ui/src/utils/moment_wrapper';
  3. describe('SingleStatCtrl', () => {
  4. const ctx = {} as any;
  5. const epoch = 1505826363746;
  6. Date.now = () => epoch;
  7. const $scope = {
  8. $on: () => {},
  9. };
  10. const $injector = {
  11. get: () => {},
  12. };
  13. const $sanitize = {};
  14. SingleStatCtrl.prototype.panel = {
  15. events: {
  16. on: () => {},
  17. emit: () => {},
  18. },
  19. };
  20. SingleStatCtrl.prototype.dashboard = {
  21. isTimezoneUtc: jest.fn(() => true),
  22. };
  23. SingleStatCtrl.prototype.events = {
  24. on: () => {},
  25. };
  26. function singleStatScenario(desc, func) {
  27. describe(desc, () => {
  28. ctx.setup = setupFunc => {
  29. beforeEach(() => {
  30. ctx.ctrl = new SingleStatCtrl($scope, $injector, {}, $sanitize);
  31. setupFunc();
  32. ctx.ctrl.onDataReceived(ctx.data);
  33. ctx.data = ctx.ctrl.data;
  34. });
  35. };
  36. func(ctx);
  37. });
  38. }
  39. singleStatScenario('with defaults', ctx => {
  40. ctx.setup(() => {
  41. ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 1], [20, 2]] }];
  42. });
  43. it('Should use series avg as default main value', () => {
  44. expect(ctx.data.value).toBe(15);
  45. expect(ctx.data.valueRounded).toBe(15);
  46. });
  47. it('should set formatted falue', () => {
  48. expect(ctx.data.valueFormatted).toBe('15');
  49. });
  50. });
  51. singleStatScenario('showing serie name instead of value', ctx => {
  52. ctx.setup(() => {
  53. ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 1], [20, 2]] }];
  54. ctx.ctrl.panel.valueName = 'name';
  55. });
  56. it('Should use series avg as default main value', () => {
  57. expect(ctx.data.value).toBe(0);
  58. expect(ctx.data.valueRounded).toBe(0);
  59. });
  60. it('should set formatted value', () => {
  61. expect(ctx.data.valueFormatted).toBe('test.cpu1');
  62. });
  63. });
  64. singleStatScenario('showing last iso time instead of value', ctx => {
  65. ctx.setup(() => {
  66. ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }];
  67. ctx.ctrl.panel.valueName = 'last_time';
  68. ctx.ctrl.panel.format = 'dateTimeAsIso';
  69. ctx.ctrl.dashboard.isTimezoneUtc = () => false;
  70. });
  71. it('Should use time instead of value', () => {
  72. expect(ctx.data.value).toBe(1505634997920);
  73. expect(ctx.data.valueRounded).toBe(1505634997920);
  74. });
  75. it('should set formatted value', () => {
  76. expect(dateTime(ctx.data.valueFormatted).valueOf()).toBe(1505634997000);
  77. });
  78. });
  79. singleStatScenario('showing last iso time instead of value (in UTC)', ctx => {
  80. ctx.setup(() => {
  81. ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 5000]] }];
  82. ctx.ctrl.panel.valueName = 'last_time';
  83. ctx.ctrl.panel.format = 'dateTimeAsIso';
  84. ctx.ctrl.dashboard.isTimezoneUtc = () => true;
  85. });
  86. it('should set value', () => {
  87. expect(ctx.data.valueFormatted).toBe('1970-01-01 00:00:05');
  88. });
  89. });
  90. singleStatScenario('showing last us time instead of value', ctx => {
  91. ctx.setup(() => {
  92. ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }];
  93. ctx.ctrl.panel.valueName = 'last_time';
  94. ctx.ctrl.panel.format = 'dateTimeAsUS';
  95. ctx.ctrl.dashboard.isTimezoneUtc = () => false;
  96. });
  97. it('Should use time instead of value', () => {
  98. expect(ctx.data.value).toBe(1505634997920);
  99. expect(ctx.data.valueRounded).toBe(1505634997920);
  100. });
  101. it('should set formatted value', () => {
  102. expect(ctx.data.valueFormatted).toBe(dateTime(1505634997920).format('MM/DD/YYYY h:mm:ss a'));
  103. });
  104. });
  105. singleStatScenario('showing last us time instead of value (in UTC)', ctx => {
  106. ctx.setup(() => {
  107. ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 5000]] }];
  108. ctx.ctrl.panel.valueName = 'last_time';
  109. ctx.ctrl.panel.format = 'dateTimeAsUS';
  110. ctx.ctrl.dashboard.isTimezoneUtc = () => true;
  111. });
  112. it('should set formatted value', () => {
  113. expect(ctx.data.valueFormatted).toBe('01/01/1970 12:00:05 am');
  114. });
  115. });
  116. singleStatScenario('showing last time from now instead of value', ctx => {
  117. ctx.setup(() => {
  118. ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }];
  119. ctx.ctrl.panel.valueName = 'last_time';
  120. ctx.ctrl.panel.format = 'dateTimeFromNow';
  121. });
  122. it('Should use time instead of value', () => {
  123. expect(ctx.data.value).toBe(1505634997920);
  124. expect(ctx.data.valueRounded).toBe(1505634997920);
  125. });
  126. it('should set formatted value', () => {
  127. expect(ctx.data.valueFormatted).toBe('2 days ago');
  128. });
  129. });
  130. singleStatScenario('showing last time from now instead of value (in UTC)', ctx => {
  131. ctx.setup(() => {
  132. ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }];
  133. ctx.ctrl.panel.valueName = 'last_time';
  134. ctx.ctrl.panel.format = 'dateTimeFromNow';
  135. });
  136. it('should set formatted value', () => {
  137. expect(ctx.data.valueFormatted).toBe('2 days ago');
  138. });
  139. });
  140. singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', ctx => {
  141. ctx.setup(() => {
  142. ctx.data = [{ target: 'test.cpu1', datapoints: [[99.999, 1], [99.99999, 2]] }];
  143. ctx.ctrl.panel.valueName = 'avg';
  144. ctx.ctrl.panel.format = 'none';
  145. });
  146. it('Should be rounded', () => {
  147. expect(ctx.data.value).toBe(99.999495);
  148. expect(ctx.data.valueRounded).toBe(100);
  149. });
  150. it('should set formatted value', () => {
  151. expect(ctx.data.valueFormatted).toBe('100');
  152. });
  153. });
  154. singleStatScenario('When value to text mapping is specified', ctx => {
  155. ctx.setup(() => {
  156. ctx.data = [{ target: 'test.cpu1', datapoints: [[9.9, 1]] }];
  157. ctx.ctrl.panel.valueMaps = [{ value: '10', text: 'OK' }];
  158. });
  159. it('value should remain', () => {
  160. expect(ctx.data.value).toBe(9.9);
  161. });
  162. it('round should be rounded up', () => {
  163. expect(ctx.data.valueRounded).toBe(10);
  164. });
  165. it('Should replace value with text', () => {
  166. expect(ctx.data.valueFormatted).toBe('OK');
  167. });
  168. });
  169. singleStatScenario('When range to text mapping is specified for first range', ctx => {
  170. ctx.setup(() => {
  171. ctx.data = [{ target: 'test.cpu1', datapoints: [[41, 50]] }];
  172. ctx.ctrl.panel.mappingType = 2;
  173. ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
  174. });
  175. it('Should replace value with text OK', () => {
  176. expect(ctx.data.valueFormatted).toBe('OK');
  177. });
  178. });
  179. singleStatScenario('When range to text mapping is specified for other ranges', ctx => {
  180. ctx.setup(() => {
  181. ctx.data = [{ target: 'test.cpu1', datapoints: [[65, 75]] }];
  182. ctx.ctrl.panel.mappingType = 2;
  183. ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
  184. });
  185. it('Should replace value with text NOT OK', () => {
  186. expect(ctx.data.valueFormatted).toBe('NOT OK');
  187. });
  188. });
  189. describe('When table data', () => {
  190. const tableData = [
  191. {
  192. columns: [{ text: 'Time', type: 'time' }, { text: 'test1' }, { text: 'mean' }, { text: 'test2' }],
  193. rows: [[1492759673649, 'ignore1', 15, 'ignore2']],
  194. type: 'table',
  195. },
  196. ];
  197. singleStatScenario('with default values', ctx => {
  198. ctx.setup(() => {
  199. ctx.data = tableData;
  200. ctx.ctrl.panel = {
  201. emit: () => {},
  202. };
  203. ctx.ctrl.panel.tableColumn = 'mean';
  204. ctx.ctrl.panel.format = 'none';
  205. });
  206. it('Should use first rows value as default main value', () => {
  207. expect(ctx.data.value).toBe(15);
  208. expect(ctx.data.valueRounded).toBe(15);
  209. });
  210. it('should set formatted value', () => {
  211. expect(ctx.data.valueFormatted).toBe('15');
  212. });
  213. });
  214. singleStatScenario('When table data has multiple columns', ctx => {
  215. ctx.setup(() => {
  216. ctx.data = tableData;
  217. ctx.ctrl.panel.tableColumn = '';
  218. });
  219. it('Should set column to first column that is not time', () => {
  220. expect(ctx.ctrl.panel.tableColumn).toBe('test1');
  221. });
  222. });
  223. singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', ctx => {
  224. ctx.setup(() => {
  225. ctx.data = tableData;
  226. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 99.99999, 'ignore2'];
  227. ctx.ctrl.panel.mappingType = 0;
  228. ctx.ctrl.panel.tableColumn = 'mean';
  229. });
  230. it('Should be rounded', () => {
  231. expect(ctx.data.value).toBe(99.99999);
  232. expect(ctx.data.valueRounded).toBe(100);
  233. });
  234. it('should set formatted falue', () => {
  235. expect(ctx.data.valueFormatted).toBe('100');
  236. });
  237. });
  238. singleStatScenario('When value to text mapping is specified', ctx => {
  239. ctx.setup(() => {
  240. ctx.data = tableData;
  241. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 9.9, 'ignore2'];
  242. ctx.ctrl.panel.mappingType = 2;
  243. ctx.ctrl.panel.tableColumn = 'mean';
  244. ctx.ctrl.panel.valueMaps = [{ value: '10', text: 'OK' }];
  245. });
  246. it('value should remain', () => {
  247. expect(ctx.data.value).toBe(9.9);
  248. });
  249. it('round should be rounded up', () => {
  250. expect(ctx.data.valueRounded).toBe(10);
  251. });
  252. it('Should replace value with text', () => {
  253. expect(ctx.data.valueFormatted).toBe('OK');
  254. });
  255. });
  256. singleStatScenario('When range to text mapping is specified for first range', ctx => {
  257. ctx.setup(() => {
  258. ctx.data = tableData;
  259. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 41, 'ignore2'];
  260. ctx.ctrl.panel.tableColumn = 'mean';
  261. ctx.ctrl.panel.mappingType = 2;
  262. ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
  263. });
  264. it('Should replace value with text OK', () => {
  265. expect(ctx.data.valueFormatted).toBe('OK');
  266. });
  267. });
  268. singleStatScenario('When range to text mapping is specified for other ranges', ctx => {
  269. ctx.setup(() => {
  270. ctx.data = tableData;
  271. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 65, 'ignore2'];
  272. ctx.ctrl.panel.tableColumn = 'mean';
  273. ctx.ctrl.panel.mappingType = 2;
  274. ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
  275. });
  276. it('Should replace value with text NOT OK', () => {
  277. expect(ctx.data.valueFormatted).toBe('NOT OK');
  278. });
  279. });
  280. singleStatScenario('When value is string', ctx => {
  281. ctx.setup(() => {
  282. ctx.data = tableData;
  283. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 65, 'ignore2'];
  284. ctx.ctrl.panel.tableColumn = 'test1';
  285. });
  286. it('Should replace value with text NOT OK', () => {
  287. expect(ctx.data.valueFormatted).toBe('ignore1');
  288. });
  289. });
  290. singleStatScenario('When value is zero', ctx => {
  291. ctx.setup(() => {
  292. ctx.data = tableData;
  293. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 0, 'ignore2'];
  294. ctx.ctrl.panel.tableColumn = 'mean';
  295. });
  296. it('Should return zero', () => {
  297. expect(ctx.data.value).toBe(0);
  298. });
  299. });
  300. });
  301. });