From 80351494138925d9849c7cebf13fe573b8968543 Mon Sep 17 00:00:00 2001 From: Francesco Esposito <33671357+franjsco@users.noreply.github.com> Date: Sun, 22 Mar 2020 20:01:53 +0100 Subject: [PATCH] Implementazione lettura da CSV --- .editorconfig | 4 +++ .eslintrc.js | 0 .gitignore | 0 README.md | 4 +-- package-lock.json | 10 ++++++ package.json | 2 ++ public/favicon.ico | Bin public/index.html | 0 public/logo192.png | Bin public/manifest.json | 0 public/robots.txt | 0 screenshot.png | Bin src/App.css | 0 src/App.js | 0 src/App.test.js | 0 src/LICENSE | 0 src/api/DataFromRepository.js | 58 ++++++++++++++++++++++++++++++++++ src/components/Card.js | 0 src/components/Footer.js | 0 src/components/Headbar.js | 0 src/components/Infobox.js | 2 +- src/components/Layout.js | 0 src/containers/Dashboard.js | 25 ++++++++------- src/index.css | 0 src/index.js | 0 src/logo.svg | 0 src/serviceWorker.js | 0 src/setupTests.js | 0 28 files changed, 89 insertions(+), 16 deletions(-) create mode 100755 .editorconfig mode change 100644 => 100755 .eslintrc.js mode change 100644 => 100755 .gitignore mode change 100644 => 100755 README.md mode change 100644 => 100755 package-lock.json mode change 100644 => 100755 package.json mode change 100644 => 100755 public/favicon.ico mode change 100644 => 100755 public/index.html mode change 100644 => 100755 public/logo192.png mode change 100644 => 100755 public/manifest.json mode change 100644 => 100755 public/robots.txt mode change 100644 => 100755 screenshot.png mode change 100644 => 100755 src/App.css mode change 100644 => 100755 src/App.js mode change 100644 => 100755 src/App.test.js mode change 100644 => 100755 src/LICENSE create mode 100755 src/api/DataFromRepository.js mode change 100644 => 100755 src/components/Card.js mode change 100644 => 100755 src/components/Footer.js mode change 100644 => 100755 src/components/Headbar.js mode change 100644 => 100755 src/components/Infobox.js mode change 100644 => 100755 src/components/Layout.js mode change 100644 => 100755 src/containers/Dashboard.js mode change 100644 => 100755 src/index.css mode change 100644 => 100755 src/index.js mode change 100644 => 100755 src/logo.svg mode change 100644 => 100755 src/serviceWorker.js mode change 100644 => 100755 src/setupTests.js diff --git a/.editorconfig b/.editorconfig new file mode 100755 index 0000000..2f7ebba --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +root = true + +[*.js] +indent_size = 2 \ No newline at end of file diff --git a/.eslintrc.js b/.eslintrc.js old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 2a06553..97b881d --- a/README.md +++ b/README.md @@ -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 diff --git a/package-lock.json b/package-lock.json old mode 100644 new mode 100755 index 75c98a4..39ab525 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json old mode 100644 new mode 100755 index 59f6cf1..12a10dc --- a/package.json +++ b/package.json @@ -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", diff --git a/public/favicon.ico b/public/favicon.ico old mode 100644 new mode 100755 diff --git a/public/index.html b/public/index.html old mode 100644 new mode 100755 diff --git a/public/logo192.png b/public/logo192.png old mode 100644 new mode 100755 diff --git a/public/manifest.json b/public/manifest.json old mode 100644 new mode 100755 diff --git a/public/robots.txt b/public/robots.txt old mode 100644 new mode 100755 diff --git a/screenshot.png b/screenshot.png old mode 100644 new mode 100755 diff --git a/src/App.css b/src/App.css old mode 100644 new mode 100755 diff --git a/src/App.js b/src/App.js old mode 100644 new mode 100755 diff --git a/src/App.test.js b/src/App.test.js old mode 100644 new mode 100755 diff --git a/src/LICENSE b/src/LICENSE old mode 100644 new mode 100755 diff --git a/src/api/DataFromRepository.js b/src/api/DataFromRepository.js new file mode 100755 index 0000000..b7b0b34 --- /dev/null +++ b/src/api/DataFromRepository.js @@ -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; diff --git a/src/components/Card.js b/src/components/Card.js old mode 100644 new mode 100755 diff --git a/src/components/Footer.js b/src/components/Footer.js old mode 100644 new mode 100755 diff --git a/src/components/Headbar.js b/src/components/Headbar.js old mode 100644 new mode 100755 diff --git a/src/components/Infobox.js b/src/components/Infobox.js old mode 100644 new mode 100755 index e761006..1723c82 --- a/src/components/Infobox.js +++ b/src/components/Infobox.js @@ -7,7 +7,7 @@ const Infobox = (props) => { return (
Ultimo aggiornamento: - { dataAggiornamento } + { dataAggiornamento.split(' ')[0] }
); }; diff --git a/src/components/Layout.js b/src/components/Layout.js old mode 100644 new mode 100755 diff --git a/src/containers/Dashboard.js b/src/containers/Dashboard.js old mode 100644 new mode 100755 index 102e4a5..1976d0e --- a/src/containers/Dashboard.js +++ b/src/containers/Dashboard.js @@ -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®=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 { @@ -53,7 +54,7 @@ class Dashboard extends Component { @@ -63,7 +64,7 @@ class Dashboard extends Component { @@ -71,7 +72,7 @@ class Dashboard extends Component { @@ -89,7 +90,7 @@ class Dashboard extends Component { @@ -97,7 +98,7 @@ class Dashboard extends Component { @@ -105,7 +106,7 @@ class Dashboard extends Component { diff --git a/src/index.css b/src/index.css old mode 100644 new mode 100755 diff --git a/src/index.js b/src/index.js old mode 100644 new mode 100755 diff --git a/src/logo.svg b/src/logo.svg old mode 100644 new mode 100755 diff --git a/src/serviceWorker.js b/src/serviceWorker.js old mode 100644 new mode 100755 diff --git a/src/setupTests.js b/src/setupTests.js old mode 100644 new mode 100755