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 "./function";
|
||||||
export * from "./grid-algorithm";
|
export * from "./grid-algorithm";
|
||||||
export * from "./id";
|
export * from "./id";
|
||||||
export * from "./async";
|
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
|
|
||||||
import { mapWithConcurrency } from "@homarr/common";
|
|
||||||
import { ResponseError } from "@homarr/common/server";
|
import { ResponseError } from "@homarr/common/server";
|
||||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/core/infrastructure/http";
|
import { fetchWithTrustedCertificatesAsync } from "@homarr/core/infrastructure/http";
|
||||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||||
@@ -247,17 +246,25 @@ export class UmamiIntegration extends Integration {
|
|||||||
): Promise<UmamiEventSeries[]> {
|
): Promise<UmamiEventSeries[]> {
|
||||||
const authHeaders = await this.getAuthHeadersAsync();
|
const authHeaders = await this.getAuthHeadersAsync();
|
||||||
const { startAt, endAt, unit } = this.computeTimeRange(timeFrame);
|
const { startAt, endAt, unit } = this.computeTimeRange(timeFrame);
|
||||||
return mapWithConcurrency(eventNames, UMAMI_EVENT_REQUEST_CONCURRENCY, async (eventName) => {
|
const results: UmamiEventSeries[] = [];
|
||||||
const dataPoints = await this.getWebsiteEventTimeSeriesAsync(
|
for (let index = 0; index < eventNames.length; index += UMAMI_EVENT_REQUEST_CONCURRENCY) {
|
||||||
websiteId,
|
const batch = eventNames.slice(index, index + UMAMI_EVENT_REQUEST_CONCURRENCY);
|
||||||
startAt,
|
const batchResults = await Promise.all(
|
||||||
endAt,
|
batch.map(async (eventName) => {
|
||||||
unit,
|
const dataPoints = await this.getWebsiteEventTimeSeriesAsync(
|
||||||
eventName,
|
websiteId,
|
||||||
authHeaders,
|
startAt,
|
||||||
|
endAt,
|
||||||
|
unit,
|
||||||
|
eventName,
|
||||||
|
authHeaders,
|
||||||
|
);
|
||||||
|
return { eventName, dataPoints: dataPoints ?? [] };
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
return { eventName, dataPoints: dataPoints ?? [] };
|
results.push(...batchResults);
|
||||||
});
|
}
|
||||||
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getWebsiteEventMetricsAsync(
|
private async getWebsiteEventMetricsAsync(
|
||||||
|
|||||||
Reference in new issue
Block a user