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

Chore: Change Omnichannel aggregations to run on a secondary #25346

Merged
merged 2 commits into from Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/meteor/app/models/server/models/LivechatInquiry.ts
@@ -1,6 +1,7 @@
import type { ILivechatInquiryRecord } from '@rocket.chat/core-typings';
import type { FindOptions, FindCursor, UpdateResult, DeleteResult } from 'mongodb';

import { readSecondaryPreferred } from '../../../../server/database/readSecondaryPreferred';
import { Base } from './_Base';

export class LivechatInquiry extends Base {
Expand Down Expand Up @@ -244,7 +245,7 @@ export class LivechatInquiry extends Base {
aggregate.push({ $match: { _id } });
}

return collectionObj.aggregate(aggregate).toArray();
return collectionObj.aggregate(aggregate, { readPreference: readSecondaryPreferred() }).toArray();
}

removeDefaultAgentById(inquiryId: string): UpdateResult {
Expand Down
257 changes: 132 additions & 125 deletions apps/meteor/app/models/server/models/LivechatRooms.js
Expand Up @@ -4,6 +4,7 @@ import { Settings } from '@rocket.chat/models';

import { Base } from './_Base';
import Rooms from './Rooms';
import { readSecondaryPreferred } from '../../../../server/database/readSecondaryPreferred';

export class LivechatRooms extends Base {
constructor(...args) {
Expand Down Expand Up @@ -494,152 +495,158 @@ export class LivechatRooms extends Base {
}

getAnalyticsMetricsBetweenDateWithMessages(t, date, { departmentId } = {}, extraQuery) {
return this.model.rawCollection().aggregate([
{
$match: {
t,
ts: {
$gte: new Date(date.gte), // ISO Date, ts >= date.gte
$lt: new Date(date.lt), // ISODate, ts < date.lt
return this.model.rawCollection().aggregate(
[
{
$match: {
t,
ts: {
$gte: new Date(date.gte), // ISO Date, ts >= date.gte
$lt: new Date(date.lt), // ISODate, ts < date.lt
},
...(departmentId && departmentId !== 'undefined' && { departmentId }),
},
...(departmentId && departmentId !== 'undefined' && { departmentId }),
},
},
{ $addFields: { roomId: '$_id' } },
{
$lookup: {
from: 'rocketchat_message',
// mongo doesn't like _id as variable name here :(
let: { roomId: '$roomId' },
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: ['$$roomId', '$rid'],
},
{
// this is similar to do { $exists: false }
$lte: ['$t', null],
},
...(extraQuery ? [extraQuery] : []),
],
{ $addFields: { roomId: '$_id' } },
{
$lookup: {
from: 'rocketchat_message',
// mongo doesn't like _id as variable name here :(
let: { roomId: '$roomId' },
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: ['$$roomId', '$rid'],
},
{
// this is similar to do { $exists: false }
$lte: ['$t', null],
},
...(extraQuery ? [extraQuery] : []),
],
},
},
},
},
],
as: 'messages',
},
},
{
$unwind: {
path: '$messages',
preserveNullAndEmptyArrays: true,
],
as: 'messages',
},
},
},
{
$group: {
_id: {
_id: '$_id',
ts: '$ts',
departmentId: '$departmentId',
open: '$open',
servedBy: '$servedBy',
metrics: '$metrics',
{
$unwind: {
path: '$messages',
preserveNullAndEmptyArrays: true,
},
messagesCount: {
$sum: 1,
},
{
$group: {
_id: {
_id: '$_id',
ts: '$ts',
departmentId: '$departmentId',
open: '$open',
servedBy: '$servedBy',
metrics: '$metrics',
},
messagesCount: {
$sum: 1,
},
},
},
},
{
$project: {
_id: '$_id._id',
ts: '$_id.ts',
departmentId: '$_id.departmentId',
open: '$_id.open',
servedBy: '$_id.servedBy',
metrics: '$_id.metrics',
msgs: '$messagesCount',
{
$project: {
_id: '$_id._id',
ts: '$_id.ts',
departmentId: '$_id.departmentId',
open: '$_id.open',
servedBy: '$_id.servedBy',
metrics: '$_id.metrics',
msgs: '$messagesCount',
},
},
},
]);
],
{ readPreference: readSecondaryPreferred() },
);
}

getAnalyticsBetweenDate(date, { departmentId } = {}) {
return this.model.rawCollection().aggregate([
{
$match: {
t: 'l',
ts: {
$gte: new Date(date.gte), // ISO Date, ts >= date.gte
$lt: new Date(date.lt), // ISODate, ts < date.lt
return this.model.rawCollection().aggregate(
[
{
$match: {
t: 'l',
ts: {
$gte: new Date(date.gte), // ISO Date, ts >= date.gte
$lt: new Date(date.lt), // ISODate, ts < date.lt
},
...(departmentId && departmentId !== 'undefined' && { departmentId }),
},
...(departmentId && departmentId !== 'undefined' && { departmentId }),
},
},
{ $addFields: { roomId: '$_id' } },
{
$lookup: {
from: 'rocketchat_message',
// mongo doesn't like _id as variable name here :(
let: { roomId: '$roomId' },
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: ['$$roomId', '$rid'],
},
{
// this is similar to do { $exists: false }
$lte: ['$t', null],
},
],
{ $addFields: { roomId: '$_id' } },
{
$lookup: {
from: 'rocketchat_message',
// mongo doesn't like _id as variable name here :(
let: { roomId: '$roomId' },
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: ['$$roomId', '$rid'],
},
{
// this is similar to do { $exists: false }
$lte: ['$t', null],
},
],
},
},
},
},
],
as: 'messages',
},
},
{
$unwind: {
path: '$messages',
preserveNullAndEmptyArrays: true,
],
as: 'messages',
},
},
},
{
$group: {
_id: {
_id: '$_id',
ts: '$ts',
departmentId: '$departmentId',
open: '$open',
servedBy: '$servedBy',
metrics: '$metrics',
onHold: '$onHold',
{
$unwind: {
path: '$messages',
preserveNullAndEmptyArrays: true,
},
messagesCount: {
$sum: 1,
},
{
$group: {
_id: {
_id: '$_id',
ts: '$ts',
departmentId: '$departmentId',
open: '$open',
servedBy: '$servedBy',
metrics: '$metrics',
onHold: '$onHold',
},
messagesCount: {
$sum: 1,
},
},
},
},
{
$project: {
_id: '$_id._id',
ts: '$_id.ts',
departmentId: '$_id.departmentId',
open: '$_id.open',
servedBy: '$_id.servedBy',
metrics: '$_id.metrics',
msgs: '$messagesCount',
onHold: '$_id.onHold',
{
$project: {
_id: '$_id._id',
ts: '$_id.ts',
departmentId: '$_id.departmentId',
open: '$_id.open',
servedBy: '$_id.servedBy',
metrics: '$_id.metrics',
msgs: '$messagesCount',
onHold: '$_id.onHold',
},
},
},
]);
],
{ readPreference: readSecondaryPreferred() },
);
}

closeByRoomId(roomId, closeInfo) {
Expand Down