fix(db.py): remove full table scans with proper where clauses for dashboard statistics and calc in db rather than python
This commit is contained in:
parent
45315790d1
commit
8037706f0b
2 changed files with 46 additions and 17 deletions
22
router.py
22
router.py
|
|
@ -2086,22 +2086,10 @@ async def stats_proxy(request: Request, model: Optional[str] = None):
|
|||
status_code=404, detail="No token data found for this model"
|
||||
)
|
||||
|
||||
# Get time series data for the last 30 days (43200 minutes = 30 days)
|
||||
# Assuming entries are grouped by minute, 30 days = 43200 entries max
|
||||
time_series = []
|
||||
endpoint_totals = defaultdict(int) # Track tokens per endpoint
|
||||
|
||||
async for entry in db.get_latest_time_series(limit=50000):
|
||||
if entry['model'] == model:
|
||||
time_series.append({
|
||||
'endpoint': entry['endpoint'],
|
||||
'timestamp': entry['timestamp'],
|
||||
'input_tokens': entry['input_tokens'],
|
||||
'output_tokens': entry['output_tokens'],
|
||||
'total_tokens': entry['total_tokens']
|
||||
})
|
||||
# Accumulate total tokens per endpoint
|
||||
endpoint_totals[entry['endpoint']] += entry['total_tokens']
|
||||
time_series = [
|
||||
entry async for entry in db.get_time_series_for_model(model)
|
||||
]
|
||||
endpoint_distribution = await db.get_endpoint_distribution_for_model(model)
|
||||
|
||||
return {
|
||||
'model': model,
|
||||
|
|
@ -2109,7 +2097,7 @@ async def stats_proxy(request: Request, model: Optional[str] = None):
|
|||
'output_tokens': token_data['output_tokens'],
|
||||
'total_tokens': token_data['total_tokens'],
|
||||
'time_series': time_series,
|
||||
'endpoint_distribution': dict(endpoint_totals)
|
||||
'endpoint_distribution': endpoint_distribution,
|
||||
}
|
||||
|
||||
# -------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue