Skip to content

Commit

Permalink
feat(admin): admin usage log (#1448)
Browse files Browse the repository at this point in the history
* admin usage log

* users_usage in admin model
  • Loading branch information
MuhammadAshouri authored Nov 25, 2024
1 parent 4b49352 commit e157895
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
9 changes: 9 additions & 0 deletions app/db/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
JWT,
TLS,
Admin,
AdminUsageLogs,
NextPlan,
Node,
NodeUsage,
Expand Down Expand Up @@ -1069,6 +1070,14 @@ def reset_admin_usage(db: Session, dbadmin: Admin) -> int:
Returns:
Admin: The updated admin.
"""
if (dbadmin.users_usage == 0):
return dbadmin

usage_log = AdminUsageLogs(
admin=dbadmin,
used_traffic_at_reset=dbadmin.users_usage
)
db.add(usage_log)
dbadmin.users_usage = 0

db.commit()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""UsageLog table for Admin
Revision ID: d0a3960f5dad
Revises: b25e7e6be241
Create Date: 2024-11-22 19:56:53.185624
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'd0a3960f5dad'
down_revision = 'b25e7e6be241'
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('admin_usage_logs',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('admin_id', sa.Integer(), nullable=True),
sa.Column('used_traffic_at_reset', sa.BigInteger(), nullable=False),
sa.Column('reset_at', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['admin_id'], ['admins.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('admin_usage_logs')
# ### end Alembic commands ###
11 changes: 11 additions & 0 deletions app/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ class Admin(Base):
telegram_id = Column(BigInteger, nullable=True, default=None)
discord_webhook = Column(String(1024), nullable=True, default=None)
users_usage = Column(BigInteger, nullable=False, default=0)
usage_logs = relationship("AdminUsageLogs", back_populates="admin")


class AdminUsageLogs(Base):
__tablename__ = "admin_usage_logs"

id = Column(Integer, primary_key=True)
admin_id = Column(Integer, ForeignKey("admins.id"))
admin = relationship("Admin", back_populates="usage_logs")
used_traffic_at_reset = Column(BigInteger, nullable=False)
reset_at = Column(DateTime, default=datetime.utcnow)


class User(Base):
Expand Down
3 changes: 2 additions & 1 deletion app/models/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Admin(BaseModel):
is_sudo: bool
telegram_id: Optional[int]
discord_webhook: Optional[str]
users_usage: int

class Config:
orm_mode = True
Expand Down Expand Up @@ -127,4 +128,4 @@ def verify_password(self, plain_password):

class AdminValidationResult(BaseModel):
username: str
is_sudo: bool
is_sudo: bool

0 comments on commit e157895

Please sign in to comment.