Skip to content
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
3 changes: 0 additions & 3 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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=
63 changes: 0 additions & 63 deletions workers/limiter/src/dbHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
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<number> {
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
*
Expand Down
38 changes: 1 addition & 37 deletions workers/limiter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);

Expand Down Expand Up @@ -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<number> {
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 <b>${workspace.name}</b> 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
//
// /**
Expand Down
25 changes: 0 additions & 25 deletions workers/limiter/tests/dbHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
/**
Expand Down
41 changes: 0 additions & 41 deletions workers/limiter/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading