(
supabase: AsyncSupabaseClient,
session_id,
cost: Decimal | None,
events,
prompt_tokens,
completion_tokens,
errors,
)
| 60 | |
| 61 | |
| 62 | async def update_stats( |
| 63 | supabase: AsyncSupabaseClient, |
| 64 | session_id, |
| 65 | cost: Decimal | None, |
| 66 | events, |
| 67 | prompt_tokens, |
| 68 | completion_tokens, |
| 69 | errors, |
| 70 | ): |
| 71 | _current_stats = ( |
| 72 | await supabase.table("stats").select("*").eq("session_id", session_id).limit(1).single().execute() |
| 73 | ) |
| 74 | if _current_stats.data: |
| 75 | current_stats = _current_stats.data |
| 76 | else: |
| 77 | logger.error(f"Could not find stats for session {session_id}") |
| 78 | return |
| 79 | |
| 80 | current_cost = ( |
| 81 | Decimal(str(current_stats["cost"])) if current_stats and current_stats["cost"] else Decimal(0) |
| 82 | ) |
| 83 | updated_cost = current_cost + (cost if cost else Decimal(0)) |
| 84 | updated_cost = str(updated_cost) if updated_cost != Decimal(0) else None |
| 85 | |
| 86 | stats = { |
| 87 | "session_id": session_id, |
| 88 | "cost": updated_cost, |
| 89 | "events": events + (current_stats["events"] if current_stats else 0), |
| 90 | "prompt_tokens": prompt_tokens + (current_stats["prompt_tokens"] if current_stats else 0), |
| 91 | "completion_tokens": completion_tokens + (current_stats["completion_tokens"] if current_stats else 0), |
| 92 | "errors": errors + (current_stats["errors"] if current_stats else 0), |
| 93 | } |
| 94 | |
| 95 | await supabase.table("stats").upsert(stats, on_conflict="session_id").execute() |
| 96 | |
| 97 | |
| 98 | def calculate_costs(model, prompt, completion): |
no test coverage detected
searching dependent graphs…