Refactor: Use total_msats for revenue calculation

Co-authored-by: db2002dominic <db2002dominic@gmail.com>
This commit is contained in:
Cursor Agent
2025-11-16 20:12:34 +00:00
co-authored by db2002dominic
parent fbeb10ee49
commit b25abbfe9a
3 changed files with 7 additions and 7 deletions
+4 -4
View File
@@ -2893,7 +2893,7 @@ def _aggregate_metrics_by_time(
CRITICAL: This function parses specific log messages for usage tracking.
The following log messages must not be modified without updating this function:
- "received proxy request" -> counts total_requests
- "token adjustment completed" -> counts successful_chat_completions, extracts revenue_msats from cost_data.actual_cost
- "token adjustment completed" -> counts successful_chat_completions, extracts revenue_msats from cost_data.total_msats
- "upstream request failed" OR "revert payment" -> counts failed_requests
- "payment processed successfully" -> counts payment_processed
- "revert payment" -> extracts refunds_msats from max_cost_for_model
@@ -2965,7 +2965,7 @@ def _aggregate_metrics_by_time(
if "token adjustment completed" in message:
cost_data = entry.get("cost_data")
if isinstance(cost_data, dict):
actual_cost = cost_data.get("actual_cost", 0)
actual_cost = cost_data.get("total_msats", 0)
if isinstance(actual_cost, (int, float)) and actual_cost > 0:
time_buckets[bucket_key]["revenue_msats"] += actual_cost
@@ -3080,7 +3080,7 @@ def _get_summary_stats(entries: list[dict], hours_back: int = 24) -> dict:
if "token adjustment completed" in message:
cost_data = entry.get("cost_data")
if isinstance(cost_data, dict):
actual_cost = cost_data.get("actual_cost", 0)
actual_cost = cost_data.get("total_msats", 0)
if isinstance(actual_cost, (int, float)) and actual_cost > 0:
assert isinstance(stats["revenue_msats"], (int, float))
stats["revenue_msats"] = float(stats["revenue_msats"]) + float(actual_cost)
@@ -3407,7 +3407,7 @@ async def get_revenue_by_model(
model_stats[model]["successful"] += 1
cost_data = entry.get("cost_data")
if isinstance(cost_data, dict):
actual_cost = cost_data.get("actual_cost", 0)
actual_cost = cost_data.get("total_msats", 0)
if isinstance(actual_cost, (int, float)) and actual_cost > 0:
model_stats[model]["revenue_msats"] += actual_cost
+1 -1
View File
@@ -13,7 +13,7 @@ DO NOT modify or remove these messages without updating the usage tracking logic
2. "Token adjustment completed for streaming" (INFO) - routstr/upstream/base.py
"Token adjustment completed for non-streaming" (INFO) - routstr/upstream/base.py
- Used to track successful completions and revenue
- The 'cost_data.actual_cost' field is extracted for revenue calculation
- The 'cost_data.total_msats' field is extracted for revenue calculation
- Must include 'cost_data' in extra dict
3. "Payment processed successfully" (INFO) - routstr/auth.py
+2 -2
View File
@@ -454,7 +454,7 @@ class BaseUpstreamProvider:
usage_finalized = True
# IMPORTANT: Do not modify this log message - used for usage statistics tracking
# This log is parsed to track revenue from successful requests
# The 'cost_data.actual_cost' field is used to calculate total revenue
# The 'cost_data.total_msats' field is used to calculate total revenue
logger.info(
"Token adjustment completed for streaming",
extra={
@@ -560,7 +560,7 @@ class BaseUpstreamProvider:
# IMPORTANT: Do not modify this log message - used for usage statistics tracking
# This log is parsed to track revenue from successful requests
# The 'cost_data.actual_cost' field is used to calculate total revenue
# The 'cost_data.total_msats' field is used to calculate total revenue
logger.info(
"Token adjustment completed for non-streaming",
extra={