Implementazione lettura da CSV

This commit is contained in:
Francesco Esposito 2020-03-22 20:01:53 +01:00
parent a1aab491f0
commit 8035149413
28 changed files with 89 additions and 16 deletions

4
.editorconfig Executable file
View File

@ -0,0 +1,4 @@
root = true
[*.js]
indent_size = 2

0
.eslintrc.js Normal file → Executable file
View File

0
.gitignore vendored Normal file → Executable file
View File

4
README.md Normal file → Executable file
View File

@ -8,9 +8,7 @@ https://franjsco.github.io/covid19-campania/
## Provenienza dei dati
I dati nella dashbord sono prelevati da un servizio web (REST API) messo a disposizione da OpenPuglia.
Il [servizio web di OpenPuglia](https://github.com/ondata/covid19italia) è agganciato ai dati ufficiali pubblicati sul [repository della Protezione Civile](https://github.com/pcm-dpc/COVID-19) come indicato dal loro repository.
I dati nella dashbord sono prelevati direttamente dal [repository ufficiale della Protezione Civile](https://github.com/pcm-dpc/COVID-19).
## License

10
package-lock.json generated Normal file → Executable file
View File

@ -4029,6 +4029,11 @@
"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",
@ -4078,6 +4083,11 @@
}
}
},
"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",

2
package.json Normal file → Executable file
View File

@ -11,6 +11,8 @@
"@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",

0
public/favicon.ico Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

0
public/index.html Normal file → Executable file
View File

0
public/logo192.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 5.2 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

0
public/manifest.json Normal file → Executable file
View File

0
public/robots.txt Normal file → Executable file
View File

0
screenshot.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 88 KiB

After

Width:  |  Height:  |  Size: 88 KiB

0
src/App.css Normal file → Executable file
View File

0
src/App.js Normal file → Executable file
View File

0
src/App.test.js Normal file → Executable file
View File

0
src/LICENSE Normal file → Executable file
View File

58
src/api/DataFromRepository.js Executable file
View File

@ -0,0 +1,58 @@
// 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 finalURL = `${url + filename}`;
let result;
try {
const res = await axios.get(finalURL);
result = res.data;
} catch (error) {
result = null;
}
return result;
};
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;
// 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');
}
return result;
};
export default getData;

0
src/components/Card.js Normal file → Executable file
View File

0
src/components/Footer.js Normal file → Executable file
View File

0
src/components/Headbar.js Normal file → Executable file
View File

2
src/components/Infobox.js Normal file → Executable file
View File

@ -7,7 +7,7 @@ const Infobox = (props) => {
return (
<div>
<span className="font-weight-bold">Ultimo aggiornamento: </span>
{ dataAggiornamento }
{ dataAggiornamento.split(' ')[0] }
</div>
);
};

0
src/components/Layout.js Normal file → Executable file
View File

25
src/containers/Dashboard.js Normal file → Executable file
View File

@ -1,6 +1,7 @@
import React, { Component } from 'react';
import { Container, Row, Col } from 'react-bootstrap';
import axios from 'axios';
import getDataFromRepository from '../api/DataFromRepository';
import Card from '../components/Card';
import Infobox from '../components/Infobox';
@ -11,12 +12,12 @@ class Dashboard extends Component {
this.state = {};
}
componentDidMount() {
axios
.get('https://openpuglia.org/api/?q=getdatapccovid-19&reg=campania')
.then((response) => this.setState(response.data[0]));
async componentDidMount() {
const data = await getDataFromRepository();
this.setState(data);
}
render() {
const { state } = this;
@ -37,7 +38,7 @@ class Dashboard extends Component {
<Card
bgColor="warning"
title="Totale casi"
value={state['totale casi']}
value={state.totale_casi}
/>
</Col>
@ -53,7 +54,7 @@ class Dashboard extends Component {
<Card
bgColor="success"
title="Guariti"
value={state['dimessi guariti']}
value={state.dimessi_guariti}
/>
</Col>
</Row>
@ -63,7 +64,7 @@ class Dashboard extends Component {
<Card
bgColor="info"
title="Attualmente positivi"
value={state['totale attualmente positivi']}
value={state.totale_attualmente_positivi}
/>
</Col>
@ -71,7 +72,7 @@ class Dashboard extends Component {
<Card
bgColor="info"
title="Nuovi attualmente positivi"
value={state['nuovi attualmente positivi']}
value={state.nuovi_attualmente_positivi}
/>
</Col>
@ -89,7 +90,7 @@ class Dashboard extends Component {
<Card
textColor="dark"
title="Totale ospedalizzati"
value={state['totale ospedalizzati']}
value={state.totale_ospedalizzati}
/>
</Col>
@ -97,7 +98,7 @@ class Dashboard extends Component {
<Card
textColor="dark"
title="Terapia intensiva"
value={state['terapia intensiva']}
value={state.terapia_intensiva}
/>
</Col>
@ -105,7 +106,7 @@ class Dashboard extends Component {
<Card
textColor="dark"
title="Isolamento domiciliare"
value={state['isolamento domiciliare']}
value={state.isolamento_domiciliare}
/>
</Col>
</Row>

0
src/index.css Normal file → Executable file
View File

0
src/index.js Normal file → Executable file
View File

0
src/logo.svg Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

0
src/serviceWorker.js Normal file → Executable file
View File

0
src/setupTests.js Normal file → Executable file
View File