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] Return transcript/dashboards based on timezone settings #22850

Merged
merged 19 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
22 changes: 22 additions & 0 deletions app/lib/server/startup/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,28 @@ settings.addGroup('General', function() {
type: 'boolean',
});
});
this.section('Timezone', function() {
this.add('Default_Timezone_For_Reporting', 'server', {
type: 'select',
values: [{
key: 'server',
i18nLabel: 'Default_Server_Timezone',
}, {
key: 'custom',
i18nLabel: 'Default_Custom_Timezone',
}, {
key: 'user',
i18nLabel: 'Default_User_Timezone',
}],
});
this.add('Default_Custom_Timezone', '', {
type: 'timezone',
enableQuery: {
_id: 'Default_Timezone_For_Reporting',
value: 'custom',
},
});
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
});
});

settings.addGroup('Message', function() {
Expand Down
8 changes: 5 additions & 3 deletions app/livechat/client/lib/chartHandler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';

const lineChartConfiguration = ({ legends = false, anim = false, smallTicks = false }) => {
const lineChartConfiguration = ({ legends = false, anim = false, smallTicks = false, displayColors = false, tooltipCallbacks = {} }) => {
const config = {
layout: {
padding: {
Expand All @@ -17,7 +17,8 @@ const lineChartConfiguration = ({ legends = false, anim = false, smallTicks = fa
tooltips: {
enabled: true,
mode: 'point',
displayColors: false, // hide color box
displayColors,
...tooltipCallbacks,
},
scales: {
xAxes: [{
Expand Down Expand Up @@ -81,7 +82,7 @@ const lineChartConfiguration = ({ legends = false, anim = false, smallTicks = fa
};


const doughnutChartConfiguration = (title) => ({
const doughnutChartConfiguration = (title, tooltipCallbacks = {}) => ({
layout: {
padding: {
top: 0,
Expand All @@ -104,6 +105,7 @@ const doughnutChartConfiguration = (title) => ({
enabled: true,
mode: 'point',
displayColors: false, // hide color box
...tooltipCallbacks,
},
// animation: {
// duration: 0 // general animation time
Expand Down
13 changes: 10 additions & 3 deletions app/livechat/imports/server/rest/dashboards.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getAgentsProductivityMetrics,
getChatsMetrics,
} from '../../../server/lib/analytics/dashboards';
import { Users } from '../../../../models';

API.v1.addRoute('livechat/analytics/dashboards/conversation-totalizers', { authRequired: true }, {
get() {
Expand All @@ -36,7 +37,9 @@ API.v1.addRoute('livechat/analytics/dashboards/conversation-totalizers', { authR
}
end = new Date(end);

const totalizers = getConversationsMetrics({ start, end, departmentId });
const user = Users.findOneById(this.userId, { fields: { utcOffset: 1, language: 1 } });

const totalizers = getConversationsMetrics({ start, end, departmentId, user });
return API.v1.success(totalizers);
},
});
Expand All @@ -63,7 +66,9 @@ API.v1.addRoute('livechat/analytics/dashboards/agents-productivity-totalizers',
}
end = new Date(end);

const totalizers = getAgentsProductivityMetrics({ start, end, departmentId });
const user = Users.findOneById(this.userId, { fields: { utcOffset: 1, language: 1 } });

const totalizers = getAgentsProductivityMetrics({ start, end, departmentId, user });
return API.v1.success(totalizers);
},
});
Expand Down Expand Up @@ -117,7 +122,9 @@ API.v1.addRoute('livechat/analytics/dashboards/productivity-totalizers', { authR
}
end = new Date(end);

const totalizers = getProductivityMetrics({ start, end, departmentId });
const user = Users.findOneById(this.userId, { fields: { utcOffset: 1, language: 1 } });

const totalizers = getProductivityMetrics({ start, end, departmentId, user });

return API.v1.success(totalizers);
},
Expand Down
148 changes: 98 additions & 50 deletions app/livechat/server/lib/Analytics.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,85 @@
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import moment from 'moment';

import { LivechatRooms } from '../../../models';
import { secondsToHHMMSS } from '../../../utils/server';
import { getTimezone } from '../../../utils/server/lib/getTimezone';
import { Logger } from '../../../logger';

const HOURS_IN_DAY = 24;
const logger = new Logger('OmnichannelAnalytics');

export const Analytics = {
getAgentOverviewData(options) {
const from = moment(options.daterange.from);
const to = moment(options.daterange.to);
const { departmentId, utcOffset, daterange: { from: fDate, to: tDate } = {}, chartOptions: { name } = {} } = options;
const timezone = getTimezone({ utcOffset });
const from = moment.tz(fDate, 'YYYY-MM-DD', timezone).startOf('day').utc();
const to = moment.tz(tDate, 'YYYY-MM-DD', timezone).endOf('day').utc();

logger.debug(`getAgentOverviewData[${ name }] -> Using timezone ${ timezone } with date range ${ from } - ${ to }`);

if (!(moment(from).isValid() && moment(to).isValid())) {
console.log('livechat:getAgentOverviewData => Invalid dates');
logger.error('livechat:getAgentOverviewData => Invalid dates');
return;
}

if (!this.AgentOverviewData[options.chartOptions.name]) {
console.log(`Method RocketChat.Livechat.Analytics.AgentOverviewData.${ options.chartOptions.name } does NOT exist`);
if (!this.AgentOverviewData[name]) {
logger.error(`Method RocketChat.Livechat.Analytics.AgentOverviewData.${ name } does NOT exist`);
return;
}

const { departmentId } = options;

return this.AgentOverviewData[options.chartOptions.name](from, to, departmentId);
return this.AgentOverviewData[name](from, to, departmentId);
},

getAnalyticsChartData(options) {
const {
utcOffset,
departmentId,
daterange: { from: fDate, to: tDate } = {},
chartOptions: { name: chartLabel },
chartOptions: { name } = {},
} = options;

// Check if function exists, prevent server error in case property altered
if (!this.ChartData[options.chartOptions.name]) {
console.log(`Method RocketChat.Livechat.Analytics.ChartData.${ options.chartOptions.name } does NOT exist`);
if (!this.ChartData[name]) {
logger.error(`Method RocketChat.Livechat.Analytics.ChartData.${ name } does NOT exist`);
return;
}

const from = moment(options.daterange.from);
const to = moment(options.daterange.to);
const timezone = getTimezone({ utcOffset });
const from = moment.tz(fDate, 'YYYY-MM-DD', timezone).startOf('day').utc();
const to = moment.tz(tDate, 'YYYY-MM-DD', timezone).endOf('day').utc();
const isSameDay = from.diff(to, 'days') === 0;

logger.debug(`getAnalyticsChartData[${ name }] -> Using timezone ${ timezone } with date range ${ from } - ${ to }`);

if (!(moment(from).isValid() && moment(to).isValid())) {
console.log('livechat:getAnalyticsChartData => Invalid dates');
logger.error('livechat:getAnalyticsChartData => Invalid dates');
return;
}


const data = {
chartLabel: options.chartOptions.name,
chartLabel,
dataLabels: [],
dataPoints: [],
};

const { departmentId } = options;

if (from.diff(to) === 0) { // data for single day
for (let m = moment(from); m.diff(to, 'days') <= 0; m.add(1, 'hours')) {
const hour = m.format('H');
data.dataLabels.push(`${ moment(hour, ['H']).format('hA') }-${ moment((parseInt(hour) + 1) % 24, ['H']).format('hA') }`);
if (isSameDay) { // data for single day
for (let m = moment(from), currentHour = 0; currentHour < HOURS_IN_DAY; currentHour++) {
const hour = m.add(currentHour ? 1 : 0, 'hour').format('H');
const label = {
from: moment.utc().set({ hour }).tz(timezone).format('hA'),
to: moment.utc().set({ hour }).add(1, 'hour').tz(timezone).format('hA'),
};
data.dataLabels.push(`${ label.from }-${ label.to }`);

const date = {
gte: m,
lt: moment(m).add(1, 'hours'),
};

data.dataPoints.push(this.ChartData[options.chartOptions.name](date, departmentId));
data.dataPoints.push(this.ChartData[name](date, departmentId));
}
} else {
for (let m = moment(from); m.diff(to, 'days') <= 0; m.add(1, 'days')) {
Expand All @@ -68,29 +90,40 @@ export const Analytics = {
lt: moment(m).add(1, 'days'),
};

data.dataPoints.push(this.ChartData[options.chartOptions.name](date, departmentId));
data.dataPoints.push(this.ChartData[name](date, departmentId));
}
}

return data;
},

getAnalyticsOverviewData(options) {
const from = moment(options.daterange.from);
const to = moment(options.daterange.to);
const { departmentId } = options;
const {
departmentId,
utcOffset = 0,
language,
daterange: { from: fDate, to: tDate } = {},
analyticsOptions: { name } = {},
} = options;
const timezone = getTimezone({ utcOffset });
const from = moment.tz(fDate, 'YYYY-MM-DD', timezone).startOf('day').utc();
const to = moment.tz(tDate, 'YYYY-MM-DD', timezone).endOf('day').utc();

logger.debug(`getAnalyticsOverviewData[${ name }] -> Using timezone ${ timezone } with date range ${ from } - ${ to }`);

if (!(moment(from).isValid() && moment(to).isValid())) {
console.log('livechat:getAnalyticsOverviewData => Invalid dates');
logger.error('livechat:getAnalyticsOverviewData => Invalid dates');
return;
}

if (!this.OverviewData[options.analyticsOptions.name]) {
console.log(`Method RocketChat.Livechat.Analytics.OverviewData.${ options.analyticsOptions.name } does NOT exist`);
if (!this.OverviewData[name]) {
logger.error(`Method RocketChat.Livechat.Analytics.OverviewData.${ name } does NOT exist`);
return;
}

return this.OverviewData[options.analyticsOptions.name](from, to, departmentId);
const t = (s) => TAPi18n.__(s, { lng: language });

return this.OverviewData[name](from, to, departmentId, timezone, t);
},

ChartData: {
Expand Down Expand Up @@ -122,10 +155,14 @@ export const Analytics = {
Total_messages(date, departmentId) {
let total = 0;

LivechatRooms.getAnalyticsMetricsBetweenDate('l', date, { departmentId }).forEach(({ msgs }) => {
// we don't want to count visitor messages
const extraFilter = { $lte: ['$token', null] };
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
const allConversations = Promise.await(LivechatRooms.getAnalyticsMetricsBetweenDateWithMessages('l', date, { departmentId }, extraFilter).toArray());
allConversations.map(({ msgs }) => {
if (msgs) {
total += msgs;
}
return null;
});

return total;
Expand Down Expand Up @@ -242,7 +279,8 @@ export const Analytics = {
*
* @returns {Array[Object]}
*/
Conversations(from, to, departmentId) {
Conversations(from, to, departmentId, timezone, t = (v) => v) {
// TODO: most calls to db here can be done in one single call instead of one per day/hour
let totalConversations = 0; // Total conversations
let openConversations = 0; // open conversations
let totalMessages = 0; // total msgs
Expand All @@ -260,28 +298,30 @@ export const Analytics = {
totalMessagesOnWeekday.set(weekday, totalMessagesOnWeekday.has(weekday) ? totalMessagesOnWeekday.get(weekday) + msgs : msgs);
};

for (let m = moment(from); m.diff(to, 'days') <= 0; m.add(1, 'days')) {
for (let m = moment.tz(from, timezone).startOf('day').utc(), daysProcessed = 0; daysProcessed < days; daysProcessed++) {
const clonedDate = m.clone();
const date = {
gte: m,
lt: moment(m).add(1, 'days'),
gte: clonedDate,
lt: m.add(1, 'days'),
};

const result = Promise.await(LivechatRooms.getAnalyticsBetweenDate(date, { departmentId }).toArray());
totalConversations += result.length;

result.forEach(summarize(m));
result.forEach(summarize(clonedDate));
}

const busiestDay = this.getKeyHavingMaxValue(totalMessagesOnWeekday, '-'); // returns key with max value

// 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 (let m = moment(from).day(busiestDay); m <= to; m.add(7, 'days')) {
for (let m = moment.tz(from, timezone).day(busiestDay).startOf('day').utc(); m <= to; m.add(7, 'days')) {
if (m < from) { continue; }

for (let h = moment(m); h.diff(m, 'days') <= 0; h.add(1, 'hours')) {
for (let h = moment(m), currentHour = 0; currentHour < 24; currentHour++) {
const date = {
gte: h,
lt: moment(h).add(1, 'hours'),
gte: h.clone(),
lt: h.add(1, 'hours'),
};
Promise.await(LivechatRooms.getAnalyticsBetweenDate(date, { departmentId }).toArray()).forEach(({
msgs,
Expand All @@ -292,7 +332,11 @@ export const Analytics = {
}
}

const busiestHour = this.getKeyHavingMaxValue(totalMessagesInHour, -1);
const utcBusiestHour = this.getKeyHavingMaxValue(totalMessagesInHour, -1);
const busiestHour = {
to: utcBusiestHour >= 0 ? moment.utc().set({ hour: utcBusiestHour }).tz(timezone).format('hA') : '-',
from: utcBusiestHour >= 0 ? moment.utc().set({ hour: utcBusiestHour }).subtract(1, 'hour').tz(timezone).format('hA') : '',
};

const data = [{
title: 'Total_conversations',
Expand All @@ -305,13 +349,13 @@ export const Analytics = {
value: totalMessages,
}, {
title: 'Busiest_day',
value: busiestDay,
value: t(busiestDay),
}, {
title: 'Conversations_per_day',
value: (totalConversations / days).toFixed(2),
}, {
title: 'Busiest_time',
value: busiestHour > 0 ? `${ moment(busiestHour, ['H']).format('hA') }-${ moment((parseInt(busiestHour) + 1) % 24, ['H']).format('hA') }` : '-',
value: `${ busiestHour.from }${ busiestHour.to ? `- ${ busiestHour.to }` : '' }`,
}];

return data;
Expand Down Expand Up @@ -417,13 +461,13 @@ export const Analytics = {
data: [],
};

LivechatRooms.getAnalyticsMetricsBetweenDate('l', date, { departmentId }).forEach(({
servedBy,
}) => {
if (servedBy) {
this.updateMap(agentConversations, servedBy.username, 1);
const allConversations = Promise.await(LivechatRooms.getAnalyticsMetricsBetweenDateWithMessages('l', date, { departmentId }).toArray());
allConversations.map((room) => {
if (room.servedBy) {
this.updateMap(agentConversations, room.servedBy.username, 1);
total++;
}
return null;
});

agentConversations.forEach((value, key) => { // calculate percentage
Expand Down Expand Up @@ -527,13 +571,17 @@ export const Analytics = {
data: [],
};

LivechatRooms.getAnalyticsMetricsBetweenDate('l', date, { departmentId }).forEach(({
// we don't want to count visitor messages
const extraFilter = { $lte: ['$token', null] };
const allConversations = Promise.await(LivechatRooms.getAnalyticsMetricsBetweenDateWithMessages('l', date, { departmentId }, extraFilter).toArray());
allConversations.map(({
servedBy,
msgs,
}) => {
if (servedBy) {
this.updateMap(agentMessages, servedBy.username, msgs);
}
return null;
});

agentMessages.forEach((value, key) => { // calculate percentage
Expand Down
Loading