maj
This commit is contained in:
@@ -2,12 +2,14 @@
|
||||
Linux BenchTools - Main Application
|
||||
"""
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Depends
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from contextlib import asynccontextmanager
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.config import settings
|
||||
from app.db.init_db import init_db
|
||||
from app.db.session import get_db
|
||||
from app.api import benchmark, devices, links, docs
|
||||
|
||||
|
||||
@@ -68,37 +70,28 @@ async def health_check():
|
||||
|
||||
# Stats endpoint (for dashboard)
|
||||
@app.get(f"{settings.API_PREFIX}/stats")
|
||||
async def get_stats():
|
||||
async def get_stats(db: Session = Depends(get_db)):
|
||||
"""Get global statistics"""
|
||||
from sqlalchemy.orm import Session
|
||||
from app.db.session import get_db
|
||||
from app.models.device import Device
|
||||
from app.models.benchmark import Benchmark
|
||||
from sqlalchemy import func
|
||||
|
||||
db: Session = next(get_db())
|
||||
total_devices = db.query(Device).count()
|
||||
total_benchmarks = db.query(Benchmark).count()
|
||||
|
||||
try:
|
||||
total_devices = db.query(Device).count()
|
||||
total_benchmarks = db.query(Benchmark).count()
|
||||
# Get average score
|
||||
avg_score = db.query(func.avg(Benchmark.global_score)).scalar()
|
||||
|
||||
# Get average score
|
||||
avg_score = db.query(Benchmark).with_entities(
|
||||
db.func.avg(Benchmark.global_score)
|
||||
).scalar()
|
||||
# Get last benchmark date
|
||||
last_bench = db.query(Benchmark).order_by(Benchmark.run_at.desc()).first()
|
||||
last_bench_date = last_bench.run_at.isoformat() if last_bench else None
|
||||
|
||||
# Get last benchmark date
|
||||
last_bench = db.query(Benchmark).order_by(Benchmark.run_at.desc()).first()
|
||||
last_bench_date = last_bench.run_at.isoformat() if last_bench else None
|
||||
|
||||
return {
|
||||
"total_devices": total_devices,
|
||||
"total_benchmarks": total_benchmarks,
|
||||
"avg_global_score": round(avg_score, 2) if avg_score else 0,
|
||||
"last_benchmark_at": last_bench_date
|
||||
}
|
||||
|
||||
finally:
|
||||
db.close()
|
||||
return {
|
||||
"total_devices": total_devices,
|
||||
"total_benchmarks": total_benchmarks,
|
||||
"avg_global_score": round(avg_score, 2) if avg_score else 0,
|
||||
"last_benchmark_at": last_bench_date
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user