feat(statsPage): more utils and awesome requests

parent 23a09e1a
import alasql from "alasql";
import { getGetParamInUrl, setGetParamInUrl } from "../../utils/url";
import {
getUpdatedMajor,
getUpdatedMinor,
} from "../../utils/majorMinorMappings";
export const currentDatasetName = getGetParamInUrl("dataset");
/**
* Update the selected dataset
* @param newDatasetName {string}
*/
export function changeDataset(newDatasetName) {
if (newDatasetName !== currentDatasetName) {
setGetParamInUrl("dataset", newDatasetName, "reload");
}
}
export const availableDatasets = [
{
name: "daily_connections",
label: "Nombre de connexions",
info:
"Attention, les données antérieures au 15 juin 2020 ne sont qu'une estimation basse de la réalité.",
columns: [
{ name: "date", description: "Date de la statistique" },
{
......@@ -23,6 +20,42 @@ export const availableDatasets = [
description: "Nombre de connexions journalières",
},
],
exampleRequests: [
{
label: "Sélectionner toutes les données",
request: `SELECT
*
FROM ?
ORDER BY date ASC;`,
},
{
label: "Afficher le nombre de connections journalières",
graphTitle: "Nombre de connections journalières",
request: `SELECT
date AS x,
nb_connections AS y
FROM ?
ORDER BY date ASC;`,
default: true,
},
{
label: "Afficher le nombre de connections mensuelles",
graphTitle: "Nombre de connections mensuelles",
request: `WITH
data AS (
SELECT
SUBSTRING(date, 1, 7) AS month,
nb_connections
FROM ?
)
SELECT
month AS x,
sum(nb_connections) AS y
FROM data
GROUP BY month
ORDER BY x ASC;`,
},
],
},
{
name: "daily_exchange_contributions",
......@@ -49,6 +82,86 @@ export const availableDatasets = [
description: "Nombre de contributions journalières correspondant",
},
],
exampleRequests: [
{
label: "Sélectionner toutes les données",
request: `SELECT
*
FROM ?
ORDER BY date ASC;`,
},
{
label:
"Afficher le nombre contributions mensuelles (concernant l'échange et non les cours) par branche",
request: `WITH
data AS (
SELECT
SUBSTRING(date, 1, 7) AS month,
major,
nb_contributions
FROM ?
WHERE
type = 'exchange_feedback'
)
SELECT
month AS x,
major AS cat,
sum(nb_contributions) AS y
FROM data
GROUP BY
month, major
ORDER BY
x ASC, cat ASC;`,
default: true,
},
{
label: "Afficher le nombre de contributions par type",
request: `SELECT
type,
sum(nb_contributions) AS nb_contributions
FROM ?
GROUP BY type
ORDER BY type ASC;`,
},
{
label:
"Afficher le nombre de contributions (concernant l'échange et non les cours) par branche",
request: `SELECT
major,
sum(nb_contributions) AS nb_contributions
FROM ?
WHERE
type = 'exchange_feedback'
GROUP BY major
ORDER BY major ASC;`,
},
{
label:
"Afficher le nombre de contributions (concernant l'échange et non les cours) par université pour la branche IM",
request: `SELECT
university,
sum(nb_contributions) AS nb_contributions
FROM ?
WHERE
type = 'exchange_feedback'
AND major = 'IM'
GROUP BY university
ORDER BY nb_contributions DESC;`,
},
{
label:
"Afficher le nombre de contributions (concernant l'échange et non les cours) par filières pour la branche IM",
request: `SELECT
minor,
sum(nb_contributions) AS nb_contributions
FROM ?
WHERE
type = 'exchange_feedback'
AND major = 'IM'
GROUP BY minor
ORDER BY nb_contributions DESC;`,
},
],
},
];
......@@ -57,7 +170,118 @@ export const availableDatasets = [
* @param datasetName {string}
* @returns {[{name: string, description: string}]}
*/
export function getDatasetColumns(datasetName) {
export function getDatasetColumns(datasetName = currentDatasetName) {
return availableDatasets.find((dataset) => dataset.name === datasetName)
.columns;
}
/**
* Get the example requests of a dataset
*
* @param datasetName
* @returns {[{request: string, label: string, default: boolean}]}
*/
export function getDatasetExampleRequests(datasetName = currentDatasetName) {
return availableDatasets.find((dataset) => dataset.name === datasetName)
.exampleRequests;
}
/**
* Get the default request for a dataset
* @param datasetName {string}
* @returns {string}
*/
export function getDefaultRequest(datasetName = currentDatasetName) {
const exampleRequests = getDatasetExampleRequests(datasetName);
return exampleRequests.filter((el) => el.default === true)[0].request;
}
/**
* Get the dataset data from the HTML.
*
* @returns {[{}]}
*/
function getDatasetData() {
// Reconstruct the dataset from the backend
// eslint-disable-next-line no-undef
const datasetDataFromBackend = __StatsData;
const cols = Object.keys(datasetDataFromBackend);
let data = [];
// eslint-disable-next-line no-plusplus
for (let i = 0; i < datasetDataFromBackend[cols[0]].length; i++) {
const elem = {};
cols.forEach((col) => {
elem[col] = datasetDataFromBackend[col][i];
});
data.push(elem);
}
// need to remap major and minors
if (currentDatasetName === "daily_exchange_contributions") {
data = data.map((el) => {
const newMajor = getUpdatedMajor(el.major);
const newMinor = getUpdatedMinor(el.major, el.minor);
// eslint-disable-next-line no-param-reassign
el.major = newMajor;
// eslint-disable-next-line no-param-reassign
el.minor = newMinor;
return el;
});
getUpdatedMajor();
}
return data;
}
export const currentDatasetData = getDatasetData();
/**
* Get the request form the url. If no request is found, returns the default request.
* @returns {string}
*/
export function getRequestFromUrl() {
let request;
try {
const requestInfoString = getGetParamInUrl("request_info");
request = JSON.parse(atob(requestInfoString)).request;
} catch (err) {
// eslint-disable-next-line no-console
console.log(err);
}
if (typeof request !== "string" || request === "") {
return getDefaultRequest(currentDatasetName);
}
return request;
}
// eslint-disable-next-line no-shadow
export function executeSqlRequest(sqlRequest) {
return alasql.promise(sqlRequest, [currentDatasetData]);
}
/**
*
*/
export function setRequestInUrl(request) {
const requestInfo = {
request,
version: 1.0,
};
const requestInfoAsString = btoa(JSON.stringify(requestInfo));
setGetParamInUrl("request_info", requestInfoAsString, "push");
}
/**
* Update the selected dataset
* @param newDatasetName {string}
*/
export function changeDataset(newDatasetName) {
if (newDatasetName !== currentDatasetName) {
const defaultRequest = getDefaultRequest(newDatasetName);
setRequestInUrl(defaultRequest);
setGetParamInUrl("dataset", newDatasetName, "reload");
}
}
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