Skip to content

Commit 92a2301

Browse files
committed
Possible fix for incorrect agent metrics
1 parent 498abd8 commit 92a2301

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

web/src/app/api/agents/route.ts

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -95,49 +95,53 @@ export async function GET() {
9595
}
9696
})
9797

98-
// Transform the data to include parsed agent data and real usage metrics
99-
const transformedAgents = agents.map((agent) => {
98+
// First, group agents by publisher/name to get the latest version of each
99+
const latestAgents = new Map()
100+
agents.forEach((agent) => {
100101
const agentData =
101102
typeof agent.data === 'string' ? JSON.parse(agent.data) : agent.data
102103
const agentName = agentData.name || agent.id
104+
const key = `${agent.publisher.id}/${agentName}`
103105

104-
const agentKey = `${agent.publisher.id}/${agentName}`
105-
const metrics = metricsMap.get(agentKey) || {
106-
weekly_dollars: 0,
107-
total_dollars: 0,
108-
total_invocations: 0,
109-
avg_cost_per_run: 0,
110-
unique_users: 0,
111-
last_used: null,
112-
}
113-
114-
return {
115-
id: agent.id,
116-
name: agentName,
117-
description: agentData.description,
118-
publisher: agent.publisher,
119-
version: agent.version,
120-
created_at: agent.created_at,
121-
usage_count: metrics.total_invocations,
122-
weekly_spent: metrics.weekly_dollars,
123-
total_spent: metrics.total_dollars,
124-
avg_cost_per_invocation: metrics.avg_cost_per_run,
125-
unique_users: metrics.unique_users,
126-
last_used: metrics.last_used,
127-
tags: agentData.tags || [],
128-
}
129-
})
130-
131-
// Group by agent name and keep only the latest version of each (without version in output)
132-
const latestAgents = new Map()
133-
transformedAgents.forEach((agent) => {
134-
const key = `${agent.publisher.id}/${agent.name}`
135106
if (!latestAgents.has(key)) {
136-
latestAgents.set(key, agent)
107+
latestAgents.set(key, {
108+
agent,
109+
agentData,
110+
agentName,
111+
})
137112
}
138113
})
139114

140-
const result = Array.from(latestAgents.values())
115+
// Transform the latest agents with their aggregated metrics
116+
const result = Array.from(latestAgents.values()).map(
117+
({ agent, agentData, agentName }) => {
118+
const agentKey = `${agent.publisher.id}/${agentName}`
119+
const metrics = metricsMap.get(agentKey) || {
120+
weekly_dollars: 0,
121+
total_dollars: 0,
122+
total_invocations: 0,
123+
avg_cost_per_run: 0,
124+
unique_users: 0,
125+
last_used: null,
126+
}
127+
128+
return {
129+
id: agent.id,
130+
name: agentName,
131+
description: agentData.description,
132+
publisher: agent.publisher,
133+
version: agent.version,
134+
created_at: agent.created_at,
135+
usage_count: metrics.total_invocations,
136+
weekly_spent: metrics.weekly_dollars,
137+
total_spent: metrics.total_dollars,
138+
avg_cost_per_invocation: metrics.avg_cost_per_run,
139+
unique_users: metrics.unique_users,
140+
last_used: metrics.last_used,
141+
tags: agentData.tags || [],
142+
}
143+
}
144+
)
141145

142146
// Sort by weekly usage (most prominent metric)
143147
result.sort((a, b) => (b.weekly_spent || 0) - (a.weekly_spent || 0))

0 commit comments

Comments
 (0)