Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Add counter for all user status to /system #1445

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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