Skip to content

Commit

Permalink
Adds perPage argument on pageSuplier function
Browse files Browse the repository at this point in the history
  • Loading branch information
dasansol92 committed Jun 27, 2023
1 parent 92b521b commit 5c9ef42
Showing 1 changed file with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,19 @@ const iterateArtifactsBuildResult = (
};

const iterateAllListItems = async <T>(
pageSupplier: (page: number) => Promise<ListResult<T>>,
pageSupplier: (page: number, perPage: number) => Promise<ListResult<T>>,
itemCallback: (items: T[]) => void
) => {
let paging = true;
let page = 1;
const perPage = 1000;

while (paging) {
const { items, total } = await pageSupplier(page);
const { items, total } = await pageSupplier(page, perPage);

itemCallback(items);

paging = (page - 1) * 1000 + items.length < total;
paging = (page - 1) * perPage + items.length < total;
page++;
}
};
Expand Down Expand Up @@ -561,7 +562,7 @@ export class ManifestManager {
public async tryDispatch(manifest: Manifest): Promise<Error[]> {
const allPackagePolicies: PackagePolicy[] = [];
await iterateAllListItems(
(page) => this.listEndpointPolicies(page),
(page, perPage) => this.listEndpointPolicies(page, perPage),
(packagePoliciesBatch) => {
allPackagePolicies.push(...packagePoliciesBatch);
}
Expand Down Expand Up @@ -663,21 +664,21 @@ export class ManifestManager {
this.logger.info(`Committed manifest ${manifest.getSemanticVersion()}`);
}

private async listEndpointPolicies(page: number) {
private async listEndpointPolicies(page: number, perPage: number) {
return this.packagePolicyService.list(this.savedObjectsClient, {
page,
perPage: 1000,
perPage,
kuery: 'ingest-package-policies.package.name:endpoint',
});
}

private async listEndpointPolicyIds() {
const allPolicyIds: string[] = [];
await iterateAllListItems(
(page) => {
(page, perPage) => {
return this.packagePolicyService.listIds(this.savedObjectsClient, {
page,
perPage: 1000,
perPage,
kuery: 'ingest-package-policies.package.name:endpoint',
});
},
Expand Down

0 comments on commit 5c9ef42

Please sign in to comment.