How to add all value for message_count and error_count in two variables and need to include both in assign message payload ?
Please find the final JSON response -
{ "environments": [{ "dimensions": [{ "metrics": [{ "name": "sum(message_count)", "values": [{ "timestamp": 1492473600000, "value": "1.0" }, { "timestamp": 1492387200000, "value": "878.0" }, { "timestamp": 1492300800000, "value": "115.0" }, { "timestamp": 1492214400000, "value": "1161.0" }, { "timestamp": 1492128000000, "value": "305.0" }, { "timestamp": 1492041600000, "value": "540.0" }, { "timestamp": 1491955200000, "value": "134.0" }] }, { "name": "sum(error_count)", "values": [{ "timestamp": 1492473600000, "value": "0.0" }, { "timestamp": 1492387200000, "value": "366.0" }, { "timestamp": 1492300800000, "value": "0.0" }, { "timestamp": 1492214400000, "value": "36.0" },
Solved! Go to Solution.
@Aritra Datta, Try the below JS code.
var response = JSON.parse(context.getVariable("response.content"));
var metrics = response.environments[0].dimensions[0].metrics;
var errorTotal = 0;
var msgTotal = 0;
for (var i=0; i<metrics.length; i++) {
var metric = metrics[i];
var type = metric.name;
var values = metric.values;
var value = 0;
for (var j=0; j<values.length; j++) {
value += Number(values[j].value);
}
if ("sum(message_count)".equalsIgnoreCase(type)) {
msgTotal = value;
} else if ("sum(error_count)".equalsIgnoreCase(type)) {
errorTotal = value;
}
}
context.setVariable("errorTotal",errorTotal);
context.setVariable("msgTotal",msgTotal);