Repository navigation
Expand file tree
/
Copy pathadmin.py
More file actions
1331 lines (1109 loc) · 41.8 KB
/
Copy pathadmin.py
File metadata and controls
1331 lines (1109 loc) · 41.8 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
"""
Admin API routes for managing binaries, environments, and runs.
Protected by GitHub OAuth authentication.
"""
import logging
from datetime import datetime, UTC
from typing import List, Optional, Dict, Any
from fastapi import APIRouter, Depends, HTTPException, status, Response, Request
from sqlalchemy import select, delete, func, text, desc, and_
from sqlalchemy.ext.asyncio import AsyncSession
import sqlparse
from ..database import get_database
from ..admin_auth import (
require_admin_auth,
create_admin_session,
invalidate_admin_session,
)
from ..oauth import github_oauth
from ..models import (
AdminSession,
Binary,
Environment,
Run,
AuthToken,
BenchmarkResult,
MemrayBuildFailure,
Commit,
)
from ..schemas import (
BinaryCreate,
Binary as BinarySchema,
EnvironmentCreate,
Environment as EnvironmentSchema,
MemrayFailurePublic,
)
from .. import crud
from pydantic import BaseModel
import re
logger = logging.getLogger(__name__)
# Pydantic schemas for admin user management
class AdminUserCreate(BaseModel):
github_username: str
notes: Optional[str] = None
class AdminUserResponse(BaseModel):
id: int
github_username: str
added_by: str
added_at: datetime
is_active: bool
notes: Optional[str] = None
# Pydantic schemas for token management
class TokenCreate(BaseModel):
name: str
description: Optional[str] = None
class TokenResponse(BaseModel):
id: int
name: str
description: Optional[str] = None
created_at: datetime
last_used: Optional[datetime] = None
is_active: bool
token_preview: str # First 8 + last 4 characters
class TokenUpdate(BaseModel):
name: Optional[str] = None
description: Optional[str] = None
class TokenAnalytics(BaseModel):
total_tokens: int
active_tokens: int
inactive_tokens: int
used_tokens: int
never_used_tokens: int
recent_active_tokens: int
# Pydantic schemas for Commits management
class CommitUpdate(BaseModel):
message: Optional[str] = None
author: Optional[str] = None
python_major: Optional[int] = None
python_minor: Optional[int] = None
python_patch: Optional[int] = None
class CommitResponse(BaseModel):
sha: str
timestamp: datetime
message: str
author: str
python_major: int
python_minor: int
python_patch: int
run_count: Optional[int] = None
# Pydantic schemas for BenchmarkResults management
class BenchmarkResultUpdate(BaseModel):
high_watermark_bytes: Optional[int] = None
total_allocated_bytes: Optional[int] = None
allocation_histogram: Optional[List[List[int]]] = None
top_allocating_functions: Optional[List[Dict[str, Any]]] = None
class BenchmarkResultResponse(BaseModel):
id: str
run_id: str
benchmark_name: str
high_watermark_bytes: int
total_allocated_bytes: int
allocation_histogram: List[List[int]]
top_allocating_functions: List[Dict[str, Any]]
has_flamegraph: bool
router = APIRouter(prefix="/api/admin", tags=["admin"])
@router.get("/auth/github")
async def initiate_github_auth():
"""Initiate GitHub OAuth flow."""
auth_url, state = github_oauth.generate_authorization_url()
return {"auth_url": auth_url, "state": state}
@router.post("/auth/callback")
async def github_auth_callback(
code: str,
state: str,
request: Request,
response: Response,
db: AsyncSession = Depends(get_database),
):
"""Handle GitHub OAuth callback and create admin session."""
try:
# Exchange code for access token
access_token = await github_oauth.exchange_code_for_token(code, state)
# Get user info
github_user = await github_oauth.get_user_info(access_token)
# Check if user is admin
is_admin = await github_oauth.is_admin_user(github_user.login, db)
if not is_admin:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="User is not authorized as admin",
)
# Create admin session
try:
session_token = await create_admin_session(db, github_user)
except Exception as db_error:
logger.error(f"Database error creating session: {db_error}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Database error: {str(db_error)}",
)
# Set secure cookie
# Determine if we're using HTTPS
is_https = request.url.scheme == "https"
response.set_cookie(
key="admin_session",
value=session_token,
httponly=True,
secure=is_https, # Only use secure cookies on HTTPS
samesite="lax",
max_age=24 * 60 * 60, # 24 hours
domain=None, # Let the browser handle the domain
path="/", # Make cookie available for all paths
)
return {
"success": True,
"user": {
"username": github_user.login,
"name": github_user.name,
"avatar_url": github_user.avatar_url,
},
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Unexpected error in auth callback: {e}")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Authentication failed: {str(e)}",
)
@router.post("/auth/logout")
async def logout(
response: Response,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Logout admin user and invalidate session."""
await invalidate_admin_session(db, admin_session.session_token)
response.delete_cookie("admin_session")
return {"success": True}
@router.get("/me")
async def get_current_admin(
admin_session: AdminSession = Depends(require_admin_auth),
):
"""Get current admin user info."""
return {
"username": admin_session.github_username,
"name": admin_session.github_name,
"email": admin_session.github_email,
"avatar_url": admin_session.github_avatar_url,
}
# Binaries Management
@router.get("/binaries", response_model=List[BinarySchema])
async def list_binaries(
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""List all binaries."""
return await crud.get_binaries(db)
@router.post("/binaries", response_model=BinarySchema)
async def create_binary(
binary: BinaryCreate,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Create a new binary."""
# Check if binary already exists
existing = await crud.get_binary_by_id(db, binary.id)
if existing:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Binary with this ID already exists",
)
return await crud.create_binary(db, binary)
@router.put("/binaries/{binary_id}", response_model=BinarySchema)
async def update_binary(
binary_id: str,
binary_update: BinaryCreate,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Update an existing binary."""
existing = await crud.get_binary_by_id(db, binary_id)
if not existing:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Binary not found",
)
# Update fields
existing.name = binary_update.name
existing.flags = binary_update.flags
existing.description = binary_update.description
existing.color = binary_update.color
existing.icon = binary_update.icon
existing.display_order = binary_update.display_order
await db.commit()
await db.refresh(existing)
return existing
@router.delete("/binaries/{binary_id}")
async def delete_binary(
binary_id: str,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Delete a binary and all associated runs and benchmark results."""
existing = await crud.get_binary_by_id(db, binary_id)
if not existing:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Binary not found",
)
# Get all runs for this binary to cascade delete benchmark results
runs_result = await db.execute(select(Run.run_id).where(Run.binary_id == binary_id))
run_ids = [row[0] for row in runs_result.fetchall()]
# Delete all benchmark results for runs associated with this binary
if run_ids:
await db.execute(
delete(BenchmarkResult).where(BenchmarkResult.run_id.in_(run_ids))
)
# Delete all runs for this binary
await db.execute(delete(Run).where(Run.binary_id == binary_id))
# Finally delete the binary
await db.execute(delete(Binary).where(Binary.id == binary_id))
await db.commit()
return {"success": True}
# Environments Management
@router.get("/environments", response_model=List[EnvironmentSchema])
async def list_environments(
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""List all environments."""
return await crud.get_environments(db)
@router.post("/environments", response_model=EnvironmentSchema)
async def create_environment(
environment: EnvironmentCreate,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Create a new environment."""
# Check if environment already exists
existing = await crud.get_environment_by_id(db, environment.id)
if existing:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Environment with this ID already exists",
)
return await crud.create_environment(db, environment)
@router.put("/environments/{environment_id}", response_model=EnvironmentSchema)
async def update_environment(
environment_id: str,
environment_update: EnvironmentCreate,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Update an existing environment."""
existing = await crud.get_environment_by_id(db, environment_id)
if not existing:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Environment not found",
)
# Update fields
existing.name = environment_update.name
existing.description = environment_update.description
await db.commit()
await db.refresh(existing)
return existing
@router.delete("/environments/{environment_id}")
async def delete_environment(
environment_id: str,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Delete an environment and all associated runs and benchmark results."""
existing = await crud.get_environment_by_id(db, environment_id)
if not existing:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Environment not found",
)
# Get all runs for this environment to cascade delete benchmark results
runs_result = await db.execute(
select(Run.run_id).where(Run.environment_id == environment_id)
)
run_ids = [row[0] for row in runs_result.fetchall()]
# Delete all benchmark results for runs associated with this environment
if run_ids:
await db.execute(
delete(BenchmarkResult).where(BenchmarkResult.run_id.in_(run_ids))
)
# Delete all runs for this environment
await db.execute(delete(Run).where(Run.environment_id == environment_id))
# Finally delete the environment
await db.execute(delete(Environment).where(Environment.id == environment_id))
await db.commit()
return {"success": True}
# Admin Users Management
@router.get("/users", response_model=List[AdminUserResponse])
async def list_admin_users(
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""List all admin users."""
return await crud.get_admin_users(db)
@router.post("/users", response_model=AdminUserResponse)
async def create_admin_user(
user_data: AdminUserCreate,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Create a new admin user."""
# Check if user already exists
existing = await crud.get_admin_user_by_username(db, user_data.github_username)
if existing:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Admin user already exists",
)
return await crud.create_admin_user(
db, user_data.github_username, admin_session.github_username, user_data.notes
)
@router.delete("/users/{username}")
async def remove_admin_user(
username: str,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Remove an admin user."""
# Prevent removing yourself
if username == admin_session.github_username:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Cannot remove yourself from admin users",
)
success = await crud.deactivate_admin_user(db, username)
if not success:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Admin user not found",
)
return {"success": True}
# Token Management
@router.get("/tokens", response_model=List[TokenResponse])
async def list_tokens(
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""List all authentication tokens."""
tokens = await crud.get_all_auth_tokens(db)
token_responses = []
for token in tokens:
token_responses.append(
TokenResponse(
id=token.id,
name=token.name,
description=token.description,
created_at=token.created_at,
last_used=token.last_used,
is_active=token.is_active,
token_preview=f"{token.token[:8]}...{token.token[-4:]}",
)
)
return token_responses
@router.post("/tokens")
async def create_token(
token_data: TokenCreate,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Create a new authentication token."""
import secrets
# Generate secure token
token = secrets.token_bytes(32).hex()
# Create token in database
auth_token = await crud.create_auth_token(
db, token, token_data.name, token_data.description
)
return {
"success": True,
"token": token,
"token_info": TokenResponse(
id=auth_token.id,
name=auth_token.name,
description=auth_token.description,
created_at=auth_token.created_at,
last_used=auth_token.last_used,
is_active=auth_token.is_active,
token_preview=f"{token[:8]}...{token[-4:]}",
),
}
@router.put("/tokens/{token_id}")
async def update_token(
token_id: int,
token_update: TokenUpdate,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Update token name and/or description."""
result = await db.execute(select(AuthToken).where(AuthToken.id == token_id))
auth_token = result.scalars().first()
if not auth_token:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Token not found"
)
if token_update.name is not None:
auth_token.name = token_update.name
if token_update.description is not None:
auth_token.description = token_update.description
await db.commit()
await db.refresh(auth_token)
return TokenResponse(
id=auth_token.id,
name=auth_token.name,
description=auth_token.description,
created_at=auth_token.created_at,
last_used=auth_token.last_used,
is_active=auth_token.is_active,
token_preview=f"{auth_token.token[:8]}...{auth_token.token[-4:]}",
)
@router.post("/tokens/{token_id}/deactivate")
async def deactivate_token(
token_id: int,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Deactivate an authentication token."""
success = await crud.deactivate_auth_token(db, token_id)
if not success:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Token not found"
)
return {"success": True}
@router.post("/tokens/{token_id}/activate")
async def activate_token(
token_id: int,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Activate a deactivated authentication token."""
result = await db.execute(select(AuthToken).where(AuthToken.id == token_id))
auth_token = result.scalars().first()
if not auth_token:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Token not found"
)
auth_token.is_active = True
await db.commit()
return {"success": True}
@router.delete("/tokens/{token_id}")
async def delete_token(
token_id: int,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Delete an authentication token."""
result = await db.execute(select(AuthToken).where(AuthToken.id == token_id))
auth_token = result.scalars().first()
if not auth_token:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Token not found"
)
await db.delete(auth_token)
await db.commit()
return {"success": True}
@router.get("/tokens/analytics", response_model=TokenAnalytics)
async def get_token_analytics(
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Get token usage analytics."""
from datetime import timedelta
# Get basic counts
total_result = await db.execute(select(func.count(AuthToken.id)))
total_tokens = total_result.scalar()
active_result = await db.execute(
select(func.count(AuthToken.id)).where(AuthToken.is_active)
)
active_tokens = active_result.scalar()
used_result = await db.execute(
select(func.count(AuthToken.id)).where(AuthToken.last_used.is_not(None))
)
used_tokens = used_result.scalar()
# Get recent activity (last 30 days)
thirty_days_ago = datetime.now(UTC).replace(tzinfo=None) - timedelta(days=30)
recent_result = await db.execute(
select(func.count(AuthToken.id)).where(AuthToken.last_used >= thirty_days_ago)
)
recent_active = recent_result.scalar()
return TokenAnalytics(
total_tokens=total_tokens,
active_tokens=active_tokens,
inactive_tokens=total_tokens - active_tokens,
used_tokens=used_tokens,
never_used_tokens=total_tokens - used_tokens,
recent_active_tokens=recent_active,
)
# Commits Management
@router.get("/commits", response_model=List[CommitResponse])
async def list_commits(
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
skip: int = 0,
limit: int = 50,
sha: Optional[str] = None,
author: Optional[str] = None,
python_version: Optional[str] = None,
):
"""List commits with filtering and pagination."""
limit = min(limit, 100) # Prevent excessive data loading
query = select(Commit, func.count(Run.run_id).label("run_count")).outerjoin(Run).group_by(Commit.sha)
if sha:
query = query.where(Commit.sha.ilike(f"{sha}%"))
if author:
query = query.where(Commit.author.ilike(f"%{author}%"))
if python_version:
try:
major, minor = python_version.split(".")[:2]
query = query.where(
and_(
Commit.python_major == int(major),
Commit.python_minor == int(minor)
)
)
except (ValueError, IndexError):
pass # Invalid version format, ignore filter
query = query.order_by(desc(Commit.timestamp)).offset(skip).limit(limit)
result = await db.execute(query)
commits_with_counts = result.all()
commit_responses = []
for commit, run_count in commits_with_counts:
commit_responses.append(
CommitResponse(
sha=commit.sha,
timestamp=commit.timestamp,
message=commit.message,
author=commit.author,
python_major=commit.python_major,
python_minor=commit.python_minor,
python_patch=commit.python_patch,
run_count=run_count,
)
)
return commit_responses
@router.get("/commits/{sha}", response_model=CommitResponse)
async def get_commit(
sha: str,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Get a specific commit by SHA."""
result = await db.execute(
select(Commit, func.count(Run.run_id).label("run_count"))
.outerjoin(Run)
.where(Commit.sha == sha)
.group_by(Commit.sha)
)
commit_data = result.first()
if not commit_data:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Commit not found",
)
commit, run_count = commit_data
return CommitResponse(
sha=commit.sha,
timestamp=commit.timestamp,
message=commit.message,
author=commit.author,
python_major=commit.python_major,
python_minor=commit.python_minor,
python_patch=commit.python_patch,
run_count=run_count,
)
@router.put("/commits/{sha}", response_model=CommitResponse)
async def update_commit(
sha: str,
commit_update: CommitUpdate,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Update a commit's metadata."""
result = await db.execute(select(Commit).where(Commit.sha == sha))
commit = result.scalars().first()
if not commit:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Commit not found",
)
# Update fields if provided
if commit_update.message is not None:
commit.message = commit_update.message
if commit_update.author is not None:
commit.author = commit_update.author
if commit_update.python_major is not None:
commit.python_major = commit_update.python_major
if commit_update.python_minor is not None:
commit.python_minor = commit_update.python_minor
if commit_update.python_patch is not None:
commit.python_patch = commit_update.python_patch
await db.commit()
await db.refresh(commit)
# Get run count
run_count_result = await db.execute(
select(func.count(Run.run_id)).where(Run.commit_sha == sha)
)
run_count = run_count_result.scalar() or 0
return CommitResponse(
sha=commit.sha,
timestamp=commit.timestamp,
message=commit.message,
author=commit.author,
python_major=commit.python_major,
python_minor=commit.python_minor,
python_patch=commit.python_patch,
run_count=run_count,
)
@router.delete("/commits/{sha}")
async def delete_commit(
sha: str,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Delete a commit and all associated runs and benchmark results."""
result = await db.execute(select(Commit).where(Commit.sha == sha))
commit = result.scalars().first()
if not commit:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Commit not found",
)
# Get all runs for this commit
runs_result = await db.execute(select(Run.run_id).where(Run.commit_sha == sha))
run_ids = [row[0] for row in runs_result.fetchall()]
# Delete all benchmark results for runs associated with this commit
if run_ids:
await db.execute(
delete(BenchmarkResult).where(BenchmarkResult.run_id.in_(run_ids))
)
# Delete all runs for this commit
await db.execute(delete(Run).where(Run.commit_sha == sha))
# Finally delete the commit
await db.execute(delete(Commit).where(Commit.sha == sha))
await db.commit()
return {"success": True}
# BenchmarkResults Management
@router.get("/benchmark-results", response_model=List[BenchmarkResultResponse])
async def list_benchmark_results(
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
skip: int = 0,
limit: int = 50,
run_id: Optional[str] = None,
benchmark_name: Optional[str] = None,
min_memory: Optional[int] = None,
max_memory: Optional[int] = None,
):
"""List benchmark results with filtering and pagination."""
limit = min(limit, 100) # Prevent excessive data loading
query = select(BenchmarkResult)
if run_id:
query = query.where(BenchmarkResult.run_id.ilike(f"{run_id}%"))
if benchmark_name:
query = query.where(BenchmarkResult.benchmark_name.ilike(f"%{benchmark_name}%"))
if min_memory is not None:
query = query.where(BenchmarkResult.high_watermark_bytes >= min_memory)
if max_memory is not None:
query = query.where(BenchmarkResult.high_watermark_bytes <= max_memory)
query = query.order_by(desc(BenchmarkResult.id)).offset(skip).limit(limit)
result = await db.execute(query)
benchmark_results = result.scalars().all()
result_responses = []
for br in benchmark_results:
result_responses.append(
BenchmarkResultResponse(
id=br.id,
run_id=br.run_id,
benchmark_name=br.benchmark_name,
high_watermark_bytes=br.high_watermark_bytes,
total_allocated_bytes=br.total_allocated_bytes,
allocation_histogram=br.allocation_histogram,
top_allocating_functions=br.top_allocating_functions,
has_flamegraph=br.flamegraph_html is not None,
)
)
return result_responses
@router.get("/benchmark-results/{result_id}", response_model=BenchmarkResultResponse)
async def get_benchmark_result(
result_id: str,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Get a specific benchmark result by ID."""
result = await db.execute(
select(BenchmarkResult).where(BenchmarkResult.id == result_id)
)
benchmark_result = result.scalars().first()
if not benchmark_result:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Benchmark result not found",
)
return BenchmarkResultResponse(
id=benchmark_result.id,
run_id=benchmark_result.run_id,
benchmark_name=benchmark_result.benchmark_name,
high_watermark_bytes=benchmark_result.high_watermark_bytes,
total_allocated_bytes=benchmark_result.total_allocated_bytes,
allocation_histogram=benchmark_result.allocation_histogram,
top_allocating_functions=benchmark_result.top_allocating_functions,
has_flamegraph=benchmark_result.flamegraph_html is not None,
)
@router.get("/benchmark-results/{result_id}/flamegraph")
async def get_benchmark_result_flamegraph(
result_id: str,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Get the flamegraph HTML for a benchmark result."""
result = await db.execute(
select(BenchmarkResult.flamegraph_html).where(BenchmarkResult.id == result_id)
)
flamegraph_html = result.scalar()
if flamegraph_html is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Flamegraph not found for this benchmark result",
)
return {"flamegraph_html": flamegraph_html}
@router.put("/benchmark-results/{result_id}", response_model=BenchmarkResultResponse)
async def update_benchmark_result(
result_id: str,
result_update: BenchmarkResultUpdate,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Update a benchmark result's data."""
result = await db.execute(
select(BenchmarkResult).where(BenchmarkResult.id == result_id)
)
benchmark_result = result.scalars().first()
if not benchmark_result:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Benchmark result not found",
)
# Update fields if provided
if result_update.high_watermark_bytes is not None:
benchmark_result.high_watermark_bytes = result_update.high_watermark_bytes
if result_update.total_allocated_bytes is not None:
benchmark_result.total_allocated_bytes = result_update.total_allocated_bytes
if result_update.allocation_histogram is not None:
benchmark_result.allocation_histogram = result_update.allocation_histogram
if result_update.top_allocating_functions is not None:
benchmark_result.top_allocating_functions = result_update.top_allocating_functions
await db.commit()
await db.refresh(benchmark_result)
return BenchmarkResultResponse(
id=benchmark_result.id,
run_id=benchmark_result.run_id,
benchmark_name=benchmark_result.benchmark_name,
high_watermark_bytes=benchmark_result.high_watermark_bytes,
total_allocated_bytes=benchmark_result.total_allocated_bytes,
allocation_histogram=benchmark_result.allocation_histogram,
top_allocating_functions=benchmark_result.top_allocating_functions,
has_flamegraph=benchmark_result.flamegraph_html is not None,
)
@router.delete("/benchmark-results/{result_id}")
async def delete_benchmark_result(
result_id: str,
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):
"""Delete a specific benchmark result."""
result = await db.execute(
select(BenchmarkResult).where(BenchmarkResult.id == result_id)
)
benchmark_result = result.scalars().first()
if not benchmark_result:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Benchmark result not found",
)
await db.execute(delete(BenchmarkResult).where(BenchmarkResult.id == result_id))
await db.commit()
return {"success": True}
@router.post("/benchmark-results/bulk-delete")
async def bulk_delete_benchmark_results(
result_ids: List[str],
admin_session: AdminSession = Depends(require_admin_auth),
db: AsyncSession = Depends(get_database),
):