From c861a754ec493de5e7a976f32ee5226d9c9ce37f Mon Sep 17 00:00:00 2001 From: Kuchizu <70284260+Kuchizu@users.noreply.github.com> Date: Thu, 17 Sep 2026 22:26:06 +0300 Subject: [PATCH] Remove old limiter algo comparison (#591) --- .env.sample | 3 -- workers/limiter/src/dbHelper.ts | 63 -------------------------- workers/limiter/src/index.ts | 38 +--------------- workers/limiter/tests/dbHelper.test.ts | 25 ---------- workers/limiter/tests/index.test.ts | 41 ----------------- 5 files changed, 1 insertion(+), 169 deletions(-) diff --git a/.env.sample b/.env.sample index 5c19ff64..f2df9be2 100644 --- a/.env.sample +++ b/.env.sample @@ -57,6 +57,3 @@ IS_NOTIFIER_WORKER_ENABLED=false ## Url for telegram notifications about workspace blocks and unblocks TELEGRAM_LIMITER_CHAT_URL= - -## Workspace ids to compare old and new limiter counts in telegram -LIMITER_COMPARE_COUNTERS_WORKSPACE_IDS= diff --git a/workers/limiter/src/dbHelper.ts b/workers/limiter/src/dbHelper.ts index 0d0e71e5..e6948e06 100644 --- a/workers/limiter/src/dbHelper.ts +++ b/workers/limiter/src/dbHelper.ts @@ -260,69 +260,6 @@ export class DbHelper { .then(sum); } - /** - * Previous query, kept for rollout comparison - * - * @param project - project to check - * @param since - timestamp of the time from which we count the events - */ - public async getEventsCountByProjectUsingDailyEventsOld( - project: ProjectDBScheme, - since: number - ): Promise { - try { - const projectId = project._id.toString(); - const dailyEventsCollection = this.eventsDbConnection.collection('dailyEvents:' + projectId); - const firstFullDayTimestamp = this.getFirstFullDailyEventsTimestamp(since); - - const boundaryDayQuery = { - timestamp: { - $gt: since, - $lt: firstFullDayTimestamp, - }, - }; - - const [boundaryDayCount, dailyCounters] = await Promise.all([ - since < firstFullDayTimestamp - ? this.getRawEventsCountByProject(project, boundaryDayQuery) - : 0, - dailyEventsCollection - .aggregate<{ count: number }>([ - { $match: { groupingTimestamp: { $gte: firstFullDayTimestamp } } }, - { - $group: { - _id: null, - count: { $sum: '$count' }, - }, - }, - ]) - .toArray(), - ]); - - const fullDaysCount = dailyCounters.length > 0 ? dailyCounters[0].count : 0; - - return boundaryDayCount + fullDaysCount; - } catch (e) { - HawkCatcher.send(e); - throw new CriticalError(e); - } - } - - /** - * Previous query, kept for rollout comparison - * - * @param projects - projects to calculate for - * @param since - timestamp of the time from which we count the events - */ - public async getEventsCountByProjectsUsingDailyEventsOld(projects: ProjectDBScheme[], since: number): Promise { - const sum = (array: number[]): number => array.reduce((acc, val) => acc + val, 0); - - return Promise.all(projects.map( - project => this.getEventsCountByProjectUsingDailyEventsOld(project, since) - )) - .then(sum); - } - /** * Returns all projects from Database or projects of the specified workspace * diff --git a/workers/limiter/src/index.ts b/workers/limiter/src/index.ts index 1b3ca6cd..9df8c970 100644 --- a/workers/limiter/src/index.ts +++ b/workers/limiter/src/index.ts @@ -266,7 +266,7 @@ export default class LimiterWorker extends Worker { const since = Math.floor(new Date(workspace.lastChargeDate).getTime() / MS_IN_SEC); - const workspaceEventsCount = await this.getWorkspaceEventsCount(workspace, projects, since); + const workspaceEventsCount = await this.dbHelper.getEventsCountByProjectsUsingDailyEvents(projects, since); this.logger.info(`workspace ${workspace._id} events count since last charge date: ${workspaceEventsCount}`); @@ -328,42 +328,6 @@ export default class LimiterWorker extends Worker { }; } - /** - * Counts workspace events, comparing with the previous query for LIMITER_COMPARE_COUNTERS_WORKSPACE_IDS - * - * @param workspace - workspace to count events for - * @param projects - workspace projects - * @param since - timestamp of the time from which we count the events - */ - private async getWorkspaceEventsCount( - workspace: WorkspaceWithTariffPlan, - projects: ProjectDBScheme[], - since: number - ): Promise { - const compareWorkspaceIds = (process.env.LIMITER_COMPARE_COUNTERS_WORKSPACE_IDS || '').split(',').map(id => id.trim()); - - if (!compareWorkspaceIds.includes(workspace._id.toString())) { - return this.dbHelper.getEventsCountByProjectsUsingDailyEvents(projects, since); - } - - const oldAlgoStartedAt = Date.now(); - const oldAlgoCount = await this.dbHelper.getEventsCountByProjectsUsingDailyEventsOld(projects, since); - const oldAlgoTook = (Date.now() - oldAlgoStartedAt) / MS_IN_SEC; - - const newAlgoStartedAt = Date.now(); - const newAlgoCount = await this.dbHelper.getEventsCountByProjectsUsingDailyEvents(projects, since); - const newAlgoTook = (Date.now() - newAlgoStartedAt) / MS_IN_SEC; - - telegram.sendMessage( - `Workspace ${workspace.name} event count:\n` + - `Old algo: ${oldAlgoCount}, took ${oldAlgoTook}sec\n` + - `New algo: ${newAlgoCount}, took ${newAlgoTook}sec`, - telegram.TelegramBotURLs.Limiter - ); - - return newAlgoCount; - } - // Old raw counter with the opt-in switch, kept in case we need to roll back // // /** diff --git a/workers/limiter/tests/dbHelper.test.ts b/workers/limiter/tests/dbHelper.test.ts index 210d486f..bf9aa46c 100644 --- a/workers/limiter/tests/dbHelper.test.ts +++ b/workers/limiter/tests/dbHelper.test.ts @@ -844,31 +844,6 @@ describe('DbHelper', () => { }); }); - describe('getEventsCountByProjectUsingDailyEventsOld', () => { - test('Should count raw boundary-day events even without a dailyEvents bucket', async () => { - const project = createProjectMock({ workspaceId: new ObjectId() }); - const since = Math.floor(LAST_CHARGE_DATE.getTime() / MS_IN_SEC); - - await fillDatabaseWithMockedData({ - project, - eventsToMock: 0, - dailyEventsToMock: [ - { - groupingTimestamp: NEXT_MIDNIGHT_AFTER_LAST_CHARGE, - count: 3, - }, - ], - }); - await db.collection(`events:${project._id.toString()}`).insertMany([createEventMock(), createEventMock()]); - - const oldCount = await dbHelper.getEventsCountByProjectUsingDailyEventsOld(project, since); - const newCount = await dbHelper.getEventsCountByProjectUsingDailyEvents(project, since); - - expect(oldCount).toBe(5); - expect(newCount).toBe(3); - }); - }); - describe('getEventsCountByProjectsUsingDailyEvents', () => { test('Should count events, repetitions and dailyEvents for multiple projects', async () => { /** diff --git a/workers/limiter/tests/index.test.ts b/workers/limiter/tests/index.test.ts index d233a06f..f49c987b 100644 --- a/workers/limiter/tests/index.test.ts +++ b/workers/limiter/tests/index.test.ts @@ -388,47 +388,6 @@ describe('Limiter worker', () => { expect(telegram.sendMessage).not.toHaveBeenCalled(); }); - test('Should report old and new algo counts for workspaces listed for comparison', async () => { - const workspace = createWorkspaceMock({ - plan: mockedPlans.eventsLimit10000, - billingPeriodEventsCount: 0, - lastChargeDate: LAST_CHARGE_DATE, - }); - const project = createProjectMock({ workspaceId: workspace._id }); - - await fillDatabaseWithMockedData({ - workspace, - project, - eventsToMock: 5, - }); - - await db.collection(`dailyEvents:${project._id.toString()}`).deleteMany({}); - - process.env.LIMITER_COMPARE_COUNTERS_WORKSPACE_IDS = workspace._id.toString(); - - const worker = new LimiterWorker(); - - try { - await worker.start(); - await worker.handle(REGULAR_WORKSPACES_CHECK_EVENT); - await worker.finish(); - } finally { - delete process.env.LIMITER_COMPARE_COUNTERS_WORKSPACE_IDS; - } - - const workspaceInDatabase = await workspaceCollection.findOne({ - _id: workspace._id, - }); - - expect(workspaceInDatabase.billingPeriodEventsCount).toBe(0); - expect(telegram.sendMessage).toHaveBeenCalledTimes(1); - - const reportMessage = (telegram.sendMessage as jest.Mock).mock.calls[0][0]; - - expect(reportMessage).toContain('Old algo: 5'); - expect(reportMessage).toContain('New algo: 0'); - }); - test('Should not send a report when no projects are blocked or unblocked', async () => { /** * Arrange