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: Replace some cursor.count calls with countDocuments or estimatedDocumentCount #33671

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 8 additions & 8 deletions apps/meteor/app/statistics/server/lib/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ export const statistics = {

// Room statistics
statistics.totalRooms = await Rooms.col.countDocuments({});
statistics.totalChannels = await Rooms.findByType('c').count();
statistics.totalPrivateGroups = await Rooms.findByType('p').count();
statistics.totalDirect = await Rooms.findByType('d').count();
statistics.totalLivechat = await Rooms.findByType('l').count();
statistics.totalChannels = await Rooms.countByType('c');
statistics.totalPrivateGroups = await Rooms.countByType('p');
statistics.totalDirect = await Rooms.countByType('d');
statistics.totalLivechat = await Rooms.countByType('l');
statistics.totalDiscussions = await Rooms.countDiscussions();
statistics.totalThreads = await Messages.countThreads();

Expand Down Expand Up @@ -183,7 +183,7 @@ export const statistics = {

// Number of triggers
statsPms.push(
LivechatTrigger.col.count().then((count) => {
LivechatTrigger.estimatedDocumentCount().then((count) => {
statistics.totalTriggers = count;
}),
);
Expand All @@ -205,13 +205,13 @@ export const statistics = {

// Number of Email Inboxes
statsPms.push(
EmailInbox.col.count().then((count) => {
EmailInbox.estimatedDocumentCount().then((count) => {
statistics.emailInboxes = count;
}),
);

statsPms.push(
LivechatBusinessHours.col.count().then((count) => {
LivechatBusinessHours.estimatedDocumentCount().then((count) => {
statistics.BusinessHours = {
// Number of Business Hours
total: count,
Expand Down Expand Up @@ -556,7 +556,7 @@ export const statistics = {
statistics.totalUserEmail2fa = await Users.countActiveUsersEmail2faEnable({ readPreference });
statistics.totalPinned = await Messages.findPinned({ readPreference }).count();
statistics.totalStarred = await Messages.findStarred({ readPreference }).count();
statistics.totalLinkInvitation = await Invites.find().count();
statistics.totalLinkInvitation = await Invites.estimatedDocumentCount();
statistics.totalLinkInvitationUses = await Invites.countUses();
statistics.totalEmailInvitation = settings.get('Invitation_Email_Count');
statistics.totalE2ERooms = await Rooms.findByE2E({ readPreference }).count();
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"scripts": {
"start": "meteor",
"build:ci": "METEOR_DISABLE_OPTIMISTIC_CACHING=1 meteor build --server-only --directory /tmp/dist",
"dev": "meteor --exclude-archs \"web.browser.legacy, web.cordova\"",
"dev": "NODE_OPTIONS=\"--trace-warnings\" meteor --exclude-archs \"web.browser.legacy, web.cordova\"",
"dsv": "meteor npm run dev",
"ha": "meteor npm run ha:start",
"ms": "TRANSPORTER=${TRANSPORTER:-TCP} meteor npm run dev",
Expand Down
6 changes: 5 additions & 1 deletion apps/meteor/server/models/raw/BaseRaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type {
DeleteResult,
DeleteOptions,
FindOneAndDeleteOptions,
CountDocumentsOptions,
} from 'mongodb';

import { setUpdatedAt } from './setUpdatedAt';
Expand Down Expand Up @@ -497,7 +498,10 @@ export abstract class BaseRaw<
return this.col.watch(pipeline);
}

countDocuments(query: Filter<T>): Promise<number> {
countDocuments(query: Filter<T>, options?: CountDocumentsOptions): Promise<number> {
if (options) {
return this.col.countDocuments(query, options);
}
return this.col.countDocuments(query);
}

Expand Down
13 changes: 13 additions & 0 deletions apps/meteor/server/models/raw/NpsVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,17 @@ export class NpsVoteRaw extends BaseRaw<INpsVote> implements INpsVoteModel {
};
return this.updateMany(query, update);
}

countByNpsId(npsId: string): Promise<number> {
return this.countDocuments({ npsId });
}

countByNpsIdAndStatus(npsId: string, status: INpsVoteStatus): Promise<number> {
const query = {
npsId,
status,
};

return this.countDocuments(query);
}
}
19 changes: 19 additions & 0 deletions apps/meteor/server/models/raw/Rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@ export class RoomsRaw extends BaseRaw<IRoom> implements IRoomsModel {
return this.find(query, options);
}

countByTeamId(teamId: ITeam['_id']): Promise<number> {
const query: Filter<IRoom> = {
teamId,
teamMain: {
$exists: false,
},
};

return this.countDocuments(query);
}

findPaginatedByTeamIdContainingNameAndDefault(
teamId: ITeam['_id'],
name: IRoom['name'],
Expand Down Expand Up @@ -702,6 +713,14 @@ export class RoomsRaw extends BaseRaw<IRoom> implements IRoomsModel {
});
}

countRoomsInsideTeams(autoJoin = false): Promise<number> {
return this.countDocuments({
teamId: { $exists: true },
teamMain: { $exists: false },
...(autoJoin && { teamDefault: true }),
});
}

countByType(t: IRoom['t']): Promise<number> {
return this.col.countDocuments({ t });
}
Expand Down
16 changes: 8 additions & 8 deletions apps/meteor/server/models/raw/ServerEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,36 @@ export class ServerEventsRaw extends BaseRaw<IServerEvent> implements IServerEve
}

async countFailedAttemptsByUsernameSince(username: string, since: Date): Promise<number> {
return this.find({
return this.countDocuments({
'u.username': username,
't': ServerEventType.FAILED_LOGIN_ATTEMPT,
'ts': {
$gte: since,
},
}).count();
});
}

countFailedAttemptsByIpSince(ip: string, since: Date): Promise<number> {
return this.find({
return this.countDocuments({
ip,
t: ServerEventType.FAILED_LOGIN_ATTEMPT,
ts: {
$gte: since,
},
}).count();
});
}

countFailedAttemptsByIp(ip: string): Promise<number> {
return this.find({
return this.countDocuments({
ip,
t: ServerEventType.FAILED_LOGIN_ATTEMPT,
}).count();
});
}

countFailedAttemptsByUsername(username: string): Promise<number> {
return this.find({
return this.countDocuments({
'u.username': username,
't': ServerEventType.FAILED_LOGIN_ATTEMPT,
}).count();
});
}
}
8 changes: 8 additions & 0 deletions apps/meteor/server/models/raw/TeamMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export class TeamMemberRaw extends BaseRaw<ITeamMember> implements ITeamMemberMo
return options ? this.col.find({ teamId }, options) : this.col.find({ teamId }, options);
}

countByTeamId(teamId: string): Promise<number> {
return this.countDocuments({ teamId });
}

findByTeamIds(teamIds: Array<string>): FindCursor<ITeamMember>;

findByTeamIds(teamIds: Array<string>, options: FindOptions<ITeamMember>): FindCursor<ITeamMember>;
Expand Down Expand Up @@ -99,6 +103,10 @@ export class TeamMemberRaw extends BaseRaw<ITeamMember> implements ITeamMemberMo
return options ? this.col.find({ teamId, roles: role }, options) : this.col.find({ teamId, roles: role });
}

countByTeamIdAndRole(teamId: string, role: IRole['_id']): Promise<number> {
return this.countDocuments({ teamId, roles: role });
}

findByUserIdAndTeamIds(userId: string, teamIds: Array<string>, options: FindOptions<ITeamMember> = {}): FindCursor<ITeamMember> {
const query = {
userId,
Expand Down
17 changes: 13 additions & 4 deletions apps/meteor/server/models/raw/VideoConference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ import type {
} from '@rocket.chat/core-typings';
import { VideoConferenceStatus } from '@rocket.chat/core-typings';
import type { FindPaginated, InsertionModel, IVideoConferenceModel } from '@rocket.chat/model-typings';
import type { FindCursor, UpdateOptions, UpdateFilter, UpdateResult, IndexDescription, Collection, Db, FindOptions } from 'mongodb';
import type {
FindCursor,
UpdateOptions,
UpdateFilter,
UpdateResult,
IndexDescription,
Collection,
Db,
CountDocumentsOptions,
} from 'mongodb';

import { BaseRaw } from './BaseRaw';

Expand Down Expand Up @@ -63,15 +72,15 @@ export class VideoConferenceRaw extends BaseRaw<VideoConference> implements IVid
public async countByTypeAndStatus(
type: VideoConference['type'],
status: VideoConferenceStatus,
options: FindOptions<VideoConference>,
options: CountDocumentsOptions,
): Promise<number> {
return this.find(
return this.countDocuments(
{
type,
status,
},
options,
).count();
);
}

public async createDirect({
Expand Down
6 changes: 3 additions & 3 deletions apps/meteor/server/services/nps/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ export class NPSService extends ServiceClassInternal implements INPSService {
return;
}

const total = await NpsVote.findByNpsId(nps._id).count();
const total = await NpsVote.countByNpsId(nps._id);

const votesToSend = await NpsVote.findNotSentByNpsId(nps._id).toArray();

// if there is nothing to sent, check if something gone wrong
if (votesToSend.length === 0) {
// check if still has votes left to send
const totalSent = await NpsVote.findByNpsIdAndStatus(nps._id, INpsVoteStatus.SENT).count();
const totalSent = await NpsVote.countByNpsIdAndStatus(nps._id, INpsVoteStatus.SENT);
if (totalSent === total) {
await Nps.updateStatusById(nps._id, NPSStatus.SENT);
return;
Expand Down Expand Up @@ -130,7 +130,7 @@ export class NPSService extends ServiceClassInternal implements INPSService {
await NpsVote.updateVotesToSent(voteIds);
}

const totalSent = await NpsVote.findByNpsIdAndStatus(nps._id, INpsVoteStatus.SENT).count();
const totalSent = await NpsVote.countByNpsIdAndStatus(nps._id, INpsVoteStatus.SENT);
if (totalSent < total) {
// send more in five minutes
setTimeout(() => NPS.sendResults(), 5 * 60 * 1000);
Expand Down
21 changes: 8 additions & 13 deletions apps/meteor/server/services/team/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,10 @@ export class TeamService extends ServiceClassInternal implements ITeamService {

const results: ITeamInfo[] = [];
for await (const record of records) {
const rooms = Rooms.findByTeamId(record._id);
const users = TeamMember.findByTeamId(record._id);
results.push({
...record,
rooms: await rooms.count(),
numberOfUsers: await users.count(),
rooms: await Rooms.countByTeamId(record._id),
numberOfUsers: await TeamMember.countByTeamId(record._id),
});
}

Expand All @@ -279,12 +277,10 @@ export class TeamService extends ServiceClassInternal implements ITeamService {

const results: ITeamInfo[] = [];
for await (const record of records) {
const rooms = Rooms.findByTeamId(record._id);
const users = TeamMember.findByTeamId(record._id);
results.push({
...record,
rooms: await rooms.count(),
numberOfUsers: await users.count(),
rooms: await Rooms.countByTeamId(record._id),
numberOfUsers: await TeamMember.countByTeamId(record._id),
});
}

Expand Down Expand Up @@ -797,8 +793,7 @@ export class TeamService extends ServiceClassInternal implements ITeamService {

if (existingMember) {
if (existingMember.roles?.includes('owner')) {
const owners = TeamMember.findByTeamIdAndRole(team._id, 'owner');
const totalOwners = await owners.count();
const totalOwners = await TeamMember.countByTeamIdAndRole(team._id, 'owner');
if (totalOwners === 1) {
throw new Error('last-owner-can-not-be-removed');
}
Expand Down Expand Up @@ -1002,9 +997,9 @@ export class TeamService extends ServiceClassInternal implements ITeamService {

async getStatistics(): Promise<ITeamStats> {
return {
totalTeams: await Team.find({}).count(),
totalRoomsInsideTeams: await Rooms.findRoomsInsideTeams().count(),
totalDefaultRoomsInsideTeams: await Rooms.findRoomsInsideTeams(true).count(),
totalTeams: await Team.estimatedDocumentCount(),
totalRoomsInsideTeams: await Rooms.countRoomsInsideTeams(),
totalDefaultRoomsInsideTeams: await Rooms.countRoomsInsideTeams(true),
};
}

Expand Down
2 changes: 2 additions & 0 deletions packages/model-typings/src/models/INpsVoteModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ export interface INpsVoteModel extends IBaseModel<INpsVote> {
save(vote: Omit<INpsVote, '_id' | '_updatedAt'>): Promise<UpdateResult>;
updateVotesToSent(voteIds: string[]): Promise<UpdateResult | Document>;
updateOldSendingToNewByNpsId(npsId: string): Promise<UpdateResult | Document>;
countByNpsId(npsId: string): Promise<number>;
countByNpsIdAndStatus(npsId: string, status: INpsVoteStatus): Promise<number>;
}
4 changes: 4 additions & 0 deletions packages/model-typings/src/models/IRoomsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export interface IRoomsModel extends IBaseModel<IRoom> {

findByTeamId(teamId: ITeam['_id'], options?: FindOptions<IRoom>): FindCursor<IRoom>;

countByTeamId(teamId: ITeam['_id']): Promise<number>;

findPaginatedByTeamIdContainingNameAndDefault(
teamId: ITeam['_id'],
name: IRoom['name'],
Expand Down Expand Up @@ -139,6 +141,8 @@ export interface IRoomsModel extends IBaseModel<IRoom> {

findRoomsInsideTeams(autoJoin?: boolean): FindCursor<IRoom>;

countRoomsInsideTeams(autoJoin?: boolean): Promise<number>;

findOneDirectRoomContainingAllUserIDs(uid: IDirectMessageRoom['uids'], options?: FindOptions<IRoom>): Promise<IRoom | null>;

countByType(t: IRoom['t']): Promise<number>;
Expand Down
3 changes: 3 additions & 0 deletions packages/model-typings/src/models/ITeamMemberModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface ITeamMemberModel extends IBaseModel<ITeamMember> {
options?: undefined | FindOptions<ITeamMember> | FindOptions<P extends ITeamMember ? ITeamMember : P>,
): FindCursor<P> | FindCursor<ITeamMember>;

countByTeamId(teamId: string): Promise<number>;
findByTeamIds(teamIds: Array<string>): FindCursor<ITeamMember>;

findByTeamIds(teamIds: Array<string>, options: FindOptions<ITeamMember>): FindCursor<ITeamMember>;
Expand All @@ -61,6 +62,8 @@ export interface ITeamMemberModel extends IBaseModel<ITeamMember> {
options?: undefined | FindOptions<ITeamMember> | FindOptions<P extends ITeamMember ? ITeamMember : P>,
): FindCursor<P> | FindCursor<ITeamMember>;

countByTeamIdAndRole(teamId: string, role: IRole['_id']): Promise<number>;

findByUserIdAndTeamIds(userId: string, teamIds: Array<string>, options?: FindOptions<ITeamMember>): FindCursor<ITeamMember>;

findPaginatedMembersInfoByTeamId(
Expand Down
Loading