Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions app/subs/otel/[tenant]/e/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import Link from "next/link";
import { notFound, redirect } from "next/navigation";
import { findTenant, getSession, tenantPath } from "@/app/subs/otel/lib/tenant";
import { eventById } from "@/app/subs/otel/lib/queries";
import { Card, Header, kst } from "../../ui";

export const dynamic = "force-dynamic";

const PRE =
"max-h-[70vh] overflow-auto whitespace-pre-wrap break-all rounded-proofer-xs bg-proofer-bg-alt p-3 text-xs";

// 이벤트 한 건의 원본(속성, 리소스, 스코프, 본문)을 그대로 보여준다.
export default async function EventDetail({
params,
}: {
params: Promise<{ tenant: string; id: string }>;
}) {
const { tenant: slug, id } = await params;
const tenant = await findTenant(slug);
if (!tenant) notFound();
const session = await getSession(tenant);
if (!session) redirect(tenantPath(slug));

const eventId = Number(id);
if (!Number.isSafeInteger(eventId)) notFound();
const e = await eventById(tenant.id, eventId);
if (!e) notFound();

// 긴 문자열 속성(prompt, response, tool_input)은 따로 펼쳐 읽기 쉽게 한다.
const longText = Object.entries(e.attributes).filter(
([, v]) => typeof v === "string" && (v.length > 200 || v.includes("\n")),
) as [string, string][];

return (
<>
<Header session={session} />
<main className="mx-auto max-w-[1160px] space-y-4 px-6 py-8">
<Link
href={tenantPath(slug, "/events")}
className="text-sm text-proofer-gray"
>
이벤트 목록으로
</Link>
<h1 className="text-xl font-bold">
<code>{e.event_name}</code>
</h1>
<p className="text-sm text-proofer-gray">
{kst(e.at)} / {e.service_name} / {e.user_email}
{e.session_id && (
<>
{" / "}
<Link
href={tenantPath(
slug,
`/s/${encodeURIComponent(e.session_id)}`,
)}
className="underline underline-offset-2"
>
세션 리플레이
</Link>
</>
)}
{e.prompt_id && (
<>
{" / "}
<Link
href={tenantPath(slug, `/p/${e.prompt_id}`)}
className="underline underline-offset-2"
>
같은 프롬프트의 이벤트
</Link>
</>
)}
</p>
{longText.map(([k, v]) => (
<Card key={k} title={k}>
<pre className={PRE}>{v}</pre>
</Card>
))}
<Card title="attributes">
<pre className={PRE}>{JSON.stringify(e.attributes, null, 2)}</pre>
</Card>
<div className="grid gap-4 md:grid-cols-2">
<Card title="resource">
<pre className={PRE}>{JSON.stringify(e.resource, null, 2)}</pre>
</Card>
<Card title="scope / body">
<pre className={PRE}>
{JSON.stringify(
{ scope: e.scope, body: e.body, severity: e.severity },
null,
2,
)}
</pre>
</Card>
</div>
</main>
</>
);
}
177 changes: 177 additions & 0 deletions app/subs/otel/[tenant]/events/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import Link from "next/link";
import { notFound, redirect } from "next/navigation";
import { findTenant, getSession, tenantPath } from "@/app/subs/otel/lib/tenant";
import {
EVENT_PAGE,
RANGES,
eventFacets,
listEvents,
parseRange,
} from "@/app/subs/otel/lib/queries";
import { Card, Header, Table, kst, n } from "../ui";

export const dynamic = "force-dynamic";

type Search = {
days?: string;
event?: string;
user?: string;
session?: string;
prompt?: string;
before?: string;
};

const SELECT =
"h-9 rounded-proofer-xs border border-proofer-line bg-white px-2 text-sm";

