Modificate funzioni per il recupero dei dati

This commit is contained in:
Francesco Esposito 2020-04-07 20:11:00 +02:00
parent b430292d21
commit a9d402ab7e
3 changed files with 6 additions and 47 deletions

10
package-lock.json generated
View File

@ -4029,11 +4029,6 @@
"resolved": "https://registry.npmjs.org/csstype/-/csstype-2.6.9.tgz",
"integrity": "sha512-xz39Sb4+OaTsULgUERcCk+TJj8ylkL4aSVDQiX/ksxbELSqwkgt4d4RD7fovIdgJGSuNYqwZEiVjYY5l0ask+Q=="
},
"csvjson-csv2json": {
"version": "5.0.6",
"resolved": "https://registry.npmjs.org/csvjson-csv2json/-/csvjson-csv2json-5.0.6.tgz",
"integrity": "sha512-to9QlVI4yQFPzSqi6XZ97lULKs5m3K8hlE/UkbvKSw81FfDwgV9MUtSbHFJ4SRT4IA7UPkuW2EqsH/MWN5YRow=="
},
"cyclist": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/cyclist/-/cyclist-1.0.1.tgz",
@ -4083,11 +4078,6 @@
}
}
},
"date-fns": {
"version": "2.11.0",
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.11.0.tgz",
"integrity": "sha512-8P1cDi8ebZyDxUyUprBXwidoEtiQAawYPGvpfb+Dg0G6JrQ+VozwOmm91xYC0vAv1+0VmLehEPb+isg4BGUFfA=="
},
"debug": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",

View File

@ -11,8 +11,6 @@
"@testing-library/user-event": "^7.2.1",
"axios": "^0.19.2",
"bootstrap": "^4.4.1",
"csvjson-csv2json": "^5.0.6",
"date-fns": "^2.11.0",
"prop-types": "^15.7.2",
"react": "^16.13.0",
"react-bootstrap": "^1.0.0-beta.17",

View File

@ -1,13 +1,8 @@
// import axios from 'axios';
import axios from 'axios';
import csv2json from 'csvjson-csv2json';
import { format, addDays, getHours } from 'date-fns';
const getCSVRegionifromRepository = async (date) => {
const url = 'https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/';
const filename = `dpc-covid19-ita-regioni-${format(date, 'yyyyMMdd')}.csv`;
const getJsonRegioni = async () => {
const url = 'https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/';
const filename = 'dpc-covid19-ita-regioni-latest.json';
const finalURL = `${url + filename}`;
let result;
@ -22,35 +17,11 @@ const getCSVRegionifromRepository = async (date) => {
};
const convertCSVtoJSON = (csv) => {
if (!csv) {
return null;
}
return csv2json(csv, { parseNumbers: true });
};
const getData = async () => {
const actualDate = new Date();
const previousDate = addDays(actualDate, -1);
const hour = getHours(actualDate);
let result;
const nomeRegione = 'Campania';
// preleva il CSV con data attuale solamente se sono passate le 18
if (hour >= 18) {
result = await getCSVRegionifromRepository(actualDate);
}
// preleva il CSV con data precedente qualora non fosse stato trovato quello attuale
if (!result) {
result = await getCSVRegionifromRepository(previousDate);
}
// converte il CSV in JSON e filtra i dati per la regione Campania
if (result) {
const tmpJSON = convertCSVtoJSON(result);
result = tmpJSON.find((d) => d.denominazione_regione === 'Campania');
}
const datiRegione = await getJsonRegioni();
const result = datiRegione.find((regione) => regione.denominazione_regione === nomeRegione);
return result;
};