Track HiveGrid Cloud planning reference

This commit is contained in:
david committed 2026-06-26 20:48:01 +02:00
1 parent b3d5ea6764
commit e341c5938a
5784 files changed
+1104929 -3

No files matched your search

@@ -0,0 +1,107 @@
import { Params } from "react-router";
import { db } from "~/utils/db.server";
import { getTenantIdOrNull } from "~/utils/services/.server/urlService";
import { getUserInfo } from "~/utils/session.server";
export type TimeFunction = <T>(fn: Promise<T> | (() => T | Promise<T>), name: string) => Promise<T>;
export const timeFake: TimeFunction = async <T>(fn: Promise<T> | (() => T | Promise<T>), functionName = "_unidentifiedFunction_") => {
if (typeof fn === "function") {
return fn();
}
return fn;
};
export type MetricsArgs = {
time: TimeFunction;
getServerTimingHeader: () => { "Server-Timing": string };
};
export const createMetrics = async (
{ request, params, enabled = true }: { request: Request; params: Params; enabled?: boolean },
route: string
): Promise<MetricsArgs> => {
const { metrics } = await getUserInfo(request);
const timings: { route: string; functionName: string; duration: number }[] = [];
const env = process.env.NODE_ENV || "development";
const type = request.method === "GET" ? "loader" : "action";
let userId: string | null = null;
let tenantId: string | null = null;
const pathname = new URL(request.url).pathname;
const url = pathname.toString();
const metricsEnabled = metrics?.enabled && enabled && !metrics.ignoreUrls.find((f) => url.startsWith(f));
if (metricsEnabled) {
userId = (await getUserInfo(request))?.userId;
tenantId = await getTenantIdOrNull({ request: request, params: params });
}
const time: TimeFunction = async <T>(fn: Promise<T> | (() => T | Promise<T>), functionName = "_unidentifiedFunction_") => {
if (!metricsEnabled) {
return typeof fn === "function" ? await fn() : await fn;
}
const startTime = performance.now();
let result: T;
try {
result = typeof fn === "function" ? await fn() : await fn;
} catch (error) {
throw error;
}
const endTime = performance.now();
const duration = endTime - startTime;
timings.push({ route, functionName, duration });
return result;
};
const getServerTimingHeader: () => { "Server-Timing": string } = () => {
if (!metricsEnabled) {
return { "Server-Timing": "" };
}
if (metrics.logToConsole) {
// eslint-disable-next-line no-console
console.log("[Metrics]", {
env,
type,
route,
url,
timings: timings
.sort((a, b) => a.duration - b.duration)
.reverse()
.map((timing) => ({ [`${route ? route + "." : ""}${timing.functionName}`]: timing.duration.toFixed(3) })),
});
}
if (metrics.saveToDatabase) {
// store timings in parallel
const promises = timings.map((timing) =>
db.metricLog
.create({
data: {
env,
type,
route,
url,
function: timing.functionName,
duration: timing.duration,
userId,
tenantId,
},
})
.catch((e: any) => {
// eslint-disable-next-line no-console
// console.error("Error saving metrics to database", {
// message: e.message,
// });
return Promise.resolve();
})
);
Promise.all(promises);
}
const headerValue = timings.map((timing) => `${timing.functionName};dur=${timing.duration}`).join(", ");
return { "Server-Timing": headerValue };
};
return { time, getServerTimingHeader };
};
@@ -0,0 +1,136 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
type MetricServiceMocks = {
metricLogGroupBy: ReturnType<typeof vi.fn>;
adminGetAllTenantsIdsAndNames: ReturnType<typeof vi.fn>;
adminGetAllUsersNames: ReturnType<typeof vi.fn>;
};
function getMocks(): MetricServiceMocks {
const store = globalThis as typeof globalThis & { __metricServiceMocks__?: MetricServiceMocks };
store.__metricServiceMocks__ ??= {
metricLogGroupBy: vi.fn(),
adminGetAllTenantsIdsAndNames: vi.fn(),
adminGetAllUsersNames: vi.fn(),
};
return store.__metricServiceMocks__;
}
vi.mock("~/utils/db.server", () => ({
db: {
metricLog: {
groupBy: getMocks().metricLogGroupBy,
},
},
}));
vi.mock("~/utils/db/tenants.db.server", () => ({
adminGetAllTenantsIdsAndNames: getMocks().adminGetAllTenantsIdsAndNames,
}));
vi.mock("~/utils/db/users.db.server", () => ({
adminGetAllUsersNames: getMocks().adminGetAllUsersNames,
}));
import MetricService from "./MetricService";
const mocks = getMocks();
function createDeferred<T>() {
let resolve!: (value: T) => void;
const promise = new Promise<T>((promiseResolve) => {
resolve = promiseResolve;
});
return { promise, resolve };
}
describe("MetricService filters", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.metricLogGroupBy
.mockResolvedValueOnce([{ route: "/app", _count: { route: 3 } }])
.mockResolvedValueOnce([{ url: "https://app.example.test/app", _count: { url: 2 } }])
.mockResolvedValueOnce([{ function: "loader", _count: { function: 5 } }]);
mocks.adminGetAllTenantsIdsAndNames.mockResolvedValue([]);
mocks.adminGetAllUsersNames.mockResolvedValue([]);
});
it("returns translation keys for static visible metric filter labels", async () => {
const { filterableProperties } = await MetricService.getFilters({
request: new Request("https://admin.example.test/admin/metrics"),
});
expect(filterableProperties.find((filter) => filter.name === "env")).toMatchObject({
title: "admin.metrics.columns.env",
options: [
{ name: "shared.environments.production", value: "production" },
{ name: "shared.environments.staging", value: "staging" },
{ name: "shared.environments.development", value: "development" },
],
});
expect(filterableProperties.find((filter) => filter.name === "type")).toMatchObject({
title: "admin.metrics.columns.type",
options: [
{ name: "admin.metrics.types.loader", value: "loader" },
{ name: "admin.metrics.types.action", value: "action" },
],
});
expect(filterableProperties.find((filter) => filter.name === "function")).toMatchObject({
title: "admin.metrics.columns.function",
});
});
it("runs independent metric filter group queries together with a bounded result size", async () => {
const routeGroupBy = createDeferred([{ route: "/app", _count: { route: 3 } }]);
const urlGroupBy = createDeferred([{ url: "https://app.example.test/app", _count: { url: 2 } }]);
const functionGroupBy = createDeferred([{ function: "loader", _count: { function: 5 } }]);
mocks.metricLogGroupBy
.mockReset()
.mockReturnValueOnce(routeGroupBy.promise)
.mockReturnValueOnce(urlGroupBy.promise)
.mockReturnValueOnce(functionGroupBy.promise);
const filtersPromise = MetricService.getFilters({
request: new Request("https://admin.example.test/admin/metrics"),
});
await Promise.resolve();
expect(mocks.metricLogGroupBy).toHaveBeenCalledTimes(3);
expect(mocks.metricLogGroupBy).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
by: ["route"],
take: 200,
})
);
expect(mocks.metricLogGroupBy).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
by: ["url"],
take: 200,
})
);
expect(mocks.metricLogGroupBy).toHaveBeenNthCalledWith(
3,
expect.objectContaining({
by: ["function"],
take: 200,
})
);
routeGroupBy.resolve([{ route: "/app", _count: { route: 3 } }]);
urlGroupBy.resolve([{ url: "https://app.example.test/app", _count: { url: 2 } }]);
functionGroupBy.resolve([{ function: "loader", _count: { function: 5 } }]);
await expect(filtersPromise).resolves.toMatchObject({
filterableProperties: expect.arrayContaining([
expect.objectContaining({
name: "route",
options: [{ name: "/app (3)", value: "/app" }],
}),
]),
});
});
});
@@ -0,0 +1,134 @@
import { Prisma } from "@toolctrl/db/prisma-browser";
import { FilterablePropertyDto } from "~/application/dtos/data/FilterablePropertyDto";
import { db } from "~/utils/db.server";
import { adminGetAllTenantsIdsAndNames } from "~/utils/db/tenants.db.server";
import { adminGetAllUsersNames } from "~/utils/db/users.db.server";
import { getFiltersFromCurrentUrl } from "~/utils/helpers/RowPaginationHelper";
const FILTER_OPTION_LIMIT = 200;
async function getGroupBys() {
const allEnvs = [
{ name: "shared.environments.production", value: "production" },
{ name: "shared.environments.staging", value: "staging" },
{ name: "shared.environments.development", value: "development" },
];
const allTypes = [
{ name: "admin.metrics.types.loader", value: "loader" },
{ name: "admin.metrics.types.action", value: "action" },
];
const [allRoutes, allUrls, allFunctions] = await Promise.all([
db.metricLog.groupBy({
by: ["route"],
_count: { route: true },
orderBy: { _count: { route: "desc" } },
take: FILTER_OPTION_LIMIT,
}),
db.metricLog.groupBy({
by: ["url"],
_count: { url: true },
orderBy: { _count: { url: "desc" } },
take: FILTER_OPTION_LIMIT,
}),
db.metricLog.groupBy({
by: ["function"],
_count: { function: true },
orderBy: { _count: { function: "desc" } },
take: FILTER_OPTION_LIMIT,
}),
]);
return {
allEnvs,
allTypes,
allRoutes,
allUrls,
allFunctions,
};
}
async function getFilterableProperties() {
const [{ allEnvs, allTypes, allRoutes, allUrls, allFunctions }, tenants, users] = await Promise.all([
getGroupBys(),
adminGetAllTenantsIdsAndNames(),
adminGetAllUsersNames(),
]);
const filterableProperties: FilterablePropertyDto[] = [
{ name: "env", title: "admin.metrics.columns.env", options: allEnvs },
{ name: "type", title: "admin.metrics.columns.type", options: allTypes },
{ name: "route", title: "admin.metrics.columns.route", options: allRoutes.map((item) => ({ name: `${item.route} (${item._count.route})`, value: item.route })) },
{ name: "url", title: "admin.metrics.columns.url", options: allUrls.map((item) => ({ name: `${item.url} (${item._count.url})`, value: item.url })) },
{
name: "function",
title: "admin.metrics.columns.function",
options: allFunctions.map((item) => ({ name: `${item.function} (${item._count.function})`, value: item.function })),
},
{
name: "tenantId",
title: "models.tenant.object",
options: [
{ value: "null", name: "{null}" },
...tenants.map((item) => {
return {
value: item.id,
name: item.name,
};
}),
],
},
{
name: "userId",
title: "models.user.object",
options: [
{ value: "null", name: "{null}" },
...users.map((item) => {
return {
value: item.id,
name: item.email,
};
}),
],
},
];
return filterableProperties;
}
async function getFilters({ request }: { request: Request }) {
const filterableProperties = await getFilterableProperties();
// const searchParams = new URL(request.url).searchParams;
// const current = getPaginationFromCurrentUrl(searchParams);
const filters = getFiltersFromCurrentUrl(request, filterableProperties);
const q = filters.query || "";
const AND_filters: Prisma.MetricLogWhereInput[] = [];
filterableProperties.forEach((filter) => {
const value = filters.properties.find((f) => f.name === filter.name)?.value;
if (value) {
AND_filters.push({
[filter.name]: value === "null" ? null : value,
});
}
});
const OR_filters: Prisma.MetricLogWhereInput[] = [];
if (q) {
OR_filters.push(
{ env: { contains: q, mode: "insensitive" } },
{ type: { contains: q, mode: "insensitive" } },
{ route: { contains: q, mode: "insensitive" } },
{ url: { contains: q, mode: "insensitive" } },
{ function: { contains: q, mode: "insensitive" } }
);
}
const whereFilters: Prisma.MetricLogWhereInput = {};
if (OR_filters.length > 0) {
whereFilters.OR = OR_filters;
}
if (AND_filters.length > 0) {
whereFilters.AND = AND_filters;
}
return { filterableProperties, whereFilters };
}
export default {
getFilters,
};