Skip to content

Commit

Permalink
change aggs to run on secondary
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Nov 9, 2022
1 parent 23076e0 commit 202dfad
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 267 deletions.
3 changes: 2 additions & 1 deletion apps/meteor/app/models/server/models/LivechatInquiry.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 202dfad

Please sign in to comment.