Skip to content

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

fix: fix by model charts (#6208)

Max Deichmann9e77738a2fd3
diff --git a/packages/shared/src/server/repositories/dashboards.ts b/packages/shared/src/server/repositories/dashboards.ts
index 51edac36de..c72a48a9f9 100644
--- a/packages/shared/src/server/repositories/dashboards.ts
+++ b/packages/shared/src/server/repositories/dashboards.ts
@@ -286,8 +286,8 @@ export const getTotalObservationUsageByTimeByModel = async (
const query = `
SELECT
${selectTimeseriesColumn(bucketSizeInSeconds, "start_time", "start_time")},
- sumMap(usage_details) as units,
- sumMap(cost_details) as cost,
+ sumMap(usage_details)['total'] as units,
+ sumMap(cost_details)['total'] as cost,
provided_model_name
FROM observations o FINAL
${tracesFilter ? "LEFT JOIN traces t ON o.trace_id = t.id AND o.project_id = t.project_id" : ""}
@@ -301,8 +301,8 @@ export const getTotalObservationUsageByTimeByModel = async (
const result = await queryClickhouse<{
start_time: string;
- units: Record<string, number>;
- cost: Record<string, number>;
+ units: string;
+ cost: string;
provided_model_name: string;
}>({
query,
@@ -325,18 +325,8 @@ export const getTotalObservationUsageByTimeByModel = async (
return result.map((row) => ({
startTime: parseClickhouseUTCDateTimeFormat(row.start_time),
- units: Object.fromEntries(
- Object.entries(row.units ?? {}).map(([key, value]) => [
- key,
- Number(value),
- ]),
- ),
- cost: Object.fromEntries(
- Object.entries(row.cost ?? {}).map(([key, value]) => [
- key,
- Number(value),
- ]),
- ),
+ units: Number(row.units),
+ cost: Number(row.cost),
model: row.provided_model_name,
}));
};
diff --git a/web/src/__tests__/async/repositories/dashboard-repository.servertest.ts b/web/src/__tests__/async/repositories/dashboard-repository.servertest.ts
index 4cff16701b..e30a23fbfd 100644
--- a/web/src/__tests__/async/repositories/dashboard-repository.servertest.ts
+++ b/web/src/__tests__/async/repositories/dashboard-repository.servertest.ts
@@ -1,4 +1,3 @@
-import { prepareUsageDataForTimeseriesChart } from "@/src/features/dashboard/components/ModelUsageChart";
import {
orderByTimeSeries,
getObservationUsageByTypeByTime,
@@ -97,58 +96,6 @@ describe("orderByTimeSeries", () => {
);
});
- describe("aggregate time series for model cost and usage", () => {
- it("should aggregate time series for model cost and usage", async () => {
- const metricHistory = prepareUsageDataForTimeseriesChart(
- ["gpt-4o-mini", "text-embedding-ada-002"],
- [
- {
- startTime: "2025-02-10T13:30:00.000Z",
- units: {
- input: 422,
- output: 61,
- total: 483,
- },
- cost: {
- input: 0.0000633,
- output: 0.0000366,
- total: 0.0000999,
- },
- model: "gpt-4o-mini",
- },
- {
- startTime: "2025-02-10T13:30:00.000Z",
- units: {
- input: 6,
- total: 6,
- },
- cost: {
- total: 6e-7,
- },
- model: "text-embedding-ada-002",
- },
- ],
- );
-
- expect(metricHistory.get("total")).toEqual([
- {
- startTime: "2025-02-10T13:30:00.000Z",
- units: 483,
- cost: 0.0000999,
- model: "gpt-4o-mini",
- usageType: "total",
- },
- {
- startTime: "2025-02-10T13:30:00.000Z",
- units: 6,
- cost: 6e-7,
- model: "text-embedding-ada-002",
- usageType: "total",
- },
- ]);
- });
- });
-
describe("getObservationUsageByTypeByTime", () => {
const mockFilter = [
{
diff --git a/web/src/features/dashboard/components/ModelUsageChart.tsx b/web/src/features/dashboard/components/ModelUsageChart.tsx
index 130a5ede5c..d2be542325 100644
--- a/web/src/features/dashboard/components/ModelUsageChart.tsx
+++ b/web/src/features/dashboard/components/ModelUsageChart.tsx
@@ -22,13 +22,6 @@ import {
useModelSelection,
} from "@/src/features/dashboard/components/ModelSelector";
-type ModelUsageReturnType = {
- startTime: string;
- units: Record<string, number>;
- cost: Record<string, number>;
- model: string;
-};
-
export const ModelUsageChart = ({
className,
projectId,
@@ -189,18 +182,6 @@ export const ModelUsageChart = ({
},
},
);
- const typedData = (queryResult.data as ModelUsageReturnType[]) ?? [];
-
- const usageTypeMap = prepareUsageDataForTimeseriesChart(
- selectedModels,
- typedData,
- );
-
- const usageData = Array.from(usageTypeMap.values()).flat();
-
- const currentModels = [
- ...new Set(usageData.map((row) => row.model).filter(Boolean)),
- ];
const costByType =
queryCostByType.data && allModels.length > 0
@@ -229,46 +210,40 @@ export const ModelUsageChart = ({
: [];
const unitsByModel =
- usageData && allModels.length > 0
+ queryResult.data && allModels.length > 0
? fillMissingValuesAndTransform(
- extractTimeSeriesData(usageData, "startTime", [
+ extractTimeSeriesData(queryResult.data, "startTime", [
{
uniqueIdentifierColumns: [{ accessor: "model" }],
valueColumn: "units",
},
]),
- currentModels,
+ selectedModels,
)
: [];
const costByModel =
- usageData && allModels.length > 0
+ queryResult.data && allModels.length > 0
? fillMissingValuesAndTransform(
- extractTimeSeriesData(usageData, "startTime", [
+ extractTimeSeriesData(queryResult.data, "startTime", [
{
uniqueIdentifierColumns: [{ accessor: "model" }],
valueColumn: "cost",
},
]),
- currentModels,
+ selectedModels,
)
: [];
- const totalCost = usageData?.reduce(
+ const totalCost = queryResult.data?.reduce(
(acc, curr) =>
- acc +
- (curr.usageType === "total" && !isNaN(curr.cost as number)
- ? (curr.cost as number)
- : 0),
+ acc + (!isNaN(curr.cost as number) ? (curr.cost as number) : 0),
0,
);
- const totalTokens = usageData?.reduce(
+ const totalTokens = queryResult.data?.reduce(
(acc, curr) =>
- acc +
- (curr.usageType === "total" && !isNaN(curr.units as number)
- ? (curr.units as number)
- : 0),
+ acc + (!isNaN(curr.units as number) ? (curr.units as number) : 0),
0,
);
@@ -365,63 +340,3 @@ export const ModelUsageChart = ({
</DashboardCard>
);
};
-
-export function prepareUsageDataForTimeseriesChart(
- selectedModels: string[],
- typedData: ModelUsageReturnType[],
-) {
- const usageTypeMap = new Map<
- string,
- {
- startTime: string;
- units: number;
- cost: number;
- usageType: string;
- model: string;
- }[]
- >();
-
- const allUsageUnits = [
- ...new Set(typedData.flatMap((r) => Object.keys(r.units))),
- ];
-
- const uniqueDates = [
- ...new Set(typedData.flatMap((r) => new Date(r.startTime).getTime())),
- ];
-
- const uniqueModels = [...new Set(selectedModels)];
-
- allUsageUnits.forEach((uu) => {
- const unitEntries: {
- startTime: string;
- units: number;
- cost: number;
- usageType: string;
- model: string;
- }[] = [];
-
- uniqueDates.forEach((d) => {
- uniqueModels.forEach((m) => {
- const existingEntry = typedData.find(
- (td) =>
- new Date(td.startTime).getTime() === new Date(d).getTime() &&
- td.model === m,
- );
-
- const entry = {
- startTime: new Date(d).toISOString(),
- model: m,
- units: existingEntry ? existingEntry.units[uu] || 0 : 0,
- cost: existingEntry ? existingEntry.cost[uu] || 0 : 0,
- usageType: uu,
- };
-
- unitEntries.push(entry);
- });
- });
-
- usageTypeMap.set(uu, unitEntries);
- });
-
- return usageTypeMap;
-}