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 9 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
7 changes: 5 additions & 2 deletions 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,13 @@ export class LivechatDepartmentAgents extends Base {

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

// get the current agent load, sorted by the username & current chat count
const currentAvailableAgents = Promise.await(Users.getAgentLoadByUsernames(onlineUsernames, extraQuery));
KevLehman marked this conversation as resolved.
Show resolved Hide resolved

const query = {
departmentId,
username: {
$in: onlineUsernames,
$in: currentAvailableAgents.map((u) => u.username),
},
...ignoreAgentId && { agentId: { $ne: ignoreAgentId } },
};
Expand Down
66 changes: 65 additions & 1 deletion app/models/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,16 @@ export class Users extends Base {
return this.find(query);
}

getNextAgent(ignoreAgentId) { // TODO: Create class Agent
getNextAgent(ignoreAgentId, extraQuery) { // TODO: Create class Agent
// fetch all available agents (available = haven't reached max active chats)
const availableAgents = Promise.await(this.getAgentLoadByUsernames(null, extraQuery));
const availableUsernames = availableAgents.map((u) => u.username);
const extraFilters = {
...ignoreAgentId && { _id: { $ne: ignoreAgentId } },
// limit query to consider only those agents
username: { $in: availableUsernames },
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
};

const query = queryStatusAgentOnline(extraFilters);

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

// get next agent ignoring the ones reached the max amount of active chats
getAgentLoadByUsernames(usernames, customFilter) {
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
const usernameFilter = Array.isArray(usernames) ? { username: { $in: usernames } } : {};
const col = this.model.rawCollection();
return col.aggregate([
{
$match: {
...usernameFilter,
status: { $exists: true, $ne: 'offline' },
statusLivechat: 'available',
roles: 'livechat-agent',
},
},
{
$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': { $lt: Number(departmentLimit) } } };
}
}

const maxChatsPerSetting = settings.get('Livechat_maximum_chats_per_agent') as number;
const agentFilter = { $and: [{ 'livechat.maxNumberSimultaneousChat': { $gt: 0 } }, { $expr: { $lt: ['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': { $lt: 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';