Skip to content

Commit

Permalink
Add improved fetch_active query
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekuruu committed Oct 16, 2023
1 parent 645644f commit 21ff014
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion database/repositories/users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

from app.common.database.objects import DBUser, DBStats
from sqlalchemy import func, or_, and_
from datetime import datetime, timedelta
from typing import Optional, List
from sqlalchemy import func, or_

import app

Expand Down Expand Up @@ -72,6 +73,20 @@ def fetch_all(restricted: bool = False) -> List[DBUser]:
.filter(DBUser.restricted == restricted) \
.all()

def fetch_active(delta: timedelta = timedelta(days=30)) -> List[DBUser]:
return app.session.database.session.query(DBUser) \
.join(DBStats) \
.filter(DBUser.restricted == False) \
.filter(DBStats.playcount > 0) \
.filter(
# Remove inactive users from query, if they are not in the top 100
or_(
DBUser.latest_activity >= (datetime.now() - delta),
DBStats.rank >= 100
)
) \
.all()

def fetch_by_discord_id(id: int) -> Optional[DBUser]:
return app.session.database.session.query(DBUser) \
.filter(DBUser.discord_id == id) \
Expand Down

0 comments on commit 21ff014

Please sign in to comment.