gitscope
https://dev.kronoz.win/r/marzneshin.git
app/routes/system.py · v0.3.0-alpha11
Source
1 d4ec019 Dawsh 2y ago from typing import Union
2 a47eb4c Dawsh 2y ago
3 f7af2b3 Dawsh 2y ago from fastapi import APIRouter
4 f7af2b3 Dawsh 2y ago
5 f7af2b3 Dawsh 2y ago from app import __version__
6 c13a1aa Dawsh 2y ago from app.db import crud
7 d4ec019 Dawsh 2y ago from app.dependencies import DBDep, AdminDep
8 a47eb4c Dawsh 2y ago from app.models.admin import Admin
9 a47eb4c Dawsh 2y ago from app.models.system import SystemStats
10 a47eb4c Dawsh 2y ago from app.models.user import UserStatus
11 a47eb4c Dawsh 2y ago from app.utils.system import memory_usage, cpu_usage, realtime_bandwidth
12 f7af2b3 Dawsh 2y ago
13 f7af2b3 Dawsh 2y ago router = APIRouter(tags=["System"])
14 a47eb4c Dawsh 2y ago
15 a47eb4c Dawsh 2y ago
16 2de9d72 Dawsh 2y ago @router.get("/system", response_model=SystemStats)
17 c13a1aa Dawsh 2y ago def get_system_stats(db: DBDep, admin: AdminDep):
18 a47eb4c Dawsh 2y ago mem = memory_usage()
19 a47eb4c Dawsh 2y ago cpu = cpu_usage()
20 a47eb4c Dawsh 2y ago system = crud.get_system_usage(db)
21 a47eb4c Dawsh 2y ago dbadmin: Union[Admin, None] = crud.get_admin(db, admin.username)
22 a47eb4c Dawsh 2y ago
23 a47eb4c Dawsh 2y ago total_user = crud.get_users_count(db, admin=dbadmin if not admin.is_sudo else None)
24 01cdf2e Dawsh 2y ago users_active = crud.get_users_count(
25 01cdf2e Dawsh 2y ago db, status=UserStatus.active, admin=dbadmin if not admin.is_sudo else None
26 01cdf2e Dawsh 2y ago )
27 a47eb4c Dawsh 2y ago realtime_bandwidth_stats = realtime_bandwidth()
28 a47eb4c Dawsh 2y ago
29 a47eb4c Dawsh 2y ago return SystemStats(
30 a47eb4c Dawsh 2y ago version=__version__,
31 a47eb4c Dawsh 2y ago mem_total=mem.total,
32 a47eb4c Dawsh 2y ago mem_used=mem.used,
33 a47eb4c Dawsh 2y ago cpu_cores=cpu.cores,
34 a47eb4c Dawsh 2y ago cpu_usage=cpu.percent,
35 a47eb4c Dawsh 2y ago total_user=total_user,
36 a47eb4c Dawsh 2y ago users_active=users_active,
37 a47eb4c Dawsh 2y ago incoming_bandwidth=system.uplink,
38 a47eb4c Dawsh 2y ago outgoing_bandwidth=system.downlink,
39 a47eb4c Dawsh 2y ago incoming_bandwidth_speed=realtime_bandwidth_stats.incoming_bytes,
40 a47eb4c Dawsh 2y ago outgoing_bandwidth_speed=realtime_bandwidth_stats.outgoing_bytes,
41 a47eb4c Dawsh 2y ago )