Commit 22a43b4c authored by Florent Chehab's avatar Florent Chehab Committed by Maxime Emschwiller
Browse files

feature(backend): add functions to compute daily stats

parent d113daee
from datetime import timedelta
from stats_app.models import DailyConnections
from stats_app.utils import get_daily_connections, get_today_as_datetime
def update_daily_connections():
yesterday = get_today_as_datetime() - timedelta(days=1)
DailyConnections.objects.update_or_create(
date=yesterday, defaults=dict(nb_connections=get_daily_connections())
)
def update_all_stats():
update_daily_connections()
from datetime import datetime, timedelta
from django.utils.timezone import make_aware
from base_app.models import User
def get_today_as_datetime():
now = datetime.now()
today = now.replace(hour=0, minute=0, second=0, microsecond=1)
return make_aware(today)
def get_daily_connections() -> int:
today = get_today_as_datetime()
yesterday = today - timedelta(days=1)
nb_connections = User.objects.filter(
last_login__gte=yesterday, last_login__lt=today
).count()
return nb_connections
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment