Skip to content
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
127 changes: 69 additions & 58 deletions apps/meteor/server/services/omnichannel-analytics/OverviewData.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint-disable new-cap */
import type { IOmnichannelRoom, AtLeast } from '@rocket.chat/core-typings';
import type { IOmnichannelRoom } from '@rocket.chat/core-typings';
import type { ILivechatRoomsModel } from '@rocket.chat/model-typings';
import moment from 'moment-timezone';
import type { Filter } from 'mongodb';

import { secondsToHHMMSS } from '../../../lib/utils/secondsToHHMMSS';
import { weekIterator, hourIterator } from './utils';

type OverviewDataValidActions = 'Conversations' | 'Productivity';

Expand Down Expand Up @@ -47,6 +46,41 @@ export class OverviewData {
return maxKey;
}

sumAllMapKeys<T>(map: Map<T, number>): number {
let sum = 0;

map.forEach((value) => {
sum += value;
});

return sum;
}

getBusiestDay(map: Map<string, Map<string, number>>): string {
let mostMessages = -1;
let busiestDay = '-';

map.forEach((value, key) => {
const v = this.sumAllMapKeys(value);
if (v > mostMessages) {
mostMessages = v;
busiestDay = key;
}
});

return busiestDay;
}

getAllMapKeysSize<T>(map: Map<T, Map<T, number>>): number {
let size = 0;

[...map.keys()].forEach((key) => {
size += map.get(key)?.size || 0;
});

return size;
}

