🪢 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
fix: Correct sorting on all tables (#1168)
gitstart-app[bot]8c8e58e2b897
diff --git a/src/components/table/use-cases/scores.tsx b/src/components/table/use-cases/scores.tsxindex ebf950bd8a..9547837f23 100644--- a/src/components/table/use-cases/scores.tsx+++ b/src/components/table/use-cases/scores.tsx@@ -4,6 +4,7 @@ import TableLink from "@/src/components/table/table-link";import { type LangfuseColumnDef } from "@/src/components/table/types";import useColumnVisibility from "@/src/features/column-visibility/hooks/useColumnVisibility";import { useQueryFilterState } from "@/src/features/filters/hooks/useFilterState";+import { useOrderByState } from "@/src/features/orderBy/hooks/useOrderByState";import { scoresTableColsWithOptions } from "@/src/server/api/definitions/scoresTable";import { api } from "@/src/utils/api";import { type RouterInput } from "@/src/utils/types";@@ -49,11 +50,17 @@ export default function ScoresTable({]): userFilterState;+ const [orderByState, setOrderByState] = useOrderByState({+ column: "timestamp",+ order: "DESC",+ });+const scores = api.scores.all.useQuery({page: paginationState.pageIndex,limit: paginationState.pageSize,projectId,filter: filterState,+ orderBy: orderByState,});const totalCount = scores.data?.slice(1)[0]?.totalCount ?? 0;@@ -162,6 +169,8 @@ export default function ScoresTable({onChange: setPaginationState,state: paginationState,}}+ orderBy={orderByState}+ setOrderBy={setOrderByState}columnVisibility={columnVisibility}onColumnVisibilityChange={setColumnVisibility}/>diff --git a/src/features/orderBy/server/orderByToPrisma.ts b/src/features/orderBy/server/orderByToPrisma.tsindex 80a58cabbc..61940c860e 100644--- a/src/features/orderBy/server/orderByToPrisma.ts+++ b/src/features/orderBy/server/orderByToPrisma.ts@@ -14,7 +14,7 @@ export function orderByToPrismaSql(tableColumns: ColumnDefinition[],): Prisma.Sql {if (!orderBy) {- return Prisma.sql`ORDER BY t.timestamp DESC`;+ return Prisma.sql`ORDER BY t.timestamp DESC NULLS LAST`;}// Get column definition to map column to internal name, e.g. "t.id"const col = tableColumns.find(@@ -37,5 +37,7 @@ export function orderByToPrismaSql(}// Both column and order are safe, can use raw SQL- return Prisma.raw(`ORDER BY ${col.internal} ${order.data}`);+ return Prisma.raw(+ `ORDER BY ${col.internal} ${order.data} ${orderBy.order === "DESC" ? "NULLS LAST" : "NULLS FIRST"}`,+ );}diff --git a/src/server/api/routers/scores.ts b/src/server/api/routers/scores.tsindex 8bd53173dd..913b8cb30e 100644--- a/src/server/api/routers/scores.ts+++ b/src/server/api/routers/scores.ts@@ -14,11 +14,14 @@ import {type ScoreOptions,scoresTableCols,} from "@/src/server/api/definitions/scoresTable";+import { orderBy } from "@/src/server/api/interfaces/orderBy";+import { orderByToPrismaSql } from "@/src/features/orderBy/server/orderByToPrisma";import { auditLog } from "@/src/features/audit-logs/auditLog";const ScoreFilterOptions = z.object({projectId: z.string(), // Required for protectedProjectProcedurefilter: z.array(singleFilter),+ orderBy: orderBy,});const ScoreAllOptions = ScoreFilterOptions.extend({@@ -32,6 +35,11 @@ export const scoresRouter = createTRPCRouter({const filterCondition = filterToPrismaSql(input.filter, scoresTableCols);console.log("filters: ", filterCondition);+ const orderByCondition = orderByToPrismaSql(+ input.orderBy,+ scoresTableCols,+ );+const scores = await ctx.prisma.$queryRaw<Array<Score & { traceName: string; totalCount: number }>>(Prisma.sql`@@ -49,7 +57,7 @@ export const scoresRouter = createTRPCRouter({JOIN traces t ON t.id = s.trace_idWHERE t.project_id = ${input.projectId}${filterCondition}- ORDER BY s.timestamp DESC+ ${orderByCondition}LIMIT ${input.limit}OFFSET ${input.page * input.limit}`);diff --git a/src/server/api/services/query-builder.ts b/src/server/api/services/query-builder.tsindex c826a82cc5..e58f1fb0e2 100644--- a/src/server/api/services/query-builder.ts+++ b/src/server/api/services/query-builder.ts@@ -211,7 +211,7 @@ const prepareOrderByString = (return Prisma.sql`${createAggregatedColumn(safeColumn,safeAgg,- )} ${Prisma.raw(orderBy.direction)}`;+ )} ${Prisma.raw(orderBy.direction)} ${Prisma.raw(orderBy.direction === "DESC" ? "NULLS LAST" : "NULLS FIRST")}`;});const addedCte = hasCte? [Prisma.sql`date_series."date" ASC`, ...orderBys]