singlestat_specs.ts 12 KB

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