forked from bibaldo/MMM-COVID19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-COVID19.js
327 lines (300 loc) · 12.4 KB
/
MMM-COVID19.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* global Module */
/* Magic Mirror
* Module: MMM-COVID19
*
* By Jose Forte
* MIT Licensed.
*/
Module.register("MMM-COVID19", {
countriesStats: {},
globalStats: { "total_cases": "", "total_deaths": "", "total_recovered": "" }, // beautify things at start
defaults: {
header: 'COVID-19',
countries: [ "Argentina", "Italy", "Spain", "Germany" ], // default list
orderCountriesByName: false, // false will sort by total number of confirmed cases
orderAscending: false, // sort order, true = ascending, false = descending
lastUpdateInfo: false,
worldStats: false,
delta: false,
showExtraInfo: false,
rapidapiKey : "", // X-RapidAPI-Key provided at https://rapidapi.com/astsiatsko/api/coronavirus-monitor
headerRowClass: "small", // small, medium or big
infoRowClass: "big", // small, medium or big
updateInterval: 300000, // update interval in milliseconds
fadeSpeed: 4000
},
getStyles: function() {
return ["MMM-COVID19.css"]
},
getTranslations: function() {
return {
en: "translations/en.json",
de: "translations/de.json",
es: "translations/es.json"
}
},
start: function() {
this.getInfo()
this.scheduleUpdate()
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay
}
var self = this
setInterval(function() {
self.getInfo()
}, nextLoad)
},
getInfo: function () {
this.sendSocketNotification('GET_BY_COUNTRY_STATS', this.config.rapidapiKey)
if (this.config.worldStats) {
this.sendSocketNotification('GET_GLOBAL_STATS', this.config.rapidapiKey)
}
},
socketNotificationReceived: function(notification, payload) {
var self = this
if (notification === "BYCOUNTRY_RESULT") {
this.countriesStats = payload
this.updateDom(self.config.fadeSpeed)
}
if (notification === "GLOBAL_RESULT") {
this.globalStats = payload
this.updateDom(self.config.fadeSpeed)
}
},
getHeader: function() {
return this.config.header
},
getDom: function() {
var countriesList = this.config.countries
var countriesStats = this.countriesStats["countries_stat"]
var globalStats = this.globalStats
if (this.config.orderCountriesByName && countriesStats) {
countriesStats.sort(this.compareValues('country_name', this.config.orderAscending))
} else if (countriesStats) {
countriesStats.sort(this.compareValues('cases', this.config.orderAscending))
}
var wrapper = document.createElement("table")
wrapper.className = this.config.tableClass || 'covid'
// header row
var headerRow = document.createElement("tr"),
headerConfirmedCell = document.createElement("td"),
headerCasesPerMCell = document.createElement("td"),
headerNewConfirmedCell = document.createElement("td"),
headerCountryNameCell = document.createElement("td"),
headerRecoveredCell = document.createElement("td"),
headerDeathsCell = document.createElement("td"),
headerNewDeathsCell = document.createElement("td"),
headerSeriousCell = document.createElement("td"),
headerActiveCell = document.createElement("td");
headerCountryNameCell.innerHTML = ''
headerConfirmedCell.className = 'number confirmed ' + this.config.headerRowClass
headerConfirmedCell.innerHTML = this.translate('Confirmed')
headerCasesPerMCell.className = 'number ' + this.config.headerRowClass
headerCasesPerMCell.innerHTML = this.translate('Cases per M')
headerNewConfirmedCell.className = 'number confirmed ' + this.config.headerRowClass
headerNewConfirmedCell.innerHTML = this.translate('New Cases')
headerDeathsCell.className = 'number deaths ' + this.config.headerRowClass
headerDeathsCell.innerHTML = this.translate('Deaths')
headerNewDeathsCell.className = 'number deaths ' + this.config.headerRowClass
headerNewDeathsCell.innerHTML = this.translate('New Deaths')
headerSeriousCell.className = 'number serious ' + this.config.headerRowClass
headerSeriousCell.innerHTML = this.translate('Serious')
headerRecoveredCell.className = 'number recovered ' + this.config.headerRowClass
headerRecoveredCell.innerHTML = this.translate('Recovered')
headerActiveCell.className = 'number active ' + this.config.headerRowClass
headerActiveCell.innerHTML = this.translate('Active')
headerRow.appendChild(headerCountryNameCell)
headerRow.appendChild(headerConfirmedCell)
if (this.config.showExtraInfo) {
headerRow.appendChild(headerCasesPerMCell)
}
if (this.config.delta) {
headerRow.appendChild(headerNewConfirmedCell)
}
headerRow.appendChild(headerDeathsCell)
if (this.config.delta) {
headerRow.appendChild(headerNewDeathsCell)
}
if (this.config.showExtraInfo) {
headerRow.appendChild(headerSeriousCell)
}
headerRow.appendChild(headerRecoveredCell)
headerRow.appendChild(headerActiveCell)
wrapper.appendChild(headerRow)
// WorldWide row, activate it via config
if (this.config.worldStats) {
let worldRow = document.createElement("tr"),
worldNameCell = document.createElement("td"),
confirmedCell = document.createElement("td"),
newCasesCell = document.createElement("td"),
deathsCell = document.createElement("td"),
newDeathsCell = document.createElement("td"),
seriousCell = document.createElement("td"),
recoveredCell = document.createElement("td"),
activeCell = document.createElement("td"),
casesPerMCell = document.createElement("td"),
cases = globalStats["total_cases"],
newCases = globalStats["new_cases"],
deaths = globalStats["total_deaths"],
newDeaths = globalStats["new_deaths"],
totalRecovered = globalStats["total_recovered"],
serious = this.translate('N/A'),
casesPerM = this.translate('N/A');
activeCases = (cases && totalRecovered)?
this.numberWithCommas(parseInt(cases.replace(",","")) - parseInt(totalRecovered.replace(",","")))
:"";
worldNameCell.innerHTML = this.translate('Worldwide')
worldNameCell.className = this.config.infoRowClass
worldRow.className = 'world ' + this.config.infoRowClass
confirmedCell.className = 'number confirmed ' + this.config.infoRowClass
confirmedCell.innerHTML = cases
casesPerMCell.className = 'number ' + this.config.infoRowClass
casesPerMCell.innerHTML = casesPerM
newCasesCell.className = 'number confirmed ' + this.config.infoRowClass
if (newCases) {
newCasesCell.innerHTML = '+' + newCases
}
deathsCell.className = 'number deaths ' + this.config.infoRowClass
deathsCell.innerHTML = deaths
newDeathsCell.className = 'number deaths ' + this.config.infoRowClass
if (newDeaths) {
newDeathsCell.innerHTML = '+' + newDeaths
}
seriousCell.className = 'number serious ' + this.config.infoRowClass
seriousCell.innerHTML = serious
recoveredCell.className = 'number recovered ' + this.config.infoRowClass
recoveredCell.innerHTML = totalRecovered
activeCell.className = 'number active ' + this.config.infoRowClass
activeCell.innerHTML = activeCases
worldRow.appendChild(worldNameCell)
worldRow.appendChild(confirmedCell)
if (this.config.showExtraInfo) {
worldRow.appendChild(casesPerMCell)
}
if (this.config.delta) {
worldRow.appendChild(newCasesCell)
}
worldRow.appendChild(deathsCell)
if (this.config.delta) {
worldRow.appendChild(newDeathsCell)
}
if (this.config.showExtraInfo) {
worldRow.appendChild(seriousCell)
}
worldRow.appendChild(recoveredCell)
worldRow.appendChild(activeCell)
wrapper.appendChild(worldRow)
}
// countries row, one per country listed at config => countries
for (let key in countriesStats) {
let value = countriesStats[key]
if (countriesList.indexOf(value["country_name"]) != -1) {
let countryRow = document.createElement("tr"),
countryNameCell = document.createElement("td"),
confirmedCell = document.createElement("td"),
casesPerMCell = document.createElement("td"),
newCasesCell = document.createElement("td"),
deathsCell = document.createElement("td"),
newDeathsCell = document.createElement("td"),
seriousCell = document.createElement("td"),
recoveredCell = document.createElement("td"),
activeCell = document.createElement("td"),
countryName = value["country_name"],
cases = value["cases"],
deaths = value["deaths"],
serious = value["serious_critical"],
newCases = value["new_cases"],
newDeaths = value["new_deaths"],
totalRecovered = value["total_recovered"],
activeCases = value["active_cases"],
casesPerM = value["total_cases_per_1m_population"];
countryNameCell.innerHTML = this.translate(countryName)
countryNameCell.className = this.config.infoRowClass
confirmedCell.className = 'number confirmed ' + this.config.infoRowClass
confirmedCell.innerHTML = cases
casesPerMCell.className = 'number ' + this.config.infoRowClass
casesPerMCell.innerHTML = casesPerM
newCasesCell.className = 'number confirmed ' + this.config.infoRowClass
if (newCases) {
newCasesCell.innerHTML = '+' + newCases
}
deathsCell.className = 'number deaths ' + this.config.infoRowClass
deathsCell.innerHTML = deaths
newDeathsCell.className = 'number deaths ' + this.config.infoRowClass
if (newDeaths) {
newDeathsCell.innerHTML = '+' + newDeaths
}
seriousCell.className = 'number serious ' + this.config.infoRowClass
seriousCell.innerHTML = serious
recoveredCell.className = 'number recovered ' + this.config.infoRowClass
recoveredCell.innerHTML = totalRecovered
activeCell.className = 'number active ' + this.config.infoRowClass
activeCell.innerHTML = activeCases
countryRow.appendChild(countryNameCell)
countryRow.appendChild(confirmedCell)
if (this.config.showExtraInfo) {
countryRow.appendChild(casesPerMCell)
}
if (this.config.delta) {
countryRow.appendChild(newCasesCell)
}
countryRow.appendChild(deathsCell)
if (this.config.delta) {
countryRow.appendChild(newDeathsCell)
}
if (this.config.showExtraInfo) {
countryRow.appendChild(seriousCell)
}
countryRow.appendChild(recoveredCell)
countryRow.appendChild(activeCell)
wrapper.appendChild(countryRow)
}
}
if (this.config.lastUpdateInfo) {
let statsDateRow = document.createElement("tr"),
statsDateCell = document.createElement("td");
statsDateCell.innerHTML = this.translate('statistic taken at ') + this.countriesStats['statistic_taken_at'] + ' (UTC)'
if (this.config.delta) {
statsDateCell.colSpan = "7"
} else {
statsDateCell.colSpan = "5"
}
statsDateCell.className = 'last-update'
statsDateRow.appendChild(statsDateCell)
wrapper.appendChild(statsDateRow)
}
return wrapper
},
// sort according to the key (currently country_name or cases),
// sort order either ascending or descending as per variable orderAscending
compareValues: function(key, order ) {
return function innerSort(a, b) {
if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
// property doesn't exist on either object
return 0
}
let varAlpha = Number(a[key].replace(/,/g,''))
let varBeta = Number(b[key].replace(/,/g,''))
const varA = (Number.isNaN(varAlpha))
? a[key].toUpperCase() : varAlpha
const varB = (Number.isNaN(varBeta))
? b[key].toUpperCase() : varBeta
let comparison = 0
if (varA > varB) {
comparison = 1
} else if (varA < varB) {
comparison = -1
}
return (
(!order) ? (comparison * -1) : comparison
);
}
},
// insert separating commas into a number at thousands, millions, etc
numberWithCommas: function(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
},
})