Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX][ENTERPRISE] Make AutoSelect algo take current agent load in consideration #22611

Merged
merged 12 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/livechat/server/lib/routing/AutoSelection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RoutingManager } from '../RoutingManager';
import { LivechatDepartmentAgents, Users } from '../../../../models/server';
import { callbacks } from '../../../../callbacks';

/* Auto Selection Queuing method:
*
Expand All @@ -20,11 +21,12 @@ class AutoSelection {
}

getNextAgent(department, ignoreAgentId) {
const extraQuery = callbacks.run('livechat.applySimultaneousChatRestrictions', { ...department ? { departmentId: department } : {} });
if (department) {
return LivechatDepartmentAgents.getNextAgentForDepartment(department, ignoreAgentId);
return LivechatDepartmentAgents.getNextAgentForDepartment(department, ignoreAgentId, extraQuery);
}

return Users.getNextAgent(ignoreAgentId);
return Users.getNextAgent(ignoreAgentId, extraQuery);
}
}

Expand Down
6 changes: 5 additions & 1 deletion app/models/server/models/LivechatDepartmentAgents.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class LivechatDepartmentAgents extends Base {
this.remove({ departmentId });
}

getNextAgentForDepartment(departmentId, ignoreAgentId) {
getNextAgentForDepartment(departmentId, ignoreAgentId, extraQuery) {
const agents = this.findByDepartmentId(departmentId).fetch();

if (agents.length === 0) {
Expand All @@ -65,10 +65,14 @@ export class LivechatDepartmentAgents extends Base {

const onlineUsernames = _.pluck(onlineUsers.fetch(), 'username');

// get fully booked agents, to ignore them from the query
const currentUnavailableAgents = Promise.await(Users.getUnavailableAgents(departmentId, extraQuery)).map((u) => u.username);
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved

const query = {
departmentId,
username: {
$in: onlineUsernames,
$nin: currentUnavailableAgents,
},
...ignoreAgentId && { agentId: { $ne: ignoreAgentId } },
};
Expand Down
80 changes: 79 additions & 1 deletion app/models/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,15 @@ export class Users extends Base {
return this.find(query);
}

getNextAgent(ignoreAgentId) { // TODO: Create class Agent
getNextAgent(ignoreAgentId, extraQuery) { // TODO: Create class Agent
// fetch all unavailable agents, and exclude them from the selection
const unavailableAgents = Promise.await(this.getUnavailableAgents(null, extraQuery)).map((u) => u.username);
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
const extraFilters = {
...ignoreAgentId && { _id: { $ne: ignoreAgentId } },
// limit query to remove booked agents
username: { $nin: unavailableAgents },
};

const query = queryStatusAgentOnline(extraFilters);

const collectionObj = this.model.rawCollection();
Expand All @@ -208,6 +213,79 @@ export class Users extends Base {
return null;
}

// get next agent ignoring the ones reached the max amount of active chats
getUnavailableAgents(departmentId, customFilter) {
const col = this.model.rawCollection();
// if department is provided, remove the agents that are not from the selected department
const departmentFilter = departmentId ? [{
$lookup: {
from: 'rocketchat_livechat_department_agent',
let: { departmentId: '$departmentId', agentId: '$agentId' },
pipeline: [{
$match: { $expr: { $eq: ['$$agentId', '$_id'] } },
}, {
$match: { $expr: { $eq: ['$$departmentId', departmentId] } },
}],
as: 'department',
},
}, {
$match: { department: { $size: 1 } },
}] : [];

return col.aggregate([
{
$match: {
status: { $exists: true, $ne: 'offline' },
statusLivechat: 'available',
roles: 'livechat-agent',
},
},
...departmentFilter,
{
$lookup: {
from: 'rocketchat_subscription',
localField: '_id',
foreignField: 'u._id',
as: 'subs',
},
},
{
$project: {
agentId: '$_id',
'livechat.maxNumberSimultaneousChat': 1,
username: 1,
lastAssignTime: 1,
lastRoutingTime: 1,
'queueInfo.chats': {
$size: {
$filter: {
input: '$subs',
as: 'sub',
cond: {
$and: [
{ $eq: ['$$sub.t', 'l'] },
{ $eq: ['$$sub.open', true] },
{ $ne: ['$$sub.onHold', true] },
],
},
},
},
},
},
},
...customFilter ? [customFilter] : [],
{
$sort: {
'queueInfo.chats': 1,
lastAssignTime: 1,
lastRoutingTime: 1,
username: 1,
},
},
]).toArray();
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
}


getNextBotAgent(ignoreAgentId) { // TODO: Create class Agent
const query = {
roles: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { callbacks } from '../../../../../app/callbacks/server';
import { LivechatDepartment } from '../../../../../app/models/server';
import { settings } from '../../../../../app/settings/server';

callbacks.add('livechat.applySimultaneousChatRestrictions', ({ departmentId }: { departmentId?: string }) => {
if (departmentId) {
const departmentLimit = LivechatDepartment.findOneById(departmentId)?.maxNumberSimultaneousChat || 0;
if (departmentLimit > 0) {
return { $match: { 'queueInfo.chats': { $gte: Number(departmentLimit) } } };
}
}

const maxChatsPerSetting = settings.get('Livechat_maximum_chats_per_agent') as number;
const agentFilter = { $and: [{ 'livechat.maxNumberSimultaneousChat': { $gt: 0 } }, { $expr: { $gte: ['queueInfo.chats', 'livechat.maxNumberSimultaneousChat'] } }] };
// apply filter only if agent setting is 0 or is disabled
const globalFilter = maxChatsPerSetting > 0
? {
$and: [
{
$or: [
{
'livechat.maxNumberSimultaneousChat': { $exists: false },
},
{ 'livechat.maxNumberSimultaneousChat': 0 },
],
},
{ 'queueInfo.chats': { $gte: maxChatsPerSetting } },
],
}
: {};

return { $match: { $or: [agentFilter, globalFilter] } };
}, callbacks.priority.HIGH, 'livechat-apply-simultaneous-restrictions');
1 change: 1 addition & 0 deletions ee/app/livechat-enterprise/server/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ import './afterOnHoldChatResumed';
import './afterReturnRoomAsInquiry';
import './applyDepartmentRestrictions';
import './afterForwardChatToAgent';
import './applySimultaneousChatsRestrictions';