feat(stats): handle dates in backend

parent f0a69b49
......@@ -54,13 +54,39 @@ def stats(request):
Render the view that displays stats
"""
dataset = request.GET.get("dataset")
date_min = request.GET.get("date_min")
date_max = request.GET.get("date_max")
now = make_aware(datetime.now())
now_minus_365_days = now - timedelta(days=365)
get_params_are_valid = True
if (dataset != "daily_connections") and (dataset != "daily_exchange_contributions"):
get_params_are_valid = False
# set default dataset
dataset = "daily_connections"
try:
date_min_parsed = datetime.strptime(date_min, "%Y-%m-%d")
date_max_parsed = datetime.strptime(date_max, "%Y-%m-%d")
if date_min < date_max:
date_max_parsed, date_min_parsed = date_max_parsed, date_min_parsed
except: # noqa: E722
get_params_are_valid = False
now = make_aware(datetime.now())
date_min = (now - timedelta(days=365)).strftime("%Y-%m-%d")
date_max = now.strftime("%Y-%m-%d")
if not get_params_are_valid:
url = f"/stats/?dataset={dataset}&date_min={date_min}&date_max={date_max}"
request_info = request.GET.get("request_info")
if request_info:
url = f"{url}&request_info={request_info}"
return HttpResponseRedirect(url)
if dataset == "daily_connections":
daily_connections = DailyConnections.objects.filter(
date__gte=now_minus_365_days, date__lt=now
date__gte=date_min_parsed, date__lt=date_max_parsed
)
raw_data = [
{"date": dc.date.strftime("%Y-%m-%d"), "nb_connections": dc.nb_connections}
......@@ -71,7 +97,7 @@ def stats(request):
elif dataset == "daily_exchange_contributions":
daily_contributions = DailyExchangeContributionsInfo.objects.filter(
date__gte=now_minus_365_days, date__lt=now
date__gte=date_min_parsed, date__lt=date_max_parsed
).prefetch_related("university")
raw_data = [
......@@ -96,9 +122,8 @@ def stats(request):
"exchange_semester",
"nb_contributions",
]
else:
return HttpResponseRedirect("/stats/?dataset=daily_connections")
raise NotImplementedError()
stats_data = {c: [d[c] for d in raw_data] for c in cols}
return render(request, "stats.html", dict(stats_data=json.dumps(stats_data)))
......
Markdown is supported
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