singlestat.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import { SingleStatCtrl } from '../module';
  2. import moment from 'moment';
  3. describe('SingleStatCtrl', function() {
  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, function() {
  27. ctx.setup = function(setupFunc) {
  28. beforeEach(function() {
  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', function(ctx) {
  39. ctx.setup(function() {
  40. ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 1], [20, 2]] }];
  41. });
  42. it('Should use series avg as default main value', function() {
  43. expect(ctx.data.value).toBe(15);
  44. expect(ctx.data.valueRounded).toBe(15);
  45. });
  46. it('should set formatted falue', function() {
  47. expect(ctx.data.valueFormatted).toBe('15');
  48. });
  49. });
  50. singleStatScenario('showing serie name instead of value', function(ctx) {
  51. ctx.setup(function() {
  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', function() {
  56. expect(ctx.data.value).toBe(0);
  57. expect(ctx.data.valueRounded).toBe(0);
  58. });
  59. it('should set formatted value', function() {
  60. expect(ctx.data.valueFormatted).toBe('test.cpu1');
  61. });
  62. });
  63. singleStatScenario('showing last iso time instead of value', function(ctx) {
  64. ctx.setup(function() {
  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', function() {
  71. expect(ctx.data.value).toBe(1505634997920);
  72. expect(ctx.data.valueRounded).toBe(1505634997920);
  73. });
  74. it('should set formatted value', function() {
  75. expect(moment(ctx.data.valueFormatted).valueOf()).toBe(1505634997000);
  76. });
  77. });
  78. singleStatScenario('showing last iso time instead of value (in UTC)', function(ctx) {
  79. ctx.setup(function() {
  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', function() {
  86. expect(ctx.data.valueFormatted).toBe('1970-01-01 00:00:05');
  87. });
  88. });
  89. singleStatScenario('showing last us time instead of value', function(ctx) {
  90. ctx.setup(function() {
  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', function() {
  97. expect(ctx.data.value).toBe(1505634997920);
  98. expect(ctx.data.valueRounded).toBe(1505634997920);
  99. });
  100. it('should set formatted value', function() {
  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)', function(ctx) {
  105. ctx.setup(function() {
  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', function() {
  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', function(ctx) {
  116. ctx.setup(function() {
  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', function() {
  122. expect(ctx.data.value).toBe(1505634997920);
  123. expect(ctx.data.valueRounded).toBe(1505634997920);
  124. });
  125. it('should set formatted value', function() {
  126. expect(ctx.data.valueFormatted).toBe('2 days ago');
  127. });
  128. });
  129. singleStatScenario('showing last time from now instead of value (in UTC)', function(ctx) {
  130. ctx.setup(function() {
  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', function() {
  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', function(
  140. ctx
  141. ) {
  142. ctx.setup(function() {
  143. ctx.data = [{ target: 'test.cpu1', datapoints: [[99.999, 1], [99.99999, 2]] }];
  144. ctx.ctrl.panel.valueName = 'avg';
  145. ctx.ctrl.panel.format = 'none';
  146. });
  147. it('Should be rounded', function() {
  148. expect(ctx.data.value).toBe(99.999495);
  149. expect(ctx.data.valueRounded).toBe(100);
  150. });
  151. it('should set formatted value', function() {
  152. expect(ctx.data.valueFormatted).toBe('100');
  153. });
  154. });
  155. singleStatScenario('When value to text mapping is specified', function(ctx) {
  156. ctx.setup(function() {
  157. ctx.data = [{ target: 'test.cpu1', datapoints: [[9.9, 1]] }];
  158. ctx.ctrl.panel.valueMaps = [{ value: '10', text: 'OK' }];
  159. });
  160. it('value should remain', function() {
  161. expect(ctx.data.value).toBe(9.9);
  162. });
  163. it('round should be rounded up', function() {
  164. expect(ctx.data.valueRounded).toBe(10);
  165. });
  166. it('Should replace value with text', function() {
  167. expect(ctx.data.valueFormatted).toBe('OK');
  168. });
  169. });
  170. singleStatScenario('When range to text mapping is specified for first range', function(ctx) {
  171. ctx.setup(function() {
  172. ctx.data = [{ target: 'test.cpu1', datapoints: [[41, 50]] }];
  173. ctx.ctrl.panel.mappingType = 2;
  174. ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
  175. });
  176. it('Should replace value with text OK', function() {
  177. expect(ctx.data.valueFormatted).toBe('OK');
  178. });
  179. });
  180. singleStatScenario('When range to text mapping is specified for other ranges', function(ctx) {
  181. ctx.setup(function() {
  182. ctx.data = [{ target: 'test.cpu1', datapoints: [[65, 75]] }];
  183. ctx.ctrl.panel.mappingType = 2;
  184. ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
  185. });
  186. it('Should replace value with text NOT OK', function() {
  187. expect(ctx.data.valueFormatted).toBe('NOT OK');
  188. });
  189. });
  190. describe('When table data', function() {
  191. const tableData = [
  192. {
  193. columns: [{ text: 'Time', type: 'time' }, { text: 'test1' }, { text: 'mean' }, { text: 'test2' }],
  194. rows: [[1492759673649, 'ignore1', 15, 'ignore2']],
  195. type: 'table',
  196. },
  197. ];
  198. singleStatScenario('with default values', function(ctx) {
  199. ctx.setup(function() {
  200. ctx.data = tableData;
  201. ctx.ctrl.panel = {
  202. emit: () => {},
  203. };
  204. ctx.ctrl.panel.tableColumn = 'mean';
  205. ctx.ctrl.panel.format = 'none';
  206. });
  207. it('Should use first rows value as default main value', function() {
  208. expect(ctx.data.value).toBe(15);
  209. expect(ctx.data.valueRounded).toBe(15);
  210. });
  211. it('should set formatted value', function() {
  212. expect(ctx.data.valueFormatted).toBe('15');
  213. });
  214. });
  215. singleStatScenario('When table data has multiple columns', function(ctx) {
  216. ctx.setup(function() {
  217. ctx.data = tableData;
  218. ctx.ctrl.panel.tableColumn = '';
  219. });
  220. it('Should set column to first column that is not time', function() {
  221. expect(ctx.ctrl.panel.tableColumn).toBe('test1');
  222. });
  223. });
  224. singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function(
  225. ctx
  226. ) {
  227. ctx.setup(function() {
  228. ctx.data = tableData;
  229. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 99.99999, 'ignore2'];
  230. ctx.ctrl.panel.mappingType = 0;
  231. ctx.ctrl.panel.tableColumn = 'mean';
  232. });
  233. it('Should be rounded', function() {
  234. expect(ctx.data.value).toBe(99.99999);
  235. expect(ctx.data.valueRounded).toBe(100);
  236. });
  237. it('should set formatted falue', function() {
  238. expect(ctx.data.valueFormatted).toBe('100');
  239. });
  240. });
  241. singleStatScenario('When value to text mapping is specified', function(ctx) {
  242. ctx.setup(function() {
  243. ctx.data = tableData;
  244. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 9.9, 'ignore2'];
  245. ctx.ctrl.panel.mappingType = 2;
  246. ctx.ctrl.panel.tableColumn = 'mean';
  247. ctx.ctrl.panel.valueMaps = [{ value: '10', text: 'OK' }];
  248. });
  249. it('value should remain', function() {
  250. expect(ctx.data.value).toBe(9.9);
  251. });
  252. it('round should be rounded up', function() {
  253. expect(ctx.data.valueRounded).toBe(10);
  254. });
  255. it('Should replace value with text', function() {
  256. expect(ctx.data.valueFormatted).toBe('OK');
  257. });
  258. });
  259. singleStatScenario('When range to text mapping is specified for first range', function(ctx) {
  260. ctx.setup(function() {
  261. ctx.data = tableData;
  262. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 41, 'ignore2'];
  263. ctx.ctrl.panel.tableColumn = 'mean';
  264. ctx.ctrl.panel.mappingType = 2;
  265. ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
  266. });
  267. it('Should replace value with text OK', function() {
  268. expect(ctx.data.valueFormatted).toBe('OK');
  269. });
  270. });
  271. singleStatScenario('When range to text mapping is specified for other ranges', function(ctx) {
  272. ctx.setup(function() {
  273. ctx.data = tableData;
  274. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 65, 'ignore2'];
  275. ctx.ctrl.panel.tableColumn = 'mean';
  276. ctx.ctrl.panel.mappingType = 2;
  277. ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
  278. });
  279. it('Should replace value with text NOT OK', function() {
  280. expect(ctx.data.valueFormatted).toBe('NOT OK');
  281. });
  282. });
  283. singleStatScenario('When value is string', function(ctx) {
  284. ctx.setup(function() {
  285. ctx.data = tableData;
  286. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 65, 'ignore2'];
  287. ctx.ctrl.panel.tableColumn = 'test1';
  288. });
  289. it('Should replace value with text NOT OK', function() {
  290. expect(ctx.data.valueFormatted).toBe('ignore1');
  291. });
  292. });
  293. singleStatScenario('When value is zero', function(ctx) {
  294. ctx.setup(function() {
  295. ctx.data = tableData;
  296. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 0, 'ignore2'];
  297. ctx.ctrl.panel.tableColumn = 'mean';
  298. });
  299. it('Should return zero', function() {
  300. expect(ctx.data.value).toBe(0);
  301. });
  302. });
  303. });
  304. });