// 모든 이벤트를 종류, 사용자, 세션, 프롬프트로 걸러 본다. 행을 누르면 원본 전체가 보인다.
export default async function Events({
params,
searchParams,
}: {
params: Promise<{ tenant: string }>;
searchParams: Promise<Search>;
}) {
const { tenant: slug } = await params;
const q = await searchParams;
const tenant = await findTenant(slug);
if (!tenant) notFound();
const session = await getSession(tenant);
if (!session) redirect(tenantPath(slug));

const days = parseRange(q.days);
const before = Number(q.before) || undefined;
const [facets, list] = await Promise.all([
eventFacets(tenant.id, days),
listEvents(tenant.id, {
days,
event: q.event || undefined,
user: q.user || undefined,
session: q.session || undefined,
prompt: q.prompt || undefined,
before,
}),
]);

const base = tenantPath(slug, "/events");
const keep = new URLSearchParams(
Object.entries({ ...q, before: undefined }).filter(
(e): e is [string, string] => typeof e[1] === "string" && e[1] !== "",
),
);
const nextHref =
list.length === EVENT_PAGE
? `${base}?${new URLSearchParams({ ...Object.fromEntries(keep), before: String(list[list.length - 1].id) })}`
: null;

return (
<>
<Header session={session} />
<main className="mx-auto max-w-[1160px] space-y-6 px-6 py-8">
<h1 className="text-xl font-bold">이벤트</h1>
<form
method="get"
action={base}
className="flex flex-wrap items-end gap-2"
>
<select name="days" defaultValue={String(days)} className={SELECT}>
{RANGES.map((r) => (
<option key={r} value={r}>
최근 {r}일
</option>
))}
</select>
<select name="event" defaultValue={q.event ?? ""} className={SELECT}>
<option value="">모든 이벤트</option>
{facets.events.map((f) => (
<option key={f.v} value={f.v}>
{f.v} ({n(f.n)})
</option>
))}
</select>
<select name="user" defaultValue={q.user ?? ""} className={SELECT}>
<option value="">모든 사용자</option>
{facets.users.map((f) => (
<option key={f.v} value={f.v}>
{f.v} ({n(f.n)})
</option>
))}
</select>
<input
name="session"
defaultValue={q.session ?? ""}
placeholder="session.id"
className={`${SELECT} w-56`}
/>
<input
name="prompt"
defaultValue={q.prompt ?? ""}
placeholder="prompt.id"
className={`${SELECT} w-56`}
/>
<button className="h-9 rounded-proofer-xs bg-proofer-ink px-4 text-sm font-bold text-white">
적용
</button>
<Link
href={base}
className="h-9 px-2 text-sm leading-9 text-proofer-gray"
>
초기화
</Link>
</form>

<Card>
<Table
head={["시각", "이벤트", "요약", "사용자", "세션", ""]}
rows={list.map((e) => [
<span key="t" className="whitespace-nowrap">
{kst(e.at)}
</span>,
<code key="e" className="text-xs">
{e.event_name}
</code>,
<span
key="s"
className="line-clamp-1 max-w-[320px] text-proofer-gray"
>
{e.summary}
</span>,
e.user_email ?? "",
e.session_id ? (
<Link
key="sid"
href={tenantPath(
slug,
`/s/${encodeURIComponent(e.session_id)}`,
)}
className="font-mono text-xs hover:underline"
>
{e.session_id.slice(0, 8)}
</Link>
) : (
""
),
<Link
key="d"
href={tenantPath(slug, `/e/${e.id}`)}
className="whitespace-nowrap font-bold underline underline-offset-2"
>
원본
</Link>,
])}
/>
{nextHref && (
<div className="mt-4 text-right">
<Link
href={nextHref}
className="text-sm underline underline-offset-2"
>
다음 {EVENT_PAGE}건
</Link>
</div>
)}
</Card>
</main>
</>
);
}
66 changes: 46 additions & 20 deletions app/subs/otel/[tenant]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
kpis,
parseRange,
recentPrompts,
textOrNull,
} from "@/app/subs/otel/lib/queries";
import DailyChart from "./DailyChart";
import { Card, Header, Stat, Table, compact, kst, n, usd } from "./ui";
Expand Down Expand Up @@ -135,7 +136,13 @@ export default async function TenantDashboard({ params, searchParams }: Props) {
head={["사용자", "프롬프트", "세션", "비용", "마지막 활동"]}
align={["left", "right", "right", "right", "right"]}
rows={users.map((u) => [
u.user_email ?? "(없음)",
<Link
key="u"
href={`${tenantPath(slug, "/sessions")}?days=${days}&user=${encodeURIComponent(u.user_email ?? "")}`}
className="hover:underline"
>
{u.user_email ?? "(없음)"}
</Link>,
n(u.prompts),
n(u.sessions),
usd(u.cost_usd),
Expand Down Expand Up @@ -175,25 +182,44 @@ export default async function TenantDashboard({ params, searchParams }: Props) {

<Card title="최근 프롬프트">
<Table
head={["시각", "사용자", "출처", "프롬프트"]}
rows={prompts.map((p) => [
<span key="a" className="whitespace-nowrap">
{kst(p.at)}
</span>,
p.user_email ?? "",
p.service_name ?? "",
p.prompt_id ? (
<Link
key="p"
href={tenantPath(slug, `/p/${p.prompt_id}`)}
className="line-clamp-2 underline-offset-2 hover:underline"
>
{p.prompt ?? `(본문 미수집, ${p.prompt_length ?? "?"}자)`}
</Link>
) : (
(p.prompt ?? `(본문 미수집, ${p.prompt_length ?? "?"}자)`)
),
])}
head={["시각", "사용자", "출처", "프롬프트", ""]}
rows={prompts.map((p) => {
const text =
textOrNull(p.prompt) ??
`(본문 미수집, ${p.prompt_length ?? "?"}자)`;
return [
<span key="a" className="whitespace-nowrap">
{kst(p.at)}
</span>,
p.user_email ?? "",
p.service_name ?? "",
p.prompt_id ? (
<Link
key="p"
href={tenantPath(slug, `/p/${p.prompt_id}`)}
className="line-clamp-2 underline-offset-2 hover:underline"
>
{text}
</Link>
) : (
text
),
p.session_id ? (
<Link
key="r"
href={tenantPath(
slug,
`/s/${encodeURIComponent(p.session_id)}`,
)}
className="whitespace-nowrap font-bold underline underline-offset-2"
>
리플레이
</Link>
) : (
""
),
];
})}
/>
</Card>

Expand Down
Loading
Loading