gitscope
https://dev.kronoz.win/r/marzneshin.git
app/routes/system.py · v0.3.0
Source
1 f7af2b3 Dawsh 2y ago from fastapi import APIRouter
2 f7af2b3 Dawsh 2y ago
3 f7af2b3 Dawsh 2y ago from app import __version__
4 c13a1aa Dawsh 2y ago from app.db import crud
5 e5dcd82 Dawsh 2y ago from app.db.models import Admin as DBAdmin, User
6 e5dcd82 Dawsh 2y ago from app.db.models import Node
7 e5dcd82 Dawsh 2y ago from app.dependencies import DBDep, AdminDep, SudoAdminDep
8 e5dcd82 Dawsh 2y ago from app.models.node import NodeStatus
9 e5dcd82 Dawsh 2y ago from app.models.system import SystemStats, UsersStats, NodesStats, AdminsStats
10 a47eb4c Dawsh 2y ago from app.models.user import UserStatus
11 f7af2b3 Dawsh 2y ago
12 e5dcd82 Dawsh 2y ago router = APIRouter(tags=["System"], prefix="/system")
13 e5dcd82 Dawsh 2y ago
14 a47eb4c Dawsh 2y ago
15 e5dcd82 Dawsh 2y ago @router.get("/stats/admins", response_model=AdminsStats)
16 e5dcd82 Dawsh 2y ago def get_admins_stats(db: DBDep, admin: SudoAdminDep):
17 e5dcd82 Dawsh 2y ago return AdminsStats(total=db.query(DBAdmin).count())
18 a47eb4c Dawsh 2y ago
19 a47eb4c Dawsh 2y ago
20 e5dcd82 Dawsh 2y ago @router.get("/stats/nodes", response_model=NodesStats)
21 e5dcd82 Dawsh 2y ago def get_nodes_stats(db: DBDep, admin: SudoAdminDep):
22 e5dcd82 Dawsh 2y ago return NodesStats(
23 e5dcd82 Dawsh 2y ago total=db.query(Node).count(),
24 e5dcd82 Dawsh 2y ago healthy=db.query(Node).filter(Node.status == NodeStatus.healthy).count(),
25 e5dcd82 Dawsh 2y ago unhealthy=db.query(Node).filter(Node.status == NodeStatus.unhealthy).count(),
26 01cdf2e Dawsh 2y ago )
27 e5dcd82 Dawsh 2y ago
28 e5dcd82 Dawsh 2y ago
29 e5dcd82 Dawsh 2y ago @router.get("/stats/users", response_model=UsersStats)
30 e5dcd82 Dawsh 2y ago def get_users_stats(db: DBDep, admin: AdminDep):
31 e5dcd82 Dawsh 2y ago return UsersStats(
32 e5dcd82 Dawsh 2y ago total=crud.get_users_count(db, admin=admin if not admin.is_sudo else None),
33 e5dcd82 Dawsh 2y ago active=crud.get_users_count(
34 e5dcd82 Dawsh 2y ago db, admin=admin if not admin.is_sudo else None, status=UserStatus.active
35 e5dcd82 Dawsh 2y ago ),
36 e5dcd82 Dawsh 2y ago on_hold=crud.get_users_count(
37 e5dcd82 Dawsh 2y ago db,
38 e5dcd82 Dawsh 2y ago admin=admin if not admin.is_sudo else None,
39 e5dcd82 Dawsh 2y ago status=UserStatus.on_hold,
40 e5dcd82 Dawsh 2y ago ),
41 e5dcd82 Dawsh 2y ago expired=crud.get_users_count(
42 e5dcd82 Dawsh 2y ago db,
43 e5dcd82 Dawsh 2y ago admin=admin if not admin.is_sudo else None,
44 e5dcd82 Dawsh 2y ago status=UserStatus.expired,
45 e5dcd82 Dawsh 2y ago ),
46 e5dcd82 Dawsh 2y ago limited=crud.get_users_count(
47 e5dcd82 Dawsh 2y ago db,
48 e5dcd82 Dawsh 2y ago admin=admin if not admin.is_sudo else None,
49 e5dcd82 Dawsh 2y ago status=UserStatus.limited,
50 e5dcd82 Dawsh 2y ago ),
51 e5dcd82 Dawsh 2y ago online=crud.get_users_count(
52 e5dcd82 Dawsh 2y ago db, admin=admin if not admin.is_sudo else None, online=True
53 e5dcd82 Dawsh 2y ago ),
54 e5dcd82 Dawsh 2y ago recent_subscription_updates=list(
55 e5dcd82 Dawsh 2y ago i[0]
56 e5dcd82 Dawsh 2y ago for i in db.query(User.username)
57 e5dcd82 Dawsh 2y ago .filter(User.sub_updated_at != None)
58 e5dcd82 Dawsh 2y ago .order_by(User.sub_updated_at)
59 e5dcd82 Dawsh 2y ago .limit(5)
60 e5dcd82 Dawsh 2y ago ),
61 a47eb4c Dawsh 2y ago )