AVG value being calculated is incorrect

divvi_vamsi
Mega Expert

I am trying to run a glideAggregate for Average value on syslog_transaction table for response_time for last few minutes. But i observe the value being returned is the MAX value instead of AVG of all the records within that time range.

Not sure what is being missed. Below is the script:

var count = new GlideAggregate('syslog_transaction');

count.addQuery('sys_created_on','>=', gs.minutesAgo(2));

count.addAggregate('AVG', 'response_time');

count.query();

while(count.next()) {

  var avg= count.getAggregate('AVG', 'response_time');

}

gs.log('Response time Average is '+ avg);

Thanks
Divya

1 ACCEPTED SOLUTION

Hi Divya,



Can you please try below code. I tried in Background Script and seems working. Basically I have applied below formula.



Total avg: [(avg of type A* Number count of Type A) + (avg of type B * Number count of Type B)] / (Number count of Type A + Number count of Type B)



var count = new GlideAggregate('syslog_transaction');


count.addQuery('sys_created_on','>=', 'javascript:gs.minutesAgo(1)');


count.addAggregate('AVG', 'response_time');


count.addAggregate('COUNT', 'response_time');


count.groupBy('type');


count.query();


var final_avg = 0;


var cnt = 0;


var avg = 0;


var total_val = 0;


var total_count = 0;


while(count.next()) {


total_count =+ parseInt(total_count) + parseInt(count.getAggregate('COUNT', 'response_time'));


avg = count.getAggregate('AVG', 'response_time');


cnt = count.getAggregate('COUNT', 'response_time');


total_val =+ parseFloat(total_val) + (parseFloat(avg) * parseInt(cnt));


gs.log(count.type + '       Response time Average is :         '+ avg);


gs.log(count.type + '       Response time count is :         '+ cnt);


}


gs.log('***************************************************');


gs.log('total val         :' + total_val);


gs.log('total count         :' + total_count);


final_avg =+ parseFloat(total_val) / parseInt(total_count);


gs.log('final_avg:     ' + final_avg);



Hope this helps.


View solution in original post

12 REPLIES 12

Shishir Srivast
Mega Sage

Hi Divya,



I think you need to addAggregate for MAX and MIN as well and have groupby on type. Please try below code if it helps.



var count = new GlideAggregate('syslog_transaction');


count.addQuery('sys_created_on','>=', 'javascript:gs.minutesAgo(2)');


count.addAggregate('MIN', 'response_time');


count.addAggregate('MAX', 'response_time');


count.addAggregate('AVG', 'response_time');


count.groupBy('type');


count.query();


while(count.next()) {


  var avg = count.getAggregate('AVG', 'response_time');


  gs.log(count.type + 'Response time Average is '+ avg);


}


Kalaiarasan Pus
Giga Sage

You haven't done any grouping of records to get the average. Try using groupBy function in your script.


http://wiki.servicenow.com/index.php?title=GlideAggregate#gsc.tab=0


i need an avg value for the entire column values of response time and within a specific time, so that shouldnt be categorized under a particular thing. Just like the value of a list calculations in a list view.



Thanks
Divya


I think, you need to use groupBy(), i was also trying without groupBy() but wan't able to get the result in background script. You can try to get the avg based upon groupBy for last 2 minutes and then add them up like below code (anyways average will be same if you add them for different groupBy) to get the final average. Hope this helps.



var count = new GlideAggregate('syslog_transaction');


count.addQuery('sys_created_on','>=', 'javascript:gs.minutesAgo(1)');


count.addAggregate('AVG', 'response_time');


count.groupBy('type');


count.query();


var final_avg = 0;


while(count.next()) {


var avg = count.getAggregate('AVG', 'response_time');


gs.log('Response time Average is :         '+ avg);


final_avg =+ parseFloat(final_avg ) + parseFloat(avg);


}


gs.log('final_avg:     ' + final_avg);