Skip to content

Commit

Permalink
fix: Always attach avg first response time metric to the first agent …
Browse files Browse the repository at this point in the history
…who responded to the room
  • Loading branch information
matheusbsilva137 committed Dec 10, 2024
1 parent bf207b5 commit 954f8ea
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 16 deletions.
14 changes: 7 additions & 7 deletions apps/meteor/server/services/omnichannel-analytics/AgentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,15 @@ export class AgentOverviewData {
data: [],
};

await this.roomsModel.getAnalyticsMetricsBetweenDate('l', date, { departmentId }, extraQuery).forEach(({ metrics, servedBy }) => {
if (servedBy && metrics && metrics.response && metrics.response.ft) {
if (agentAvgRespTime.has(servedBy.username)) {
agentAvgRespTime.set(servedBy.username, {
frt: agentAvgRespTime.get(servedBy.username).frt + metrics.response.ft,
total: agentAvgRespTime.get(servedBy.username).total + 1,
await this.roomsModel.getAnalyticsMetricsBetweenDate('l', date, { departmentId }, extraQuery).forEach(({ metrics, responseBy }) => {
if (responseBy && metrics && metrics.response && metrics.response.ft) {
if (agentAvgRespTime.has(responseBy.username)) {
agentAvgRespTime.set(responseBy.username, {
frt: agentAvgRespTime.get(responseBy.username).frt + metrics.response.ft,
total: agentAvgRespTime.get(responseBy.username).total + 1,
});
} else {
agentAvgRespTime.set(servedBy.username, {
agentAvgRespTime.set(responseBy.username, {
frt: metrics.response.ft,
total: 1,
});
Expand Down
60 changes: 57 additions & 3 deletions apps/meteor/tests/end-to-end/api/livechat/04-dashboards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,18 +922,18 @@ describe('LIVECHAT - dashboards', function () {

describe('[livechat/analytics/agent-overview] - Average first response time', () => {
let agent: { credentials: Credentials; user: IUser & { username: string } };
let forwardAgent: { credentials: Credentials; user: IUser & { username: string } };
let originalFirstResponseTimeInSeconds: number;
let roomId: string;
const firstDelayInSeconds = 4;
const secondDelayInSeconds = 8;

before(async () => {
agent = await createAnOnlineAgent();
forwardAgent = await createAnOnlineAgent();
});

after(async () => {
await deleteUser(agent.user);
});
after(async () => Promise.all([deleteUser(agent.user), deleteUser(forwardAgent.user)]));

it('should return no average response time for an agent if no response has been sent in the period', async () => {
await startANewLivechatRoomAndTakeIt({ agent: agent.credentials });
Expand Down Expand Up @@ -984,6 +984,60 @@ describe('LIVECHAT - dashboards', function () {
expect(originalFirstResponseTimeInSeconds).to.be.greaterThanOrEqual(firstDelayInSeconds);
});

it('should correctly associate the first response time to the first agent who responded the room', async () => {
const response = await startANewLivechatRoomAndTakeIt({ agent: forwardAgent.credentials });
roomId = response.room._id;

await sendAgentMessage(roomId, 'first response from agent', forwardAgent.credentials);

await request
.post(api('livechat/room.forward'))
.set(credentials)
.send({
roomId,
userId: agent.user._id,
comment: 'test comment',
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res: Response) => {
expect(res.body).to.have.property('success', true);
});

const today = moment().startOf('day').format('YYYY-MM-DD');
const result = await request
.get(api('livechat/analytics/agent-overview'))
.query({ from: today, to: today, name: 'Avg_first_response_time' })
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200);

expect(result.body).to.have.property('success', true);
expect(result.body).to.have.property('head');
expect(result.body).to.have.property('data');
expect(result.body.data).to.be.an('array');

// The agent to whom the room has been forwarded shouldn't have their average first response time changed
const agentData = result.body.data.find(
(agentOverviewData: { name: string; value: string }) => agentOverviewData.name === agent.user.username,
);
expect(agentData).to.not.be.undefined;
expect(agentData).to.have.property('name', agent.user.username);
expect(agentData).to.have.property('value');
const averageFirstResponseTimeInSeconds = moment.duration(agentData.value).asSeconds();
expect(originalFirstResponseTimeInSeconds).to.be.equal(averageFirstResponseTimeInSeconds);

// A room's first response time should be attached to the agent who first responded to it even if it has been forwarded
const forwardAgentData = result.body.data.find(
(agentOverviewData: { name: string; value: string }) => agentOverviewData.name === forwardAgent.user.username,
);
expect(forwardAgentData).to.not.be.undefined;
expect(forwardAgentData).to.have.property('name', forwardAgent.user.username);
expect(forwardAgentData).to.have.property('value');
const forwardAgentAverageFirstResponseTimeInSeconds = moment.duration(forwardAgentData.value).asSeconds();
expect(originalFirstResponseTimeInSeconds).to.be.greaterThan(forwardAgentAverageFirstResponseTimeInSeconds);
});

it('should correctly calculate the average time of first responses for an agent', async () => {
const response = await startANewLivechatRoomAndTakeIt({ agent: agent.credentials });
roomId = response.room._id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ describe('AgentData Analytics', () => {
getAnalyticsMetricsBetweenDate(_params: ILivechatRoomsModel['getAnalyticsMetricsBetweenDate']) {
return [
{
servedBy: {
responseBy: {
username: 'agent 1',
},
metrics: {
Expand Down Expand Up @@ -772,7 +772,7 @@ describe('AgentData Analytics', () => {
getAnalyticsMetricsBetweenDate(_params: ILivechatRoomsModel['getAnalyticsMetricsBetweenDate']) {
return [
{
servedBy: {
responseBy: {
username: 'agent 1',
},
metrics: {
Expand All @@ -782,7 +782,7 @@ describe('AgentData Analytics', () => {
},
},
{
servedBy: {
responseBy: {
username: 'agent 2',
},
metrics: {
Expand Down Expand Up @@ -824,6 +824,9 @@ describe('AgentData Analytics', () => {
return [
{
servedBy: {
username: 'agent 3',
},
responseBy: {
username: 'agent 1',
},
metrics: {
Expand All @@ -834,6 +837,9 @@ describe('AgentData Analytics', () => {
},
{
servedBy: {
username: 'agent 4',
},
responseBy: {
username: 'agent 2',
},
metrics: {
Expand All @@ -844,6 +850,9 @@ describe('AgentData Analytics', () => {
},
{
servedBy: {
username: 'agent 5',
},
responseBy: {
username: 'agent 1',
},
metrics: {
Expand Down Expand Up @@ -879,12 +888,12 @@ describe('AgentData Analytics', () => {
],
});
});
it('should ignore conversations not being served by any agent', async () => {
it('should ignore conversations not responded by any agent', async () => {
const modelMock = {
getAnalyticsMetricsBetweenDate(_params: ILivechatRoomsModel['getAnalyticsMetricsBetweenDate']) {
return [
{
servedBy: undefined,
responseBy: undefined,
metrics: {
response: {
ft: 100,
Expand Down Expand Up @@ -914,7 +923,7 @@ describe('AgentData Analytics', () => {
getAnalyticsMetricsBetweenDate(_params: ILivechatRoomsModel['getAnalyticsMetricsBetweenDate']) {
return [
{
servedBy: {
responseBy: {
username: 'agent 1',
},
metrics: undefined,
Expand Down

0 comments on commit 954f8ea

Please sign in to comment.