Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

refactor: extract joinTableCoreAndMetrics to own file (#2315)

Marc Klingen8fdd4a7ca794
diff --git a/web/src/components/table/utils/joinTableCoreAndMetrics.ts b/web/src/components/table/utils/joinTableCoreAndMetrics.ts
new file mode 100644
index 0000000000..0012c64b64
--- /dev/null
+++ b/web/src/components/table/utils/joinTableCoreAndMetrics.ts
@@ -0,0 +1,46 @@
+export function joinTableCoreAndMetrics<
+ Core extends { id: string },
+ Metric extends { id: string },
+>(
+ userCoreData?: Core[],
+ userMetricsData?: Metric[],
+): {
+ status: "loading" | "error" | "success";
+ rows: (Core & Partial<Metric>)[] | undefined;
+} {
+ if (!userCoreData) {
+ return { status: "error", rows: undefined };
+ }
+
+ const userCoreDataProcessed = userCoreData;
+
+ if (!userMetricsData) {
+ // create an object with all the keys of the UserMetrics type with undefined value
+
+ return {
+ status: "success",
+ rows: userCoreDataProcessed.map((u) => ({
+ ...u,
+ ...({} as Partial<Metric>),
+ })),
+ };
+ }
+
+ const metricsById = userMetricsData.reduce<Record<string, Metric>>(
+ (acc, metric) => {
+ acc[metric.id] = metric;
+ return acc;
+ },
+ {},
+ );
+
+ const joinedData = userCoreDataProcessed.map((userCore) => {
+ const metrics = metricsById[userCore.id];
+ return {
+ ...userCore,
+ ...(metrics ?? ({} as Partial<Metric>)),
+ };
+ });
+
+ return { status: "success", rows: joinedData };
+}
diff --git a/web/src/pages/project/[projectId]/users.tsx b/web/src/pages/project/[projectId]/users.tsx
index 2788f3ccde..555e3c05a8 100644
--- a/web/src/pages/project/[projectId]/users.tsx
+++ b/web/src/pages/project/[projectId]/users.tsx
@@ -19,6 +19,7 @@ import { type Score } from "@langfuse/shared";
import { utcDateOffsetByDays } from "@/src/utils/dates";
import { useSession } from "next-auth/react";
import { usersTableCols } from "@/src/server/api/definitions/usersTable";
+import { joinTableCoreAndMetrics } from "@/src/components/table/utils/joinTableCoreAndMetrics";
export type ScoreFilterInput = Omit<RouterInput["users"]["all"], "projectId">;
@@ -284,50 +285,3 @@ export default function UsersPage() {
</FullScreenPage>
);
}
-
-function joinTableCoreAndMetrics<
- Core extends { id: string },
- Metric extends { id: string },
->(
- userCoreData?: Core[],
- userMetricsData?: Metric[],
-): {
- status: "loading" | "error" | "success";
- rows: (Core & Partial<Metric>)[] | undefined;
-} {
- if (!userCoreData) {
- return { status: "error", rows: undefined };
- }
-
- const userCoreDataProcessed = userCoreData;
-
- if (!userMetricsData) {
- // create an object with all the keys of the UserMetrics type with undefined value
-
- return {
- status: "success",
- rows: userCoreDataProcessed.map((u) => ({
- ...u,
- ...({} as Partial<Metric>),
- })),
- };
- }
-
- const metricsById = userMetricsData.reduce<Record<string, Metric>>(
- (acc, metric) => {
- acc[metric.id] = metric;
- return acc;
- },
- {},
- );
-
- const joinedData = userCoreDataProcessed.map((userCore) => {
- const metrics = metricsById[userCore.id];
- return {
- ...userCore,
- ...(metrics ?? ({} as Partial<Metric>)),
- };
- });
-
- return { status: "success", rows: joinedData };
-}