@@ -9,7 +9,7 @@ export async function GET() {
99 try {
1010 const oneWeekAgo = new Date ( Date . now ( ) - 7 * 24 * 60 * 60 * 1000 )
1111
12- // Get all published agents with their publisher info and real usage metrics
12+ // Get all published agents with their publisher info
1313 const agents = await db
1414 . select ( {
1515 id : schema . agentConfig . id ,
@@ -29,13 +29,11 @@ export async function GET() {
2929 )
3030 . orderBy ( sql `${ schema . agentConfig . created_at } DESC` )
3131
32- // Get all-time usage metrics for all agents
32+ // Get all-time usage metrics for published agents only (those with publisher_id and agent_name)
3333 const usageMetrics = await db
3434 . select ( {
35- agent_id : schema . agentRun . agent_id ,
3635 publisher_id : schema . agentRun . publisher_id ,
3736 agent_name : schema . agentRun . agent_name ,
38- agent_version : schema . agentRun . agent_version ,
3937 total_invocations : sql < number > `COUNT(*)` ,
4038 total_dollars : sql < number > `COALESCE(SUM(${ schema . agentRun . total_credits } ) / 100.0, 0)` ,
4139 avg_cost_per_run : sql < number > `COALESCE(AVG(${ schema . agentRun . total_credits } ) / 100.0, 0)` ,
@@ -46,20 +44,19 @@ export async function GET() {
4644 . where (
4745 and (
4846 eq ( schema . agentRun . status , 'completed' ) ,
49- sql `${ schema . agentRun . agent_id } != 'test-agent'`
47+ sql `${ schema . agentRun . agent_id } != 'test-agent'` ,
48+ sql `${ schema . agentRun . publisher_id } IS NOT NULL` ,
49+ sql `${ schema . agentRun . agent_name } IS NOT NULL`
5050 )
5151 )
5252 . groupBy (
53- schema . agentRun . agent_id ,
5453 schema . agentRun . publisher_id ,
55- schema . agentRun . agent_name ,
56- schema . agentRun . agent_version
54+ schema . agentRun . agent_name
5755 )
5856
59- // Get weekly usage metrics separately
57+ // Get weekly usage metrics for published agents only
6058 const weeklyMetrics = await db
6159 . select ( {
62- agent_id : schema . agentRun . agent_id ,
6360 publisher_id : schema . agentRun . publisher_id ,
6461 agent_name : schema . agentRun . agent_name ,
6562 weekly_dollars : sql < number > `COALESCE(SUM(${ schema . agentRun . total_credits } ) / 100.0, 0)` ,
@@ -69,54 +66,39 @@ export async function GET() {
6966 and (
7067 eq ( schema . agentRun . status , 'completed' ) ,
7168 gte ( schema . agentRun . created_at , oneWeekAgo ) ,
72- sql `${ schema . agentRun . agent_id } != 'test-agent'`
69+ sql `${ schema . agentRun . agent_id } != 'test-agent'` ,
70+ sql `${ schema . agentRun . publisher_id } IS NOT NULL` ,
71+ sql `${ schema . agentRun . agent_name } IS NOT NULL`
7372 )
7473 )
7574 . groupBy (
76- schema . agentRun . agent_id ,
7775 schema . agentRun . publisher_id ,
7876 schema . agentRun . agent_name
7977 )
8078
81- // Create weekly metrics map
79+ // Create weekly metrics map by publisher/agent_name
8280 const weeklyMap = new Map ( )
8381 weeklyMetrics . forEach ( ( metric ) => {
84- const keys = [
85- metric . agent_id ,
86- metric . publisher_id && metric . agent_name
87- ? `${ metric . publisher_id } /${ metric . agent_name } `
88- : null ,
89- ] . filter ( Boolean )
90-
91- keys . forEach ( ( key ) => {
82+ if ( metric . publisher_id && metric . agent_name ) {
83+ const key = `${ metric . publisher_id } /${ metric . agent_name } `
9284 weeklyMap . set ( key , Number ( metric . weekly_dollars ) )
93- } )
85+ }
9486 } )
9587
96- // Create a map of usage metrics by agent identifier
88+ // Create a map of usage metrics by publisher/agent_name
9789 const metricsMap = new Map ( )
9890 usageMetrics . forEach ( ( metric ) => {
99- // Try to match by full agent_id first, then by publisher/name combination
100- const keys = [
101- metric . agent_id ,
102- metric . publisher_id && metric . agent_name
103- ? `${ metric . publisher_id } /${ metric . agent_name } `
104- : null ,
105- ] . filter ( Boolean )
106-
107- keys . forEach ( ( key ) => {
108- const existingMetric = metricsMap . get ( key )
109- if ( ! existingMetric || existingMetric . last_used < metric . last_used ) {
110- metricsMap . set ( key , {
111- weekly_dollars : weeklyMap . get ( key ) || 0 ,
112- total_dollars : Number ( metric . total_dollars ) ,
113- total_invocations : Number ( metric . total_invocations ) ,
114- avg_cost_per_run : Number ( metric . avg_cost_per_run ) ,
115- unique_users : Number ( metric . unique_users ) ,
116- last_used : metric . last_used ,
117- } )
118- }
119- } )
91+ if ( metric . publisher_id && metric . agent_name ) {
92+ const key = `${ metric . publisher_id } /${ metric . agent_name } `
93+ metricsMap . set ( key , {
94+ weekly_dollars : weeklyMap . get ( key ) || 0 ,
95+ total_dollars : Number ( metric . total_dollars ) ,
96+ total_invocations : Number ( metric . total_invocations ) ,
97+ avg_cost_per_run : Number ( metric . avg_cost_per_run ) ,
98+ unique_users : Number ( metric . unique_users ) ,
99+ last_used : metric . last_used ,
100+ } )
101+ }
120102 } )
121103
122104 // Transform the data to include parsed agent data and real usage metrics
@@ -126,22 +108,20 @@ export async function GET() {
126108 const agentName = agentData . name || agent . id
127109
128110 const agentKey = `${ agent . publisher . id } /${ agentName } `
129- const metrics = metricsMap . get ( agentKey ) ||
130- metricsMap . get ( agent . id ) || {
131- weekly_dollars : 0 ,
132- total_dollars : 0 ,
133- total_invocations : 0 ,
134- avg_cost_per_run : 0 ,
135- unique_users : 0 ,
136- last_used : null ,
137- }
111+ const metrics = metricsMap . get ( agentKey ) || {
112+ weekly_dollars : 0 ,
113+ total_dollars : 0 ,
114+ total_invocations : 0 ,
115+ avg_cost_per_run : 0 ,
116+ unique_users : 0 ,
117+ last_used : null ,
118+ }
138119
139120 return {
140121 id : agent . id ,
141122 name : agentName ,
142123 description : agentData . description ,
143124 publisher : agent . publisher ,
144- version : agent . version ,
145125 created_at : agent . created_at ,
146126 usage_count : metrics . total_invocations ,
147127 weekly_spent : metrics . weekly_dollars ,
@@ -153,7 +133,7 @@ export async function GET() {
153133 }
154134 } )
155135
156- // Group by agent name and keep only the latest version of each
136+ // Group by agent name and keep only the latest version of each (without version in output)
157137 const latestAgents = new Map ( )
158138 transformedAgents . forEach ( ( agent ) => {
159139 const key = `${ agent . publisher . id } /${ agent . name } `
0 commit comments