All files swsBucketStats.js

22.73% Statements 5/22
0% Branches 0/10
0% Functions 0/2
23.81% Lines 5/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50              1x 1x 1x                                                   1x                         1x  
/**
 * Bucket Statistic: count value per specified buckets.
 * Used to show histogram
 */
 
'use strict';
 
var util = require('util');
var debug = require('debug')('sws:errors');
var swsUtil = require('./swsUtil');
 
// Bucket Statistic: count value per specified buckets.
// buckets: array of upper bounds for buckets: [0.1,0.2,0.5,1,10,20,50]
function swsBucketStats(buckets) {
 
    // Total count of events that have been observed
    this.count = 0;
 
    // Buckets boundaries, with default bucket values
    this.buckets = [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000];
 
    // Values
    this.values = [0,0,0,0,0,0,0,0,0,0,0,0];
 
    if(typeof buckets === "object" ){
        if( Array.isArray(buckets)){
            this.buckets = Array.from(buckets);
            this.values = new Array(this.buckets.length);
            this.values.fill(0);
            this.values.push(0);
        }
    }
 
}
 
swsBucketStats.prototype.countValue = function(value) {
    this.count++;
    var valuePlaced = false;
    for( var i=0; i<this.buckets.length; i++ ){
        if( !valuePlaced && (value <= this.buckets[i]) ) {
            this.values[i]++;
            valuePlaced = true;
        }
    }
    // Place value to last bucket ( <= Infinity ) if it's not placed in other bucket
    if(!valuePlaced) this.values[this.values.length - 1]++;
};
 
module.exports = swsBucketStats;