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;
}