DX Application Performance Management

Expand all | Collapse all

Javascript Calculator - Rolling Metric Count

  • 1.  Javascript Calculator - Rolling Metric Count

    Posted May 19, 2014 12:12 PM

    Has anyone done a rolling metric count using a JS calculator?  I'm trying to show on a dashboard the number of X occurrances for the last 24 hours.  However, I want this metric to be LIVE.  Meaning, when looking at a live view, I want the metric to show the sum of the last 24 hours for a give metric.

    Anyone have a way of doing this within Introscope?

    Thanks.



  • 2.  RE: Javascript Calculator - Rolling Metric Count

    Posted May 19, 2014 01:19 PM

    Would this be always the last 24 hours or would "for the current day" be acceptable instead? I ask because one is far easier to write (and lower overhead) than the other.



  • 3.  RE: Javascript Calculator - Rolling Metric Count

    Posted May 19, 2014 01:22 PM

    I think current day would be acceptable.  



  • 4.  RE: Javascript Calculator - Rolling Metric Count
    Best Answer

    Posted May 19, 2014 02:04 PM
      |   view attached

    This is completely untested, but should get you close to what you want, then (attached it as a file as well, in case the formatting doesn't come through). 

    /*
    
    Title: 
    
     Daily Running Total.js
    
    Description: For the specific metric and agent, aggregates the running sum for the current date. 
    
    Author: 
     Jack Butler, Ben Friedman
    
    Version:
     1.0 2014-05-19
    */
    var lastCheck = new Date();
    
    
    
    
    
    
    
    
    
    
    // date of last check; when current date is a day after last day, reset the running sums
    var agentList = {}; 
    
    
    
    
    
    
    
    
    
    
    
    // list of running sums, by agent (global to preserve value between executions of script)
    
    function execute(metricData,javascriptResultSetHelper) {
    
    for(var j=0; j < metricData.length; j++) {
    
    
    
    
    
    // iterate through each found metric
    
    
    var agent = metricData[j].agentName.processURL
    
    
    
    // used for reporting the metric back as part of the same agent
    
    
    var metric = metricData[j].metric.name;
    
    
    
    
    
    // the metric name, i.e. Responses Per Interval
    
    
    var value = metricData[j].timeslicedValue.value;
    
    
    // the current value of the metric
    
    
    var frequency = metricData[j].frequency;
    
    
    
    
    // the update frequency of the metric
    
    
    var sum = 0;
    
    
    
    
    
    
    
    
    
    
    
    // the known running sum
    
    
    
    
    
    if(daysBetween(lastCheck,new Date()) < 1) {
    //only preserve the existing running sum if on the same date; otherwise, reset to zero
    
    
    
    // retrieve the existing running total for this target, if it exists
    
    
    
    if(agentList[agent] != undefined) {
    
    
    
    
    sum = agentList[agent]['sum'];
    
    
    
    } 
    
    
    } 
    
    
    
    
    
    //calculate the running sum by adding the current value to the existing sum (or zero, if the date has incremented by 1 day)
    
    
    sum += value;
    
    
    
    
    
    // update the target list
    
    
    agentList[agent] = { "metric":metric, "frequency":frequency, "sum":sum, "lastSeen":new Date() }; 
    
    }
    
    
    
    var agentRemovalList = {}; 
    
    
    
    
    
    
    
    
    
    // list of agents that are no longer reporting and should be removed, based on lastSeen property
    
    
    // now iterate found targets and report calculated metrics, build array of metrics
    
    for (var agent in agentList) {
    
    
    if(daysBetween(agentList[agent]['lastSeen'],new Date()) < 1) {
    //ignore agents that have been offline for more than a day
    
    
    
    // Derive the new metric name from the matching metric
    
    
    
    var metricName = agent + "|" + metric + " Daily Running Total";
    
    
    
    
    // add the calculated values to the result set
    
    
    
    javascriptResultSetHelper.addMetric(metricName,
    
    
    
    
    agentList[agent]['sum'],
    
    
    
    
    Packages.com.wily.introscope.spec.metric.MetricTypes.kLongMonotonicallyIncreasingCounter,
    
    
    
    
    agentList[agent]['frequency']);
    
    
    } else { //add this agent to the list of agents to be removed
    
    
    
    agentRemovalList[agent] = agentList[agent];
    
    
    }
    
    }
    
    
    
    // now clean up the agentList
    
    for (var agent in agentRemovalList) {
    
    
    delete agentList[agent];
    
    }
    
    
    
    //set last check date for comparison in the next run
    
    lastCheck = new Date();
    
    
    
    // return the result set
    
    return javascriptResultSetHelper;
    }
    
    // Calculate the difference in days between two dates; 
    function daysBetween(first, second) {
        return (  Math.round((second.getTime()-first.getTime())/(1000*60*60*24))  );
    }
    
    function getAgentRegex() {
    
    //TODO: Customize this to match your desired agent expression
    
    return ".*";
    }
    function getMetricRegex() { 
        //TODO: Customize this to match your desired metric expression
    
    return "Some Metric RegEx";
    }
    
    // return false if the script should not run on the MOM
    // default is true.
    function runOnMOM() {
    
    return false;
    }
    
    // must return a multiple of default system frequency
    function getFrequency() {
    
    return 1 * Packages.com.wily.introscope.spec.metric.Frequency.kDefaultSystemFrequencyInSeconds;
    }

     

    Attachment(s)

    js
    Daily Running Total.js   3 KB 1 version


  • 5.  RE: Javascript Calculator - Rolling Metric Count

    Broadcom Employee
    Posted May 19, 2014 02:56 PM

    Jack,

    When you get a chance, please add this to the Documents so people to find it when looking for JS calc examples.



  • 6.  RE: Javascript Calculator - Rolling Metric Count

    Posted May 19, 2014 04:42 PM

    This has been done; the script is accesible via the Document Library, here



  • 7.  RE: Javascript Calculator - Rolling Metric Count

    Broadcom Employee
    Posted May 19, 2014 04:44 PM

    You rock! Thanks for the code. yes



  • 8.  RE: Javascript Calculator - Rolling Metric Count

    Posted May 21, 2014 01:47 PM

    Thank you indeed!

    Tested this out today.  I had to make some changes based on our environment of course, but it seems to work as expected.  I'll let it run a few days and see how it behaves day to day.

    Thanks again!



  • 9.  RE: Javascript Calculator - Rolling Metric Count

    Posted May 22, 2014 10:23 AM

    Ok a little update...

     

    It looks like the script will keep accumulating indefinitely because the 'lastCheck' is always going to be 15 seconds less than the current check... right?

    I think I'm going to modify the code to remove the hour, min, second from the Date so it will (hopefully) only compare the current day.



  • 10.  RE: Javascript Calculator - Rolling Metric Count

    Posted May 22, 2014 10:34 AM

    This may be how I'm combining the metrics into a different single metric...

    I think your script works as expected Jack, just not how I'm trying to use it... but I think I can tweak it.  I'll report back later.



  • 11.  RE: Javascript Calculator - Rolling Metric Count

    Posted May 22, 2014 10:44 AM

    You could always do something like the below: 

    if(lastCheck.getDate() <> (new Date()).getDate()) {

     

    Even with a 15 second run time, eventually the calendar date will tick over and you'll reset. 



  • 12.  RE: Javascript Calculator - Rolling Metric Count

    Posted May 22, 2014 04:26 PM

    Ok I tweaked it a bit for my situation.  I'm combining several metrics into one metric, as well as rolling them up.  Here's what I ended up with:

    /*
        Title:           Daily Running Total.js
        Description: For the specific metric and agent, aggregates the running sum for the current date.
        Author:          Jack Butler, Ben Friedman
        Version:         1.1 2014-05-22
    */

    // date of last check; when current date is a day after last day, reset the running sums
    var lastCheck = new Date();
    lastCheck = lastCheck.getDate();

    // list of running sums, by agent (global to preserve value between executions of script)
    var agentList = {};                                                                                            

    function execute(metricData,javascriptResultSetHelper) {
        // new date for each execution
        var newCheck = new Date();
        newCheck = newCheck.getDate();

        // set metric name for this rollup metric
        var metricName = "|Static|Metric|Path:ForTotal";

        // the known running sum for all metrics returned
        var sum = 0;

        // iterate through each found metric
        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].metric.name;            // the metric name, i.e. Responses Per Interval
            var value       = metricData[j].timeslicedValue.value;  // the current value of the metric
            var frequency   = metricData[j].frequency;              // the update frequency of the metric

            // only preserve the existing running sum if on the same date; otherwise, reset to zero
            if (lastCheck == newCheck) {
                // retrieve the existing running total for this target, if it exists
                if(agentList[agent] != undefined) {
                    sum = agentList[agent]['sum'];
                }
            }

            // calculate the running sum by adding the current value to the existing sum
            // (or zero, if the date has incremented by 1 day)
            sum += value;

            agentList[agent] = { "metric":metricName, "frequency":frequency, "sum":sum, "lastSeen":newCheck };
        }

        // now iterate found targets and report calculated metrics, build array of metrics
        for (var agent in agentList) {
            metricPath = agent + metricName;

            // add the calculated values to the existing result set
            javascriptResultSetHelper.addMetric(metricPath,
                    agentList[agent]['sum'],
                    Packages.com.wily.introscope.spec.metric.MetricTypes.kLongMonotonicallyIncreasingCounter,
                    agentList[agent]['frequency']);

        }

        //set last check date for comparison in the next run
        lastCheck = new Date();
        lastCheck = lastCheck.getDate();

        // return the result set
        return javascriptResultSetHelper;
    }

    function getAgentRegex() {
        //TODO: Customize this to match your desired agent expression
        return ".*AgentRegex.*";
    }
    function getMetricRegex() {
        //TODO: Customize this to match your desired metric expression
        return ".*MetricRegex.*";
    }

    // return false if the script should not run on the MOM
    // default is true.
    function runOnMOM() {
        return false;
    }

    // must return a multiple of default system frequency
    function getFrequency() {
        return 1 * Packages.com.wily.introscope.spec.metric.Frequency.kDefaultSystemFrequencyInSeconds;
    }



  • 13.  RE: Javascript Calculator - Rolling Metric Count

    Posted May 22, 2014 10:48 AM

    Yea, it looks like it's an issue with how I'm trying to implement.

    I'm grabbing the metrics from several similar metrics, and creating a single 'rollup' metric.  Because the 'agentList' doesn't contain this rollup metric, it's just adding indefinitely.