AnsweredAssumed Answered

Javascript Calculator Value Not Resetting Each Interval

Question asked by Bennnnnnn on May 11, 2015
Latest reply on May 11, 2015 by Bennnnnnn

Hello everyone.  I'm trying to write a js calc that will take a bunch of metrics and create several 'rollup' metrics for a given interval.  The problem I'm having is that the value is not reporting on a per interval basis.  It just keeps adding up over time...even if I hard code the value to 0.  What am I missing here?

 

function execute(metricData,javascriptResultSetHelper) {
    var domains = {};
    var envs = {};


    for(var j=0; j < metricData.length; j++) {
        var agent       = metricData[j].agentName.processURL;     // used for reporting the metric back as part of the same agent
        var metric      = metricData[j].agentMetric.attributeURL; // the full metric path
        var value       = metricData[j].timeslicedValue.value;    // the current value of the metric
        var frequency   = metricData[j].frequency;                // the update frequency of the metric


        var splitMetric = metric.split("\\|");
        var env         = splitMetric[0];
        var device      = splitMetric[1];
        var domainPath  = splitMetric[2];
        var domain      = domainPath.split(":");
        var dom         = domain[0];
        
        var envRollupPath = agent + "|" + env;
        var domRollupPath = agent + "|" + env + "|Domains|" + dom;
        
        if (envs[envRollupPath] != undefined) {
            envs[envRollupPath]["val"] += value;
        } else {
            envs[envRollupPath] = {"val":value, "freq":frequency};
        }
        
        if (domains[domRollupPath] != undefined) {
            domains[domRollupPath]["val"] += value;
        } else {
            domains[domRollupPath] = {"val":value, "freq":frequency};
        }
    }


    // now iterate found targets and report calculated metrics, build array of metrics
    
    for (var env in envs) {
        var metricPath = env + ":Responses Per Interval";
        log.info("[JS CALC] " + env + ": " + envs[env]["val"]);


        // add the calculated values to the existing result set
        javascriptResultSetHelper.addMetric(metricPath,
                envs[env]["val"],
                javascriptResultSetHelper.kIntegerFluctuatingCounter,
                envs[env]["freq"]);
                
        envs[env]["val"] = 0;
    }
    
    for (var dom in domains) {
        var metricPath = dom + ":Responses Per Interval";
        log.info("[JS CALC] " + dom + ": " + domains[dom]["val"]);


        // add the calculated values to the existing result set
        javascriptResultSetHelper.addMetric(metricPath,
                domains[dom]["val"],
                javascriptResultSetHelper.kIntegerFluctuatingCounter,
                domains[dom]["freq"]);
                
        domains[dom]["val"] = 0;
    }
    
    // return the result set
    return javascriptResultSetHelper;
}

Outcomes