- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2017 05:46 PM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2017 07:23 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2017 07:41 PM
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);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2017 08:34 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2017 12:39 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2017 02:00 PM
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);