Skip to content

Commit

Permalink
Uses common method and refactor to use batch instead
Browse files Browse the repository at this point in the history
  • Loading branch information
dasansol92 committed Jun 27, 2023
1 parent 88d5d70 commit dce6775
Showing 1 changed file with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,17 @@ const iterateArtifactsBuildResult = (

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

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

for (const item of items) {
await itemCallback(item);
}
itemCallback(items);

paging = (page - 1) * 20 + items.length < total;
paging = (page - 1) * 1000 + items.length < total;
page++;
}
};
Expand Down Expand Up @@ -560,8 +558,8 @@ export class ManifestManager {
const allPackagePolicies: PackagePolicy[] = [];
await iterateAllListItems(
(page) => this.listEndpointPolicies(page),
(packagePolicy) => {
allPackagePolicies.push(packagePolicy);
(packagePoliciesBatch) => {
allPackagePolicies.push(...packagePoliciesBatch);
}
);

Expand Down Expand Up @@ -672,22 +670,18 @@ export class ManifestManager {

private async listEndpointPolicyIds() {
const allPolicyIds: string[] = [];

let paging = true;
let page = 1;

while (paging) {
const { items, total } = await this.packagePolicyService.listIds(this.savedObjectsClient, {
page,
perPage: 1000,
kuery: 'ingest-package-policies.package.name:endpoint',
});

allPolicyIds.push(...items);

paging = (page - 1) * 1000 + items.length < total;
page++;
}
await iterateAllListItems(
(page) => {
return this.packagePolicyService.listIds(this.savedObjectsClient, {
page,
perPage: 1000,
kuery: 'ingest-package-policies.package.name:endpoint',
});
},
(packagePolicyIdsBatch) => {
allPolicyIds.push(...packagePolicyIdsBatch);
}
);
return allPolicyIds;
}

Expand Down

0 comments on commit dce6775

Please sign in to comment.