singlestat_specs.ts 10 KB

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