feat: aissurance plugin initial commit
This commit is contained in:
parent
d163fea154
commit
392034f839
22 changed files with 1645 additions and 2 deletions
28
db.py
28
db.py
|
|
@ -255,6 +255,34 @@ class TokenDatabase:
|
|||
}
|
||||
return None
|
||||
|
||||
async def get_last_used_map(self) -> dict:
|
||||
"""Return {(endpoint, model): latest_unix_timestamp} from the time series.
|
||||
|
||||
Read-only aggregate used by the compliance collector to derive each
|
||||
model's ``last_used``. Computed in SQL in a single pass.
|
||||
"""
|
||||
db = await self._get_connection()
|
||||
async with self._operation_lock:
|
||||
async with db.execute('''
|
||||
SELECT endpoint, model, MAX(timestamp)
|
||||
FROM token_time_series
|
||||
GROUP BY endpoint, model
|
||||
''') as cursor:
|
||||
rows = await cursor.fetchall()
|
||||
return {(row[0], row[1]): row[2] for row in rows}
|
||||
|
||||
async def get_token_totals_since(self, cutoff_ts: int) -> tuple[int, int]:
|
||||
"""Return (input_tokens, output_tokens) summed over entries at/after cutoff_ts."""
|
||||
db = await self._get_connection()
|
||||
async with self._operation_lock:
|
||||
async with db.execute('''
|
||||
SELECT COALESCE(SUM(input_tokens), 0), COALESCE(SUM(output_tokens), 0)
|
||||
FROM token_time_series
|
||||
WHERE timestamp >= ?
|
||||
''', (cutoff_ts,)) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return (int(row[0]), int(row[1])) if row else (0, 0)
|
||||
|
||||
async def aggregate_time_series_older_than(self, days: int, trim_old: bool = False) -> int:
|
||||
"""
|
||||
Aggregate time_series entries older than 'days' days into daily aggregates by
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue