All files / lib swsErrors.js

95.65% Statements 22/23
90% Branches 9/10
75% Functions 3/4
95.45% Lines 21/22

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 51 52 53 54 55 56 57              1x 1x 1x           1x     1x     1x     1x         1x 16x     8x 2x   8x   8x 4x 4x 4x           1x 8x 4x   8x       1x  
/**
 * Created by sv2 on 2/18/17.
 * Errors stats
 */
 
'use strict';
 
var util = require('util');
var debug = require('debug')('sws:errors');
var swsUtil = require('./swsUtil');
 
function swsErrors() {
 
 
    // Store counts per each error code
    this.statuscode_count = {};
 
    // Store Top not found path
    this.top_not_found = {};
 
    // Store Top server error path
    this.top_server_error = {};
}
 
swsErrors.prototype.getStats = function() {
    return { statuscode: this.statuscode_count, topnotfound: this.top_not_found, topservererror: this.top_server_error };
};
 
// Add information about error
swsErrors.prototype.countResponse = function (res) {
    if(!swsUtil.isError(res.statusCode)) return;
 
    // Increase count by code
    if(!(res.statusCode in this.statuscode_count)) {
        this.statuscode_count[res.statusCode] = 0;
    }
    this.statuscode_count[res.statusCode]++;
 
    if(res.statusCode==404){
        this.countPathHit(res._swsReq.sws.originalUrl,this.top_not_found);
    }else Eif(res.statusCode==500){
        this.countPathHit(res._swsReq.sws.originalUrl,this.top_server_error);
    }
 
};
 
// Check if this qualifies as longest request, and store is yes
swsErrors.prototype.countPathHit = function(path,store) {
    if(!(path in store)) {
        store[path] = 0;
    }
    store[path]++;
};
 
 
module.exports = swsErrors;