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

initial implementation #703

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions jslib/angular/src/services/jslib-services.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LOCALE_ID, NgModule } from "@angular/core";

import { ApiService as ApiServiceAbstraction } from "@/jslib/common/src/abstractions/api.service";
import { AppIdService as AppIdServiceAbstraction } from "@/jslib/common/src/abstractions/appId.service";
import { BatchingService as BatchingServiceAbstraction } from "@/jslib/common/src/abstractions/batching.service";
import { BroadcasterService as BroadcasterServiceAbstraction } from "@/jslib/common/src/abstractions/broadcaster.service";
import { CryptoService as CryptoServiceAbstraction } from "@/jslib/common/src/abstractions/crypto.service";
import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from "@/jslib/common/src/abstractions/cryptoFunction.service";
Expand All @@ -19,6 +20,7 @@ import { Account } from "@/jslib/common/src/models/domain/account";
import { GlobalState } from "@/jslib/common/src/models/domain/globalState";
import { ApiService } from "@/jslib/common/src/services/api.service";
import { AppIdService } from "@/jslib/common/src/services/appId.service";
import { BatchingService } from "@/jslib/common/src/services/batching.service";
import { ConsoleLogService } from "@/jslib/common/src/services/consoleLog.service";
import { CryptoService } from "@/jslib/common/src/services/crypto.service";
import { EnvironmentService } from "@/jslib/common/src/services/environment.service";
Expand Down Expand Up @@ -138,6 +140,11 @@ import { ValidationService } from "./validation.service";
),
deps: [StorageServiceAbstraction, SECURE_STORAGE],
}),
safeProvider({
provide: BatchingServiceAbstraction,
useClass: BatchingService,
useAngularDecorators: true,
}),
] satisfies SafeProvider[],
})
export class JslibServicesModule {}
6 changes: 3 additions & 3 deletions jslib/common/src/abstractions/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { ApiTokenRequest } from "../models/request/identityToken/apiTokenRequest
import { PasswordTokenRequest } from "../models/request/identityToken/passwordTokenRequest";
import { SsoTokenRequest } from "../models/request/identityToken/ssoTokenRequest";
import { OrganizationImportRequest } from "../models/request/organizationImportRequest";
import { IdentityCaptchaResponse } from '../models/response/identityCaptchaResponse';
import { IdentityTokenResponse } from '../models/response/identityTokenResponse';
import { IdentityTwoFactorResponse } from '../models/response/identityTwoFactorResponse';
import { IdentityCaptchaResponse } from "../models/response/identityCaptchaResponse";
import { IdentityTokenResponse } from "../models/response/identityTokenResponse";
import { IdentityTwoFactorResponse } from "../models/response/identityTwoFactorResponse";

export abstract class ApiService {
postIdentityToken: (
Expand Down
13 changes: 13 additions & 0 deletions jslib/common/src/abstractions/batching.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { GroupEntry } from "@/src/models/groupEntry";
import { UserEntry } from "@/src/models/userEntry";

import { OrganizationImportRequest } from "../models/request/organizationImportRequest";

export abstract class BatchingService {
batchRequests: (
groups: GroupEntry[],
users: UserEntry[],
removeDisabled: boolean,
overwriteExisting: boolean,
) => OrganizationImportRequest[];
}
67 changes: 67 additions & 0 deletions jslib/common/src/services/batching.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { GroupEntry } from "@/src/models/groupEntry";
import { UserEntry } from "@/src/models/userEntry";

import { BatchingService } from "./batching.service";

describe("BatchingService", () => {
let batchingService: BatchingService;
let userSimulator: (userCount: number) => UserEntry[];
let groupSimulator: (userCount: number) => GroupEntry[];

beforeEach(async () => {
batchingService = new BatchingService();

userSimulator = (userCount: number) => {
const simulatedArray: UserEntry[] = [];
for (let i = 0; i < userCount; i += batchingService.batchSize) {
for (let j = 0; j <= batchingService.batchSize; j++) {
simulatedArray.push(new UserEntry());
}
}
return simulatedArray;
};

groupSimulator = (groupCount: number) => {
const simulatedArray: GroupEntry[] = [];
for (let i = 0; i < groupCount; i += batchingService.batchSize) {
for (let j = 0; j <= batchingService.batchSize; j++) {
simulatedArray.push(new GroupEntry());
}
}
return simulatedArray;
};
});

it("Batches requests for > 2000 users", () => {
const mockGroups = groupSimulator(11000);
const mockUsers = userSimulator(11000);

const requests = batchingService.batchRequests(mockGroups, mockUsers, true, true);

expect(requests.length).toEqual(
Math.ceil(mockGroups.length / batchingService.batchSize) +
Math.ceil(mockUsers.length / batchingService.batchSize),
);
});

it("Does not batch requests for < 2000 users", () => {
const mockGroups = groupSimulator(200);
const mockUsers = userSimulator(200);

const requests = batchingService.batchRequests(mockGroups, mockUsers, true, true);

expect(requests.length).toEqual(
Math.ceil(mockGroups.length / batchingService.batchSize) +
Math.ceil(mockUsers.length / batchingService.batchSize),
);
});

it("Retuns an empty array when there are no users or groups", () => {
const mockGroups = groupSimulator(0);
const mockUsers = userSimulator(0);

const requests = batchingService.batchRequests(mockGroups, mockUsers, true, true);

expect(requests).toEqual([]);
});
});
64 changes: 64 additions & 0 deletions jslib/common/src/services/batching.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { BatchingService as BatchingServiceAbstraction } from "@/jslib/common/src/abstractions/batching.service";
import { OrganizationImportRequest } from "@/jslib/common/src/models/request/organizationImportRequest";

import { GroupEntry } from "@/src/models/groupEntry";
import { UserEntry } from "@/src/models/userEntry";

export class BatchingService implements BatchingServiceAbstraction {
batchSize = 2000;

batchRequests(
groups: GroupEntry[],
users: UserEntry[],
removeDisabled: boolean,
overwriteExisting: boolean,
): OrganizationImportRequest[] {
const requests: OrganizationImportRequest[] = [];

if (users.length > 0) {
const usersRequest = users.map((u) => {
return {
email: u.email,
externalId: u.externalId,
deleted: u.deleted || (removeDisabled && u.disabled),
};
});

// Partition users
for (let i = 0; i < usersRequest.length; i += this.batchSize) {
const u = usersRequest.slice(i, i + this.batchSize);
const req = new OrganizationImportRequest({
groups: [],
users: u,
largeImport: true,
overwriteExisting,
});
requests.push(req);
}
}

if (groups.length > 0) {
const groupRequest = groups.map((g) => {
return {
name: g.name,
externalId: g.externalId,
memberExternalIds: Array.from(g.userMemberExternalIds),
};
});

// Partition groups
for (let i = 0; i < groupRequest.length; i += this.batchSize) {
const g = groupRequest.slice(i, i + this.batchSize);
const req = new OrganizationImportRequest({
groups: g,
users: [],
largeImport: true,
overwriteExisting,
});
requests.push(req);
}
}

return requests;
}
}
Loading
Loading