singlestat.jest.ts 12 KB

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