refactor: minimize review fixes
This commit is contained in:
1 parent
396b78aa3e
commit
7a56a7d74f
3 files changed
+18
-35
No files matched your search
@@ -1,23 +0,0 @@
|
||||
export async function mapWithConcurrency<T, U>(
|
||||
values: readonly T[],
|
||||
concurrency: number,
|
||||
mapper: (value: T, index: number) => Promise<U>,
|
||||
): Promise<U[]> {
|
||||
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;
|
||||
}
|
||||
@@ -13,4 +13,3 @@ export * from "./theme";
|
||||
export * from "./function";
|
||||
export * from "./grid-algorithm";
|
||||
export * from "./id";
|
||||
export * from "./async";
|
||||
@@ -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<UmamiEventSeries[]> {
|
||||
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(
|
||||
|
||||
Reference in new issue
Block a user