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
17 changes: 0 additions & 17 deletions workers/limiter/src/dbHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,6 @@ export class DbHelper {
await this.workspacesCollection.bulkWrite(operations);
}

/**
* Method to change workspace isBlocked state
*
* @param workspaceId - id of the workspace to be changed
* @param isBlocked - new isBlocked state of the workspace
*/
public async changeWorkspaceBlockedState(workspaceId: string, isBlocked: boolean): Promise<void> {
await this.workspacesCollection.updateOne(
{ _id: new ObjectId(workspaceId) },
{
$set: {
isBlocked,
},
}
);
}

/**
* Returns total event counts for last billing period
*
Expand Down
23 changes: 19 additions & 4 deletions workers/limiter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export default class LimiterWorker extends Worker {
* @param event - event to handle
*/
private async handleBlockWorkspaceEvent(event: BlockWorkspaceEvent): Promise<void> {
this.logger.info('handle block workspace event', event);

const workspace = await this.dbHelper.getWorkspacesWithTariffPlans(event.workspaceId);

if (!workspace) {
Expand All @@ -126,7 +128,13 @@ export default class LimiterWorker extends Worker {
const workspaceProjects = await this.dbHelper.getProjects(event.workspaceId);
const projectIds = workspaceProjects.map(project => project._id.toString());

await this.dbHelper.changeWorkspaceBlockedState(event.workspaceId, true);
const { updatedWorkspace } = await this.prepareWorkspaceUsageUpdate(workspace, workspaceProjects);

updatedWorkspace.isBlocked = true;
await this.dbHelper.updateWorkspacesEventsCountAndIsBlocked([updatedWorkspace]);

this.logger.info('workspace blocked in db ', event.workspaceId)

await this.redis.appendBannedProjects(projectIds);

this.sendSingleWorkspaceReport(workspaceProjects, workspace, 'blocked');
Expand Down Expand Up @@ -159,16 +167,18 @@ export default class LimiterWorker extends Worker {
/**
* If workspace should be blocked by quota - then do not unblock it
*/
const { shouldBeBlockedByQuota } = await this.prepareWorkspaceUsageUpdate(workspace, workspaceProjects);
const { shouldBeBlockedByQuota, updatedWorkspace } = await this.prepareWorkspaceUsageUpdate(workspace, workspaceProjects);

if (shouldBeBlockedByQuota) {
return;
}

await this.dbHelper.changeWorkspaceBlockedState(event.workspaceId, false);
updatedWorkspace.isBlocked = false;

await this.dbHelper.updateWorkspacesEventsCountAndIsBlocked([updatedWorkspace]);
await this.redis.removeBannedProjects(projectIds);

this.sendSingleWorkspaceReport(workspaceProjects, workspace, 'unblocked');
this.sendSingleWorkspaceReport(workspaceProjects, updatedWorkspace, 'unblocked');
}

/**
Expand Down Expand Up @@ -229,6 +239,8 @@ export default class LimiterWorker extends Worker {
private async prepareWorkspaceUsageUpdate(
workspace: WorkspaceWithTariffPlan, projects: ProjectDBScheme[]
): Promise<WorkspaceReport> {
this.logger.info('prepareWorkspaceUsageUpdate');

/**
* If last charge date is not specified, then we skip checking it
* In the next time the Paymaster worker starts, it will set lastChargeDate for this workspace
Expand All @@ -247,6 +259,9 @@ export default class LimiterWorker extends Worker {
const since = Math.floor(new Date(workspace.lastChargeDate).getTime() / MS_IN_SEC);

const workspaceEventsCount = await this.dbHelper.getEventsCountByProjects(projects, since);

this.logger.info(`workspace ${workspace._id} events count since last charge date: ${workspaceEventsCount}`);

const usedQuota = workspaceEventsCount / workspace.tariffPlan.eventsLimit;
const quotaNotification = NOTIFY_ABOUT_LIMIT.reverse().find(quota => quota < usedQuota);

Expand Down
28 changes: 0 additions & 28 deletions workers/limiter/tests/dbHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,34 +272,6 @@ describe('DbHelper', () => {
});
});

describe('changeWorkspaceBlockedState', () => {
test('Should change workspace blocked state', async () => {
/**
* Arrange
*/
const workspace = createWorkspaceMock({
plan: mockedPlans.eventsLimit10,
billingPeriodEventsCount: 0,
lastChargeDate: new Date(),
isBlocked: false,
});

await workspaceCollection.insertOne(workspace);

/**
* Act
*/
await dbHelper.changeWorkspaceBlockedState(workspace._id.toString(), true);

/**
* Assert
*/
const updatedWorkspace = await workspaceCollection.findOne({ _id: workspace._id });

expect(updatedWorkspace.isBlocked).toBe(true);
});
});

describe('getEventsCountByProject', () => {
test('Should count events and repetitions for a project', async () => {
/**
Expand Down
Loading