| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- import { describe, beforeEach, afterEach, it, sinon, expect, angularMocks } from 'test/lib/common';
- import helpers from 'test/specs/helpers';
- import { SingleStatCtrl } from '../module';
- import moment from 'moment';
- describe('SingleStatCtrl', function() {
- var ctx = new helpers.ControllerTestContext();
- var epoch = 1505826363746;
- var clock;
- function singleStatScenario(desc, func) {
- describe(desc, function() {
- ctx.setup = function(setupFunc) {
- beforeEach(angularMocks.module('grafana.services'));
- beforeEach(angularMocks.module('grafana.controllers'));
- beforeEach(
- angularMocks.module(function($compileProvider) {
- $compileProvider.preAssignBindingsEnabled(true);
- })
- );
- beforeEach(ctx.providePhase());
- beforeEach(ctx.createPanelController(SingleStatCtrl));
- beforeEach(function() {
- setupFunc();
- ctx.ctrl.onDataReceived(ctx.data);
- ctx.data = ctx.ctrl.data;
- });
- };
- func(ctx);
- });
- }
- singleStatScenario('with defaults', function(ctx) {
- ctx.setup(function() {
- ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 1], [20, 2]] }];
- });
- it('Should use series avg as default main value', function() {
- expect(ctx.data.value).to.be(15);
- expect(ctx.data.valueRounded).to.be(15);
- });
- it('should set formatted falue', function() {
- expect(ctx.data.valueFormatted).to.be('15');
- });
- });
- singleStatScenario('showing serie name instead of value', function(ctx) {
- ctx.setup(function() {
- ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 1], [20, 2]] }];
- ctx.ctrl.panel.valueName = 'name';
- });
- it('Should use series avg as default main value', function() {
- expect(ctx.data.value).to.be(0);
- expect(ctx.data.valueRounded).to.be(0);
- });
- it('should set formatted value', function() {
- expect(ctx.data.valueFormatted).to.be('test.cpu1');
- });
- });
- singleStatScenario('showing last iso time instead of value', function(ctx) {
- ctx.setup(function() {
- ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }];
- ctx.ctrl.panel.valueName = 'last_time';
- ctx.ctrl.panel.format = 'dateTimeAsIso';
- });
- it('Should use time instead of value', function() {
- expect(ctx.data.value).to.be(1505634997920);
- expect(ctx.data.valueRounded).to.be(1505634997920);
- });
- it('should set formatted value', function() {
- expect(ctx.data.valueFormatted).to.be(moment(1505634997920).format('YYYY-MM-DD HH:mm:ss'));
- });
- });
- singleStatScenario('showing last iso time instead of value (in UTC)', function(ctx) {
- ctx.setup(function() {
- ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }];
- ctx.ctrl.panel.valueName = 'last_time';
- ctx.ctrl.panel.format = 'dateTimeAsIso';
- ctx.setIsUtc(true);
- });
- it('should set formatted value', function() {
- expect(ctx.data.valueFormatted).to.be(moment.utc(1505634997920).format('YYYY-MM-DD HH:mm:ss'));
- });
- });
- singleStatScenario('showing last us time instead of value', function(ctx) {
- ctx.setup(function() {
- ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }];
- ctx.ctrl.panel.valueName = 'last_time';
- ctx.ctrl.panel.format = 'dateTimeAsUS';
- });
- it('Should use time instead of value', function() {
- expect(ctx.data.value).to.be(1505634997920);
- expect(ctx.data.valueRounded).to.be(1505634997920);
- });
- it('should set formatted value', function() {
- expect(ctx.data.valueFormatted).to.be(moment(1505634997920).format('MM/DD/YYYY h:mm:ss a'));
- });
- });
- singleStatScenario('showing last us time instead of value (in UTC)', function(ctx) {
- ctx.setup(function() {
- ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }];
- ctx.ctrl.panel.valueName = 'last_time';
- ctx.ctrl.panel.format = 'dateTimeAsUS';
- ctx.setIsUtc(true);
- });
- it('should set formatted value', function() {
- expect(ctx.data.valueFormatted).to.be(moment.utc(1505634997920).format('MM/DD/YYYY h:mm:ss a'));
- });
- });
- singleStatScenario('showing last time from now instead of value', function(ctx) {
- beforeEach(() => {
- clock = sinon.useFakeTimers(epoch);
- });
- ctx.setup(function() {
- ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }];
- ctx.ctrl.panel.valueName = 'last_time';
- ctx.ctrl.panel.format = 'dateTimeFromNow';
- });
- it('Should use time instead of value', function() {
- expect(ctx.data.value).to.be(1505634997920);
- expect(ctx.data.valueRounded).to.be(1505634997920);
- });
- it('should set formatted value', function() {
- expect(ctx.data.valueFormatted).to.be('2 days ago');
- });
- afterEach(() => {
- clock.restore();
- });
- });
- singleStatScenario('showing last time from now instead of value (in UTC)', function(ctx) {
- beforeEach(() => {
- clock = sinon.useFakeTimers(epoch);
- });
- ctx.setup(function() {
- ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }];
- ctx.ctrl.panel.valueName = 'last_time';
- ctx.ctrl.panel.format = 'dateTimeFromNow';
- ctx.setIsUtc(true);
- });
- it('should set formatted value', function() {
- expect(ctx.data.valueFormatted).to.be('2 days ago');
- });
- afterEach(() => {
- clock.restore();
- });
- });
- singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function(
- ctx
- ) {
- ctx.setup(function() {
- ctx.data = [{ target: 'test.cpu1', datapoints: [[99.999, 1], [99.99999, 2]] }];
- });
- it('Should be rounded', function() {
- expect(ctx.data.value).to.be(99.999495);
- expect(ctx.data.valueRounded).to.be(100);
- });
- it('should set formatted value', function() {
- expect(ctx.data.valueFormatted).to.be('100');
- });
- });
- singleStatScenario('When value to text mapping is specified', function(ctx) {
- ctx.setup(function() {
- ctx.data = [{ target: 'test.cpu1', datapoints: [[9.9, 1]] }];
- ctx.ctrl.panel.valueMaps = [{ value: '10', text: 'OK' }];
- });
- it('value should remain', function() {
- expect(ctx.data.value).to.be(9.9);
- });
- it('round should be rounded up', function() {
- expect(ctx.data.valueRounded).to.be(10);
- });
- it('Should replace value with text', function() {
- expect(ctx.data.valueFormatted).to.be('OK');
- });
- });
- singleStatScenario('When range to text mapping is specified for first range', function(ctx) {
- ctx.setup(function() {
- ctx.data = [{ target: 'test.cpu1', datapoints: [[41, 50]] }];
- ctx.ctrl.panel.mappingType = 2;
- ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
- });
- it('Should replace value with text OK', function() {
- expect(ctx.data.valueFormatted).to.be('OK');
- });
- });
- singleStatScenario('When range to text mapping is specified for other ranges', function(ctx) {
- ctx.setup(function() {
- ctx.data = [{ target: 'test.cpu1', datapoints: [[65, 75]] }];
- ctx.ctrl.panel.mappingType = 2;
- ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
- });
- it('Should replace value with text NOT OK', function() {
- expect(ctx.data.valueFormatted).to.be('NOT OK');
- });
- });
- describe('When table data', function() {
- const tableData = [
- {
- columns: [{ text: 'Time', type: 'time' }, { text: 'test1' }, { text: 'mean' }, { text: 'test2' }],
- rows: [[1492759673649, 'ignore1', 15, 'ignore2']],
- type: 'table',
- },
- ];
- singleStatScenario('with default values', function(ctx) {
- ctx.setup(function() {
- ctx.data = tableData;
- ctx.ctrl.panel.tableColumn = 'mean';
- });
- it('Should use first rows value as default main value', function() {
- expect(ctx.data.value).to.be(15);
- expect(ctx.data.valueRounded).to.be(15);
- });
- it('should set formatted value', function() {
- expect(ctx.data.valueFormatted).to.be('15');
- });
- });
- singleStatScenario('When table data has multiple columns', function(ctx) {
- ctx.setup(function() {
- ctx.data = tableData;
- ctx.ctrl.panel.tableColumn = '';
- });
- it('Should set column to first column that is not time', function() {
- expect(ctx.ctrl.panel.tableColumn).to.be('test1');
- });
- });
- singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function(
- ctx
- ) {
- ctx.setup(function() {
- ctx.data = tableData;
- ctx.data[0].rows[0] = [1492759673649, 'ignore1', 99.99999, 'ignore2'];
- ctx.ctrl.panel.tableColumn = 'mean';
- });
- it('Should be rounded', function() {
- expect(ctx.data.value).to.be(99.99999);
- expect(ctx.data.valueRounded).to.be(100);
- });
- it('should set formatted falue', function() {
- expect(ctx.data.valueFormatted).to.be('100');
- });
- });
- singleStatScenario('When value to text mapping is specified', function(ctx) {
- ctx.setup(function() {
- ctx.data = tableData;
- ctx.data[0].rows[0] = [1492759673649, 'ignore1', 9.9, 'ignore2'];
- ctx.ctrl.panel.tableColumn = 'mean';
- ctx.ctrl.panel.valueMaps = [{ value: '10', text: 'OK' }];
- });
- it('value should remain', function() {
- expect(ctx.data.value).to.be(9.9);
- });
- it('round should be rounded up', function() {
- expect(ctx.data.valueRounded).to.be(10);
- });
- it('Should replace value with text', function() {
- expect(ctx.data.valueFormatted).to.be('OK');
- });
- });
- singleStatScenario('When range to text mapping is specified for first range', function(ctx) {
- ctx.setup(function() {
- ctx.data = tableData;
- ctx.data[0].rows[0] = [1492759673649, 'ignore1', 41, 'ignore2'];
- ctx.ctrl.panel.tableColumn = 'mean';
- ctx.ctrl.panel.mappingType = 2;
- ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
- });
- it('Should replace value with text OK', function() {
- expect(ctx.data.valueFormatted).to.be('OK');
- });
- });
- singleStatScenario('When range to text mapping is specified for other ranges', function(ctx) {
- ctx.setup(function() {
- ctx.data = tableData;
- ctx.data[0].rows[0] = [1492759673649, 'ignore1', 65, 'ignore2'];
- ctx.ctrl.panel.tableColumn = 'mean';
- ctx.ctrl.panel.mappingType = 2;
- ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }];
- });
- it('Should replace value with text NOT OK', function() {
- expect(ctx.data.valueFormatted).to.be('NOT OK');
- });
- });
- singleStatScenario('When value is string', function(ctx) {
- ctx.setup(function() {
- ctx.data = tableData;
- ctx.data[0].rows[0] = [1492759673649, 'ignore1', 65, 'ignore2'];
- ctx.ctrl.panel.tableColumn = 'test1';
- });
- it('Should replace value with text NOT OK', function() {
- expect(ctx.data.valueFormatted).to.be('ignore1');
- });
- });
- singleStatScenario('When value is zero', function(ctx) {
- ctx.setup(function() {
- ctx.data = tableData;
- ctx.data[0].rows[0] = [1492759673649, 'ignore1', 0, 'ignore2'];
- ctx.ctrl.panel.tableColumn = 'mean';
- });
- it('Should return zero', function() {
- expect(ctx.data.value).to.be(0);
- });
- });
- });
- });
|