singlestat_specs.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 us time instead of value', 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 = 'dateTimeAsUS';
  74. });
  75. it('Should use time instead of value', function() {
  76. expect(ctx.data.value).to.be(1505634997920);
  77. expect(ctx.data.valueRounded).to.be(1505634997920);
  78. });
  79. it('should set formatted value', function() {
  80. expect(ctx.data.valueFormatted).to.be(moment(1505634997920).format('MM/DD/YYYY h:mm:ss a'));
  81. });
  82. });
  83. singleStatScenario('showing last time from now instead of value', function(ctx) {
  84. beforeEach(() => {
  85. clock = sinon.useFakeTimers(epoch);
  86. });
  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 = 'dateTimeFromNow';
  91. });
  92. it('Should use time instead of value', function() {
  93. expect(ctx.data.value).to.be(1505634997920);
  94. expect(ctx.data.valueRounded).to.be(1505634997920);
  95. });
  96. it('should set formatted value', function() {
  97. expect(ctx.data.valueFormatted).to.be('2 days ago');
  98. });
  99. afterEach(() => {
  100. clock.restore();
  101. });
  102. });
  103. singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function(
  104. ctx
  105. ) {
  106. ctx.setup(function() {
  107. ctx.data = [{ target: 'test.cpu1', datapoints: [[99.999, 1], [99.99999, 2]] }];
  108. });
  109. it('Should be rounded', function() {
  110. expect(ctx.data.value).to.be(99.999495);
  111. expect(ctx.data.valueRounded).to.be(100);
  112. });
  113. it('should set formatted value', function() {
  114. expect(ctx.data.valueFormatted).to.be('100');
  115. });
  116. });
  117. singleStatScenario('When value to text mapping is specified', function(ctx) {
  118. ctx.setup(function() {
  119. ctx.data = [{ target: 'test.cpu1', datapoints: [[9.9, 1]] }];
  120. ctx.ctrl.panel.valueMaps = [{ value: '10', text: 'OK' }];
  121. });
  122. it('value should remain', function() {
  123. expect(ctx.data.value).to.be(9.9);
  124. });
  125. it('round should be rounded up', function() {
  126. expect(ctx.data.valueRounded).to.be(10);
  127. });
  128. it('Should replace value with text', function() {
  129. expect(ctx.data.valueFormatted).to.be('OK');
  130. });
  131. });
  132. singleStatScenario('When range to text mapping is specified for first range', function(ctx) {
  133. ctx.setup(function() {
  134. ctx.data = [{ target: 'test.cpu1', datapoints: [[41, 50]] }];
  135. ctx.ctrl.panel.mappingType = 2;
  136. ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
  137. });
  138. it('Should replace value with text OK', function() {
  139. expect(ctx.data.valueFormatted).to.be('OK');
  140. });
  141. });
  142. singleStatScenario('When range to text mapping is specified for other ranges', function(ctx) {
  143. ctx.setup(function() {
  144. ctx.data = [{ target: 'test.cpu1', datapoints: [[65, 75]] }];
  145. ctx.ctrl.panel.mappingType = 2;
  146. ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
  147. });
  148. it('Should replace value with text NOT OK', function() {
  149. expect(ctx.data.valueFormatted).to.be('NOT OK');
  150. });
  151. });
  152. describe('When table data', function() {
  153. const tableData = [
  154. {
  155. columns: [{ text: 'Time', type: 'time' }, { text: 'test1' }, { text: 'mean' }, { text: 'test2' }],
  156. rows: [[1492759673649, 'ignore1', 15, 'ignore2']],
  157. type: 'table',
  158. },
  159. ];
  160. singleStatScenario('with default values', function(ctx) {
  161. ctx.setup(function() {
  162. ctx.data = tableData;
  163. ctx.ctrl.panel.tableColumn = 'mean';
  164. });
  165. it('Should use first rows value as default main value', function() {
  166. expect(ctx.data.value).to.be(15);
  167. expect(ctx.data.valueRounded).to.be(15);
  168. });
  169. it('should set formatted value', function() {
  170. expect(ctx.data.valueFormatted).to.be('15');
  171. });
  172. });
  173. singleStatScenario('When table data has multiple columns', function(ctx) {
  174. ctx.setup(function() {
  175. ctx.data = tableData;
  176. ctx.ctrl.panel.tableColumn = '';
  177. });
  178. it('Should set column to first column that is not time', function() {
  179. expect(ctx.ctrl.panel.tableColumn).to.be('test1');
  180. });
  181. });
  182. singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function(
  183. ctx
  184. ) {
  185. ctx.setup(function() {
  186. ctx.data = tableData;
  187. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 99.99999, 'ignore2'];
  188. ctx.ctrl.panel.tableColumn = 'mean';
  189. });
  190. it('Should be rounded', function() {
  191. expect(ctx.data.value).to.be(99.99999);
  192. expect(ctx.data.valueRounded).to.be(100);
  193. });
  194. it('should set formatted falue', function() {
  195. expect(ctx.data.valueFormatted).to.be('100');
  196. });
  197. });
  198. singleStatScenario('When value to text mapping is specified', function(ctx) {
  199. ctx.setup(function() {
  200. ctx.data = tableData;
  201. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 9.9, 'ignore2'];
  202. ctx.ctrl.panel.tableColumn = 'mean';
  203. ctx.ctrl.panel.valueMaps = [{ value: '10', text: 'OK' }];
  204. });
  205. it('value should remain', function() {
  206. expect(ctx.data.value).to.be(9.9);
  207. });
  208. it('round should be rounded up', function() {
  209. expect(ctx.data.valueRounded).to.be(10);
  210. });
  211. it('Should replace value with text', function() {
  212. expect(ctx.data.valueFormatted).to.be('OK');
  213. });
  214. });
  215. singleStatScenario('When range to text mapping is specified for first range', function(ctx) {
  216. ctx.setup(function() {
  217. ctx.data = tableData;
  218. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 41, 'ignore2'];
  219. ctx.ctrl.panel.tableColumn = 'mean';
  220. ctx.ctrl.panel.mappingType = 2;
  221. ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
  222. });
  223. it('Should replace value with text OK', function() {
  224. expect(ctx.data.valueFormatted).to.be('OK');
  225. });
  226. });
  227. singleStatScenario('When range to text mapping is specified for other ranges', function(ctx) {
  228. ctx.setup(function() {
  229. ctx.data = tableData;
  230. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 65, 'ignore2'];
  231. ctx.ctrl.panel.tableColumn = 'mean';
  232. ctx.ctrl.panel.mappingType = 2;
  233. ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
  234. });
  235. it('Should replace value with text NOT OK', function() {
  236. expect(ctx.data.valueFormatted).to.be('NOT OK');
  237. });
  238. });
  239. singleStatScenario('When value is string', function(ctx) {
  240. ctx.setup(function() {
  241. ctx.data = tableData;
  242. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 65, 'ignore2'];
  243. ctx.ctrl.panel.tableColumn = 'test1';
  244. });
  245. it('Should replace value with text NOT OK', function() {
  246. expect(ctx.data.valueFormatted).to.be('ignore1');
  247. });
  248. });
  249. singleStatScenario('When value is zero', function(ctx) {
  250. ctx.setup(function() {
  251. ctx.data = tableData;
  252. ctx.data[0].rows[0] = [1492759673649, 'ignore1', 0, 'ignore2'];
  253. ctx.ctrl.panel.tableColumn = 'mean';
  254. });
  255. it('Should return zero', function() {
  256. expect(ctx.data.value).to.be(0);
  257. });
  258. });
  259. });
  260. });