forked from christophschuhmann/school-bud-e-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
1582 lines (1399 loc) · 59.4 KB
/
admin.py
File metadata and controls
1582 lines (1399 loc) · 59.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# admin.py
"""
Buddy Admin API (FastAPI)
This module exposes administrative endpoints for:
- Users: CRUD, credits, API keys, assignment to projects
- Projects: CRUD, credits pools, allowance settlement, bootstrap helpers
- Pricing/Providers/Routes: CRUD & bootstrap
- Usage & Ledger: raw logs, aggregated usage overviews, PDF/ZIP export
- Backup/Restore (SQLite only)
New in this version:
- Usage overview PDF rendered in A4 **landscape**.
- If a **specific user** is selected but has **no usage**, an all-zero row is shown.
- Support for **Unassigned Users**:
* `project_id = -1` means "users with no project".
* `/admin/users?unassigned=true` lists users without a project.
* When `user_id` is provided to usage endpoints, project filtering is ignored.
"""
# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
import os
import io
import csv
import json
import zipfile
import sqlite3
import tempfile
import random
import string
from urllib.parse import urlsplit # <-- NEW: for public base URL normalization
from datetime import datetime, timezone, timedelta, date
from decimal import Decimal, ROUND_DOWN
from typing import Any, Dict, List, Tuple, Optional
from fastapi import APIRouter, Depends, HTTPException, Form, UploadFile, File, Query
from fastapi.responses import (
StreamingResponse,
FileResponse,
JSONResponse,
Response, # used for PDF/ZIP/HTML payloads
)
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, delete, text
from sqlalchemy.orm import selectinload
from db import get_session, IS_SQLITE, DATA_DIR
from models import (
User,
ApiKey,
ModelPricing,
ModelType,
ProviderEndpoint,
Project,
ProjectShare,
SplitStrategy,
UsageLog,
CreditLedger,
RoutePref,
RouteKind,
ProjectAllowance, # legacy row store of remaining allowance
AllowanceInterval,
generate_api_key,
)
router = APIRouter(prefix="/admin", tags=["admin"])
# -----------------------------------------------------------------------------
# Helpers
# -----------------------------------------------------------------------------
def _dec(x) -> str:
"""Decimal/None -> str without crashing."""
return str(x if x is not None else 0)
def _parse_ids(csv_ids: str) -> List[int]:
"""Parse comma-separated list of integral IDs."""
return [int(s) for s in (csv_ids or "").split(",") if s.strip().isdigit()]
def _now() -> datetime:
"""UTC 'now' with tzinfo."""
return datetime.now(timezone.utc)
def _compute_next(now: datetime, interval: Optional[AllowanceInterval]) -> Optional[datetime]:
"""Compute next settlement time from an interval."""
if not interval:
return None
if interval == AllowanceInterval.DAILY:
return now + timedelta(days=1)
if interval == AllowanceInterval.WEEKLY:
return now + timedelta(weeks=1)
if interval == AllowanceInterval.MONTHLY:
return now + timedelta(days=30)
return None
def _next_from_interval(now: datetime, interval: Optional[AllowanceInterval]) -> Optional[datetime]:
"""Back-compat alias for computing next settlement."""
return _compute_next(now, interval)
# Small username helpers used by /projects/init
_ADJ = ["bright","quick","calm","brave","clever","fresh","merry","pure","swift","kind",
"bold","sharp","chill","eager","happy","jolly","neat","proud","slick","solid"]
_ANM = ["otter","lynx","panda","eagle","tiger","wolf","swan","koala","hare","orca",
"fox","seal","owl","yak","mole","crab","ibis","kiwi","boar","yak"]
def _username_for(i: int, pid: int) -> str:
idx = (i * 37 + pid * 101) % (len(_ADJ) * len(_ANM))
a = _ADJ[idx % len(_ADJ)]
b = _ANM[(idx // len(_ADJ)) % len(_ANM)]
return f"{a}-{b}-{i:02d}"
def _rand_name(n: int = 6) -> str:
"""Random alnum suffix for synthetic emails."""
alphabet = string.ascii_lowercase + string.digits
return "".join(random.choices(alphabet, k=max(3, n)))
# ---- NEW: public base URL for composite keys --------------------------------
# Try to import HOST/PORT from your main entrypoint, else fall back to env/defaults.
try:
from main import HOST, PORT # type: ignore
except Exception:
HOST = os.getenv("BUDDY_HOST", "0.0.0.0")
try:
PORT = int(os.getenv("BUDDY_PORT", "8787"))
except Exception:
PORT = 8787 # safe fallback
def _public_base_url() -> str:
"""
Returns the base URL appended after '#' in composite keys.
Priority:
1) BUDDY_PUBLIC_BASE (recommended) Â e.g. 'http://65.109.157.234:8787'
2) Auto-detect **IPv4** public IP and build '<scheme>://<ipv4>:<port>'.
Optional overrides:
- BUDDY_PUBLIC_SCHEME (default: 'http')
- BUDDY_PUBLIC_PORT (default: PORT)
3) Fallback to 'http://<HOST>:<PORT>'.
Note: We intentionally prefer IPv4 so the suffix looks like 'http://65.109.157.234:8787'.
"""
# simple module-level cache
global _PUBLIC_BASE_CACHE # type: ignore
try:
if _PUBLIC_BASE_CACHE:
return _PUBLIC_BASE_CACHE # type: ignore
except NameError:
_PUBLIC_BASE_CACHE = None # type: ignore
# 1) Explicit config wins
val = (os.getenv("BUDDY_PUBLIC_BASE") or "").strip().rstrip("/")
if val:
if "://" not in val:
val = f"http://{val}"
from urllib.parse import urlsplit
parts = urlsplit(val)
_PUBLIC_BASE_CACHE = f"{parts.scheme}://{parts.netloc}" # type: ignore
return _PUBLIC_BASE_CACHE # type: ignore
# 2) Auto-detect IPv4 public IP (short timeouts)
public_ip_v4 = None
endpoints_v4 = (
"https://ipv4.icanhazip.com",
"https://api.ipify.org", # returns IPv4 if stack prefers v4
"https://checkip.amazonaws.com", # often IPv4
)
for ep in endpoints_v4:
try:
import urllib.request
with urllib.request.urlopen(ep, timeout=1.5) as resp:
text = (resp.read() or b"").decode("utf-8").strip()
import ipaddress
ip = ipaddress.ip_address(text)
if ip.version == 4:
public_ip_v4 = text
break
except Exception:
continue
if public_ip_v4:
scheme = (os.getenv("BUDDY_PUBLIC_SCHEME") or "http").strip() or "http"
port_env = (os.getenv("BUDDY_PUBLIC_PORT") or "").strip()
try:
port = int(port_env) if port_env else int(PORT) # PORT imported/defined earlier
except Exception:
port = 8787
_PUBLIC_BASE_CACHE = f"{scheme}://{public_ip_v4}:{port}" # type: ignore
return _PUBLIC_BASE_CACHE # type: ignore
# 3) Fallback (dev/local)
_PUBLIC_BASE_CACHE = f"http://{HOST}:{PORT}" # type: ignore
return _PUBLIC_BASE_CACHE # type: ignore
# ---- ENCODED address suffix (simple reversible scheme) -----------------------
def _encoded_addr_suffix() -> str:
"""
Returns an opaque, alphanumeric string that encodes '<host>:<port>'.
- Source host/port come from _public_base_url()
- Encoding: XOR each byte with 0x5A, then Base32 encode without '=' padding
- Prefix 'v1' allows future changes
- Contains only [A-Z2-7] (Base32 alphabet) prefixed by 'v1'
Example output: 'v1MFRGGZDFMZTWQ2LK'
"""
import base64
from urllib.parse import urlsplit
base = _public_base_url() # e.g., 'http://65.109.157.234:8787' or 'http://[2001:db8::1]:8787'
parts = urlsplit(base)
host = parts.hostname or "127.0.0.1"
port = parts.port or 80
# Build 'host:port' (IPv6 hostname is already without brackets from urlsplit)
plain = f"{host}:{port}".encode("utf-8")
# Super-simple reversible obfuscation (NOT cryptographic security)
xored = bytes(b ^ 0x5A for b in plain)
token = base64.b32encode(xored).decode("ascii").rstrip("=") # make it neat
return "v1" + token
# ---- Date helpers for usage filters -------------------------------------------------
def _parse_date(s: str | None) -> datetime | None:
"""Parse 'YYYY-MM-DD' or ISO datetime; returns naive -> will be made UTC later."""
if not s:
return None
try:
if len(s) == 10:
return datetime.fromisoformat(s) # naive midnight
return datetime.fromisoformat(s.replace("Z", ""))
except Exception:
return None
def _range_guard(start: datetime | None, end: datetime | None) -> tuple[datetime, datetime]:
"""Ensure [start, end) datetimes exist and are UTC-aware. Default: current month."""
now = _now()
if not start and not end:
start = datetime(now.year, now.month, 1, tzinfo=timezone.utc)
if now.month == 12:
end = datetime(now.year + 1, 1, 1, tzinfo=timezone.utc)
else:
end = datetime(now.year, now.month + 1, 1, tzinfo=timezone.utc)
if start and start.tzinfo is None:
start = start.replace(tzinfo=timezone.utc)
if end and end.tzinfo is None:
end = end.replace(tzinfo=timezone.utc)
return start, end
def _iso(dt: datetime | None) -> str | None:
"""Safe ISO formatting."""
return dt.isoformat() if dt else None
# -----------------------------------------------------------------------------
# USAGE: Connections (chronological), Overview (aggregated), PDF/ZIP export
# -----------------------------------------------------------------------------
@router.get("/usage/connections")
async def usage_connections(
project_id: int | None = Query(None, description="Project filter; use -1 for unassigned users"),
user_id: int | None = Query(None, description="If provided, project filter is ignored"),
start: str | None = Query(None, description="YYYY-MM-DD or ISO start (inclusive)"),
end: str | None = Query(None, description="YYYY-MM-DD or ISO end (exclusive)"),
limit: int | None = Query(None),
session: AsyncSession = Depends(get_session),
):
"""
Chronological list of usage rows.
- If `user_id` is provided: project filter is ignored.
- If `project_id == -1`: shows users without a project (unassigned).
"""
sdt = _parse_date(start)
edt = _parse_date(end)
sdt, edt = _range_guard(sdt, edt)
stmt = select(UsageLog, User).join(User, User.id == UsageLog.user_id)
if user_id:
stmt = stmt.where(User.id == user_id)
elif project_id is not None:
if project_id == -1:
stmt = stmt.where(User.project_id.is_(None))
else:
stmt = stmt.where(User.project_id == project_id)
if sdt:
stmt = stmt.where(UsageLog.created_at >= sdt)
if edt:
stmt = stmt.where(UsageLog.created_at < edt)
stmt = stmt.order_by(UsageLog.created_at.asc())
if limit:
stmt = stmt.limit(limit)
q = await session.execute(stmt)
rows = q.all()
out = []
for ul, u in rows:
out.append({
"id": ul.id,
"created_at": _iso(ul.created_at),
"user_id": u.id,
"email": u.email,
"provider": ul.provider,
"model": ul.model,
"model_type": ul.model_type.value if ul.model_type else None,
"input": ul.input_count,
"output": ul.output_count,
"billed": _dec(ul.billed_credits),
})
return {
"project_id": project_id,
"user_id": user_id,
"start": _iso(sdt),
"end": _iso(edt),
"items": out,
}
@router.get("/usage/overview")
async def usage_overview(
project_id: int | None = Query(None, description="Project filter; -1 = unassigned users"),
user_id: int | None = Query(None, description="If provided, project filter is ignored"),
start: str | None = Query(None),
end: str | None = Query(None),
session: AsyncSession = Depends(get_session),
):
"""
Aggregated usage per user and per model type.
Improvements:
- When a specific user is selected and they have no usage in the range, we still
return a single zero row for that user.
- When a project is selected and 'All users' is chosen, we include **all** users that
belong to the project (or all unassigned users for project_id == -1), even if they
have zero usage.
- For normal projects (project_id not None/-1), we also append two pseudo rows:
* "Common pool (balance)"
* "Project pool (balance)"
Their usage cells are zero, and the "Total credits" cell shows the current balance,
so the exported PDF makes the remaining funds explicit.
"""
sdt = _parse_date(start)
edt = _parse_date(end)
sdt, edt = _range_guard(sdt, edt)
# Base query: usage joined with user
stmt = select(UsageLog, User).join(User, User.id == UsageLog.user_id)
# IMPORTANT: a specific user overrides project filtering
if user_id:
stmt = stmt.where(User.id == user_id)
elif project_id is not None:
if project_id == -1:
stmt = stmt.where(User.project_id.is_(None))
else:
stmt = stmt.where(User.project_id == project_id)
if sdt:
stmt = stmt.where(UsageLog.created_at >= sdt)
if edt:
stmt = stmt.where(UsageLog.created_at < edt)
q = await session.execute(stmt)
rows = q.all()
# Optional project header info (also used for balances)
proj_info: Optional[Dict[str, Any]] = None
if project_id == -1:
proj_info = {
"id": None,
"name": "Unassigned Users",
"credits": None,
"common_pool_balance": None,
"allowance_interval": None,
"allowance_per_user": None,
}
elif project_id:
p = await session.get(Project, project_id)
if p:
proj_info = {
"id": p.id,
"name": p.name,
"credits": _dec(p.credits),
"common_pool_balance": _dec(p.common_pool_balance),
"allowance_interval": p.allowance_interval.value if p.allowance_interval else None,
"allowance_per_user": _dec(p.allowance_per_user),
}
# Aggregate per user and per model type
from decimal import Decimal
users: dict[int, dict] = {}
by_type_totals = {t: {"input": 0, "output": 0, "credits": Decimal(0)} for t in ("LLM","VLM","TTS","ASR","OTHER")}
grand_total = Decimal(0)
def _ensure_user(u: User):
if u.id not in users:
users[u.id] = {
"user_id": u.id,
"email": u.email,
"by_type": {t: {"input": 0, "output": 0, "credits": Decimal(0)} for t in ("LLM","VLM","TTS","ASR","OTHER")},
"total_credits": Decimal(0),
}
for ul, u in rows:
t = (ul.model_type.value if ul.model_type else "OTHER")
_ensure_user(u)
users[u.id]["by_type"][t]["input"] += int(ul.input_count or 0)
users[u.id]["by_type"][t]["output"] += int(ul.output_count or 0)
users[u.id]["by_type"][t]["credits"] += (ul.billed_credits or Decimal(0))
by_type_totals[t]["input"] += int(ul.input_count or 0)
by_type_totals[t]["output"] += int(ul.output_count or 0)
by_type_totals[t]["credits"] += (ul.billed_credits or Decimal(0))
users[u.id]["total_credits"] += (ul.billed_credits or Decimal(0))
grand_total += (ul.billed_credits or Decimal(0))
# (A) If a specific user was requested but had no usage, inject a zero row
if user_id and user_id not in users:
u = await session.get(User, user_id)
if u:
_ensure_user(u)
# (B) If a project (or unassigned scope) is selected and "All users" is chosen,
# include *all* users in that scope with zero rows.
if not user_id:
scope_users = []
if project_id == -1:
uq = await session.execute(select(User).where(User.project_id.is_(None)))
scope_users = list(uq.scalars().all())
elif project_id:
uq = await session.execute(select(User).where(User.project_id == project_id))
scope_users = list(uq.scalars().all())
# Ensure each appears at least with zeros
for u in scope_users:
_ensure_user(u)
# Build JSON users list (sorted by numeric user id)
users_list = []
for uid, rec in sorted(users.items(), key=lambda kv: kv[0]):
users_list.append({
"user_id": rec["user_id"],
"email": rec["email"],
"by_type": {k: {"input": v["input"], "output": v["output"], "credits": _dec(v["credits"])} for k, v in rec["by_type"].items()},
"total_credits": _dec(rec["total_credits"]),
})
# (C) Append pseudo rows for balances when a real project is selected
if project_id and project_id != -1 and proj_info:
zero_types = {t: {"input": 0, "output": 0, "credits": "0"} for t in ("LLM","VLM","TTS","ASR")}
users_list.append({
"user_id": f"CP-{proj_info['id']}",
"email": "Common pool (balance)",
"by_type": zero_types,
"total_credits": proj_info.get("common_pool_balance") or "0",
})
users_list.append({
"user_id": f"PB-{proj_info['id']}",
"email": "Project pool (balance)",
"by_type": zero_types,
"total_credits": proj_info.get("credits") or "0",
})
# NOTE: these pseudo rows are NOT added to 'totals' (they are balances, not usage).
totals_out = {k: {"input": v["input"], "output": v["output"], "credits": _dec(v["credits"])} for k, v in by_type_totals.items()}
return {
"project": proj_info,
"user_id": user_id,
"start": _iso(sdt), "end": _iso(edt),
"users": users_list,
"totals": {"by_type": totals_out, "grand_total_credits": _dec(grand_total)},
}
@router.get("/usage/overview/pdf")
async def usage_overview_pdf(
project_id: int | None = Query(None),
user_id: int | None = Query(None),
start: str | None = Query(None),
end: str | None = Query(None),
session: AsyncSession = Depends(get_session),
):
"""
Render the aggregated overview as PDF (WeasyPrint). Falls back to HTML if WeasyPrint
is not available. Uses A4 **landscape** to fit all columns on one page.
"""
data = await usage_overview(project_id=project_id, user_id=user_id, start=start, end=end, session=session)
title = "Usage Overview"
hdr = data.get("project") or {}
period = f"{data.get('start','?')} ? {data.get('end','?')}"
def esc(s):
return (s or "").replace("&", "&").replace("<", "<").replace(">", ">")
# per-user rows
rows_html = ""
types = ["LLM", "VLM", "TTS", "ASR"]
for u in data["users"]:
cells = [esc(u["email"] or f"user-{u['user_id']}")]
for t in types:
bt = u["by_type"].get(t, {"input": 0, "output": 0, "credits": "0"})
cells.append(str(bt["input"]))
cells.append(str(bt["output"]))
cells.append(str(bt["credits"]))
cells.append(esc(u["total_credits"]))
rows_html += "<tr>" + "".join(f"<td>{c}</td>" for c in cells) + "</tr>"
totals = data["totals"]["by_type"]
total_row_cells = ["<b>Totals</b>"]
for t in types:
bt = totals.get(t, {"input": 0, "output": 0, "credits": "0"})
total_row_cells += [f"<b>{bt['input']}</b>", f"<b>{bt['output']}</b>", f"<b>{bt['credits']}</b>"]
total_row_cells.append(f"<b>{data['totals']['grand_total_credits']}</b>")
proj_block = ""
if hdr:
proj_block = f"""
<div class="proj">
<div><b>Project:</b> {esc(hdr.get('name'))} (id {hdr.get('id')})</div>
<div><b>Common pool:</b> {esc(hdr.get('common_pool_balance'))}</div>
<div><b>Remaining project pool:</b> {esc(hdr.get('credits'))}</div>
<div><b>Allowance:</b> {esc(hdr.get('allowance_interval') or '-')} / {esc(hdr.get('allowance_per_user') or '0')}</div>
</div>"""
html = f"""
<!doctype html><html><head><meta charset="utf-8"><title>{title}</title>
<style>
@page {{ size: A4 landscape; margin: 10mm; }} /* Landscape to fit the wide table */
body {{ font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif; font-size: 11px; }}
h1 {{ font-size: 18px; margin: 0 0 6px }}
.meta {{ color: #555; margin: 0 0 12px }}
.proj div {{ margin: 2px 0 }}
table {{ border-collapse: collapse; width: 100%; margin-top: 10px; table-layout: fixed; }}
th, td {{ border: 1px solid #ccc; padding: 6px 6px; text-align: right; word-break: break-word; }}
th:first-child, td:first-child {{ text-align: left }}
thead th {{ background: #f5f5f5 }}
tfoot td {{ background: #fafafa }}
</style></head><body>
<h1>{title}</h1>
<div class="meta">Period: {esc(period)}</div>
{proj_block}
<table>
<thead>
<tr>
<th>User</th>
<th colspan="3">LLM (in, out, credits)</th>
<th colspan="3">VLM (in, out, credits)</th>
<th colspan="3">TTS (in, out, credits)</th>
<th colspan="3">ASR (in, out, credits)</th>
<th>Total credits</th>
</tr>
</thead>
<tbody>{rows_html}</tbody>
<tfoot><tr>{''.join(f'<td>{c}</td>' for c in total_row_cells)}</tr></tfoot>
</table>
</body></html>
"""
try:
from weasyprint import HTML
pdf_bytes = HTML(string=html).write_pdf()
fname = f"usage_overview_{(hdr.get('name') or 'all').replace(' ','_')}_{(data.get('start') or 'x')}_{(data.get('end') or 'y')}.pdf"
return Response(
pdf_bytes,
media_type="application/pdf",
headers={"Content-Disposition": f'attachment; filename="{fname}"'},
)
except Exception:
# Fallback for environments without WeasyPrint
return Response(html, media_type="text/html")
@router.get("/usage/overview/bulk_zip")
async def usage_overview_bulk_zip(
start: str | None = Query(None),
end: str | None = Query(None),
session: AsyncSession = Depends(get_session),
):
"""
Create a ZIP containing one usage overview document per project.
If WeasyPrint is unavailable, HTML fallbacks are embedded instead of PDFs.
"""
pq = await session.execute(select(Project))
projects = pq.scalars().all()
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
for p in projects:
data = await usage_overview(project_id=p.id, start=start, end=end, session=session)
# Keep it simple here: JSON pretty-printed, optionally rendered as PDF if WeasyPrint exists.
html = json.dumps(data, indent=2)
try:
from weasyprint import HTML
pdf_bytes = HTML(string=f"<pre>{html}</pre>").write_pdf()
ext = "pdf"
payload = pdf_bytes
except Exception:
ext = "html"
payload = f"<pre>{html}</pre>".encode("utf-8")
fname = f"{p.name.replace(' ','_')}_{(data.get('start') or 'x')}_{(data.get('end') or 'y')}.{ext}"
zf.writestr(fname, payload)
buf.seek(0)
return Response(
buf.read(),
media_type="application/zip",
headers={"Content-Disposition": 'attachment; filename="usage_overviews.zip"'},
)
# -----------------------------------------------------------------------------
# USERS
# -----------------------------------------------------------------------------
@router.get("/users")
async def list_users(
project_id: int | None = None,
unassigned: bool = Query(False, description="Return users with no project"),
q: str | None = None,
sort: str = "id",
order: str = "asc",
session: AsyncSession = Depends(get_session),
):
"""
List users with effective credits and (optionally) synthetic rows per project.
Filters:
- `project_id` filters to a project (if -1, returns unassigned users).
- `unassigned=true` also returns users with no project (independent of project_id).
The output preserves the prior synthetic rows behavior:
- CommonPool[<project_id>]
- ProjectBudget[<project_id>]
when listing for a specific project.
"""
# ---- load real users (+ their projects) ----
uq = select(User).options(selectinload(User.project))
# New: unassigned support and sentinel project_id == -1
if unassigned or (project_id == -1):
uq = uq.where(User.project_id.is_(None))
elif project_id is not None:
uq = uq.where(User.project_id == project_id)
ures = await session.execute(uq)
users = list(ures.scalars().all())
# project shares for effective credit calc
shares: Dict[Tuple[int, int], Decimal] = {}
qs = await session.execute(select(ProjectShare))
for s in qs.scalars().all():
shares[(s.project_id, s.user_id)] = Decimal(s.share_ratio or 0)
# allowances (remaining)
allowances: Dict[Tuple[int, int], Decimal] = {}
qa = await session.execute(select(ProjectAllowance))
for a in qa.scalars().all():
allowances[(a.project_id, a.user_id)] = Decimal(a.remaining or 0)
def effective_credits(u: User) -> Decimal:
eff = Decimal(u.credits or 0)
if u.project:
if u.project.has_common_pool:
# allowance only; keep behavior as before (common pool not included in user eff)
allow = allowances.get((u.project_id, u.id))
if allow is None:
allow = Decimal(u.project.allowance_per_user or 0)
eff = Decimal(u.credits or 0) + allow
else:
ratio = shares.get((u.project_id, u.id), Decimal(0))
eff = Decimal(u.credits or 0) + (ratio * Decimal(u.project.credits or 0))
return eff
out: list[dict[str, Any]] = []
# real users
for u in users:
row = {
"id": u.id,
"email": u.email,
"first_name": u.first_name,
"last_name": u.last_name,
"project_id": u.project_id,
"is_active": u.is_active,
"credits": _dec(effective_credits(u)),
}
# next/last settlement hints
last_dt = getattr(u.project, "last_settled_at", None) if u.project else None
next_dt = getattr(u.project, "next_settle_at", None) if u.project else None
if (not next_dt) and u.project and u.project.allowance_interval:
base = last_dt or _now()
next_dt = _compute_next(base, u.project.allowance_interval)
row["last_settlement_at"] = last_dt.isoformat() if last_dt else None
row["next_settlement_at"] = next_dt.isoformat() if next_dt else None
out.append(row)
# ---- add synthetic rows per project (CommonPool, ProjectBudget) ----
# Only when listing a concrete project (not unassigned/global).
pq = select(Project)
if project_id not in (None, -1) and not unassigned:
pq = pq.where(Project.id == project_id)
pres = await session.execute(pq)
projects = list(pres.scalars().all())
for p in projects:
# CommonPool row
if p.has_common_pool:
out.append({
"display_id": f"CP-{p.id}", # shown in UI
"email": f"CommonPool[{p.id}]",
"first_name": "CommonPool",
"last_name": "",
"project_id": p.id,
"is_active": True,
"credits": _dec(p.common_pool_balance),
})
# ProjectBudget row
out.append({
"display_id": f"PB-{p.id}",
"email": f"ProjectBudget[{p.id}]",
"first_name": "ProjectBudget",
"last_name": "",
"project_id": p.id,
"is_active": True,
"credits": _dec(p.credits),
})
# ---- search filter (email / names) ----
if q:
ql = q.lower()
def _hit(r: dict) -> bool:
return any(
(r.get(k) or "").lower().find(ql) >= 0
for k in ("email", "first_name", "last_name")
)
out = [r for r in out if _hit(r)]
# ---- sorting (stable across numeric + synthetic ids) ----
def _dec2(x):
try:
return Decimal(str(x))
except Exception:
return Decimal(0)
sort = (sort or "id").lower()
reverse = (order or "asc").lower() == "desc"
def _key(r: dict):
if sort == "credits":
return (_dec2(r.get("credits")), r.get("email", "").lower())
if sort == "email":
return (r.get("email", "").lower(),)
if sort == "project_id":
return (str(r.get("project_id") or ""), r.get("email", "").lower())
# default: id (numeric ids first, then synthetic by display_id)
rid = r.get("id")
if isinstance(rid, int):
return (0, rid)
return (1, r.get("display_id", ""))
out.sort(key=_key, reverse=reverse)
return out
@router.post("/users")
async def create_user(
email: str = Form(...),
first_name: str = Form(""),
last_name: str = Form(""),
project_id: int | None = Form(None),
session: AsyncSession = Depends(get_session),
):
u = User(email=email, first_name=first_name, last_name=last_name, project_id=project_id)
session.add(u)
await session.flush()
await session.commit()
return {"id": u.id}
@router.post("/users/{user_id}/update")
async def update_user(
user_id: int,
email: str | None = Form(None),
first_name: str | None = Form(None),
last_name: str | None = Form(None),
project_id: int | None = Form(None),
is_active: bool | None = Form(None),
session: AsyncSession = Depends(get_session),
):
u = await session.get(User, user_id)
if not u:
raise HTTPException(404, f"User {user_id} not found.")
if email is not None:
u.email = email
if first_name is not None:
u.first_name = first_name
if last_name is not None:
u.last_name = last_name
if project_id is not None:
if project_id == "" or project_id == "null":
u.project_id = None
else:
p = await session.get(Project, int(project_id))
if not p:
raise HTTPException(400, f"Project {project_id} does not exist.")
u.project_id = p.id
if is_active is not None:
u.is_active = bool(int(is_active)) if isinstance(is_active, str) and is_active.isdigit() else bool(is_active)
await session.commit()
return {"ok": True}
@router.post("/users/delete")
async def delete_users(
ids: str = Form(...),
session: AsyncSession = Depends(get_session),
):
id_list = _parse_ids(ids)
if not id_list:
return {"ok": True, "deleted": 0}
await session.execute(delete(ApiKey).where(ApiKey.user_id.in_(id_list)))
await session.execute(delete(ProjectShare).where(ProjectShare.user_id.in_(id_list)))
await session.execute(delete(ProjectAllowance).where(ProjectAllowance.user_id.in_(id_list)))
await session.execute(delete(UsageLog).where(UsageLog.user_id.in_(id_list)))
await session.execute(delete(CreditLedger).where(CreditLedger.user_id.in_(id_list)))
await session.execute(delete(User).where(User.id.in_(id_list)))
await session.commit()
return {"ok": True, "deleted": len(id_list)}
@router.post("/users/{user_id}/delete")
async def delete_user(user_id: int, session: AsyncSession = Depends(get_session)):
await session.execute(delete(ApiKey).where(ApiKey.user_id == user_id))
await session.execute(delete(ProjectShare).where(ProjectShare.user_id == user_id))
await session.execute(delete(ProjectAllowance).where(ProjectAllowance.user_id == user_id))
await session.execute(delete(UsageLog).where(UsageLog.user_id == user_id))
await session.execute(delete(CreditLedger).where(CreditLedger.user_id == user_id))
await session.execute(delete(User).where(User.id == user_id))
await session.commit()
return {"ok": True}
@router.post("/users/{user_id}/apikey")
async def create_or_rotate_key(user_id: int, session: AsyncSession = Depends(get_session)):
u = await session.get(User, user_id)
if not u:
raise HTTPException(404, f"User {user_id} not found.")
await session.execute(delete(ApiKey).where(ApiKey.user_id == user_id))
public, h = generate_api_key(12)
session.add(ApiKey(user_id=u.id, key_hash=h))
await session.commit()
# Return composite with ENCODED address suffix
composite = f"{public}#{_encoded_addr_suffix()}"
return {"api_key": composite}
# ---- Ledger helper -----------------------------------------------------------
from decimal import Decimal
async def _log_ledger(session: AsyncSession, *, user_id: int, delta: Decimal, reason: str = "") -> None:
"""Append a row to the CreditLedger."""
session.add(CreditLedger(user_id=user_id, delta=Decimal(str(delta)), reason=reason))
@router.post("/users/{user_id}/credits")
async def set_credits(user_id: int, credits: float = Form(...), session: AsyncSession = Depends(get_session)):
u = await session.get(User, user_id)
if not u:
raise HTTPException(404, "User not found")
old = Decimal(str(u.credits or 0))
new = Decimal(str(credits))
delta = new - old
u.credits = new
session.add(u)
# NEW: write a ledger entry so the Admin "Ledger" page has something to show
await _log_ledger(session, user_id=u.id, delta=delta, reason="admin:set_credits")
await session.commit()
return {"ok": True, "credits": str(u.credits)}
@router.post("/users/{user_id}/project")
async def assign_project(user_id: int, project_id: int | None = Form(None), session: AsyncSession = Depends(get_session)):
u = await session.get(User, user_id)
if not u:
raise HTTPException(404, "User not found")
if project_id in (None, "", "null"):
u.project_id = None
else:
p = await session.get(Project, int(project_id))
if not p:
raise HTTPException(404, "Project not found")
u.project_id = p.id
session.add(u)
await session.commit()
return {"ok": True}
# -----------------------------------------------------------------------------
# PRICING
# -----------------------------------------------------------------------------
@router.get("/pricing")
async def list_pricing(session: AsyncSession = Depends(get_session)):
q = await session.execute(select(ModelPricing))
rows = q.scalars().all()
return [
{
"id": p.id,
"model": p.model,
"provider": p.provider,
"model_type": p.model_type.value if p.model_type else None,
"p_input": _dec(p.price_per_input_token),
"p_output": _dec(p.price_per_output_token),
"p_char": _dec(p.price_per_character),
"p_sec": _dec(p.price_per_second),
}
for p in rows
]
@router.post("/pricing")
async def upsert_pricing(
model: str = Form(...),
provider: str = Form(...),
model_type: ModelType = Form(...),
price_per_input_token: float = Form(0),
price_per_output_token: float = Form(0),
price_per_character: float = Form(0),
price_per_second: float = Form(0),
session: AsyncSession = Depends(get_session),
):
q = await session.execute(select(ModelPricing).where(ModelPricing.model == model, ModelPricing.provider == provider))
mp = q.scalar_one_or_none()
if not mp:
mp = ModelPricing(model=model, provider=provider, model_type=model_type)
mp.price_per_input_token = Decimal(str(price_per_input_token))
mp.price_per_output_token = Decimal(str(price_per_output_token))
mp.price_per_character = Decimal(str(price_per_character))
mp.price_per_second = Decimal(str(price_per_second))
session.add(mp)
await session.commit()
return {"ok": True}
@router.post("/pricing/delete")
async def delete_pricing(
ids: str = Form(...),
session: AsyncSession = Depends(get_session),
):
id_list = _parse_ids(ids)
if not id_list:
return {"ok": True, "deleted": 0}
await session.execute(delete(ModelPricing).where(ModelPricing.id.in_(id_list)))
await session.commit()
return {"ok": True, "deleted": len(id_list)}
# -----------------------------------------------------------------------------
# PROVIDERS
# -----------------------------------------------------------------------------
@router.get("/providers")
async def list_providers(session: AsyncSession = Depends(get_session)):
q = await session.execute(select(ProviderEndpoint))
rows = q.scalars().all()
return [
{"id": p.id, "name": p.name, "base_url": p.base_url or "", "api_key": bool(p.api_key)}
for p in rows
]
@router.post("/providers")
async def upsert_provider(
name: str = Form(...),
base_url: str | None = Form(None),
api_key: str | None = Form(None),
session: AsyncSession = Depends(get_session),
):
q = await session.execute(select(ProviderEndpoint).where(ProviderEndpoint.name == name))
pe = q.scalar_one_or_none()
if not pe:
pe = ProviderEndpoint(name=name)
pe.base_url = base_url or pe.base_url
if api_key is not None:
pe.api_key = api_key or None
session.add(pe)
await session.commit()
return {"ok": True}
@router.post("/providers/{provider_id}/apikey")
async def set_provider_key(
provider_id: int,
api_key: str = Form(""),
session: AsyncSession = Depends(get_session),
):
pe = await session.get(ProviderEndpoint, provider_id)