Unverified Commit 678fc7aa authored by Estelle Veisemburger's avatar Estelle Veisemburger Committed by Florent Chehab
Browse files

feat(backend): link database

parent 122f9e36
from datetime import datetime, timedelta
import logging
import json
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
from django.shortcuts import render
from django.utils.timezone import make_aware
from webpack_loader.utils import get_files
from backend_app.utils import clean_route
......@@ -16,6 +18,7 @@ from _cron_tasks import (
clear_and_clean_sessions,
update_daily_stats,
)
from stats_app.models import DailyConnections, DailyExchangeContributionsInfo
logger = logging.getLogger("django")
......@@ -52,195 +55,40 @@ def stats(request):
"""
dataset = request.GET.get("dataset")
if dataset == "1":
now = make_aware(datetime.now())
now_minus_365_days = now - timedelta(days=365)
if dataset == "daily_connections":
daily_connections = DailyConnections.objects.filter(
date__gte=now_minus_365_days,
date__lt=now,
)
raw_data = [
{"date": "2020-06-06", "nb_connections": 5},
{"date": "2020-06-05", "nb_connections": 25},
{"date": "2020-06-04", "nb_connections": 1},
{"date": "2020-06-03", "nb_connections": 1},
{"date": "2020-06-02", "nb_connections": 4},
{"date": "2020-06-01", "nb_connections": 1},
{"date": "2020-05-31", "nb_connections": 0},
{"date": "2020-05-30", "nb_connections": 2},
{"date": "2020-05-29", "nb_connections": 1},
{"date": "2020-05-28", "nb_connections": 0},
{"date": "2020-05-27", "nb_connections": 1},
{"date": "2020-05-26", "nb_connections": 0},
{"date": "2020-05-25", "nb_connections": 0},
{"date": "2020-05-25", "nb_connections": 0},
{"date": "2020-05-24", "nb_connections": 3},
{"date": "2020-05-23", "nb_connections": 2},
{"date": "2020-05-22", "nb_connections": 2},
{"date": "2020-05-22", "nb_connections": 2},
{"date": "2020-05-21", "nb_connections": 2},
{"date": "2020-05-20", "nb_connections": 1},
{
"date": dc.date.strftime("%Y-%m-%d"),
"nb_connections": dc.nb_connections
}
for dc in daily_connections
]
cols = ["date", "nb_connections"]
stats_data = {c: [d[c] for d in raw_data] for c in cols}
elif dataset == "2":
elif dataset == "daily_exchange_contributions":
daily_contributions = DailyExchangeContributionsInfos.objects.filter(
date__gte=now_minus_365_days,
date__lt=now,
).prefetch_related("university")
raw_data = [
{
"date": "2020-06-04",
"university": "337 - Universidad Autónoma de Aguascalientes",
"major": "GU",
"minor": "SR",
"exchange_semester": "A2019",
"nb_contributions": 1,
},
{
"date": "2020-05-30",
"university": "26 - Cranfield University",
"major": "IM",
"minor": "MARS",
"exchange_semester": "A2019",
"nb_contributions": 1,
},
{
"date": "2020-05-24",
"university": "343 - Universidad Pontificia Comillas",
"major": "TC",
"minor": "",
"exchange_semester": "A2018",
"nb_contributions": 1,
},
{
"date": "2020-04-26",
"university": "344 - Technical University of Košice",
"major": "TC",
"minor": "",
"exchange_semester": "P2020",
"nb_contributions": 1,
},
{
"date": "2020-04-10",
"university": "221 - École nationale supérieure des mines de Rabat",
"major": "IM",
"minor": "PIL",
"exchange_semester": "A2019",
"nb_contributions": 7,
},
{
"date": "2020-03-24",
"university": "51 - University of Glasgow",
"major": "IM",
"minor": "CMI",
"exchange_semester": "A2019",
"nb_contributions": 6,
},
{
"date": "2020-03-22",
"university": "53 - Technische Universität Braunschweig",
"major": "IM",
"minor": "CMI",
"exchange_semester": "A2019",
"nb_contributions": 4,
},
{
"date": "2020-03-14",
"university": "53 - Technische Universität Braunschweig",
"major": "TC",
"minor": "",
"exchange_semester": "A2017",
"nb_contributions": 7,
},
{
"date": "2020-03-14",
"university": "39 - Université du Québec à Chicoutimi",
"major": "IM",
"minor": "CMI",
"exchange_semester": "A2019",
"nb_contributions": 3,
},
{
"date": "2020-03-11",
"university": "405 - National Chiao Tung University",
"major": "IM",
"minor": "CMI",
"exchange_semester": "A2019",
"nb_contributions": 6,
},
{
"date": "2020-03-11",
"university": "200 - University of Science and Technology",
"major": "IM",
"minor": "MARS",
"exchange_semester": "A2019",
"nb_contributions": 3,
},
{
"date": "2020-03-11",
"university": "170 - Silesian University of Technology",
"major": "IM",
"minor": "CMI",
"exchange_semester": "A2019",
"nb_contributions": 1,
},
{
"date": "2020-03-11",
"university": "71 - Polytechnique Montréal",
"major": "IM",
"minor": "MARS",
"exchange_semester": "A2019",
"nb_contributions": 5,
},
{
"date": "2020-03-10",
"university": "375 - Politechnika Wrocławska",
"major": "IM",
"minor": "CMI",
"exchange_semester": "A2019",
"nb_contributions": 5,
},
{
"date": "2020-02-19",
"university": "376 - Warsaw University of Life Sciences",
"major": "GB",
"minor": "IAA",
"exchange_semester": "P2019",
"nb_contributions": 1,
},
{
"date": "2020-02-12",
"university": "301 - Université de technologie sino-européenne de l'université de Shanghai",
"major": "HuTech",
"minor": "Spe-GI",
"exchange_semester": "P2019",
"nb_contributions": 1,
},
{
"date": "2020-02-10",
"university": "57 - Technische Universität Graz",
"major": "TC",
"minor": "",
"exchange_semester": "P2018",
"nb_contributions": 9,
},
{
"date": "2020-02-05",
"university": "71 - Polytechnique Montréal",
"major": "IM",
"minor": "SIM",
"exchange_semester": "A2019",
"nb_contributions": 2,
},
{
"date": "2020-02-02",
"university": "51 - University of Glasgow",
"major": "IM",
"minor": "CMI",
"exchange_semester": "A2019",
"nb_contributions": 6,
},
{
"date": "2020-01-31",
"university": "375 - Politechnika Wrocławska",
"major": "IM",
"minor": "CMI",
"exchange_semester": "A2019",
"nb_contributions": 1,
},
"date": dc.date.strftime("%Y-%m-%d"),
"university": f"{dc.university.pk} - {dc.university.name}",
"major": dc.major,
"minor": dc.minor,
"exchange_semester": dc.exchange_semester,
"nb_contributions": dc.nb_contributions,
}
for dc in daily_contributions
]
cols = [
......@@ -252,11 +100,10 @@ def stats(request):
"nb_contributions",
]
stats_data = {c: [d[c] for d in raw_data] for c in cols}
else:
return HttpResponseRedirect("/stats/?dataset=1")
return HttpResponseRedirect("/stats/?dataset=daily_connections")
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)))
......
......@@ -51,7 +51,7 @@ const defaultRequest = `SELECT
nb_connections AS y
FROM ?
ORDER BY x ASC;
`;
`; //à changer selon le dataset
function getRequestFromUrl() {
const getParamsInUrl = new URL(document.location).searchParams;
......@@ -85,6 +85,28 @@ function setRequestInUrl(request) {
window.history.replaceState(null, null, `?${getParamsInUrl.toString()}`);
}
function getDatasetFromUrl() {
const getParamsInUrl = new URL(document.location).searchParams;
let datasetName = "daily_connections";
try {
datasetName = getParamsInUrl.has("dataset")
? getParamsInUrl.get("dataset")
: "daily_connections";
} catch (err) {
console.log(err);
}
return datasetName;
}
function setDatasetInUrl(datasetName) {
const getParamsInUrl = new URL(document.location).searchParams;
getParamsInUrl.set("dataset", datasetName);
window.history.replaceState(null, null, `?${getParamsInUrl.toString()}`);
}
const requestFromUrl = getRequestFromUrl();
function SqlInterface() {
......
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