async Conversations(
from: moment.Moment,
to: moment.Moment,
Expand All @@ -55,73 +89,50 @@ export class OverviewData {
t = (v: string): string => v,
extraQuery: Filter<IOmnichannelRoom> = {},
) {
// TODO: most calls to db here can be done in one single call instead of one per day/hour
let totalConversations = 0; // Total conversations
const analyticsMap = new Map();
let openConversations = 0; // open conversations
let totalMessages = 0; // total msgs
const totalMessagesOnWeekday = new Map(); // total messages on weekdays i.e Monday, Tuesday...
const totalMessagesInHour = new Map(); // total messages in hour 0, 1, ... 23 of weekday
let totalConversations = 0; // Total conversations
const days = to.diff(from, 'days') + 1; // total days

const summarize =
(m: moment.Moment) =>
({ metrics, msgs, onHold = false }: AtLeast<IOmnichannelRoom, 'metrics' | 'msgs' | 'onHold'>) => {
if (metrics && !metrics.chatDuration && !onHold) {
openConversations++;
}
totalMessages += msgs;

const weekday = m.format('dddd'); // @string: Monday, Tuesday ...
totalMessagesOnWeekday.set(weekday, totalMessagesOnWeekday.has(weekday) ? totalMessagesOnWeekday.get(weekday) + msgs : msgs);
};

const m = moment.tz(from, timezone).startOf('day').utc();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const _ of Array(days).fill(0)) {
const clonedDate = m.clone();
const date = {
gte: clonedDate,
lt: m.add(1, 'days'),
};
// eslint-disable-next-line no-await-in-loop
// @ts-expect-error - Check extraquery usage on this func
const result = await this.roomsModel.getAnalyticsBetweenDate(date, { departmentId }, extraQuery).toArray();
totalConversations += result.length;

result.forEach(summarize(clonedDate));
}
const date = {
gte: moment.tz(from, timezone).startOf('day').utc(),
lt: moment.tz(to, timezone).endOf('day').utc(),
};

const busiestDay = this.getKeyHavingMaxValue<string>(totalMessagesOnWeekday, '-'); // returns key with max value
// @ts-expect-error - Check extraquery usage on this func
const cursor = this.roomsModel.getAnalyticsBetweenDate(date, { departmentId }, extraQuery);

for await (const room of cursor) {
totalConversations++;

// TODO: this code assumes the busiest day is the same every week, which may not be true
// This means that for periods larger than 1 week, the busiest hour won't be the "busiest hour"
// on the period, but the busiest hour on the busiest day. (sorry for busiest excess)
// iterate through all busiestDay in given date-range and find busiest hour
for await (const m of weekIterator(from, to, timezone)) {
if (m < from) {
continue;
if (room.metrics && !room.metrics.chatDuration && !room.onHold) {
openConversations++;
}
const creationDay = moment.tz(room.ts, timezone).format('DD-MM-YYYY'); // @string: 01-01-2021
const creationHour = moment.tz(room.ts, timezone).format('H'); // @int : 0, 1, ... 23

for await (const h of hourIterator(m)) {
const date = {
gte: h.clone(),
lt: h.add(1, 'hours'),
};
// @ts-expect-error - Check extraquery usage on this func
(await this.roomsModel.getAnalyticsBetweenDate(date, { departmentId }, extraQuery).toArray()).forEach(({ msgs }) => {
const dayHour = h.format('H'); // @int : 0, 1, ... 23
totalMessagesInHour.set(dayHour, totalMessagesInHour.has(dayHour) ? totalMessagesInHour.get(dayHour) + msgs : msgs);
});
if (!analyticsMap.has(creationDay)) {
analyticsMap.set(creationDay, new Map());
}

const dayMap = analyticsMap.get(creationDay);

if (!dayMap.has(creationHour)) {
dayMap.set(creationHour, 0);
}

dayMap.set(creationHour, dayMap.get(creationHour) ? dayMap.get(creationHour) + room.msgs : room.msgs);
totalMessages += room.msgs;
}

const utcBusiestHour = this.getKeyHavingMaxValue<number>(totalMessagesInHour, -1);
const busiestHour = {
to: utcBusiestHour >= 0 ? moment.tz(timezone).set({ hour: utcBusiestHour }).format('hA') : '',
from: utcBusiestHour >= 0 ? moment.tz(timezone).set({ hour: utcBusiestHour }).subtract(1, 'hour').format('hA') : '',
};
// @ts-expect-error - Check extraquery usage on this func
const onHoldConversations = await this.roomsModel.getOnHoldConversationsBetweenDate(from, to, departmentId, extraQuery);
const busiestDayFromMap = this.getBusiestDay(analyticsMap); // returns busiest day based on the number of messages sent on that day
const busiestHour = this.getKeyHavingMaxValue<number>(analyticsMap.get(busiestDayFromMap) || new Map(), -1); // returns key with max value
const busiestTimeFrom = busiestHour >= 0 ? moment.tz(`${busiestHour}`, 'H', timezone).format('hA') : ''; // @string: 12AM, 1AM ...
const busiestTimeTo = busiestHour >= 0 ? moment.tz(`${busiestHour}`, 'H', timezone).add(1, 'hour').format('hA') : ''; // @string: 1AM, 2AM ...
const busiestDay = busiestDayFromMap !== '-' ? moment.tz(busiestDayFromMap, 'DD-MM-YYYY', timezone).format('dddd') : ''; // @string: Monday, Tuesday ...

return [
{
Expand All @@ -142,15 +153,15 @@ export class OverviewData {
},
{
title: 'Busiest_day',
value: t(busiestDay),
value: t(busiestDay) || '-',
},
{
title: 'Conversations_per_day',
value: (totalConversations / days).toFixed(2),
},
{
title: 'Busiest_time',
value: `${busiestHour.from}${busiestHour.to ? ` - ${busiestHour.to}` : ''}`,
value: `${busiestTimeFrom}${busiestTimeTo ? ` - ${busiestTimeTo}` : ''}` || '-',
},
];
}
Expand Down
7 changes: 4 additions & 3 deletions apps/meteor/server/services/omnichannel-analytics/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import moment from 'moment';
const HOURS_IN_DAY = 24;

export async function* dayIterator(from: moment.Moment, to: moment.Moment): AsyncGenerator<moment.Moment> {
const m = moment(from).startOf('day');
while (m.diff(to, 'days') <= 0) {
yield moment(m);
const m = from.clone().startOf('day');
const f = to.clone().startOf('day');
while (m.diff(f, 'days') <= 0) {
yield m;
m.add(1, 'days');
}
}
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/tests/end-to-end/api/livechat/04-dashboards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,9 +780,9 @@ describe('LIVECHAT - dashboards', function () {
{ title: 'Open_conversations', value: 4 },
{ title: 'On_Hold_conversations', value: 1 },
// { title: 'Total_messages', value: 6 },
// { title: 'Busiest_day', value: moment().format('dddd') }, // TODO: need to check y this return a day before
// { title: 'Busiest_day', value: moment().format('dddd') },
{ title: 'Conversations_per_day', value: '3.50' },
{ title: 'Busiest_time', value: '' },
// { title: 'Busiest_time', value: '' },
];

expectedResult.forEach((expected) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ describe('OverviewData Analytics', () => {
});
it('should call the correct action with the correct parameters', async () => {
const overview = new OverviewData({
getAnalyticsBetweenDate: () => ({
toArray: () => [],
}),
getAnalyticsBetweenDate: () => [],
getOnHoldConversationsBetweenDate: () => 0,
getAnalyticsMetricsBetweenDate: () => [],
} as any);
Expand Down Expand Up @@ -75,12 +73,102 @@ describe('OverviewData Analytics', () => {
});
});

describe('getAllMapKeysSize', () => {
it('should return the sum of all map keys', () => {
const overview = new OverviewData({} as any);
const map = new Map();
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);
expect(overview.sumAllMapKeys(map)).to.be.equal(6);
});
it('should return 0 if the map is empty', () => {
const overview = new OverviewData({} as any);
const map = new Map();
expect(overview.sumAllMapKeys(map)).to.be.equal(0);
});
});

describe('getBusiestDay', () => {
it('should return the day with the most messages', () => {
const overview = new OverviewData({} as any);
const map = new Map();
map.set(
'Monday',
new Map([
['1', 1],
['2', 2],
]),
);
map.set(
'Tuesday',
new Map([
['13', 1],
['15', 2],
]),
);
map.set(
'Sunday',
new Map([
['12', 2],
['23', 2],
]),
);
expect(overview.getBusiestDay(map)).to.be.equal('Sunday');
});
it('should return the first day with the most messages if theres a tie', () => {
const overview = new OverviewData({} as any);
const map = new Map();
map.set(
'Monday',
new Map([
['1', 1],
['2', 2],
]),
);
map.set(
'Tuesday',
new Map([
['13', 1],
['15', 2],
]),
);
map.set(
'Sunday',
new Map([
['12', 1],
['23', 2],
]),
);
expect(overview.getBusiestDay(map)).to.be.equal('Monday');
});
it('should return the default key if the map is empty', () => {
const overview = new OverviewData({} as any);
const map = new Map();
expect(overview.getBusiestDay(map)).to.be.equal('-');
});
});

describe('sumAllMapKeys', () => {
it('should return the sum of all map keys', () => {
const overview = new OverviewData({} as any);
const map = new Map();
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);
expect(overview.sumAllMapKeys(map)).to.be.equal(6);
});
it('should return 0 if the map is empty', () => {
const overview = new OverviewData({} as any);
const map = new Map();
expect(overview.sumAllMapKeys(map)).to.be.equal(0);
});
});

describe('Conversations', () => {
it('should return all values as 0 when theres no data', async () => {
const overview = new OverviewData({
getAnalyticsBetweenDate: () => ({
toArray: () => [],
}),
getAnalyticsBetweenDate: () => [],
getOnHoldConversationsBetweenDate: () => 0,
} as any);
const result = await overview.Conversations(moment(), moment(), '', 'UTC', (v: string): string => v, {});
Expand All @@ -91,14 +179,12 @@ describe('OverviewData Analytics', () => {
{ title: 'Total_messages', value: 0 },
{ title: 'Busiest_day', value: '-' },
{ title: 'Conversations_per_day', value: '0.00' },
{ title: 'Busiest_time', value: '' },
{ title: 'Busiest_time', value: '-' },
]);
});
it('should return all values as 0 when theres data but not on the period we pass', async () => {
const overview = new OverviewData({
getAnalyticsBetweenDate: () => ({
toArray: () => analytics({ gte: moment().set('month', 9).toDate(), lt: moment().set('month', 9).toDate() }),
}),
getAnalyticsBetweenDate: () => analytics({ gte: moment().set('month', 9).toDate(), lt: moment().set('month', 9).toDate() }),
getOnHoldConversationsBetweenDate: () => 0,
} as any);
const result = await overview.Conversations(moment(), moment(), '', 'UTC', (v: string): string => v, {});
Expand All @@ -109,14 +195,12 @@ describe('OverviewData Analytics', () => {
{ title: 'Total_messages', value: 0 },
{ title: 'Busiest_day', value: '-' },
{ title: 'Conversations_per_day', value: '0.00' },
{ title: 'Busiest_time', value: '' },
{ title: 'Busiest_time', value: '-' },
]);
});
it('should return the correct values when theres data on the period we pass', async () => {
const overview = new OverviewData({
getAnalyticsBetweenDate: (date: { gte: Date; lt: Date }) => ({
toArray: () => analytics(date),
}),
getAnalyticsBetweenDate: (date: { gte: Date; lt: Date }) => analytics(date),
getOnHoldConversationsBetweenDate: () => 1,
} as any);

Expand Down