-
Notifications
You must be signed in to change notification settings - Fork 382
/
TracesBarListChart.tsx
123 lines (116 loc) 路 3.56 KB
/
TracesBarListChart.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { api } from "@/src/utils/api";
import { type FilterState } from "@/src/features/filters/types";
import { ExpandListButton } from "@/src/features/dashboard/components/cards/ChevronButton";
import { useState } from "react";
import DocPopup from "@/src/components/layouts/doc-popup";
import { DashboardCard } from "@/src/features/dashboard/components/cards/DashboardCard";
import { TotalMetric } from "@/src/features/dashboard/components/TotalMetric";
import { BarList } from "@tremor/react";
import { NoData } from "@/src/features/dashboard/components/NoData";
import { compactNumberFormatter } from "@/src/utils/numbers";
export const TracesBarListChart = ({
className,
projectId,
globalFilterState,
}: {
className?: string;
projectId: string;
globalFilterState: FilterState;
}) => {
const [isExpanded, setIsExpanded] = useState(false);
const timeFilter = globalFilterState.map((f) =>
f.type === "datetime" ? { ...f, column: "timestamp" } : f,
);
const totalTraces = api.dashboard.chart.useQuery(
{
projectId,
from: "traces",
select: [{ column: "traceId", agg: "COUNT" }],
filter: timeFilter,
},
{
trpc: {
context: {
skipBatch: true,
},
},
},
);
const traces = api.dashboard.chart.useQuery(
{
projectId,
from: "traces",
select: [{ column: "traceId", agg: "COUNT" }, { column: "traceName" }],
filter: timeFilter,
groupBy: [{ column: "traceName", type: "string" }],
orderBy: [{ column: "traceId", direction: "DESC", agg: "COUNT" }],
},
{
trpc: {
context: {
skipBatch: true,
},
},
},
);
const transformedTraces = traces.data
? traces.data.map((item) => {
return {
name: item.traceName ? (item.traceName as string) : "Unknown",
value: item.countTraceId as number,
};
})
: [];
const maxNumberOfEntries = { collapsed: 5, expanded: 20 };
const adjustedData = isExpanded
? transformedTraces.slice(0, maxNumberOfEntries.expanded)
: transformedTraces.slice(0, maxNumberOfEntries.collapsed);
return (
<DashboardCard
className={className}
title={"Traces"}
description={null}
isLoading={traces.isLoading || totalTraces.isLoading}
>
<>
<TotalMetric
metric={compactNumberFormatter(
totalTraces.data?.[0]?.countTraceId as number,
)}
description={"Total traces tracked"}
/>
{adjustedData.length > 0 ? (
<>
<BarList
data={adjustedData}
valueFormatter={(number: number) =>
Intl.NumberFormat("us").format(number).toString()
}
className="mt-6"
showAnimation={true}
color={"indigo"}
/>
</>
) : (
<NoData noDataText="No data">
<DocPopup
description="Traces contain details about LLM applications and can be created using the SDK."
href="https://langfuse.com/docs/integrations/sdk#1-backend-tracing"
/>
</NoData>
)}
<ExpandListButton
isExpanded={isExpanded}
setExpanded={setIsExpanded}
totalLength={transformedTraces.length}
maxLength={maxNumberOfEntries.collapsed}
expandText={
transformedTraces.length > maxNumberOfEntries.expanded
? `Show top ${maxNumberOfEntries.expanded}`
: "Show all"
}
/>
</>
</DashboardCard>
);
};