🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
perf(models-table): lazy load lastUsed column (#10849)
Hassieb Pakzadfe7625a39189
diff --git a/web/src/components/table/use-cases/models.tsx b/web/src/components/table/use-cases/models.tsxindex 3023e675ea..efcf36414e 100644--- a/web/src/components/table/use-cases/models.tsx+++ b/web/src/components/table/use-cases/models.tsx@@ -25,6 +25,7 @@ import {TooltipContent,TooltipTrigger,} from "@/src/components/ui/tooltip";+import { Skeleton } from "@/src/components/ui/skeleton";import { LangfuseIcon } from "@/src/components/LangfuseLogo";import { useRouter } from "next/router";import { PriceUnitSelector } from "@/src/features/models/components/PriceUnitSelector";@@ -44,7 +45,6 @@ export type ModelTableRow = {prices?: Record<string, number>;tokenizerId?: string;config?: Prisma.JsonValue;- lastUsed?: Date | null;serverResponse: GetModelResult;};@@ -91,6 +91,18 @@ export default function ModelTable({ projectId }: { projectId: string }) {},);const totalCount = models.data?.totalCount ?? null;++ const modelIds = models.data?.models.map((m) => m.id) ?? [];+ const lastUsed = api.models.lastUsedByModelIds.useQuery(+ { projectId, modelIds },+ {+ enabled: models.isSuccess && modelIds.length > 0,+ refetchOnWindowFocus: false,+ refetchOnMount: true,+ refetchOnReconnect: false,+ staleTime: 1000 * 60 * 10,+ },+ );const { priceUnit } = usePriceUnitMultiplier();const [rowHeight, setRowHeight] = useRowHeightLocalStorage("models", "m");@@ -224,7 +236,8 @@ export default function ModelTable({ projectId }: { projectId: string }) {enableHiding: true,size: 120,cell: ({ row }) => {- const value: Date | null | undefined = row.getValue("lastUsed");+ if (!lastUsed.data) return <Skeleton className="h-4 w-20" />;+ const value = lastUsed.data[row.original.modelId];return value?.toLocaleString() ?? "";},},@@ -280,7 +293,6 @@ export default function ModelTable({ projectId }: { projectId: string }) {prices,tokenizerId: model.tokenizerId ?? undefined,config: model.tokenizerConfig,- lastUsed: model.lastUsed,serverResponse: model,};};diff --git a/web/src/features/models/validation.ts b/web/src/features/models/validation.tsindex 3b9ee403c3..04695bc707 100644--- a/web/src/features/models/validation.ts+++ b/web/src/features/models/validation.ts@@ -56,7 +56,6 @@ export const GetModelResultSchema = z.object({]),tokenizerId: TokenizerSchema,pricingTiers: z.array(PricingTierSchema),- lastUsed: z.date().nullish(),});export type GetModelResult = z.infer<typeof GetModelResultSchema>;diff --git a/web/src/server/api/routers/models.ts b/web/src/server/api/routers/models.tsindex 18f1775e09..200b5a314b 100644--- a/web/src/server/api/routers/models.ts+++ b/web/src/server/api/routers/models.ts@@ -185,60 +185,54 @@ export const modelRouter = createTRPCRouter({.parse(allModelsQueryResult);const totalCount = z.coerce.number().parse(totalCountQuery[0].count);- // Check whether model was used in last month+ return {+ models: paginateArray({ data: allModels, page, limit }),+ totalCount,+ };+ }),++ lastUsedByModelIds: protectedProjectProcedure+ .input(+ z.object({+ projectId: z.string(),+ modelIds: z.array(z.string()),+ }),+ )+ .query(async ({ input }) => {+ const { projectId, modelIds } = input;++ if (modelIds.length === 0) return {};+const lastUsedQuery = `- SELECT- internal_model_id as modelId,- MAX(start_time) as lastUsed- FROM- observations- WHERE- project_id = {projectId: String}- AND internal_model_id IS NOT NULL- GROUP BY- internal_model_id+ SELECT+ internal_model_id as modelId,+ MAX(start_time) as lastUsed+ FROM observations+ WHERE project_id = {projectId: String}+ AND type = 'GENERATION'+ AND internal_model_id IN ({modelIds: Array(String)})+ GROUP BY internal_model_id`;- const lastUsedQueryResult = ModelLastUsedQueryResult.safeParse(+ const result = ModelLastUsedQueryResult.safeParse(await queryClickhouse({query: lastUsedQuery,- params: { projectId },+ params: { projectId, modelIds },}),);- if (lastUsedQueryResult.success) {- const lastUsedMap = new Map(- lastUsedQueryResult.data.map((res) => [res.modelId, res.lastUsed]),- );-- const sortedModels = allModels- .map((m) => ({ ...m, lastUsed: lastUsedMap.get(m.id) }))- .sort((a, b) => {- const lastUsedA = lastUsedMap.get(a.id);- const lastUsedB = lastUsedMap.get(b.id);-- if (lastUsedA && lastUsedB) {- return lastUsedB.getTime() - lastUsedA.getTime();- } else if (lastUsedA) {- return -1;- } else if (lastUsedB) {- return 1;- }-- return 0;- });+ if (!result.success) return {};- return {- models: paginateArray({ data: sortedModels, page, limit }),- totalCount,- };- }+ return result.data.reduce(+ (acc, { modelId, lastUsed }) => {+ acc[modelId] = lastUsed;- return {- models: paginateArray({ data: allModels, page, limit }),- totalCount,- };+ return acc;+ },+ {} as Record<string, Date>,+ );}),+upsert: protectedProjectProcedure.input(UpsertModelSchema).mutation(async ({ input, ctx }) => {