Skip to content

Commit

Permalink
refactor: use sqlalchemy instead of raw query
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsoheil committed Feb 21, 2024
1 parent 31f5fb1 commit 40b1bda
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
19 changes: 16 additions & 3 deletions app/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

from sqlalchemy import (JSON, BigInteger, Boolean, Column, DateTime, Enum,
Float, ForeignKey, Integer, String, Table,
UniqueConstraint)
UniqueConstraint, func)
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import relationship
from sqlalchemy.sql.expression import text
from sqlalchemy.sql.expression import text, select

from app import xray
from app.db.base import Base
Expand Down Expand Up @@ -46,7 +47,7 @@ class User(Base):
nullable=False,
default=UserDataLimitResetStrategy.no_reset,
)
usage_logs = relationship("UserUsageResetLogs", back_populates="user")
usage_logs = relationship("UserUsageResetLogs", back_populates="user") # maybe rename it to reset_usage_logs?
expire = Column(Integer, nullable=True)
admin_id = Column(Integer, ForeignKey("admins.id"))
admin = relationship("Admin", back_populates="users")
Expand All @@ -60,6 +61,18 @@ class User(Base):
on_hold_timeout = Column(DateTime, nullable=True, default=None)
edit_at = Column(DateTime, nullable=True, default=None)

@hybrid_property
def reseted_usage(self):
return sum([log.used_traffic_at_reset for log in self.usage_logs])

@reseted_usage.expression
def reseted_usage(self):
return (
select([func.sum(UserUsageResetLogs.used_traffic_at_reset)]).
where(UserUsageResetLogs.user_id == self.id).
label('reseted_usage')
)

@property
def lifetime_used_traffic(self):
return (
Expand Down
8 changes: 5 additions & 3 deletions cli/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rich.console import Console
from rich.panel import Panel
from sqlalchemy.exc import IntegrityError
from sqlalchemy.sql import text
from sqlalchemy import func
from decouple import config, UndefinedValueError

from app.db import GetDB
Expand Down Expand Up @@ -37,12 +37,14 @@ def validate_discord_webhook(value: str) -> Union[str, None]:

def calculate_admin_usage(admin_id: int) -> int:
with GetDB() as db:
return db.execute(text(f'SELECT SUM(node_user_usages.used_traffic) / 1024 / 1024 / 1024 FROM node_user_usages JOIN users ON users.id = node_user_usages.user_id AND users.admin_id = {admin_id};')).one()[0]
usage = db.query(func.sum(User.used_traffic)).filter_by(admin_id=admin_id).first()[0]
return 0 if not usage else int(usage / 1024 / 1024 / 1024) # to GB


def calculate_admin_reseted_usage(admin_id: int) -> int:
with GetDB() as db:
return db.execute(text(f'SELECT SUM(used_traffic_at_reset) / 1024 / 1024 / 1024 FROM user_usage_logs JOIN users ON users.id = user_usage_logs.user_id AND users.admin_id = {admin_id};')).one()[0]
usage = db.query(func.sum(User.reseted_usage)).filter_by(admin_id=admin_id).first()[0]
return 0 if not usage else int(usage / 1024 / 1024 / 1024) # to GB


@app.command(name="list")
Expand Down

0 comments on commit 40b1bda

Please sign in to comment.