Skip to content

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

feat: skip batching trpc routes for dashboards (#643)

Max Deichmann44e4010e154a
diff --git a/src/features/dashboard/components/ChartScores.tsx b/src/features/dashboard/components/ChartScores.tsx
index 9e289ca7d2..61a54beb6d 100644
--- a/src/features/dashboard/components/ChartScores.tsx
+++ b/src/features/dashboard/components/ChartScores.tsx
@@ -20,26 +20,35 @@ export function ChartScores(props: {
globalFilterState: FilterState;
projectId: string;
}) {
- const scores = api.dashboard.chart.useQuery({
- projectId: props.projectId,
- from: "traces_scores",
- select: [{ column: "scoreName" }, { column: "value", agg: "AVG" }],
- filter:
- props.globalFilterState.map((f) =>
- f.type === "datetime" ? { ...f, column: "timestamp" } : f,
- ) ?? [],
- groupBy: [
- {
- type: "datetime",
- column: "timestamp",
- temporalUnit: dateTimeAggregationSettings[props.agg].date_trunc,
+ const scores = api.dashboard.chart.useQuery(
+ {
+ projectId: props.projectId,
+ from: "traces_scores",
+ select: [{ column: "scoreName" }, { column: "value", agg: "AVG" }],
+ filter:
+ props.globalFilterState.map((f) =>
+ f.type === "datetime" ? { ...f, column: "timestamp" } : f,
+ ) ?? [],
+ groupBy: [
+ {
+ type: "datetime",
+ column: "timestamp",
+ temporalUnit: dateTimeAggregationSettings[props.agg].date_trunc,
+ },
+ {
+ type: "string",
+ column: "scoreName",
+ },
+ ],
+ },
+ {
+ trpc: {
+ context: {
+ skipBatch: true,
+ },
},
- {
- type: "string",
- column: "scoreName",
- },
- ],
- });
+ },
+ );
const extractedScores = scores.data
? fillMissingValuesAndTransform(
diff --git a/src/features/dashboard/components/LatencyChart.tsx b/src/features/dashboard/components/LatencyChart.tsx
index 6d5f52af41..4b978ad51a 100644
--- a/src/features/dashboard/components/LatencyChart.tsx
+++ b/src/features/dashboard/components/LatencyChart.tsx
@@ -27,30 +27,44 @@ export const LatencyChart = ({
globalFilterState: FilterState;
agg: DateTimeAggregationOption;
}) => {
- const latencies = api.dashboard.chart.useQuery({
- projectId,
- from: "observations",
- select: [
- { column: "duration", agg: "50thPercentile" },
- { column: "duration", agg: "90thPercentile" },
- { column: "duration", agg: "95thPercentile" },
- { column: "duration", agg: "99thPercentile" },
- { column: "model" },
- ],
- filter:
- [
- ...globalFilterState,
- { type: "string", column: "type", operator: "=", value: "GENERATION" },
- ] ?? [],
- groupBy: [
- {
- type: "datetime",
- column: "startTime",
- temporalUnit: dateTimeAggregationSettings[agg].date_trunc,
+ const latencies = api.dashboard.chart.useQuery(
+ {
+ projectId,
+ from: "observations",
+ select: [
+ { column: "duration", agg: "50thPercentile" },
+ { column: "duration", agg: "90thPercentile" },
+ { column: "duration", agg: "95thPercentile" },
+ { column: "duration", agg: "99thPercentile" },
+ { column: "model" },
+ ],
+ filter:
+ [
+ ...globalFilterState,
+ {
+ type: "string",
+ column: "type",
+ operator: "=",
+ value: "GENERATION",
+ },
+ ] ?? [],
+ groupBy: [
+ {
+ type: "datetime",
+ column: "startTime",
+ temporalUnit: dateTimeAggregationSettings[agg].date_trunc,
+ },
+ { type: "string", column: "model" },
+ ],
+ },
+ {
+ trpc: {
+ context: {
+ skipBatch: true,
+ },
},
- { type: "string", column: "model" },
- ],
- });
+ },
+ );
const allModels = getAllModels(projectId, globalFilterState);
diff --git a/src/features/dashboard/components/MetricTable.tsx b/src/features/dashboard/components/MetricTable.tsx
index 39fc21b7c8..63f56be574 100644
--- a/src/features/dashboard/components/MetricTable.tsx
+++ b/src/features/dashboard/components/MetricTable.tsx
@@ -21,18 +21,27 @@ export const MetricTable = ({
column: "timestamp",
}));
- const metrics = api.dashboard.chart.useQuery({
- projectId,
- from: "traces_observations",
- select: [
- { column: "totalTokenCost" },
- { column: "totalTokens", agg: "SUM" },
- { column: "model" },
- ],
- filter: localFilters ?? [],
- groupBy: [{ type: "string", column: "model" }],
- orderBy: [{ column: "totalTokenCost", direction: "DESC" }],
- });
+ const metrics = api.dashboard.chart.useQuery(
+ {
+ projectId,
+ from: "traces_observations",
+ select: [
+ { column: "totalTokenCost" },
+ { column: "totalTokens", agg: "SUM" },
+ { column: "model" },
+ ],
+ filter: localFilters ?? [],
+ groupBy: [{ type: "string", column: "model" }],
+ orderBy: [{ column: "totalTokenCost", direction: "DESC" }],
+ },
+ {
+ trpc: {
+ context: {
+ skipBatch: true,
+ },
+ },
+ },
+ );
const totalTokens = metrics.data?.reduce(
(acc, curr) =>
diff --git a/src/features/dashboard/components/ModelUsageChart.tsx b/src/features/dashboard/components/ModelUsageChart.tsx
index c2e1cefdc6..61fca8ba87 100644
--- a/src/features/dashboard/components/ModelUsageChart.tsx
+++ b/src/features/dashboard/components/ModelUsageChart.tsx
@@ -30,28 +30,37 @@ export const ModelUsageChart = ({
globalFilterState: FilterState;
agg: DateTimeAggregationOption;
}) => {
- const tokens = api.dashboard.chart.useQuery({
- projectId,
- from: "observations",
- select: [
- { column: "totalTokens", agg: "SUM" },
- { column: "totalTokenCost" },
- { column: "model" },
- ],
- filter: globalFilterState ?? [],
- groupBy: [
- {
- type: "datetime",
- column: "startTime",
- temporalUnit: dateTimeAggregationSettings[agg].date_trunc,
- },
- {
- type: "string",
- column: "model",
+ const tokens = api.dashboard.chart.useQuery(
+ {
+ projectId,
+ from: "observations",
+ select: [
+ { column: "totalTokens", agg: "SUM" },
+ { column: "totalTokenCost" },
+ { column: "model" },
+ ],
+ filter: globalFilterState ?? [],
+ groupBy: [
+ {
+ type: "datetime",
+ column: "startTime",
+ temporalUnit: dateTimeAggregationSettings[agg].date_trunc,
+ },
+ {
+ type: "string",
+ column: "model",
+ },
+ ],
+ orderBy: [{ column: "totalTokenCost", direction: "DESC" }],
+ },
+ {
+ trpc: {
+ context: {
+ skipBatch: true,
+ },
},
- ],
- orderBy: [{ column: "totalTokenCost", direction: "DESC" }],
- });
+ },
+ );
const allModels = getAllModels(projectId, globalFilterState);
diff --git a/src/features/dashboard/components/ScoresTable.tsx b/src/features/dashboard/components/ScoresTable.tsx
index 77225505cc..a4abc6d11e 100644
--- a/src/features/dashboard/components/ScoresTable.tsx
+++ b/src/features/dashboard/components/ScoresTable.tsx
@@ -22,37 +22,55 @@ export const ScoresTable = ({
column: "timestamp",
}));
- const metrics = api.dashboard.chart.useQuery({
- projectId,
- from: "traces_scores",
- select: [
- { column: "scoreName" },
- { column: "scoreId", agg: "COUNT" },
- { column: "value", agg: "AVG" },
- ],
- filter: localFilters ?? [],
- groupBy: [{ type: "string", column: "scoreName" }],
- orderBy: [{ column: "scoreId", direction: "DESC", agg: "COUNT" }],
- });
-
- const [zeroValueScores, oneValueScores] = [0, 1].map((i) =>
- api.dashboard.chart.useQuery({
+ const metrics = api.dashboard.chart.useQuery(
+ {
projectId,
from: "traces_scores",
- select: [{ column: "scoreName" }, { column: "scoreId", agg: "COUNT" }],
- filter:
- [
- ...localFilters,
- {
- column: "value",
- operator: "=",
- value: i,
- type: "number",
- },
- ] ?? [],
+ select: [
+ { column: "scoreName" },
+ { column: "scoreId", agg: "COUNT" },
+ { column: "value", agg: "AVG" },
+ ],
+ filter: localFilters ?? [],
groupBy: [{ type: "string", column: "scoreName" }],
orderBy: [{ column: "scoreId", direction: "DESC", agg: "COUNT" }],
- }),
+ },
+ {
+ trpc: {
+ context: {
+ skipBatch: true,
+ },
+ },
+ },
+ );
+
+ const [zeroValueScores, oneValueScores] = [0, 1].map((i) =>
+ api.dashboard.chart.useQuery(
+ {
+ projectId,
+ from: "traces_scores",
+ select: [{ column: "scoreName" }, { column: "scoreId", agg: "COUNT" }],
+ filter:
+ [
+ ...localFilters,
+ {
+ column: "value",
+ operator: "=",
+ value: i,
+ type: "number",
+ },
+ ] ?? [],
+ groupBy: [{ type: "string", column: "scoreName" }],
+ orderBy: [{ column: "scoreId", direction: "DESC", agg: "COUNT" }],
+ },
+ {
+ trpc: {
+ context: {
+ skipBatch: true,
+ },
+ },
+ },
+ ),
);
if (!zeroValueScores || !oneValueScores) {
diff --git a/src/features/dashboard/components/TracesBarListChart.tsx b/src/features/dashboard/components/TracesBarListChart.tsx
index e002578e3a..bc0e230d75 100644
--- a/src/features/dashboard/components/TracesBarListChart.tsx
+++ b/src/features/dashboard/components/TracesBarListChart.tsx
@@ -23,21 +23,39 @@ export const TracesBarListChart = ({
f.type === "datetime" ? { ...f, column: "timestamp" } : f,
) ?? [];
- const totalTraces = api.dashboard.chart.useQuery({
- projectId,
- from: "traces",
- select: [{ column: "traceId", agg: "COUNT" }],
- filter: timeFilter,
- });
+ 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" }],
- });
+ 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) => {
diff --git a/src/features/dashboard/components/TracesTimeSeriesChart.tsx b/src/features/dashboard/components/TracesTimeSeriesChart.tsx
index d5796d2fae..42e339f74f 100644
--- a/src/features/dashboard/components/TracesTimeSeriesChart.tsx
+++ b/src/features/dashboard/components/TracesTimeSeriesChart.tsx
@@ -23,22 +23,31 @@ export const TracesTimeSeriesChart = ({
globalFilterState: FilterState;
agg: DateTimeAggregationOption;
}) => {
- const traces = api.dashboard.chart.useQuery({
- projectId,
- from: "traces",
- select: [{ column: "traceId", agg: "COUNT" }],
- filter:
- globalFilterState.map((f) =>
- f.type === "datetime" ? { ...f, column: "timestamp" } : f,
- ) ?? [],
- groupBy: [
- {
- type: "datetime",
- column: "timestamp",
- temporalUnit: dateTimeAggregationSettings[agg].date_trunc,
+ const traces = api.dashboard.chart.useQuery(
+ {
+ projectId,
+ from: "traces",
+ select: [{ column: "traceId", agg: "COUNT" }],
+ filter:
+ globalFilterState.map((f) =>
+ f.type === "datetime" ? { ...f, column: "timestamp" } : f,
+ ) ?? [],
+ groupBy: [
+ {
+ type: "datetime",
+ column: "timestamp",
+ temporalUnit: dateTimeAggregationSettings[agg].date_trunc,
+ },
+ ],
+ },
+ {
+ trpc: {
+ context: {
+ skipBatch: true,
+ },
},
- ],
- });
+ },
+ );
const transformedTraces = traces.data
? traces.data.map((item) => {
diff --git a/src/features/dashboard/components/UserChart.tsx b/src/features/dashboard/components/UserChart.tsx
index 2298593ed9..6a2ef51bb3 100644
--- a/src/features/dashboard/components/UserChart.tsx
+++ b/src/features/dashboard/components/UserChart.tsx
@@ -27,41 +27,59 @@ export const UserChart = ({
agg: DateTimeAggregationOption;
}) => {
const [isExpanded, setIsExpanded] = useState(false);
- const user = api.dashboard.chart.useQuery({
- projectId,
- from: "traces_observations",
- select: [
- { column: "totalTokenCost" },
- { column: "user" },
- { column: "traceId", agg: "COUNT" },
- ],
- filter: globalFilterState ?? [],
- groupBy: [
- {
- type: "string",
- column: "user",
+ const user = api.dashboard.chart.useQuery(
+ {
+ projectId,
+ from: "traces_observations",
+ select: [
+ { column: "totalTokenCost" },
+ { column: "user" },
+ { column: "traceId", agg: "COUNT" },
+ ],
+ filter: globalFilterState ?? [],
+ groupBy: [
+ {
+ type: "string",
+ column: "user",
+ },
+ ],
+ orderBy: [{ column: "totalTokenCost", direction: "DESC" }],
+ },
+ {
+ trpc: {
+ context: {
+ skipBatch: true,
+ },
},
- ],
- orderBy: [{ column: "totalTokenCost", direction: "DESC" }],
- });
+ },
+ );
- const traces = api.dashboard.chart.useQuery({
- projectId,
- from: "traces",
- select: [{ column: "user" }, { column: "traceId", agg: "COUNT" }],
- filter:
- globalFilterState.map((f) => ({
- ...f,
- column: "timestamp",
- })) ?? [],
- groupBy: [
- {
- type: "string",
- column: "user",
+ const traces = api.dashboard.chart.useQuery(
+ {
+ projectId,
+ from: "traces",
+ select: [{ column: "user" }, { column: "traceId", agg: "COUNT" }],
+ filter:
+ globalFilterState.map((f) => ({
+ ...f,
+ column: "timestamp",
+ })) ?? [],
+ groupBy: [
+ {
+ type: "string",
+ column: "user",
+ },
+ ],
+ orderBy: [{ column: "traceId", agg: "COUNT", direction: "DESC" }],
+ },
+ {
+ trpc: {
+ context: {
+ skipBatch: true,
+ },
},
- ],
- orderBy: [{ column: "traceId", agg: "COUNT", direction: "DESC" }],
- });
+ },
+ );
const transformedNumberOfTraces: BarChartDataPoint[] = traces.data
? traces.data
diff --git a/src/features/dashboard/components/hooks.ts b/src/features/dashboard/components/hooks.ts
index d0d23fc4f7..0d5fedfc9c 100644
--- a/src/features/dashboard/components/hooks.ts
+++ b/src/features/dashboard/components/hooks.ts
@@ -7,17 +7,31 @@ export const getAllModels = (
projectId: string,
globalFilterState: FilterState,
) => {
- const allModels = api.dashboard.chart.useQuery({
- projectId,
- from: "observations",
- select: [{ column: "model" }],
- filter:
- [
- ...globalFilterState,
- { type: "string", column: "type", operator: "=", value: "GENERATION" },
- ] ?? [],
- groupBy: [{ type: "string", column: "model" }],
- });
+ const allModels = api.dashboard.chart.useQuery(
+ {
+ projectId,
+ from: "observations",
+ select: [{ column: "model" }],
+ filter:
+ [
+ ...globalFilterState,
+ {
+ type: "string",
+ column: "type",
+ operator: "=",
+ value: "GENERATION",
+ },
+ ] ?? [],
+ groupBy: [{ type: "string", column: "model" }],
+ },
+ {
+ trpc: {
+ context: {
+ skipBatch: true,
+ },
+ },
+ },
+ );
return allModels.data ? extractAllModels(allModels.data) : [];
};
diff --git a/src/utils/api.ts b/src/utils/api.ts
index 1af7f0d11d..60f5850b34 100644
--- a/src/utils/api.ts
+++ b/src/utils/api.ts
@@ -4,7 +4,13 @@
*
* We also create a few inference helpers for input and output types.
*/
-import { createTRPCProxyClient, httpBatchLink, loggerLink } from "@trpc/client";
+import {
+ createTRPCProxyClient,
+ httpBatchLink,
+ httpLink,
+ loggerLink,
+ splitLink,
+} from "@trpc/client";
import { createTRPCNext } from "@trpc/next";
import { type inferRouterInputs, type inferRouterOutputs } from "@trpc/server";
import superjson from "superjson";
@@ -39,8 +45,20 @@ export const api = createTRPCNext<AppRouter>({
process.env.NODE_ENV === "development" ||
(opts.direction === "down" && opts.result instanceof Error),
}),
- httpBatchLink({
- url: `${getBaseUrl()}/api/trpc`,
+ splitLink({
+ condition(op) {
+ console.log("op", op);
+ // check for context property `skipBatch`
+ return op.context.skipBatch === true;
+ },
+ // when condition is true, use normal request
+ true: httpLink({
+ url: `${getBaseUrl()}/api/trpc`,
+ }),
+ // when condition is false, use batching
+ false: httpBatchLink({
+ url: `${getBaseUrl()}/api/trpc`,
+ }),
}),
],
};