Name

global.InteractionStatsProvider

Description

Implements extension point global.AWAStatsStore Extension point to store stats in AWAQueueStats,AWAChannelStats and AWAInstranceStats tables

Script

var InteractionStatsProvider = Class.create();
InteractionStatsProvider.prototype = {
  initialize: function() {},

  _addValueToKey: function(map, key, property, value) {
      map[key] = map[key] || {};
      map[key][property] = value;
  },

  _query: function(table, encodedQuery, query, groupBy, aggr) {
      var ga = new GlideAggregate(table);
      if (encodedQuery) ga.addEncodedQuery(encodedQuery);
      if (query) ga.addQuery(query);
      if (aggr) ga.addAggregate(aggr);
      if (groupBy) ga.groupBy(groupBy);
      ga.query();
      return ga;
  },

  _queryAndGetAggregateCount: function(table, encodedQuery, query, groupBy, aggr) {
      var ga = this._query(table, encodedQuery, query, groupBy, aggr);
      var count = 0;
      if (ga.next()) return ga.getAggregate(aggr);
      return 0;
  },

  getQueueStats: function(queueVsGroups, windowStartTime, windowEndTime) {
      var map = {};
      var queueStats = {};
      var statDefaultValue = {
          interactions_closed: 0,
          avg_duration_closed_interactions: 0
      };
      var interactionsClosed = new GlideAggregate('awa_interaction_work_item');
      var dateQueryForClosedInteractions = "doc_closed_at>=" + windowStartTime.getValue() + '^' + 'doc_closed_at<=' + windowEndTime.getValue();
      interactionsClosed.addQuery(dateQueryForClosedInteractions);
      interactionsClosed.addEncodedQuery('doc_state=closed_complete^ORdoc_state=closed_abandoned');
      interactionsClosed.groupBy('wi_queue');
      interactionsClosed.addAggregate('COUNT');
      interactionsClosed.addAggregate('AVG', 'doc_duration');
      interactionsClosed.query();
      while (interactionsClosed.next()) {
          this._addValueToKey(queueStats, interactionsClosed.getValue('wi_queue'), "interactions_closed", interactionsClosed.getAggregate('COUNT'));
          this._addValueToKey(queueStats, interactionsClosed.getValue('wi_queue'), "avg_duration_closed_interactions",
              new GlideDuration(interactionsClosed.getAggregate('AVG', 'doc_duration')).getNumericValue() / 1000);
      }
      map["queueStats"] = queueStats;
      map["statDefaultValue"] = statDefaultValue;
      return map;
  },

  getChannelStats: function(channelVsGroups, windowStartTime, windowEndTime) {
  	var map = {};
  	map["channelStats"] = {};
      map["statDefaultValue"] = {};
      return map;
  },

  getInstanceStats: function(windowStartTime, windowEndTime) {
      var map = {};
      var vaInteractionsInLast15Min = this._queryAndGetAggregateCount('interaction', 'type=chat^virtual_agent=true^sys_created_onRELATIVEGE@minute@ago@15', null, null, 'COUNT');
      var vaInteractionsInLastHour = this._queryAndGetAggregateCount('interaction', 'type=chat^virtual_agent=true^sys_created_onRELATIVEGE@hour@ago@1', null, null, 'COUNT');
      var dateQuery = "closed_at>=" + windowStartTime.getValue() + '^' + 'closed_at<=' + windowEndTime.getValue();
      var vaInteractionsResolved = this._queryAndGetAggregateCount('interaction', 'type=chat^virtual_agent=true^sys_created_onRELATIVEGE@hour@ago@1', dateQuery, null, 'COUNT');
      dateQuery = "live_handoff_time>=" + windowStartTime.getValue() + '^' + 'live_handoff_time<=' + windowEndTime.getValue();
      var vaInteractionsTransferred = this._queryAndGetAggregateCount('interaction', 'type=chat^virtual_agent=true^agent_chat=true', dateQuery, null, 'COUNT');
      map["va_interactions_received_in_last_15_minutes"] = vaInteractionsInLast15Min;
      map["va_interactions_received_in_last_hour"] = vaInteractionsInLastHour;
      map["va_interactions_resolved"] = vaInteractionsResolved;
      map["va_interactions_transferred"] = vaInteractionsTransferred;
      return map;
  },

  type: 'InteractionStatsProvider'
};

Sys ID

4b09def295b7d010f8773ca1dcdbe6d3

Offical Documentation

Official Docs: