Skip to content

Commit

Permalink
Merge pull request #335 from jlssmt/hospital-index
Browse files Browse the repository at this point in the history
Hospital index
will solve merging conflict of package lock afterwards
  • Loading branch information
DutchmanNL authored Nov 16, 2023
2 parents 0c9ea32 + 564807b commit 03b2894
Show file tree
Hide file tree
Showing 8 changed files with 7,558 additions and 61 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG_OLD.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Older changes
## 0.8.3 (2021-08-20)
* (jlssmt) Code refactoring
* (DutchmanNL) Bugfix: Unwanted deletion of data points for Germany solved

## 0.8.2 (2021-08-17)
* (DutchmanNL & jlssmt) Small code improvements to prepare stable release

Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ As first value, the name from the warning message must be taken from the log. Th
* (DutchmanNL)
-->

### 0.8.8-0 (2021-11-19)
* (jlssmt) added hospital index for germany and federal states of germany

### 0.8.7 (2021-11-17)
* (DutchmanNL) Bugfix: Added missing definitions
* (jlssmt) Error handling for missing state attribute definitions Optimized
Expand All @@ -111,10 +114,6 @@ As first value, the name from the warning message must be taken from the log. Th
* (jlssmt) fix umlauts in hospital service
* (jlssmt) extend api timer to fix 502 errors

### 0.8.3 (2021-08-20)
* (jlssmt) Code refactoring
* (DutchmanNL) Bugfix: Unwanted deletion of data points for Germany solved

## License
MIT License

Expand Down
26 changes: 13 additions & 13 deletions io-package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
{
"common": {
"name": "coronavirus-statistics",
"version": "0.8.7",
"version": "0.8.8-0",
"news": {
"0.8.8-0": {
"en": "added hospital index for germany and federal states of germany",
"de": "Krankenhausindex für Deutschland und Bundesländer hinzugefügt",
"ru": "добавлен индекс больниц для Германии и федеральных земель германии",
"pt": "adicionado índice de hospitais para a Alemanha e estados federais da Alemanha",
"nl": "toegevoegde ziekenhuisindex voor duitsland en deelstaten van duitsland",
"fr": "index des hôpitaux ajouté pour l'allemagne et les états fédéraux d'allemagne",
"it": "aggiunto indice ospedaliero per la Germania e gli stati federali della Germania",
"es": "índice hospitalario agregado para alemania y los estados federales de alemania",
"pl": "dodano indeks szpitalny dla Niemiec i krajów związkowych Niemiec",
"zh-cn": "添加了德国和德国联邦州的医院索引"
},
"0.8.7": {
"en": "Bugfix: Added missing definitions\nError handling for missing state attribute definitions Optimized",
"de": "Bugfix: Fehlende Definitionen hinzugefügt\nFehlerbehandlung bei fehlenden Zustandsattributdefinitionen Optimiert",
Expand Down Expand Up @@ -230,18 +242,6 @@
"es": "* \nagregue \"Casos por 100k\" en total y durante los últimos 7 días para los Bundesländer de Alemania",
"pl": "* \ndodać „Przypadki na 100 000” łącznie iw ciągu ostatnich 7 dni dla niemieckich landów",
"zh-cn": "* \n在过去7天里为德国的Bundesländer添加“每10万个案例数”"
},
"0.6.1": {
"en": "Added cases per 100k during the last 7 days for Germany",
"de": "In den letzten 7 Tagen wurden für Deutschland Fälle pro 100.000 hinzugefügt",
"ru": "Добавлены случаи на 100 тысяч за последние 7 дней для Германии",
"pt": "Adicionados casos por 100k durante os últimos 7 dias para a Alemanha",
"nl": "Toegevoegde gevallen per 100.000 gedurende de afgelopen 7 dagen voor Duitsland",
"fr": "Ajout de cas par 100 000 au cours des 7 derniers jours pour l'Allemagne",
"it": "Aggiunti casi per 100.000 negli ultimi 7 giorni per la Germania",
"es": "Casos agregados por cada 100k durante los últimos 7 días para Alemania",
"pl": "Dodano przypadki na 100 000 w ciągu ostatnich 7 dni dla Niemiec",
"zh-cn": "最近7天内德国每10万新增案件"
}
},
"title": "COVID-19 live information",
Expand Down
49 changes: 49 additions & 0 deletions lib/services/hospital-index.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const {default: axios} = require('axios');
const csv = require('csvtojson');

class HospitalIndexService {

/**
* calls rki github to get hospital index data
*
* @returns hospital index data array
*/
static refreshGermanHospitalIndexData() {

return axios.get('https://raw.githubusercontent.com/robert-koch-institut/COVID-19-Hospitalisierungen_in_Deutschland/master/Aktuell_Deutschland_COVID-19-Hospitalisierungen.csv', {responseType: 'blob'})
.then(response => response.data)
.then(data => csv().fromString(data))
.then(data => data.filter(item => item.Datum === new Date().toLocaleDateString('fr-CA')))
.then(data => data.filter(item => item.Altersgruppe === '00+'))
.catch(error => {
throw new Error(`Cannot get hospital index data from rki: ${error}`);
});
}

/**
* @param data$ whole hospital index data$ promise
* @param federalState "Schleswig-Holstein"
* @returns {Promise<*>} Object of hospital index data$ for input
*/
static getGermanHospitalIndexByFederalState(data$, federalState) {
return data$
.then(data => data.filter(item => item.Bundesland === federalState)[0])
.then(data => {
if (!data || data.length === 0) throw new Error();
return data;
})
.catch(error => {
throw new Error(`Cannot get hospital index data for ${federalState} from rki: ${error}`);
});
}

/**
* @param data$ whole hospital data index promise
* @returns {*} hospital index promise for overall germany only without federal states
*/
static getGermanOverallHospitalIndex(data$) {
return HospitalIndexService.getGermanHospitalIndexByFederalState(data$, 'Bundesgebiet');
}
}

module.exports = HospitalIndexService;
32 changes: 29 additions & 3 deletions lib/stateAttr.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const stateAttrb = {
'daily_people_vaccinated_per_hundred' : {
name: 'Amount of people daily vaccinated per hundred',
role: 'value',
},
},
'deaths' : {
name: 'Amount of current registered deaths',
type: 'number',
Expand Down Expand Up @@ -337,9 +337,35 @@ const stateAttrb = {
'bundesland' : {
name: 'Bundesland',
role: 'value',
unit: '%',
},

'Hospital_Index' : {
name: 'Hospitalisierungsindex',
role: 'value',
},
'7T_Hospitalisierung_Faelle' : {
name: '7T_Hospitalisierung_Faelle',
role: 'value',
},
'7T_Hospitalisierung_Inzidenz' : {
name: '7T_Hospitalisierung_Inzidenz',
role: 'value',
},
'Datum' : {
name: 'Datum',
role: 'date',
},
'Bundesland' : {
name: 'Bundesland',
role: 'value',
},
'Bundesland_Id' : {
name: 'Bundesland_Id',
role: 'value',
},
'Altersgruppe' : {
name: 'Altersgruppe',
role: 'value',
},
};

module.exports = stateAttrb;
47 changes: 36 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const HospitalService = require('./lib/services/hospital.service');
const VaccinationService = require('./lib/services/vaccination.service');
const HospitalIndexService = require('./lib/services/hospital-index.service');

// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
Expand All @@ -18,6 +19,7 @@ let allGermanyFederalStates = [], allGermanCountyDetails = [], allGermanyCountie
let allGermanyFederalStatesLoaded = null, allGermanyCountiesLoaded = null, allGermanyCitiesLoaded = null;
let vaccinationData$;
let germanHospitalData$;
let germanHospitalIndexData$;

// Translator if country names are not iso conform
const countryTranslator = require('./lib/countryTranslator');
Expand Down Expand Up @@ -150,6 +152,7 @@ class Covid19 extends utils.Adapter {
await this.writeVaccinationDataForCountry(country, await VaccinationService.getVaccinationDataByIsoCode(vaccinationData$, countryObject.code.iso3));
if (country === 'Germany') {
await this.writeHospitalDataForId(country, await HospitalService.getGermanOverallHospitalData(germanHospitalData$));
await this.writeHospitalIndexDataForId(country, await HospitalIndexService.getGermanHospitalIndexByFederalState(germanHospitalIndexData$, 'Bundesgebiet'));
}
} catch (error) {
this.log.debug(`Cannot write data for ${country}: ${error}`);
Expand Down Expand Up @@ -407,18 +410,16 @@ class Covid19 extends utils.Adapter {

try {
await this.writeHospitalDataForId(channelName, await HospitalService.getGermanHospitalDataByFederalState(germanHospitalData$, federalStateName));
// Create hospital channel for each Federal State
await this.extendObjectAsync(`${channelName}.Hospital`, {
type: 'channel',
common: {
name: `Hospital`,
},
native: {},
});
} catch (error) {
this.log.debug(`Cannot write hospital data for ${channelName}: ${error}`);
}

try {
await this.writeHospitalIndexDataForId(channelName, await HospitalIndexService.getGermanHospitalIndexByFederalState(germanHospitalIndexData$, federalStateName));
} catch (error) {
this.log.debug(`Cannot write hospital index data for ${channelName}: ${error}`);
}

if (vaccDataGermany != null) {
if (germanyVaccinationData[federalStateName]) {
await this.extendObjectAsync(`${channelName}._Impfungen`, {
Expand Down Expand Up @@ -709,6 +710,8 @@ class Covid19 extends utils.Adapter {
.catch(error => this.log.warn(`Vaccination Data Warning: ${error}`)); // load all vaccination data
germanHospitalData$ = HospitalService.refreshGermanHospitalData()
.catch(error => this.log.warn(`Hospital Data Warning: ${error}`)); // load german hospital data
germanHospitalIndexData$ = HospitalIndexService.refreshGermanHospitalIndexData()
.catch(error => this.log.warn(`Hospital Index Data Warning: ${error}`)); // load german hospital index
await loadAll(); // Global Worldwide statistics
await loadCountries(); // Detailed Worldwide statistics by country

Expand Down Expand Up @@ -905,15 +908,14 @@ class Covid19 extends utils.Adapter {

/**
* @param id "Germany"
* @param data Object to write
* @param data Object to write
*/
async writeHospitalDataForId(id, data) {
if (!data) {
if (!id || !data) {
this.log.debug(`Cannot write hospital data for ${id}, if this error continues please report a bug to the developer! Totals: ${JSON.stringify(data)}`);
return;
}


await this.extendObjectAsync(`${id}.Hospital`, {
type: 'channel',
common: {
Expand All @@ -927,6 +929,29 @@ class Covid19 extends utils.Adapter {
}
}

/**
* @param id "Germany"
* @param data Object to write
*/
async writeHospitalIndexDataForId(id, data) {
if (!id || !data) {
this.log.debug(`Cannot write hospital index data for ${id}, if this error continues please report a bug to the developer! Totals: ${JSON.stringify(data)}`);
return;
}

await this.extendObjectAsync(`${id}.Hospital_Index`, {
type: 'channel',
common: {
name: `Hospital Index`,
},
native: {},
});

for (const key of Object.keys(data)) {
await this.localCreateState(`${id}.Hospital_Index.${key}`, key, data[key]);
}
}

async createFolderStructure(country) {

// Create country folder
Expand Down
Loading

0 comments on commit 03b2894

Please sign in to comment.