@@ -3,19 +3,16 @@ import type { DateRangePickerValue } from "@tremor/react";
33import Papa from "papaparse" ;
44import type { EntityBreakdown , EntitySpendData , EntityType , ExportMetadata , ExportScope } from "./types" ;
55
6- // Helper function to extract team_id from api_key_breakdown
7- const extractTeamIdFromApiKeyBreakdown = ( apiKeyBreakdown : Record < string , any > | undefined ) : string | null => {
8- if ( ! apiKeyBreakdown ) return null ;
9-
10- // Look through all API keys to find the first non-null team_id
11- for ( const apiKeyData of Object . values ( apiKeyBreakdown ) ) {
12- const teamId = ( apiKeyData as any ) ?. metadata ?. team_id ;
13- if ( teamId ) {
14- return teamId ;
15- }
16- }
17- return null ;
18- } ;
6+ // Resolve display name for an entity. For teams the teamAliasMap provides
7+ // a human-readable alias; for every other entity type the entity key itself
8+ // (tag name, org id, customer id, …) is already the correct label.
9+ const resolveEntityDisplay = (
10+ entity : string ,
11+ teamAliasMap : Record < string , string > ,
12+ ) : { id : string ; alias : string } => ( {
13+ id : entity ,
14+ alias : teamAliasMap [ entity ] || entity ,
15+ } ) ;
1916
2017// Mirrors backend SpendMetrics fields (litellm/types/activity_tracking.py).
2118// If the backend adds a field, add it here too.
@@ -68,18 +65,7 @@ export const getEntityBreakdown = (
6865
6966 spendData . results . forEach ( ( day ) => {
7067 Object . entries ( resolveEntities ( day . breakdown ) ) . forEach ( ( [ entity , data ] : [ string , any ] ) => {
71- // Extract team_id from api_key_breakdown metadata (not data.metadata which is empty)
72- const teamId = extractTeamIdFromApiKeyBreakdown ( data . api_key_breakdown ) || entity ;
73- // Extract key_alias from the first API key that has one
74- const apiKeyBreakdown = data . api_key_breakdown || { } ;
75- let keyAlias : string | null = null ;
76- for ( const apiKeyData of Object . values ( apiKeyBreakdown ) ) {
77- const alias = ( apiKeyData as any ) ?. metadata ?. key_alias ;
78- if ( alias ) {
79- keyAlias = alias ;
80- break ;
81- }
82- }
68+ const { id, alias } = resolveEntityDisplay ( entity , teamAliasMap ) ;
8369
8470 if ( ! entitySpend [ entity ] ) {
8571 entitySpend [ entity ] = {
@@ -95,8 +81,8 @@ export const getEntityBreakdown = (
9581 cache_creation_input_tokens : 0 ,
9682 } ,
9783 metadata : {
98- alias : keyAlias || teamAliasMap [ teamId ] || entity ,
99- id : teamId ,
84+ alias,
85+ id,
10086 } ,
10187 } ;
10288 }
@@ -124,14 +110,12 @@ export const generateDailyData = (
124110
125111 spendData . results . forEach ( ( day ) => {
126112 Object . entries ( resolveEntities ( day . breakdown ) ) . forEach ( ( [ entity , data ] : [ string , any ] ) => {
127- // Extract team_id from api_key_breakdown metadata (not data.metadata which is empty)
128- const teamId = extractTeamIdFromApiKeyBreakdown ( data . api_key_breakdown ) ;
129- const teamAlias = teamId ? teamAliasMap [ teamId ] || null : null ;
113+ const { id, alias } = resolveEntityDisplay ( entity , teamAliasMap ) ;
130114
131115 dailyBreakdown . push ( {
132116 Date : day . date ,
133- [ entityLabel ] : teamAlias || "-" ,
134- [ `${ entityLabel } ID` ] : teamId || "-" ,
117+ [ entityLabel ] : alias ,
118+ [ `${ entityLabel } ID` ] : id ,
135119 "Spend ($)" : formatNumberWithCommas ( data . metrics . spend , 4 ) ,
136120 Requests : data . metrics . api_requests ,
137121 "Successful Requests" : data . metrics . successful_requests ,
@@ -151,12 +135,12 @@ export const generateDailyWithKeysData = (
151135 entityLabel : string ,
152136 teamAliasMap : Record < string , string > = { } ,
153137) : any [ ] => {
154- // Aggregate by unique (Date, Team ID, Key ID) combination to prevent duplicates
138+ // Aggregate by unique (Date, Entity ID, Key ID) combination to prevent duplicates
155139 const aggregatedData : {
156140 [ key : string ] : {
157141 Date : string ;
158- teamId : string ;
159- teamAlias : string | null ;
142+ entityId : string ;
143+ entityAlias : string ;
160144 keyId : string ;
161145 keyAlias : string | null ;
162146 metrics : {
@@ -173,23 +157,22 @@ export const generateDailyWithKeysData = (
173157
174158 spendData . results . forEach ( ( day ) => {
175159 Object . entries ( resolveEntities ( day . breakdown ) ) . forEach ( ( [ entity , data ] : [ string , any ] ) => {
160+ const { id : entityId , alias : entityAlias } = resolveEntityDisplay ( entity , teamAliasMap ) ;
176161 const apiKeyBreakdown = data . api_key_breakdown || { } ;
177162
178163 // Iterate through each API key in the breakdown
179164 Object . entries ( apiKeyBreakdown ) . forEach ( ( [ keyId , keyData ] : [ string , any ] ) => {
180165 const keyAlias = keyData ?. metadata ?. key_alias || null ;
181- const teamId = keyData ?. metadata ?. team_id || entity ;
182- const teamAlias = teamId ? teamAliasMap [ teamId ] || null : null ;
183166
184- // Create unique key for aggregation: Date_TeamID_KeyID
185- const uniqueKey = `${ day . date } _${ teamId } _${ keyId } ` ;
167+ // Create unique key for aggregation: Date_EntityID_KeyID
168+ const uniqueKey = `${ day . date } _${ entityId } _${ keyId } ` ;
186169
187170 if ( ! aggregatedData [ uniqueKey ] ) {
188- // First time seeing this (Date, Team ID, Key ID) combination
171+ // First time seeing this (Date, Entity ID, Key ID) combination
189172 aggregatedData [ uniqueKey ] = {
190173 Date : day . date ,
191- teamId ,
192- teamAlias ,
174+ entityId ,
175+ entityAlias ,
193176 keyId,
194177 keyAlias,
195178 metrics : {
@@ -219,8 +202,8 @@ export const generateDailyWithKeysData = (
219202 // Convert aggregated data to array format
220203 const dailyKeyBreakdown = Object . values ( aggregatedData ) . map ( ( item ) => ( {
221204 Date : item . Date ,
222- [ entityLabel ] : item . teamAlias || "-" ,
223- [ `${ entityLabel } ID` ] : item . teamId || "-" ,
205+ [ entityLabel ] : item . entityAlias ,
206+ [ `${ entityLabel } ID` ] : item . entityId ,
224207 "Key Alias" : item . keyAlias || "-" ,
225208 "Key ID" : item . keyId ,
226209 "Spend ($)" : formatNumberWithCommas ( item . metrics . spend , 4 ) ,
@@ -273,16 +256,13 @@ export const generateDailyWithModelsData = (
273256 } ) ;
274257
275258 Object . entries ( dailyEntityModels ) . forEach ( ( [ entity , models ] ) => {
276- const entityData = resolveEntities ( day . breakdown ) [ entity ] ;
277- // Extract team_id from api_key_breakdown metadata (not entityData.metadata which is empty)
278- const teamId = extractTeamIdFromApiKeyBreakdown ( entityData ?. api_key_breakdown ) ;
279- const teamAlias = teamId ? teamAliasMap [ teamId ] || null : null ;
259+ const { id, alias } = resolveEntityDisplay ( entity , teamAliasMap ) ;
280260
281261 Object . entries ( models ) . forEach ( ( [ model , metrics ] : [ string , any ] ) => {
282262 dailyModelBreakdown . push ( {
283263 Date : day . date ,
284- [ entityLabel ] : teamAlias || "-" ,
285- [ `${ entityLabel } ID` ] : teamId || "-" ,
264+ [ entityLabel ] : alias ,
265+ [ `${ entityLabel } ID` ] : id ,
286266 Model : model ,
287267 "Spend ($)" : formatNumberWithCommas ( metrics . spend , 4 ) ,
288268 Requests : metrics . requests ,
0 commit comments