From b4b2d1ce74588cb1e753546de49193e71d89ddfd Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Thu, 18 Jan 2024 12:53:14 -0300 Subject: [PATCH 1/6] chore: create new index for session model --- apps/meteor/server/models/raw/Sessions.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/meteor/server/models/raw/Sessions.ts b/apps/meteor/server/models/raw/Sessions.ts index c02fc8b5de99..7dba022450ac 100644 --- a/apps/meteor/server/models/raw/Sessions.ts +++ b/apps/meteor/server/models/raw/Sessions.ts @@ -984,6 +984,7 @@ export class SessionsRaw extends BaseRaw implements ISessionsModel { { key: { userId: 1, sessionId: 1 } }, { key: { type: 1, year: 1, month: 1, day: 1 } }, { key: { sessionId: 1, instanceId: 1, year: 1, month: 1, day: 1 } }, + { key: { instanceId: 1, year: 1, month: 1, day: 1 } }, { key: { _computedAt: 1 }, expireAfterSeconds: 60 * 60 * 24 * 45 }, { key: { 'loginToken': 1, 'logoutAt': 1, 'userId': 1, 'device.name': 1, 'device.os.name': 1, 'logintAt': -1 }, From 4c558e791f0678388e792f40f3e3dd4ecac4748b Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Thu, 18 Jan 2024 14:44:13 -0300 Subject: [PATCH 2/6] fix: properties index order --- apps/meteor/server/models/raw/Sessions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/server/models/raw/Sessions.ts b/apps/meteor/server/models/raw/Sessions.ts index 7dba022450ac..25ba8c0844c4 100644 --- a/apps/meteor/server/models/raw/Sessions.ts +++ b/apps/meteor/server/models/raw/Sessions.ts @@ -984,7 +984,7 @@ export class SessionsRaw extends BaseRaw implements ISessionsModel { { key: { userId: 1, sessionId: 1 } }, { key: { type: 1, year: 1, month: 1, day: 1 } }, { key: { sessionId: 1, instanceId: 1, year: 1, month: 1, day: 1 } }, - { key: { instanceId: 1, year: 1, month: 1, day: 1 } }, + { key: { year: 1, month: 1, day: 1, instanceId: 1 } }, { key: { _computedAt: 1 }, expireAfterSeconds: 60 * 60 * 24 * 45 }, { key: { 'loginToken': 1, 'logoutAt': 1, 'userId': 1, 'device.name': 1, 'device.os.name': 1, 'logintAt': -1 }, From 905f73a0d0e9abb2f447339a9b1a715936f5557d Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Fri, 19 Jan 2024 11:20:32 -0300 Subject: [PATCH 3/6] fix: ensure sessionId on Sessions documents --- apps/meteor/app/api/server/api.ts | 32 +++++++++++-------- .../app/statistics/server/lib/SAUMonitor.ts | 2 +- .../externals/meteor/ddp-common.d.ts | 1 + apps/meteor/server/models/raw/Sessions.ts | 25 +++++++++------ 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/apps/meteor/app/api/server/api.ts b/apps/meteor/app/api/server/api.ts index b74163903604..08e1ef17e348 100644 --- a/apps/meteor/app/api/server/api.ts +++ b/apps/meteor/app/api/server/api.ts @@ -108,6 +108,22 @@ const getRequestIP = (req: Request): string | null => { return forwardedFor[forwardedFor.length - httpForwardedCount]; }; +const generateConnection = ( + ipAddress: string, + httpHeaders: Record, +): { + id: string; + close: () => void; + clientAddress: string; + httpHeaders: Record; +} => ({ + id: Random.id(), + // eslint-disable-next-line @typescript-eslint/no-empty-function + close() {}, + httpHeaders, + clientAddress: ipAddress, +}); + let prometheusAPIUserAgent = false; export class APIClass extends Restivus { @@ -569,14 +585,7 @@ export class APIClass extends Restivus { let result; - const connection = { - id: Random.id(), - // eslint-disable-next-line @typescript-eslint/no-empty-function - close() {}, - token: this.token, - httpHeaders: this.request.headers, - clientAddress: this.requestIp, - }; + const connection = { ...generateConnection(this.requestIp, this.request.headers), token: this.token }; try { if (options.deprecationVersion) { @@ -761,12 +770,7 @@ export class APIClass extends Restivus { const args = loginCompatibility(this.bodyParams, request); const invocation = new DDPCommon.MethodInvocation({ - connection: { - // eslint-disable-next-line @typescript-eslint/no-empty-function - close() {}, - httpHeaders: this.request.headers, - clientAddress: getRequestIP(request) || '', - }, + connection: generateConnection(getRequestIP(request) || '', this.request.headers), }); let auth; diff --git a/apps/meteor/app/statistics/server/lib/SAUMonitor.ts b/apps/meteor/app/statistics/server/lib/SAUMonitor.ts index b3aa68337106..0db63a929491 100644 --- a/apps/meteor/app/statistics/server/lib/SAUMonitor.ts +++ b/apps/meteor/app/statistics/server/lib/SAUMonitor.ts @@ -146,7 +146,7 @@ export class SAUMonitorClass { const searchTerm = this._getSearchTerm(data); - await Sessions.insertOne({ ...data, searchTerm, createdAt: new Date() }); + await Sessions.createOrUpdate({ ...data, searchTerm }); } private async _finishSessionsFromDate(yesterday: Date, today: Date): Promise { diff --git a/apps/meteor/definition/externals/meteor/ddp-common.d.ts b/apps/meteor/definition/externals/meteor/ddp-common.d.ts index 3c1f3b854131..3a7f1538ab40 100644 --- a/apps/meteor/definition/externals/meteor/ddp-common.d.ts +++ b/apps/meteor/definition/externals/meteor/ddp-common.d.ts @@ -5,6 +5,7 @@ declare module 'meteor/ddp-common' { class MethodInvocation { constructor(options: { connection: { + id: string; close: () => void; clientAddress: string; httpHeaders: Record; diff --git a/apps/meteor/server/models/raw/Sessions.ts b/apps/meteor/server/models/raw/Sessions.ts index 25ba8c0844c4..208cc1d60aa0 100644 --- a/apps/meteor/server/models/raw/Sessions.ts +++ b/apps/meteor/server/models/raw/Sessions.ts @@ -1432,11 +1432,15 @@ export class SessionsRaw extends BaseRaw implements ISessionsModel { }; } + private isValidData(data: Omit): boolean { + return data.year && data.month && data.day && data.sessionId && data.instanceId; + } + async createOrUpdate(data: Omit): Promise { // TODO: check if we should create a session when there is no loginToken or not const { year, month, day, sessionId, instanceId } = data; - if (!year || !month || !day || !sessionId || !instanceId) { + if (!this.isValidData(data)) { return; } @@ -1589,16 +1593,17 @@ export class SessionsRaw extends BaseRaw implements ISessionsModel { sessions.forEach((doc) => { const { year, month, day, sessionId, instanceId } = doc; delete doc._id; - - ops.push({ - updateOne: { - filter: { year, month, day, sessionId, instanceId }, - update: { - $set: doc, + if (this.isValidData(doc)) { + ops.push({ + updateOne: { + filter: { year, month, day, sessionId, instanceId }, + update: { + $set: doc, + }, + upsert: true, }, - upsert: true, - }, - }); + }); + } }); return this.col.bulkWrite(ops, { ordered: false }); From c31c381da791a4c37660fab6f5c4e7c1aeb7de0f Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Fri, 19 Jan 2024 11:21:21 -0300 Subject: [PATCH 4/6] fix: remove unnecessary index --- apps/meteor/server/models/raw/Sessions.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/meteor/server/models/raw/Sessions.ts b/apps/meteor/server/models/raw/Sessions.ts index 208cc1d60aa0..def0a5f46512 100644 --- a/apps/meteor/server/models/raw/Sessions.ts +++ b/apps/meteor/server/models/raw/Sessions.ts @@ -984,7 +984,6 @@ export class SessionsRaw extends BaseRaw implements ISessionsModel { { key: { userId: 1, sessionId: 1 } }, { key: { type: 1, year: 1, month: 1, day: 1 } }, { key: { sessionId: 1, instanceId: 1, year: 1, month: 1, day: 1 } }, - { key: { year: 1, month: 1, day: 1, instanceId: 1 } }, { key: { _computedAt: 1 }, expireAfterSeconds: 60 * 60 * 24 * 45 }, { key: { 'loginToken': 1, 'logoutAt': 1, 'userId': 1, 'device.name': 1, 'device.os.name': 1, 'logintAt': -1 }, From 647bb879c7ac063cbd8c7f5ba8f2cd0f28ad40cb Mon Sep 17 00:00:00 2001 From: Marcos Spessatto Defendi Date: Fri, 19 Jan 2024 11:22:59 -0300 Subject: [PATCH 5/6] Create clever-parrots-count.md --- .changeset/clever-parrots-count.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/clever-parrots-count.md diff --git a/.changeset/clever-parrots-count.md b/.changeset/clever-parrots-count.md new file mode 100644 index 000000000000..cfc37f51719b --- /dev/null +++ b/.changeset/clever-parrots-count.md @@ -0,0 +1,5 @@ +--- +"@rocket.chat/meteor": patch +--- + +Fixed a bug where some sessions were being saved without a sessionId From f66e82c9a8bdd9435f8886f1a6a650a69d2f85ce Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Fri, 19 Jan 2024 11:41:49 -0300 Subject: [PATCH 6/6] fix: fix type --- apps/meteor/server/models/raw/Sessions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/server/models/raw/Sessions.ts b/apps/meteor/server/models/raw/Sessions.ts index def0a5f46512..5fdd0ea6828d 100644 --- a/apps/meteor/server/models/raw/Sessions.ts +++ b/apps/meteor/server/models/raw/Sessions.ts @@ -1432,7 +1432,7 @@ export class SessionsRaw extends BaseRaw implements ISessionsModel { } private isValidData(data: Omit): boolean { - return data.year && data.month && data.day && data.sessionId && data.instanceId; + return Boolean(data.year && data.month && data.day && data.sessionId && data.instanceId); } async createOrUpdate(data: Omit): Promise {