|
|
|
|
@ -1,7 +1,9 @@ |
|
|
|
|
import moment from 'moment'; |
|
|
|
|
|
|
|
|
|
import Analytics from '../../../../../app/models/server/raw/Analytics'; |
|
|
|
|
import AnalyticsRaw from '../../../../../app/models/server/raw/Analytics'; |
|
|
|
|
import { roomTypes } from '../../../../../app/utils'; |
|
|
|
|
import { Messages } from '../../../../../app/models/server/raw'; |
|
|
|
|
import { Analytics } from '../../../../../app/models/server'; |
|
|
|
|
import { convertDateToInt, diffBetweenDaysInclusive, convertIntToDate, getTotalOfWeekItems } from './date'; |
|
|
|
|
|
|
|
|
|
export const handleMessagesSent = (message, room) => { |
|
|
|
|
@ -9,7 +11,7 @@ export const handleMessagesSent = (message, room) => { |
|
|
|
|
if (!roomTypesToShow.includes(room.t)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
Promise.await(Analytics.saveMessageSent({ |
|
|
|
|
Promise.await(AnalyticsRaw.saveMessageSent({ |
|
|
|
|
date: convertDateToInt(message.ts), |
|
|
|
|
room, |
|
|
|
|
})); |
|
|
|
|
@ -21,25 +23,40 @@ export const handleMessagesDeleted = (message, room) => { |
|
|
|
|
if (!roomTypesToShow.includes(room.t)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
Promise.await(Analytics.saveMessageDeleted({ |
|
|
|
|
Promise.await(AnalyticsRaw.saveMessageDeleted({ |
|
|
|
|
date: convertDateToInt(message.ts), |
|
|
|
|
room, |
|
|
|
|
})); |
|
|
|
|
return message; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const fillFirstDaysOfMessagesIfNeeded = async (date) => { |
|
|
|
|
const messagesFromAnalytics = await AnalyticsRaw.findByTypeBeforeDate({ |
|
|
|
|
type: 'messages', |
|
|
|
|
date: convertDateToInt(date), |
|
|
|
|
}).toArray(); |
|
|
|
|
if (!messagesFromAnalytics.length) { |
|
|
|
|
const startOfPeriod = moment(convertIntToDate(date)).subtract(90, 'days').toDate(); |
|
|
|
|
const messages = await Messages.getTotalOfMessagesSentByDate({ |
|
|
|
|
start: startOfPeriod, |
|
|
|
|
end: date, |
|
|
|
|
}); |
|
|
|
|
messages.forEach((message) => Analytics.insert(message)); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const findWeeklyMessagesSentData = async ({ start, end }) => { |
|
|
|
|
const daysBetweenDates = diffBetweenDaysInclusive(end, start); |
|
|
|
|
const endOfLastWeek = moment(start).clone().subtract(1, 'days').toDate(); |
|
|
|
|
const startOfLastWeek = moment(endOfLastWeek).clone().subtract(daysBetweenDates, 'days').toDate(); |
|
|
|
|
const today = convertDateToInt(end); |
|
|
|
|
const yesterday = convertDateToInt(moment(end).clone().subtract(1, 'days').toDate()); |
|
|
|
|
const currentPeriodMessages = await Analytics.getMessagesSentTotalByDate({ |
|
|
|
|
const currentPeriodMessages = await AnalyticsRaw.getMessagesSentTotalByDate({ |
|
|
|
|
start: convertDateToInt(start), |
|
|
|
|
end: convertDateToInt(end), |
|
|
|
|
options: { count: daysBetweenDates, sort: { _id: -1 } }, |
|
|
|
|
}); |
|
|
|
|
const lastPeriodMessages = await Analytics.getMessagesSentTotalByDate({ |
|
|
|
|
const lastPeriodMessages = await AnalyticsRaw.getMessagesSentTotalByDate({ |
|
|
|
|
start: convertDateToInt(startOfLastWeek), |
|
|
|
|
end: convertDateToInt(endOfLastWeek), |
|
|
|
|
options: { count: daysBetweenDates, sort: { _id: -1 } }, |
|
|
|
|
@ -62,7 +79,7 @@ export const findWeeklyMessagesSentData = async ({ start, end }) => { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const findMessagesSentOrigin = async ({ start, end }) => { |
|
|
|
|
const origins = await Analytics.getMessagesOrigin({ |
|
|
|
|
const origins = await AnalyticsRaw.getMessagesOrigin({ |
|
|
|
|
start: convertDateToInt(start), |
|
|
|
|
end: convertDateToInt(end), |
|
|
|
|
}); |
|
|
|
|
@ -76,7 +93,7 @@ export const findMessagesSentOrigin = async ({ start, end }) => { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const findTopFivePopularChannelsByMessageSentQuantity = async ({ start, end }) => { |
|
|
|
|
const channels = await Analytics.getMostPopularChannelsByMessagesSentQuantity({ |
|
|
|
|
const channels = await AnalyticsRaw.getMostPopularChannelsByMessagesSentQuantity({ |
|
|
|
|
start: convertDateToInt(start), |
|
|
|
|
end: convertDateToInt(end), |
|
|
|
|
options: { count: 5, sort: { messages: -1 } }, |
|
|
|
|
|