diff --git a/packages/common/src/async.ts b/packages/common/src/async.ts deleted file mode 100644 index c40d54976..000000000 --- a/packages/common/src/async.ts +++ /dev/null @@ -1,23 +0,0 @@ -export async function mapWithConcurrency( - values: readonly T[], - concurrency: number, - mapper: (value: T, index: number) => Promise, -): Promise { - if (!Number.isSafeInteger(concurrency) || concurrency < 1) { - throw new Error("Concurrency must be a positive integer"); - } - - const results: U[] = []; - let nextIndex = 0; - const workerCount = Math.min(concurrency, values.length); - const workers = Array.from({ length: workerCount }, async () => { - while (nextIndex < values.length) { - const index = nextIndex; - nextIndex += 1; - results[index] = await mapper(values[index] as T, index); - } - }); - - await Promise.all(workers); - return results; -} diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index b869d132d..f67a771ad 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -13,4 +13,3 @@ export * from "./theme"; export * from "./function"; export * from "./grid-algorithm"; export * from "./id"; -export * from "./async"; diff --git a/packages/integrations/src/umami/umami-integration.ts b/packages/integrations/src/umami/umami-integration.ts index fecfde7c4..5927f0d6a 100644 --- a/packages/integrations/src/umami/umami-integration.ts +++ b/packages/integrations/src/umami/umami-integration.ts @@ -1,6 +1,5 @@ import dayjs from "dayjs"; -import { mapWithConcurrency } from "@homarr/common"; import { ResponseError } from "@homarr/common/server"; import { fetchWithTrustedCertificatesAsync } from "@homarr/core/infrastructure/http"; import { createLogger } from "@homarr/core/infrastructure/logs"; @@ -247,17 +246,25 @@ export class UmamiIntegration extends Integration { ): Promise { const authHeaders = await this.getAuthHeadersAsync(); const { startAt, endAt, unit } = this.computeTimeRange(timeFrame); - return mapWithConcurrency(eventNames, UMAMI_EVENT_REQUEST_CONCURRENCY, async (eventName) => { - const dataPoints = await this.getWebsiteEventTimeSeriesAsync( - websiteId, - startAt, - endAt, - unit, - eventName, - authHeaders, + const results: UmamiEventSeries[] = []; + for (let index = 0; index < eventNames.length; index += UMAMI_EVENT_REQUEST_CONCURRENCY) { + const batch = eventNames.slice(index, index + UMAMI_EVENT_REQUEST_CONCURRENCY); + const batchResults = await Promise.all( + batch.map(async (eventName) => { + const dataPoints = await this.getWebsiteEventTimeSeriesAsync( + websiteId, + startAt, + endAt, + unit, + eventName, + authHeaders, + ); + return { eventName, dataPoints: dataPoints ?? [] }; + }), ); - return { eventName, dataPoints: dataPoints ?? [] }; - }); + results.push(...batchResults); + } + return results; } private async getWebsiteEventMetricsAsync(