singlestat.test.ts 11 KB

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