Skip to content

Commit

Permalink
Fixed analytics as per list
Browse files Browse the repository at this point in the history
Signed-off-by: knrt10 <tripathi.kautilya@gmail.com>
  • Loading branch information
knrt10 committed Sep 5, 2019
1 parent 35153cb commit fff4735
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import { FlowRouter } from 'meteor/kadira:flow-router';
Expand All @@ -11,7 +12,7 @@ import { setTimeRange } from '../../../lib/timeHandler';
import { visitorNavigationHistory } from '../../../collections/LivechatVisitorNavigation';
import { LivechatSession } from '../../../collections/LivechatSession';
import { RocketChatTabBar, popover } from '../../../../../ui-utils';
import { t } from '../../../../../utils';
import { t, handleError } from '../../../../../utils';

import './livechatRealTimeVisitorsDashboard.html';

Expand Down Expand Up @@ -43,28 +44,20 @@ const updateChartData = (chartId, label, data) => {
updateChart(chartContexts[chartId], label, data);
};

const updateChatsChart = () => {
const createdAt = {
$gte: moment().startOf('day').toDate(),
$lte: moment().endOf('day').toDate(),
};
const updateChatsChart = (query) => {
const chats = {
chatting: LivechatSession.find({ chatStatus: 'Chatting', createdAt }).count(),
notStarted: LivechatSession.find({ chatStatus: 'Not Started', createdAt }).count(),
closed: LivechatSession.find({ chatStatus: 'Closed', createdAt }).count(),
chatting: LivechatSession.find({ chatStatus: 'Chatting', ...query }).count(),
notStarted: LivechatSession.find({ chatStatus: 'Not Started', ...query }).count(),
closed: LivechatSession.find({ chatStatus: 'Closed', ...query }).count(),
};

updateChartData('lc-status-chart', 'Chatting', [chats.chatting]);
updateChartData('lc-status-chart', 'Not Started', [chats.notStarted]);
updateChartData('lc-status-chart', 'Closed', [chats.closed]);
};

const updateSessionOverviews = () => {
const createdAt = {
$gte: moment().startOf('day').toDate(),
$lte: moment().endOf('day').toDate(),
};
const data = getSessionOverviewData(LivechatSession.find({ createdAt }));
const updateSessionOverviews = (query) => {
const data = getSessionOverviewData(LivechatSession.find(query));

templateInstance.sessionOverview.set(data);
};
Expand Down Expand Up @@ -281,19 +274,28 @@ Template.livechatRealTimeVisitorsDashboard.onCreated(function() {
}
});

const updateSessionDashboard = (fields) => {
if (fields.chatStatus) {
updateChatsChart();
}
updateSessionOverviews();
const updateSessionDashboard = () => {
Meteor.call('livechat:getSessionFilter', this.filter.get(), function(err, result) {
if (err) {
return handleError(err);
}
updateChatsChart(result);
updateSessionOverviews(result);
});
};

this.autorun(() => {
if (this.filter.get()) {
updateSessionDashboard();
}
});

LivechatSession.find().observeChanges({
changed(id, fields) {
updateSessionDashboard(fields);
changed() {
updateSessionDashboard();
},
added(id, fields) {
updateSessionDashboard(fields);
added() {
updateSessionDashboard();
},
});
});
1 change: 1 addition & 0 deletions app/livechat/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import './methods/getAnalyticsOverviewData';
import './methods/getInitialData';
import './methods/getNextAgent';
import './methods/getRoutingConfig';
import './methods/getSessionFilter';
import './methods/loadHistory';
import './methods/loginByToken';
import './methods/pageVisited';
Expand Down
60 changes: 60 additions & 0 deletions app/livechat/server/methods/getSessionFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Meteor } from 'meteor/meteor';
import moment from 'moment';

import { hasPermission } from '../../../authorization';

Meteor.methods({
'livechat:getSessionFilter'(filter = {}) {
if (!this.userId) {
return this.error(new Meteor.Error('error-not-authorized', 'Not authorized', { publish: 'livechat:getSessionFilter' }));
}

if (!hasPermission(this.userId, 'view-livechat-manager')) {
return this.error(new Meteor.Error('error-not-authorized', 'Not authorized', { publish: 'livechat:getSessionFilter' }));
}

const { fromTime, toTime, valueTime } = filter;

if (!(moment(fromTime).isValid() && moment(toTime).isValid())) {
return this.error(new Meteor.Error('error-invalid-time', 'Invalid Time', { publish: 'livechat:sessions' }));
}

const query = {};
const timeFilter = ['last-thirty-minutes', 'last-hour', 'last-six-hour', 'last-twelve-hour'];
if (fromTime && toTime && valueTime) {
if (timeFilter.includes(valueTime)) {
query.createdAt = {
$gte: moment(toTime).toDate(),
$lt: moment(fromTime).toDate(),
};
} else {
const from = moment(fromTime).add(1, 'days');
const to = moment(toTime).add(1, 'days');
if (moment(from).diff(to) === 0) {
query.createdAt = {
$gte: moment(from).utcOffset(0).set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).toDate(),
$lt: moment(to).utcOffset(0).set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).add(1, 'days').toDate(),
};
} else {
query.createdAt = {
$gte: moment(from).utcOffset(0).set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).toDate(),
$lte: moment(to).utcOffset(0).set({ hour: 23, minute: 59, second: 59, millisecond: 0 }).toDate(),
};
}
}
}

if (filter.name) {
query['visitorInfo.name'] = new RegExp(filter.name, 'i');
}
if (filter.status) {
query.status = filter.status;
}

if (filter.chatStatus) {
query.chatStatus = filter.chatStatus;
}

return query;
},
});

0 comments on commit fff4735

Please sign in to comment.