Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,311 @@
|
||||
import { Prisma } from "@toolctrl/db/prisma-browser";
|
||||
import { LoaderFunctionArgs, useLoaderData } from "react-router";
|
||||
import { Link, useSearchParams } from "react-router";
|
||||
import clsx from "clsx";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { FilterablePropertyDto } from "~/application/dtos/data/FilterablePropertyDto";
|
||||
import { RowHeaderDisplayDto } from "~/application/dtos/data/RowHeaderDisplayDto";
|
||||
import WarningBanner from "~/components/ui/banners/WarningBanner";
|
||||
import ServerError from "~/components/ui/errors/ServerError";
|
||||
import InputCombobox from "~/components/ui/input/InputCombobox";
|
||||
import InputFilters from "~/components/ui/input/InputFilters";
|
||||
import EditPageLayout from "~/components/ui/layouts/EditPageLayout";
|
||||
import { FilterableValueLink } from "~/components/ui/links/FilterableValueLink";
|
||||
import TableSimple from "~/components/ui/tables/TableSimple";
|
||||
import SpeedBadge from "~/modules/metrics/components/SpeedBadge";
|
||||
import MetricService from "~/modules/metrics/services/MetricService";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
||||
import { createLocaleFormatters } from "~/locale/format";
|
||||
|
||||
const defaultGroupBy = ["function"];
|
||||
|
||||
type ItemDto = {
|
||||
userId: string | null;
|
||||
tenantId: string | null;
|
||||
function: string;
|
||||
route: string;
|
||||
url: string;
|
||||
env: string;
|
||||
type: string;
|
||||
_avg: {
|
||||
duration: number | null;
|
||||
};
|
||||
_count: {
|
||||
_all: number;
|
||||
};
|
||||
};
|
||||
|
||||
type LoaderData = {
|
||||
items: ItemDto[];
|
||||
users: { id: string; email: string }[];
|
||||
tenants: { id: string; name: string }[];
|
||||
filterableProperties: FilterablePropertyDto[];
|
||||
};
|
||||
export const loader = async ({ request }: LoaderFunctionArgs) => {
|
||||
await verifyUserHasPermission(request, "admin.metrics.view");
|
||||
const { filterableProperties, whereFilters } = await MetricService.getFilters({ request });
|
||||
const searchParams = new URL(request.url).searchParams;
|
||||
|
||||
let users: { id: string; email: string }[] = [];
|
||||
let tenants: { id: string; name: string }[] = [];
|
||||
|
||||
let groupBy = getGroupByValues(searchParams);
|
||||
let items: ItemDto[] = [];
|
||||
if (groupBy.length > 0) {
|
||||
const data = await db.metricLog.groupBy({
|
||||
by: groupBy.map((x) => x as Prisma.MetricLogScalarFieldEnum),
|
||||
where: whereFilters,
|
||||
_avg: { duration: true },
|
||||
_count: { _all: true },
|
||||
orderBy: {
|
||||
_avg: { duration: "desc" },
|
||||
},
|
||||
});
|
||||
items = data.map((x) => x as ItemDto);
|
||||
const userIds: string[] = [];
|
||||
const tenantIds: string[] = [];
|
||||
for (const item of items) {
|
||||
if (item.userId && !userIds.includes(item.userId)) {
|
||||
userIds.push(item.userId);
|
||||
}
|
||||
if (item.tenantId && !tenantIds.includes(item.tenantId)) {
|
||||
tenantIds.push(item.tenantId);
|
||||
}
|
||||
}
|
||||
users = await db.user.findMany({
|
||||
where: { id: { in: userIds } },
|
||||
select: { id: true, email: true },
|
||||
});
|
||||
tenants = await db.tenant.findMany({
|
||||
where: { id: { in: tenantIds } },
|
||||
select: { id: true, name: true },
|
||||
});
|
||||
}
|
||||
const data: LoaderData = {
|
||||
items,
|
||||
users,
|
||||
tenants,
|
||||
filterableProperties,
|
||||
};
|
||||
return data;
|
||||
};
|
||||
|
||||
function getGroupByValues(searchParams: URLSearchParams) {
|
||||
const groupByValues = searchParams
|
||||
.getAll("groupBy")
|
||||
.filter((x) => x)
|
||||
.sort();
|
||||
const groupBy: Prisma.MetricLogScalarFieldEnum[] = [];
|
||||
for (const param of groupByValues) {
|
||||
if (Object.keys(Prisma.MetricLogScalarFieldEnum).includes(param)) {
|
||||
groupBy.push(param as Prisma.MetricLogScalarFieldEnum);
|
||||
}
|
||||
}
|
||||
return groupBy.length > 0 ? groupBy.map((x) => x.toString()) : defaultGroupBy;
|
||||
}
|
||||
|
||||
export default function () {
|
||||
const { i18n, t } = useTranslation();
|
||||
const formatters = useMemo(() => createLocaleFormatters(i18n.language), [i18n.language]);
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const [groupBy, setGroupBy] = useState<string[]>(getGroupByValues(searchParams));
|
||||
|
||||
const [headers, setHeaders] = useState<RowHeaderDisplayDto<ItemDto>[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (getGroupByValues(searchParams).sort().join(",") !== groupBy.sort().join(",")) {
|
||||
searchParams.delete("groupBy");
|
||||
groupBy.forEach((by) => {
|
||||
searchParams.append("groupBy", by);
|
||||
});
|
||||
setSearchParams(searchParams);
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [groupBy]);
|
||||
|
||||
useEffect(() => {
|
||||
const headers: RowHeaderDisplayDto<ItemDto>[] = [];
|
||||
let currentGroupBy = groupBy;
|
||||
if (currentGroupBy.length === 0) {
|
||||
currentGroupBy = defaultGroupBy;
|
||||
}
|
||||
const getCountLink = (item: ItemDto) => {
|
||||
const searchParams = new URLSearchParams();
|
||||
searchParams.set("pageSize", "100");
|
||||
currentGroupBy.forEach((groupBy) => {
|
||||
if (groupBy === "function") {
|
||||
searchParams.append("function", item.function);
|
||||
} else if (groupBy === "route") {
|
||||
searchParams.append("route", item.route);
|
||||
} else if (groupBy === "url") {
|
||||
searchParams.append("url", item.url);
|
||||
} else if (groupBy === "env") {
|
||||
searchParams.append("env", item.env);
|
||||
} else if (groupBy === "type") {
|
||||
searchParams.append("type", item.type);
|
||||
} else if (groupBy === "userId") {
|
||||
searchParams.append("userId", item.userId ?? "null");
|
||||
} else if (groupBy === "tenantId") {
|
||||
searchParams.append("tenantId", item.tenantId ?? "null");
|
||||
}
|
||||
});
|
||||
return adminPath(`metrics/logs?${searchParams.toString()}`, { mode: "host-aware" });
|
||||
};
|
||||
const getUser = (item: ItemDto) => {
|
||||
return data.users.find((x) => x.id === item.userId);
|
||||
};
|
||||
const getTenant = (item: ItemDto) => {
|
||||
return data.tenants.find((x) => x.id === item.tenantId);
|
||||
};
|
||||
if (groupBy.includes("env")) {
|
||||
headers.push({
|
||||
name: "env",
|
||||
title: t("admin.metrics.columns.env"),
|
||||
value: (item) => <FilterableValueLink name="env" value={item.env} />,
|
||||
});
|
||||
}
|
||||
if (groupBy.includes("type")) {
|
||||
headers.push({
|
||||
name: "type",
|
||||
title: t("admin.metrics.columns.type"),
|
||||
value: (item) => <FilterableValueLink name="type" value={item.type} />,
|
||||
});
|
||||
}
|
||||
if (groupBy.includes("route")) {
|
||||
headers.push({
|
||||
name: "route",
|
||||
title: t("admin.metrics.columns.route"),
|
||||
value: (item) => <FilterableValueLink name="route" value={item.route} />,
|
||||
});
|
||||
}
|
||||
if (groupBy.includes("url")) {
|
||||
headers.push({
|
||||
name: "url",
|
||||
title: t("admin.metrics.columns.url"),
|
||||
value: (item) => <FilterableValueLink name="url" value={item.url} />,
|
||||
});
|
||||
}
|
||||
if (groupBy.includes("function")) {
|
||||
headers.push({
|
||||
name: "function",
|
||||
title: t("admin.metrics.columns.function"),
|
||||
value: (item) => (
|
||||
<div className={clsx(item.function === "_unidentifiedFunction_" && "text-red-500")}>
|
||||
<FilterableValueLink name="function" value={item.function} />
|
||||
</div>
|
||||
),
|
||||
className: "w-full",
|
||||
});
|
||||
}
|
||||
if (groupBy.includes("userId")) {
|
||||
headers.push({
|
||||
name: "userId",
|
||||
title: t("admin.metrics.columns.user"),
|
||||
value: (item) => <FilterableValueLink name="userId" value={getUser(item)?.email ?? ""} />,
|
||||
});
|
||||
}
|
||||
if (groupBy.includes("tenantId")) {
|
||||
headers.push({
|
||||
name: "tenantId",
|
||||
title: t("admin.metrics.columns.tenant"),
|
||||
value: (item) => <FilterableValueLink name="tenantId" value={getTenant(item)?.name ?? ""} />,
|
||||
});
|
||||
}
|
||||
headers.push({
|
||||
name: "count",
|
||||
title: t("admin.metrics.columns.count"),
|
||||
value: (item) => (
|
||||
<Link to={getCountLink(item)} className="hover:underline">
|
||||
{formatters.number(Number(item._count._all))}
|
||||
</Link>
|
||||
),
|
||||
});
|
||||
headers.push({
|
||||
name: "speed",
|
||||
title: t("admin.metrics.columns.speed"),
|
||||
value: (item) => <SpeedBadge duration={Number(item._avg.duration)} />,
|
||||
});
|
||||
headers.push({
|
||||
name: "duration",
|
||||
title: t("admin.metrics.columns.avgDuration"),
|
||||
value: (item) => (
|
||||
<div>
|
||||
{formatters.number(Number(item._avg.duration), {
|
||||
style: "unit",
|
||||
unit: "millisecond",
|
||||
unitDisplay: "short",
|
||||
minimumFractionDigits: 3,
|
||||
maximumFractionDigits: 3,
|
||||
})}
|
||||
</div>
|
||||
),
|
||||
});
|
||||
// headers.push({
|
||||
// name: "all",
|
||||
// title: "All",
|
||||
// value: (item) => <div>{JSON.stringify(item)}</div>,
|
||||
// });
|
||||
|
||||
setHeaders(headers);
|
||||
}, [data, formatters, groupBy, t]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<EditPageLayout
|
||||
tabs={[
|
||||
{
|
||||
name: t("admin.metrics.tabs.summary"),
|
||||
routePath: adminPath("metrics/summary", { mode: "host-aware" }),
|
||||
},
|
||||
{
|
||||
name: t("admin.metrics.tabs.logs"),
|
||||
routePath: adminPath("metrics/logs", { mode: "host-aware" }),
|
||||
},
|
||||
{
|
||||
name: t("admin.metrics.tabs.settings"),
|
||||
routePath: adminPath("metrics/settings", { mode: "host-aware" }),
|
||||
},
|
||||
]}
|
||||
>
|
||||
<div className="space-y-2">
|
||||
<div className="flex w-full items-center space-x-2">
|
||||
<InputCombobox
|
||||
name="groupBy"
|
||||
prefix={t("admin.metrics.groupBy.prefix")}
|
||||
className="bg-background"
|
||||
selectPlaceholder={t("admin.metrics.groupBy.placeholder")}
|
||||
options={[
|
||||
{ value: "env", name: t("admin.metrics.columns.env") },
|
||||
{ value: "type", name: t("admin.metrics.columns.type") },
|
||||
{ value: "route", name: t("admin.metrics.columns.route") },
|
||||
{ value: "url", name: t("admin.metrics.columns.url") },
|
||||
{ value: "userId", name: t("admin.metrics.columns.user") },
|
||||
{ value: "tenantId", name: t("admin.metrics.columns.tenant") },
|
||||
{ value: "function", name: t("admin.metrics.columns.function") },
|
||||
]}
|
||||
value={groupBy}
|
||||
onChange={(value) => {
|
||||
setGroupBy(value as string[]);
|
||||
}}
|
||||
/>
|
||||
<div className="grow">{/* <InputSearchWithURL /> */}</div>
|
||||
<InputFilters size="sm" filters={data.filterableProperties} />
|
||||
</div>
|
||||
{groupBy.length === 0 ? (
|
||||
<WarningBanner title={t("admin.metrics.groupBy.title")} text={t("admin.metrics.groupBy.required")} />
|
||||
) : (
|
||||
<TableSimple items={data.items} headers={headers} />
|
||||
)}
|
||||
</div>
|
||||
</EditPageLayout>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function ErrorBoundary() {
|
||||
return <ServerError />;
|
||||
}
|
||||
Reference in new issue
Block a user