Chat average wait time calculation

Davina
Giga Contributor

Hi,

 

This is my first time implementing the chat functionality in SN.

 

I have a question around the Business Rule SNC - Chat Queue Average Wait Time

 

Has anyone built the average wait time business rule to set this based on the calls in the past hour rather than the last 20 calls.

 

So check how many calls in the last hour, check the wait time for each, then calculate the average?

 

As always, Any help is much appreciated!

6 REPLIES 6

Mark Stanger
Giga Sage

I think you should be able to adjust the query in that business rule to look something like this...



    var gr = new GlideRecord('chat_queue_entry');


    gr.addQuery('queue', current.queue);


    gr.addQuery('action', 'accepted');


    gr.addEncodedQuery('sys_updated_onRELATIVEGE@hour@ago@1');


    gr.orderByDesc('sys_updated_on');


    gr.setLimit(20);


    gr.query();



The 'addEncodedQuery' line limits it to things updated in the last hour.   You can optionally remove the 'setLimit' line if you'd like this to be based on more than 20 records, but I think it's probably good to leave it there as 20 records should be enough to give you a good wait time calculation.


Hi Mark



I used your code and modified OOTB business rule to following:



var avgWaitTime = getChatQueueAvgWaitTime();


updateChatQueueAvgWaitTime(avgWaitTime);




function getChatQueueAvgWaitTime() {


    var cnt = 0;


    var ms = 0;


    var gr = new GlideRecord('chat_queue_entry');


    gr.addQuery('queue', current.queue);


    gr.addQuery('action', 'accepted');


    gr.addEncodedQuery('sys_updated_onRELATIVEGE@hour@ago@1');


    //gr.orderByDesc('sys_updated_on');


    //gr.setLimit(20);


    gr.query();


    while (gr.next()) {


          cnt++;


          ms += gr.wait_time.getGlideObject().getNumericValue();


    }


   


    if (cnt == 0)


          return 0;


         


    return ms / cnt;


}




function updateChatQueueAvgWaitTime(avgWaitTime) {


    var gr = new GlideRecord('chat_queue');


    if (gr.get(current.queue)) {


          gr.average_wait_time.setDateNumericValue(avgWaitTime);


          gr.update();


    }


}




As far as I understand it should return "0"(zero) if there were no chat requests in last one hour but it returned 6s or 8s as average wait time when I tested. Could you explain this behaviour.




Many thanks!!


Davina
Giga Contributor

Thanks Mark! I will give this a go


RichardSaunders
Tera Guru

Is the average wait time calculation based on all Connect Support groups?



We have 5 or 6 groups in IT & HR and it would be useful to know the average wait time corresponds to the chat queue and not all chats in total.



Can anyone confirm?



EDIT: On line 17 it says current.queue, excuse me if i'm answering my own question but does this mean it DOES only calculate the average of the last 20 chats per queue?



/**


Service-now.com



Description:


    When a chat queue entry is accepted, compute the average wait time or the queue by averaging the last 20 chat queue entries


**/




var avgWaitTime = getChatQueueAvgWaitTime();


updateChatQueueAvgWaitTime(avgWaitTime);




function getChatQueueAvgWaitTime() {


    var cnt = 0;


    var ms = 0;


    var gr = new GlideRecord('chat_queue_entry');


    gr.addQuery('queue', current.queue);


    gr.addQuery('state', '2'); // WORK_IN_PROGRESS


    gr.orderByDesc('sys_updated_on');


    gr.setLimit(20);


  gr.query();


    while (gr.next()) {


          cnt++;


          ms += gr.wait_time.getGlideObject().getNumericValue();


    }


   


    if (cnt == 0)


          return 0;


         


    return ms / cnt;


}




function updateChatQueueAvgWaitTime(avgWaitTime) {


    var gr = new GlideRecord('chat_queue');


    if (gr.get(current.queue)) {


          gr.average_wait_time.setDateNumericValue(avgWaitTime);


          gr.update();


    }


}