Skip to content

Commit

Permalink
feat(api): Add counter for all user status to /system (#1445)
Browse files Browse the repository at this point in the history
* feat(api): Add counter for all user status to `/system`

* feat(api): Add online users in last 24h to `/system`

* use utc time and make online counter dynamic
  • Loading branch information
ImMohammad20000 authored Nov 22, 2024
1 parent e599916 commit f5a0c25
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/db/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from enum import Enum
from typing import Dict, List, Optional, Tuple, Union

from sqlalchemy import and_, delete, or_
from sqlalchemy import and_, delete, func, or_
from sqlalchemy.orm import Query, Session, joinedload
from sqlalchemy.sql.functions import coalesce

Expand Down Expand Up @@ -1480,3 +1480,10 @@ def delete_notification_reminder(db: Session, dbreminder: NotificationReminder)
db.delete(dbreminder)
db.commit()
return


def count_online_users(db: Session, hours: int = 24):
twenty_four_hours_ago = datetime.utcnow() - timedelta(hours=hours)
query = db.query(func.count(User.id)).filter(User.online_at.isnot(
None), func.datetime(User.online_at) >= twenty_four_hours_ago)
return query.scalar()
5 changes: 5 additions & 0 deletions app/models/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ class SystemStats(BaseModel):
cpu_cores: int
cpu_usage: float
total_user: int
online_at_last_24H: int
users_active: int
users_on_hold: int
users_disabled: int
users_expired: int
users_limited: int
incoming_bandwidth: int
outgoing_bandwidth: int
incoming_bandwidth_speed: int
Expand Down
18 changes: 18 additions & 0 deletions app/routers/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ def get_system_stats(
users_active = crud.get_users_count(
db, status=UserStatus.active, admin=dbadmin if not admin.is_sudo else None
)
users_disabled = crud.get_users_count(
db, status=UserStatus.disabled, admin=dbadmin if not admin.is_sudo else None
)
users_on_hold = crud.get_users_count(
db, status=UserStatus.on_hold, admin=dbadmin if not admin.is_sudo else None
)
users_expired = crud.get_users_count(
db, status=UserStatus.expired, admin=dbadmin if not admin.is_sudo else None
)
users_limited = crud.get_users_count(
db, status=UserStatus.limited, admin=dbadmin if not admin.is_sudo else None
)
online_users = crud.count_online_users(db, 24)
realtime_bandwidth_stats = realtime_bandwidth()

return SystemStats(
Expand All @@ -37,7 +50,12 @@ def get_system_stats(
cpu_cores=cpu.cores,
cpu_usage=cpu.percent,
total_user=total_user,
online_at_last_24H=online_users,
users_active=users_active,
users_disabled=users_disabled,
users_expired=users_expired,
users_limited=users_limited,
users_on_hold=users_on_hold,
incoming_bandwidth=system.uplink,
outgoing_bandwidth=system.downlink,
incoming_bandwidth_speed=realtime_bandwidth_stats.incoming_bytes,
Expand Down

0 comments on commit f5a0c25

Please sign in to comment.