Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/api/ApiSummaryPage.tsx
T

185 lines
7.5 KiB
TypeScript

// "Übersicht" — API calls grouped by method/endpoint/status, on the same list
// chrome as the neighbouring tabs.
//
// Replaces the boilerplate InputCombobox + InputFilters + TableSimple stack of
// ApiKeyLogsSummary for the tenant tab. The grouping control keeps its URL
// contract (repeated `groupBy` parameters) and sits inside the filter bar, next
// to the facets it orders. The row link into "API-Aufrufe" carries the group's
// values as filters, exactly as before.
import { useTranslation } from "react-i18next";
import { Link } from "react-router";
import { FeatherLayers } from "@subframe/core";
import type { FilterablePropertyDto } from "~/application/dtos/data/FilterablePropertyDto";
import type { ApiCallSummaryDto } from "~/modules/api/dtos/ApiCallSummaryDto";
import { useLocaleFormatters } from "~/locale/format";
import { Alert } from "~/ui/components/Alert";
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
import { DropdownMenuMultiSelect } from "~/ui/components/DropdownMenuMultiSelect";
import { EmptyState } from "~/ui/components/EmptyState";
import { FilterBar } from "~/ui/components/FilterBar";
import { Table } from "~/ui/components/Table";
import { ApiFilterBar } from "./ApiFilterBar";
import { methodTone, speedLabelKey, speedTone, statusTone } from "./ApiLogsPage";
/** Group-by keys the service accepts; the tenant tab has no tenant grouping. */
export const GROUP_BY_KEYS = ["method", "endpoint", "params", "status"] as const;
export type GroupByKey = (typeof GROUP_BY_KEYS)[number];
export type ApiSummaryActions = {
onSearchChange: (value: string) => void;
onToggleProperty: (name: string, value: string) => void;
onToggleGroupBy: (key: GroupByKey) => void;
onReset: () => void;
};
const CELL_CLASS = "text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary";
const MUTED_CLASS = "text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary";
const GROUP_LABEL_KEY: Record<GroupByKey, string> = {
method: "models.apiKeyLog.method",
endpoint: "models.apiKeyLog.endpoint",
params: "models.apiKeyLog.params",
status: "models.apiKeyLog.status",
};
export function ApiSummaryPage({
items,
properties,
groupBy,
search,
detailHrefFor,
actions,
}: {
items: readonly ApiCallSummaryDto[];
properties: readonly FilterablePropertyDto[];
groupBy: readonly GroupByKey[];
search: string;
/** Link into the calls tab, pre-filtered with this row's group values. */
detailHrefFor: (item: ApiCallSummaryDto) => string;
actions: ApiSummaryActions;
}): React.JSX.Element {
const { t } = useTranslation();
const formatters = useLocaleFormatters();
return (
<div className="flex w-full flex-col gap-4" data-ui-id="ui.settings.apiSummary">
<ApiFilterBar
uiId="ui.settings.apiSummary.filter"
properties={properties}
search={search}
onSearchChange={actions.onSearchChange}
onToggleProperty={actions.onToggleProperty}
onReset={actions.onReset}
afterFacets={
<FilterBar.StatusFilterButton
icon={<FeatherLayers />}
label={t("models.apiKeyLog.groupBy.prefix")}
showCount={groupBy.length > 0}
count={groupBy.length > 0 ? String(groupBy.length) : ""}
data-ui-id="ui.settings.apiSummary.groupBy"
dropdown={
<DropdownMenuMultiSelect>
{GROUP_BY_KEYS.map((key) => (
<DropdownMenuMultiSelect.DropdownItem
key={key}
showCheckbox
selected={groupBy.includes(key)}
onClick={() => actions.onToggleGroupBy(key)}
data-ui-id={`ui.settings.apiSummary.groupBy.${key}`}
>
{t(GROUP_LABEL_KEY[key])}
</DropdownMenuMultiSelect.DropdownItem>
))}
</DropdownMenuMultiSelect>
}
/>
}
/>
{groupBy.length === 0 ? (
<Alert variant="warning" title={t("models.apiKeyLog.groupBy.title")} description={t("models.apiKeyLog.groupBy.required")} />
) : items.length === 0 ? (
<div data-ui-id="ui.settings.apiSummary.empty">
<EmptyState outlined title={t("models.apiKeyLog.empty.title")} body={t("models.apiKeyLog.empty.description")} />
</div>
) : (
<div className="w-full overflow-x-auto" data-ui-id="ui.settings.apiSummary.table">
<Table
header={
<Table.HeaderRow>
{groupBy.map((key) => (
<Table.HeaderCell key={key}>{t(GROUP_LABEL_KEY[key])}</Table.HeaderCell>
))}
<Table.HeaderCell>{t("models.apiKeyLog.apiCalls")}</Table.HeaderCell>
<Table.HeaderCell>{t("models.apiKeyLog.avgDuration")}</Table.HeaderCell>
<Table.HeaderCell>{t("models.apiKeyLog.speed")}</Table.HeaderCell>
<Table.HeaderCell />
</Table.HeaderRow>
}
>
{items.map((item, index) => {
const average = item._avg.duration === null ? null : Number(item._avg.duration);
return (
<Table.Row key={`${index}-${item.method}-${item.endpoint}`}>
{groupBy.map((key) => (
<Table.Cell key={key}>{groupCell(key, item)}</Table.Cell>
))}
<Table.Cell>
<span className={CELL_CLASS}>
{t("models.apiKeyLog.calls", { count: Number(item._count._all), formattedCount: formatters.number(Number(item._count._all)) })}
</span>
</Table.Cell>
<Table.Cell>
{average === null ? (
<span className={MUTED_CLASS}>{t("shared.notAvailable")}</span>
) : (
<span className={CELL_CLASS}>
{formatters.number(average, { style: "unit", unit: "millisecond", unitDisplay: "short", maximumFractionDigits: 3 })}
</span>
)}
</Table.Cell>
<Table.Cell>
{average === null ? (
<span className={MUTED_CLASS}>{t("shared.notAvailable")}</span>
) : (
<BadgeFeedback farbe={speedTone(average)} size="small">
{t(speedLabelKey(average))}
</BadgeFeedback>
)}
</Table.Cell>
<Table.Cell variant="align-right">
<Link to={detailHrefFor(item)} className="text-body-small-regular-0 text-text-feedback-link-default underline">
{t("shared.details")}
</Link>
</Table.Cell>
</Table.Row>
);
})}
</Table>
</div>
)}
</div>
);
function groupCell(key: GroupByKey, item: ApiCallSummaryDto) {
if (key === "method") {
return (
<BadgeFeedback farbe={methodTone(item.method)} size="small">
{item.method}
</BadgeFeedback>
);
}
if (key === "status") {
return item.status === null ? (
<span className={MUTED_CLASS}>{t("shared.unknown")}</span>
) : (
<BadgeFeedback farbe={statusTone(item.status)} size="small">
{String(item.status)}
</BadgeFeedback>
);
}
return <span className="font-mono text-body-small-regular-0 text-text-neutral-primary">{key === "endpoint" ? item.endpoint : item.params}</span>;
}
}