perf: reduce widget and runtime overhead
This commit is contained in:
1 parent
5a7607ddf7
commit
14c7e58e17
33 files changed
+779
-339
No files matched your search
@@ -45,7 +45,7 @@ export interface ImmichAsset {
|
||||
}
|
||||
|
||||
const logger = createLogger({ module: "immich-integration" });
|
||||
const randomPhotoCount = 50;
|
||||
const carouselPhotoCount = 50;
|
||||
|
||||
export class ImmichIntegration extends Integration {
|
||||
constructor(integration: IntegrationInput) {
|
||||
@@ -74,7 +74,7 @@ export class ImmichIntegration extends Integration {
|
||||
const assets = await searchRandom(
|
||||
{
|
||||
randomSearchDto: {
|
||||
size: randomPhotoCount,
|
||||
size: carouselPhotoCount,
|
||||
type: AssetTypeEnum.Image,
|
||||
visibility: AssetVisibility.Timeline,
|
||||
},
|
||||
@@ -125,13 +125,15 @@ export class ImmichIntegration extends Integration {
|
||||
let nextPage: string | null = null;
|
||||
|
||||
do {
|
||||
const metadataSearchDto: MetadataSearchDto = { albumIds: [albumId] };
|
||||
if (nextPage !== null) {
|
||||
metadataSearchDto.page = Number(nextPage);
|
||||
}
|
||||
const searchResult = await searchAssets({ metadataSearchDto }, requestOptions);
|
||||
assets.push(...searchResult.assets.items);
|
||||
nextPage = searchResult.assets.nextPage;
|
||||
const metadataSearchDto: MetadataSearchDto = {
|
||||
albumIds: [albumId],
|
||||
type: AssetTypeEnum.Image,
|
||||
};
|
||||
if (nextPage !== null) metadataSearchDto.page = Number(nextPage);
|
||||
|
||||
const result = await searchAssets({ metadataSearchDto }, requestOptions);
|
||||
assets.push(...result.assets.items);
|
||||
nextPage = result.assets.nextPage;
|
||||
} while (nextPage !== null);
|
||||
|
||||
return assets;
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
// @vitest-environment node
|
||||
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import type { IntegrationSecret } from "../../base/types";
|
||||
import { ImmichIntegration } from "../immich-integration";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
getAlbumInfo: vi.fn(),
|
||||
searchAssets: vi.fn(),
|
||||
createImageAsync: vi.fn<(url: string) => Promise<string>>(),
|
||||
}));
|
||||
|
||||
vi.mock("@immich/sdk", () => ({
|
||||
AssetMediaSize: { Preview: "preview" },
|
||||
AssetTypeEnum: { Image: "IMAGE" },
|
||||
AssetVisibility: { Timeline: "timeline" },
|
||||
getAlbumInfo: mocks.getAlbumInfo,
|
||||
getAllAlbums: vi.fn(),
|
||||
getMyUser: vi.fn(),
|
||||
getServerStatistics: vi.fn(),
|
||||
init: vi.fn(),
|
||||
searchAssets: mocks.searchAssets,
|
||||
searchRandom: vi.fn(),
|
||||
searchUsers: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/image-proxy", () => ({
|
||||
ImageProxy: class {
|
||||
createImageAsync = mocks.createImageAsync;
|
||||
},
|
||||
}));
|
||||
|
||||
const secrets: IntegrationSecret[] = [{ kind: "apiKey", value: "immich-token" }];
|
||||
const integration = new ImmichIntegration({
|
||||
id: "test-immich",
|
||||
name: "Test Immich",
|
||||
url: "https://immich.example.com",
|
||||
externalUrl: null,
|
||||
decryptedSecrets: secrets,
|
||||
});
|
||||
|
||||
const createAsset = (index: number) => ({
|
||||
id: `asset-${index}`,
|
||||
type: "IMAGE",
|
||||
thumbhash: null,
|
||||
fileCreatedAt: "2026-01-01T00:00:00.000Z",
|
||||
fileModifiedAt: "2026-01-01T00:00:00.000Z",
|
||||
updatedAt: "2026-01-01T00:00:00.000Z",
|
||||
});
|
||||
|
||||
describe("ImmichIntegration.getAlbumAsync", () => {
|
||||
beforeEach(() => {
|
||||
mocks.getAlbumInfo.mockReset();
|
||||
mocks.searchAssets.mockReset();
|
||||
mocks.createImageAsync.mockClear();
|
||||
});
|
||||
|
||||
test("keeps every image across paginated album results", async () => {
|
||||
mocks.getAlbumInfo.mockResolvedValue({ id: "album-id", albumName: "Large album" });
|
||||
mocks.searchAssets
|
||||
.mockResolvedValueOnce({
|
||||
assets: {
|
||||
items: Array.from({ length: 6 }, (_, index) => createAsset(index)),
|
||||
nextPage: "2",
|
||||
},
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
assets: {
|
||||
items: Array.from({ length: 6 }, (_, index) => createAsset(index + 6)),
|
||||
nextPage: null,
|
||||
},
|
||||
});
|
||||
mocks.createImageAsync.mockImplementation(async (url) => `proxied:${url}`);
|
||||
|
||||
const album = await integration.getAlbumAsync("album-id");
|
||||
|
||||
expect(mocks.searchAssets).toHaveBeenCalledTimes(2);
|
||||
expect(mocks.searchAssets.mock.calls.map(([input]) => input)).toStrictEqual([
|
||||
{ metadataSearchDto: { albumIds: ["album-id"], type: "IMAGE" } },
|
||||
{ metadataSearchDto: { albumIds: ["album-id"], type: "IMAGE", page: 2 } },
|
||||
]);
|
||||
expect(album.assets.map(({ id }) => id)).toStrictEqual(Array.from({ length: 12 }, (_, index) => `asset-${index}`));
|
||||
expect(mocks.createImageAsync).toHaveBeenCalledTimes(12);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,196 @@
|
||||
import { Request, Response } from "undici";
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/core/infrastructure/http";
|
||||
|
||||
import { UmamiIntegration } from "../umami-integration";
|
||||
|
||||
vi.mock("@homarr/core/infrastructure/http", () => ({
|
||||
fetchWithTrustedCertificatesAsync: vi.fn(),
|
||||
}));
|
||||
|
||||
const mockFetch = vi.mocked(fetchWithTrustedCertificatesAsync);
|
||||
const PAGE_SIZE = 5_000;
|
||||
const WEBSITE_ID = "site-1";
|
||||
|
||||
interface RequestedUrl {
|
||||
path: string;
|
||||
params: URLSearchParams;
|
||||
}
|
||||
|
||||
const requestedUrls: RequestedUrl[] = [];
|
||||
|
||||
const setupMockFetch = (handler: (requested: RequestedUrl) => unknown) => {
|
||||
mockFetch.mockImplementation((url) => {
|
||||
const urlString = typeof url === "string" ? url : url instanceof Request ? url.url : url.toString();
|
||||
const parsed = new URL(urlString);
|
||||
const requested = { path: parsed.pathname, params: parsed.searchParams };
|
||||
requestedUrls.push(requested);
|
||||
return Promise.resolve(
|
||||
new Response(JSON.stringify(handler(requested) ?? []), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
}) as unknown as Awaited<ReturnType<typeof fetchWithTrustedCertificatesAsync>>,
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const createIntegration = () =>
|
||||
new UmamiIntegration({
|
||||
id: "test-umami",
|
||||
name: "Test Umami",
|
||||
url: "https://umami.example.com/api",
|
||||
externalUrl: null,
|
||||
decryptedSecrets: [{ kind: "apiKey", value: "test-key" }],
|
||||
});
|
||||
|
||||
const rawEvent = (index: number, eventName: string, hour: number) => ({
|
||||
id: `event-${index}`,
|
||||
websiteId: WEBSITE_ID,
|
||||
sessionId: `session-${index}`,
|
||||
eventType: 2,
|
||||
eventName,
|
||||
createdAt: `2026-08-09T${String(hour).padStart(2, "0")}:30:00.000Z`,
|
||||
});
|
||||
|
||||
describe("UmamiIntegration", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
requestedUrls.length = 0;
|
||||
});
|
||||
|
||||
describe("getEventNamesAsync", () => {
|
||||
test("reads names from the aggregated metrics endpoint", async () => {
|
||||
setupMockFetch(({ path }) => {
|
||||
if (path.endsWith("/metrics")) {
|
||||
return [
|
||||
{ x: "signup", y: 12 },
|
||||
{ x: "purchase", y: 3 },
|
||||
{ x: "signup", y: 1 },
|
||||
];
|
||||
}
|
||||
throw new Error(`unexpected request to ${path}`);
|
||||
});
|
||||
|
||||
expect(await createIntegration().getEventNamesAsync(WEBSITE_ID)).toEqual(["purchase", "signup"]);
|
||||
expect(requestedUrls).toHaveLength(1);
|
||||
expect(requestedUrls[0]?.path).toContain("/metrics");
|
||||
expect(requestedUrls[0]?.params.get("type")).toBe("event");
|
||||
expect(requestedUrls.some((requested) => requested.path.endsWith("/events"))).toBe(false);
|
||||
});
|
||||
|
||||
test("drops empty names and returns them sorted", async () => {
|
||||
setupMockFetch(() => [
|
||||
{ x: "beta", y: 1 },
|
||||
{ x: null, y: 5 },
|
||||
{ x: "alpha", y: 2 },
|
||||
]);
|
||||
|
||||
expect(await createIntegration().getEventNamesAsync(WEBSITE_ID)).toEqual(["alpha", "beta"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getMultiEventTimeSeriesAsync", () => {
|
||||
test("pages through raw event records and aggregates across pages", async () => {
|
||||
const firstPage = Array.from({ length: PAGE_SIZE }, (_, index) => rawEvent(index, "signup", 10));
|
||||
const secondPage = Array.from({ length: 5 }, (_, index) => rawEvent(PAGE_SIZE + index, "signup", 11));
|
||||
|
||||
setupMockFetch(({ path, params }) => {
|
||||
if (!path.endsWith("/events")) return [];
|
||||
return { data: params.get("page") === "1" ? firstPage : secondPage };
|
||||
});
|
||||
|
||||
const result = await createIntegration().getMultiEventTimeSeriesAsync(WEBSITE_ID, "24h", ["signup"]);
|
||||
|
||||
expect(requestedUrls.map((requested) => requested.params.get("page"))).toEqual(["1", "2"]);
|
||||
expect(requestedUrls[0]?.params.get("pageSize")).toBe(String(PAGE_SIZE));
|
||||
expect(requestedUrls[0]?.params.get("event")).toBe("signup");
|
||||
expect(requestedUrls[0]?.params.get("eventName")).toBe("signup");
|
||||
expect(result[0]?.dataPoints.reduce((sum, point) => sum + point.y, 0)).toBe(PAGE_SIZE + 5);
|
||||
expect(result[0]?.dataPoints).toHaveLength(2);
|
||||
});
|
||||
|
||||
test("stops after one page when the instance returns aggregated buckets", async () => {
|
||||
setupMockFetch(() => [
|
||||
{ x: "2026-08-09 10:00:00", y: 4 },
|
||||
{ x: "2026-08-09 11:00:00", y: 7 },
|
||||
]);
|
||||
|
||||
const result = await createIntegration().getMultiEventTimeSeriesAsync(WEBSITE_ID, "24h", ["signup"]);
|
||||
|
||||
expect(requestedUrls).toHaveLength(1);
|
||||
expect(result[0]?.dataPoints).toEqual([
|
||||
{ x: "2026-08-09 10:00:00", y: 4 },
|
||||
{ x: "2026-08-09 11:00:00", y: 7 },
|
||||
]);
|
||||
});
|
||||
|
||||
test("stops instead of double counting when the instance ignores the page parameter", async () => {
|
||||
const page = Array.from({ length: PAGE_SIZE }, (_, index) => rawEvent(index, "signup", 10));
|
||||
setupMockFetch(() => ({ data: page }));
|
||||
|
||||
const result = await createIntegration().getMultiEventTimeSeriesAsync(WEBSITE_ID, "24h", ["signup"]);
|
||||
|
||||
expect(requestedUrls).toHaveLength(2);
|
||||
expect(result[0]?.dataPoints.reduce((sum, point) => sum + point.y, 0)).toBe(PAGE_SIZE);
|
||||
});
|
||||
|
||||
test("discards partial data when a later page fails", async () => {
|
||||
const firstPage = Array.from({ length: PAGE_SIZE }, (_, index) => rawEvent(index, "signup", 10));
|
||||
mockFetch.mockImplementation((url) => {
|
||||
const urlString = typeof url === "string" ? url : url instanceof Request ? url.url : url.toString();
|
||||
const parsed = new URL(urlString);
|
||||
const isSecondPage = parsed.searchParams.get("page") === "2";
|
||||
return Promise.resolve(
|
||||
new Response(isSecondPage ? "upstream failure" : JSON.stringify({ data: firstPage }), {
|
||||
status: isSecondPage ? 500 : 200,
|
||||
headers: { "content-type": isSecondPage ? "text/plain" : "application/json" },
|
||||
}) as unknown as Awaited<ReturnType<typeof fetchWithTrustedCertificatesAsync>>,
|
||||
);
|
||||
});
|
||||
|
||||
const result = await createIntegration().getMultiEventTimeSeriesAsync(WEBSITE_ID, "24h", ["signup"]);
|
||||
|
||||
expect(mockFetch).toHaveBeenCalledTimes(2);
|
||||
expect(result[0]?.dataPoints).toEqual([]);
|
||||
});
|
||||
|
||||
test("probes beyond the record ceiling without counting the sentinel page", async () => {
|
||||
setupMockFetch(({ params }) => {
|
||||
const page = Number(params.get("page"));
|
||||
return {
|
||||
data: Array.from({ length: PAGE_SIZE }, (_, index) => rawEvent(page * PAGE_SIZE + index, "signup", 10)),
|
||||
};
|
||||
});
|
||||
|
||||
const result = await createIntegration().getMultiEventTimeSeriesAsync(WEBSITE_ID, "24h", ["signup"]);
|
||||
|
||||
expect(requestedUrls).toHaveLength(21);
|
||||
expect(result[0]?.dataPoints.reduce((sum, point) => sum + point.y, 0)).toBe(20 * PAGE_SIZE);
|
||||
});
|
||||
|
||||
test("requests event series one at a time", async () => {
|
||||
let inFlight = 0;
|
||||
let maxInFlight = 0;
|
||||
mockFetch.mockImplementation(async () => {
|
||||
inFlight++;
|
||||
maxInFlight = Math.max(maxInFlight, inFlight);
|
||||
await new Promise((resolve) => setTimeout(resolve, 1));
|
||||
inFlight--;
|
||||
return new Response(JSON.stringify([{ x: "2026-08-09 10:00:00", y: 1 }]), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
}) as unknown as Awaited<ReturnType<typeof fetchWithTrustedCertificatesAsync>>;
|
||||
});
|
||||
|
||||
const result = await createIntegration().getMultiEventTimeSeriesAsync(WEBSITE_ID, "24h", [
|
||||
"signup",
|
||||
"purchase",
|
||||
"refund",
|
||||
]);
|
||||
|
||||
expect(result).toHaveLength(3);
|
||||
expect(maxInFlight).toBe(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -29,9 +29,21 @@ import {
|
||||
|
||||
// Umami event type ID for custom events (type 1 = page view, type 2 = custom event)
|
||||
const UMAMI_CUSTOM_EVENT_TYPE = 2;
|
||||
const UMAMI_EVENT_PAGE_SIZE = 5_000;
|
||||
const UMAMI_EVENT_PAGE_MAX = 20;
|
||||
|
||||
const logger = createLogger({ module: "umami-integration" });
|
||||
|
||||
const pad = (number: number) => String(number).padStart(2, "0");
|
||||
|
||||
const extractDataArray = (json: unknown): unknown[] => {
|
||||
if (Array.isArray(json)) return json;
|
||||
if (json && typeof json === "object" && "data" in json && Array.isArray((json as { data: unknown }).data)) {
|
||||
return (json as { data: unknown[] }).data;
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
export class UmamiIntegration extends Integration {
|
||||
protected async testingAsync(input: IntegrationTestingInput): Promise<TestingResult> {
|
||||
const authHeaders = await this.getAuthHeadersAsync();
|
||||
@@ -170,32 +182,14 @@ export class UmamiIntegration extends Integration {
|
||||
|
||||
public async getEventNamesAsync(websiteId: string): Promise<string[]> {
|
||||
const authHeaders = await this.getAuthHeadersAsync();
|
||||
const now = Date.now();
|
||||
const endAt = Date.now();
|
||||
const startAt = dayjs().subtract(30, "day").valueOf();
|
||||
const url = this.url(`/websites/${websiteId}/events`, {
|
||||
startAt: startAt.toString(),
|
||||
endAt: now.toString(),
|
||||
pageSize: "100000",
|
||||
});
|
||||
const response = await fetchWithTrustedCertificatesAsync(url, { headers: authHeaders });
|
||||
if (!response.ok) {
|
||||
logger.warn("Failed to load event names", { status: response.status, websiteId });
|
||||
return [];
|
||||
}
|
||||
const json = await response.json();
|
||||
const rawArray: unknown[] = Array.isArray(json)
|
||||
? json
|
||||
: json && typeof json === "object" && "data" in json && Array.isArray((json as { data: unknown }).data)
|
||||
? (json as { data: unknown[] }).data
|
||||
: [];
|
||||
const metrics = await this.getWebsiteMetricsAsync(websiteId, startAt, endAt, "event", authHeaders);
|
||||
const names = new Set<string>();
|
||||
for (const item of rawArray) {
|
||||
const record = item as Record<string, unknown>;
|
||||
if (typeof record.eventName === "string" && record.eventName) {
|
||||
names.add(record.eventName);
|
||||
}
|
||||
for (const metric of metrics) {
|
||||
if (metric.x) names.add(metric.x);
|
||||
}
|
||||
return [...names].sort();
|
||||
return [...names].toSorted();
|
||||
}
|
||||
|
||||
public async getActiveVisitorsAsync(websiteId: string): Promise<number> {
|
||||
@@ -243,19 +237,18 @@ export class UmamiIntegration extends Integration {
|
||||
): Promise<UmamiEventSeries[]> {
|
||||
const authHeaders = await this.getAuthHeadersAsync();
|
||||
const { startAt, endAt, unit } = this.computeTimeRange(timeFrame);
|
||||
const results = await Promise.all(
|
||||
eventNames.map(async (eventName) => {
|
||||
const dataPoints = await this.getWebsiteEventTimeSeriesAsync(
|
||||
websiteId,
|
||||
startAt,
|
||||
endAt,
|
||||
unit,
|
||||
eventName,
|
||||
authHeaders,
|
||||
);
|
||||
return { eventName, dataPoints: dataPoints ?? [] };
|
||||
}),
|
||||
);
|
||||
const results: UmamiEventSeries[] = [];
|
||||
for (const eventName of eventNames) {
|
||||
const dataPoints = await this.getWebsiteEventTimeSeriesAsync(
|
||||
websiteId,
|
||||
startAt,
|
||||
endAt,
|
||||
unit,
|
||||
eventName,
|
||||
authHeaders,
|
||||
);
|
||||
results.push({ eventName, dataPoints: dataPoints ?? [] });
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
@@ -306,79 +299,82 @@ export class UmamiIntegration extends Integration {
|
||||
eventName: string,
|
||||
authHeaders: Record<string, string>,
|
||||
): Promise<UmamiPageviewDataPoint[] | undefined> {
|
||||
const url = this.url(`/websites/${websiteId}/events`, {
|
||||
startAt: startAt.toString(),
|
||||
endAt: endAt.toString(),
|
||||
unit,
|
||||
timezone: "UTC",
|
||||
eventName,
|
||||
pageSize: "100000",
|
||||
});
|
||||
const response = await fetchWithTrustedCertificatesAsync(url, {
|
||||
headers: authHeaders,
|
||||
});
|
||||
const truncate = (isoDate: string): string => {
|
||||
const date = new Date(isoDate);
|
||||
const dateString = `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}`;
|
||||
if (unit === "hour") return `${dateString} ${pad(date.getUTCHours())}:00:00`;
|
||||
if (unit === "month") return `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-01 00:00:00`;
|
||||
return `${dateString} 00:00:00`;
|
||||
};
|
||||
|
||||
if (!response.ok) {
|
||||
logger.warn("Failed to load event time series", { status: response.status, websiteId, eventName });
|
||||
return undefined;
|
||||
}
|
||||
const buckets = new Map<string, number>();
|
||||
let previousFirstId: string | undefined;
|
||||
let truncated = false;
|
||||
|
||||
const json = await response.json();
|
||||
const rawArray: unknown[] = Array.isArray(json)
|
||||
? json
|
||||
: json && typeof json === "object" && "data" in json && Array.isArray((json as { data: unknown }).data)
|
||||
? (json as { data: unknown[] }).data
|
||||
: [];
|
||||
for (let page = 1; ; page++) {
|
||||
const url = this.url(`/websites/${websiteId}/events`, {
|
||||
startAt: startAt.toString(),
|
||||
endAt: endAt.toString(),
|
||||
unit,
|
||||
timezone: "UTC",
|
||||
event: eventName,
|
||||
eventName,
|
||||
page: page.toString(),
|
||||
pageSize: UMAMI_EVENT_PAGE_SIZE.toString(),
|
||||
});
|
||||
const response = await fetchWithTrustedCertificatesAsync(url, { headers: authHeaders });
|
||||
if (!response.ok) {
|
||||
logger.warn("Failed to load event time series", { status: response.status, websiteId, eventName, page });
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (rawArray.length === 0) return undefined;
|
||||
const rawArray = extractDataArray(await response.json());
|
||||
if (rawArray.length === 0) break;
|
||||
if (page > UMAMI_EVENT_PAGE_MAX) {
|
||||
truncated = true;
|
||||
break;
|
||||
}
|
||||
|
||||
const firstItem = rawArray[0] as Record<string, unknown>;
|
||||
const firstItem = rawArray[0] as Record<string, unknown>;
|
||||
if ("x" in firstItem && "y" in firstItem) {
|
||||
const points = umamiPageviewDataPointSchema.array().parse(rawArray);
|
||||
return points.length === 0 ? undefined : points;
|
||||
}
|
||||
if (!("createdAt" in firstItem)) return undefined;
|
||||
|
||||
// Two response shapes coexist:
|
||||
// - Umami self-hosted returns aggregated buckets: { x: timestamp, y: count }
|
||||
// - Umami Cloud returns raw event records: { createdAt, eventName, eventType }
|
||||
// We aggregate the raw Cloud records ourselves into the same bucket grid so
|
||||
// the result joins cleanly with /pageviews timestamps.
|
||||
const firstId =
|
||||
typeof firstItem.id === "string"
|
||||
? firstItem.id
|
||||
: typeof firstItem.createdAt === "string"
|
||||
? `${String(firstItem.sessionId ?? "")}@${firstItem.createdAt}`
|
||||
: undefined;
|
||||
if (page > 1 && firstId !== undefined && firstId === previousFirstId) {
|
||||
logger.warn("Umami ignored event paging, stopping after the first page", { websiteId, eventName });
|
||||
break;
|
||||
}
|
||||
previousFirstId = firstId;
|
||||
|
||||
// Aggregated { x, y } format — parse directly (self-hosted instances)
|
||||
if ("x" in firstItem && "y" in firstItem) {
|
||||
const points = umamiPageviewDataPointSchema.array().parse(rawArray);
|
||||
return points.length === 0 ? undefined : points;
|
||||
}
|
||||
|
||||
// Raw event records — aggregate by time bucket (Umami Cloud)
|
||||
if ("createdAt" in firstItem) {
|
||||
const pad = (number: number) => String(number).padStart(2, "0");
|
||||
// Format must match Umami's pageview x-axis timestamps: "YYYY-MM-DD HH:MM:SS"
|
||||
const truncate = (isoDate: string): string => {
|
||||
const date = new Date(isoDate);
|
||||
const dateString = `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}`;
|
||||
if (unit === "hour") {
|
||||
return `${dateString} ${pad(date.getUTCHours())}:00:00`;
|
||||
}
|
||||
if (unit === "month") {
|
||||
return `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-01 00:00:00`;
|
||||
}
|
||||
return `${dateString} 00:00:00`;
|
||||
};
|
||||
|
||||
const buckets = new Map<string, number>();
|
||||
for (const item of rawArray) {
|
||||
const event = item as { eventType?: number; eventName?: string; createdAt: string };
|
||||
if (event.eventType !== UMAMI_CUSTOM_EVENT_TYPE || event.eventName !== eventName) continue;
|
||||
const bucket = truncate(event.createdAt);
|
||||
buckets.set(bucket, (buckets.get(bucket) ?? 0) + 1);
|
||||
}
|
||||
if (buckets.size === 0) return undefined;
|
||||
return (
|
||||
Array.from(buckets.entries())
|
||||
.sort(([itemA], [itemB]) => itemA.localeCompare(itemB))
|
||||
// eslint-disable-next-line id-length
|
||||
.map(([x, y]) => ({ x, y }))
|
||||
);
|
||||
|
||||
if (rawArray.length < UMAMI_EVENT_PAGE_SIZE) break;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
if (truncated) {
|
||||
logger.warn("Event time series hit the page ceiling, counts are a lower bound", {
|
||||
websiteId,
|
||||
eventName,
|
||||
maxRecords: UMAMI_EVENT_PAGE_SIZE * UMAMI_EVENT_PAGE_MAX,
|
||||
});
|
||||
}
|
||||
if (buckets.size === 0) return undefined;
|
||||
return Array.from(buckets.entries())
|
||||
.toSorted(([itemA], [itemB]) => itemA.localeCompare(itemB))
|
||||
.map(([x, y]) => ({ x, y }));
|
||||
}
|
||||
|
||||
private async getWebsiteStatsAsync(
|
||||
|
||||
Reference in new issue
Block a user