-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
633 lines (545 loc) · 18.7 KB
/
main.go
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
package main
import (
"bytes"
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/influxdata/influxdb/client"
"github.com/metakeule/fmtdate"
)
type WorldWideData struct {
Date time.Time
State string
NewCases int64
NewDeaths int64
TotalCases int64
TotalDeaths int64
}
type ProvinceJsonData struct {
Data string `json:"data"`
Stato string `json:"stato"`
CodiceRegione int `json:"codice_regione"`
DenominazioneRegione string `json:"denominazione_regione"`
CodiceProvincia int `json:"codice_provincia"`
DenominazioneProvincia string `json:"denominazione_provincia"`
SiglaProvincia string `json:"sigla_provincia"`
Lat float64 `json:"lat"`
Long float64 `json:"long"`
TotaleCasi int `json:"totale_casi"`
Datetime time.Time
}
type RegionsJsonData struct {
Data string `json:"data"`
Stato string `json:"stato"`
CodiceRegione int `json:"codice_regione"`
DenominazioneRegione string `json:"denominazione_regione"`
Lat float64 `json:"lat"`
Long float64 `json:"long"`
RicoveratiConSintomi int `json:"ricoverati_con_sintomi"`
TerapiaIntensiva int `json:"terapia_intensiva"`
TotaleOspedalizzati int `json:"totale_ospedalizzati"`
IsolamentoDomiciliare int `json:"isolamento_domiciliare"`
TotaleAttualmentePositivi int `json:"totale_positivi"`
NuoviAttualmentePositivi int `json:"nuovi_positivi"`
DimessiGuariti int `json:"dimessi_guariti"`
Deceduti int `json:"deceduti"`
TotaleCasi int `json:"totale_casi"`
Tamponi int `json:"tamponi"`
Datetime time.Time
}
const andamentoProvince string = "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-province.json"
const andamentoNazionale string = "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-andamento-nazionale.json"
const andamentoRegioni string = "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-regioni.json"
const andamentoMondiale string = "https://covid.ourworldindata.org/data/full_data.csv"
// var sha256_andamentoProvince [32]byte
// var sha256_andamentoNazionale [32]byte
// var sha256_andamentoRegioni [32]byte
// var sha256_andamentoMondiale [32]byte
const DB_NAME string = "MyDB"
const HOSTNAME string = "http://localhost"
const telegramUrl string = "https://api.telegram.org/bot%s/sendMessage?chat_id=@covid19_dashboard&text=%s"
const timeFormat string = "Mon Jan _2 15:04:05 2006"
const telegramMessage string = "[%s] New data are available at https://covid19.hackx.com/d/OO1TC7XZk/covid-19-cases?orgId=1"
const gitUrl string = "https://api.github.com/repos/pcm-dpc/COVID-19/commits"
type requestData struct {
Hours int `json:"hours"`
Minutes int `json:"minutes"`
Second int `json:"second"`
Url string `json:"url"`
}
func (r *requestData) setTime(t time.Time) {
r.Hours = t.Hour()
r.Minutes = t.Minute()
r.Second = t.Second()
}
func (r *requestData) resetTime() {
r.Hours = 18
r.Minutes = 0
r.Second = 0
}
type LambdaResponse struct {
Time time.Time `json:"time"`
Updated bool `json:"updated"`
LatestCommit time.Time `json:"commitTime"`
TargetTime time.Time `json:"targetTime"`
}
var startTime time.Time
var endTime time.Time
func inTimeSpan(check time.Time) bool {
if startTime.Before(endTime) {
return !check.Before(startTime) && !check.After(endTime)
}
if startTime.Equal(endTime) {
return check.Equal(startTime)
}
return !startTime.After(check) || !endTime.Before(check)
}
func main() {
var (
con *client.Client // Client for push data into InfluxDB
host *url.URL // Host related to the InfluxDB instance
resp *http.Response // Response of the http request
f *os.File // LogFile
provinceData []ProvinceJsonData // Data related to the provinces
nationalData []RegionsJsonData // Data related to the nations
regionData []RegionsJsonData // Data related to the regions
wordData []WorldWideData // Data related to the world
data []byte // Body related to the response
body string // Body related to the response (string)
err error
)
if f, err = os.OpenFile("log/log.txt", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666); err != nil {
panic(err)
}
log.SetOutput(f)
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
if loc, err := time.LoadLocation("Europe/Rome"); err != nil {
panic(err)
} else {
time.Local = loc
}
t := time.Now()
startTime = time.Date(t.Year(), t.Month(), t.Day(), 17, 50, 0, 0, time.Local)
endTime = time.Date(t.Year(), t.Month(), t.Day(), 19, 0, 0, 0, time.Local)
// Request body to send to the lambda in order to verify that the given repository have a commit after the given time
reqData := requestData{
Hours: 18,
Minutes: 0,
Second: 0,
Url: gitUrl,
}
lambdaUrl := flag.String("lambdaUrl", "", "Url related to the lambda delegated to verify that a commit is made")
telegramBot := flag.String("telegramBot", "", "Telegram bot id")
flag.Parse()
if *lambdaUrl == "" {
panic("No lambda url specified")
}
if *telegramBot == "" {
panic("No Telegram bot id specified")
}
// Initialize the URL for the InfluxDB instance
if host, err = url.Parse(HOSTNAME + ":8086"); err != nil {
panic(err)
}
// Initialize influxdb database
initDatabase()
for {
// Check that the current time is in the span (from ~18:00 to ~19:00)
if inTimeSpan(time.Now()) {
if data, err = json.Marshal(reqData); err != nil {
panic(err)
}
// Send the request to the lambda
if resp, err = http.Post(*lambdaUrl, "application/json", bytes.NewBuffer(data)); err != nil {
panic(err)
}
// Retrieve the body
if body, err = getBody(resp.Body); err != nil {
panic(err)
}
var lambdaResponse LambdaResponse
if err = json.Unmarshal([]byte(body), &lambdaResponse); err != nil {
panic(err)
}
log.Printf("Response: %+v\n", lambdaResponse)
// Check the body in order to verify the result of the lambda
if lambdaResponse.Updated {
// Initialize the InfluxDB client
if con, err = client.NewClient(client.Config{URL: *host}); err != nil {
panic(err)
}
// Verify that InfluxDB is available
if _, _, err = con.Ping(); err != nil {
panic(err)
}
// Retrieve the data and save into InfluxDB
provinceData = retrieveProvinceData(andamentoProvince)
dbResponse := saveInfluxProvinceData(provinceData, con)
log.Printf("%+v\n", dbResponse)
provinceData = nil
nationalData = retrieveNationalData(andamentoNazionale)
dbResponse = saveInfluxNationalData(nationalData, con, "state_data")
log.Printf("%+v\n", dbResponse)
nationalData = nil
regionData = retrieveNationalData(andamentoRegioni)
dbResponse = saveInfluxNationalData(regionData, con, "regions_data")
log.Printf("%+v\n", dbResponse)
regionData = nil
wordData = retrieveWorldWideData(andamentoMondiale)
dbResponse = saveInfluxWordlData(wordData, con)
log.Printf("%+v\n", dbResponse)
dbResponse = retrieveTotalRegionData(andamentoRegioni, con, "total_regions_data", 100)
log.Printf("%+v\n", dbResponse)
// message that have to be sent in telegram group
tgMessage := fmt.Sprintf(telegramMessage, time.Now().Format(timeFormat))
tgUrl := fmt.Sprintf(telegramUrl, *telegramBot, url.QueryEscape(tgMessage))
if resp, err = http.Get(tgUrl); err != nil {
panic(err)
}
if body, err = getBody(resp.Body); err != nil {
panic(err)
}
log.Printf("Response: %s\n", body)
reqData.setTime(time.Now())
} else {
// Wait 5 minuts before check another time that a commit is made
log.Println("Waiting 5 Minutes")
time.Sleep(time.Duration(5 * time.Minute))
}
} else {
log.Printf("Current [%+v] date is not in the interval\n", time.Now())
t := time.Now()
d := startTime.Sub(t)
if d < 0 {
startTime = startTime.Add(24 * time.Hour)
endTime = endTime.Add(24 * time.Hour)
d += 23 * time.Hour
reqData.resetTime()
}
log.Printf("Sleeping %+v\n", d)
time.Sleep(d)
}
}
}
func initDatabase() {
var (
req *http.Request
resp *http.Response
c *http.Client = &http.Client{}
bodyBytes []byte
err error
apiUrl string = "http://localhost:8086"
resource string = "/query"
data url.Values = url.Values{}
)
data.Set("q", "CREATE DATABASE "+DB_NAME)
u, _ := url.ParseRequestURI(apiUrl)
u.Path = resource
u.RawQuery = data.Encode()
urlStr := fmt.Sprintf("%v", u)
if req, err = http.NewRequest("POST", urlStr, nil); err != nil {
panic(err)
}
if resp, err = c.Do(req); err != nil {
panic(err)
}
if resp.StatusCode != http.StatusOK {
if bodyBytes, err = ioutil.ReadAll(resp.Body); err != nil {
panic(err)
}
_ = resp.Body.Close()
bodyString := string(bodyBytes)
log.Printf("Error: %s\n", bodyString)
} else {
log.Printf("Database [%s] initialized\n", DB_NAME)
}
}
func saveInfluxProvinceData(provinceData []ProvinceJsonData, con *client.Client) *client.Response {
var dbResponse *client.Response // Response related to the data pushed into InfluxDB
var err error
// Initialize the list of event that have to be pushed into InfluxDB
pts := make([]client.Point, len(provinceData))
for i := range provinceData {
log.Println("Case: ", provinceData[i])
pts[i] = client.Point{
Measurement: "all_province_data",
Tags: nil,
Time: provinceData[i].Datetime,
Fields: map[string]interface{}{provinceData[i].DenominazioneProvincia: provinceData[i].TotaleCasi}}
}
bps := client.BatchPoints{Points: pts, Database: DB_NAME}
if dbResponse, err = con.Write(bps); err != nil {
panic(err)
}
return dbResponse
}
func retrieveTotalRegionData(urlPath string, con *client.Client, dbName string, minCases int) *client.Response {
var httpResponse *http.Response
var dbResponse *client.Response
var err error
var jsonData []RegionsJsonData
// Retrieve the fresh data related to covid-19
if httpResponse, err = http.Get(urlPath); err != nil {
panic(err)
}
defer httpResponse.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(httpResponse.Body)
newBytes := buf.Bytes()
trimmedBytes := bytes.Trim(newBytes, "\xef\xbb\xbf")
json.Unmarshal(trimmedBytes, &jsonData)
_ = httpResponse.Body.Close()
var pts []client.Point
for _, d := range jsonData {
if d.TotaleCasi >= minCases {
var m map[string]interface{} = make(map[string]interface{})
var t time.Time
// Parse the time into a standard one
if t, err = fmtdate.Parse("YYYY-MM-DDThh:mm:ss", d.Data); err != nil {
panic(err)
}
m[d.DenominazioneRegione] = d.TotaleCasi
pt := client.Point{
Measurement: dbName,
Tags: map[string]string{"regione": d.DenominazioneRegione},
Time: t,
Fields: m}
pts = append(pts, pt)
}
}
bps := client.BatchPoints{Points: pts, Database: DB_NAME}
if dbResponse, err = con.Write(bps); err != nil {
panic(err)
}
return dbResponse
}
func retrieveProvinceData(urlPath string) []ProvinceJsonData {
var httpResponse *http.Response
var err error
var jsonData []ProvinceJsonData
// Retrieve the fresh data related to covid-19
if httpResponse, err = http.Get(urlPath); err != nil {
panic(err)
}
defer httpResponse.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(httpResponse.Body)
newBytes := buf.Bytes()
trimmedBytes := bytes.Trim(newBytes, "\xef\xbb\xbf")
json.Unmarshal(trimmedBytes, &jsonData)
_ = httpResponse.Body.Close()
var data []ProvinceJsonData
// tempSHA := sha256.Sum256(buf.Bytes())
// if tempSHA == sha256_andamentoProvince { // No modification was made for this commit
// // Return an empty array, is not necessary to perform any update into influxdb
// log.Printf("File %s was not modified with this commit!", andamentoProvince)
// return data
// }
// // update the SHA
// sha256_andamentoProvince = tempSHA
for i := range jsonData {
if jsonData[i].TotaleCasi > 0 {
var t time.Time
// Parse the time into a standard one
if t, err = fmtdate.Parse("YYYY-MM-DDThh:mm:ss", jsonData[i].Data); err != nil {
panic(err)
}
jsonData[i].Datetime = t
data = append(data, jsonData[i])
}
}
log.Printf("Retrieved %d data\n", len(jsonData))
// Set the local time
if loc, err := time.LoadLocation("Europe/Rome"); err != nil {
panic(err)
} else {
time.Local = loc
}
return data
}
func retrieveNationalData(urlPath string) []RegionsJsonData {
var httpResponse *http.Response
var err error
var jsonData []RegionsJsonData
// Retrieve the fresh data related to covid-19
if httpResponse, err = http.Get(urlPath); err != nil {
panic(err)
}
defer httpResponse.Body.Close()
buf := new(bytes.Buffer)
if _, err = buf.ReadFrom(httpResponse.Body); err != nil {
log.Fatalf("Unable to read from national data response buffer! %s", err.Error())
}
newBytes := buf.Bytes()
trimmedBytes := bytes.Trim(newBytes, "\xef\xbb\xbf")
if err = json.Unmarshal(trimmedBytes, &jsonData); err != nil {
log.Fatalf("Unable to unmarshall national data! %s", err.Error())
}
_ = httpResponse.Body.Close()
var data []RegionsJsonData
// tempSHA := sha256.Sum256(buf.Bytes())
// if tempSHA == sha256_andamentoNazionale { // No modification was made for this commit
// // Return an empty array, is not necessary to perform any update into influxdb
// log.Printf("File %s was not modified with this commit!", andamentoNazionale)
// return data
// }
// // update the SHA
// sha256_andamentoProvince = tempSHA
for i := range jsonData {
var t time.Time
// Parse the time into a standard one
if t, err = fmtdate.Parse("YYYY-MM-DDThh:mm:ss", jsonData[i].Data); err != nil {
panic(err)
}
jsonData[i].Datetime = t
data = append(data, jsonData[i])
}
log.Printf("Retrieved %d data\n", len(jsonData))
// Set the local time
if loc, err := time.LoadLocation("Europe/Rome"); err != nil {
panic(err)
} else {
time.Local = loc
}
return data
}
func saveInfluxNationalData(provinceData []RegionsJsonData, con *client.Client, dbName string) *client.Response {
var dbResponse *client.Response // Response related to the data pushed into InfluxDB
var err error
// Initialize the list of event that have to be pushed into InfluxDB
pts := make([]client.Point, len(provinceData))
for i := range provinceData {
log.Println("Case: ", provinceData[i])
var m map[string]interface{} = make(map[string]interface{})
m["codice_regione"] = provinceData[i].CodiceRegione
m["denominazione_regione"] = provinceData[i].DenominazioneRegione
m["lat"] = provinceData[i].Lat
m["long"] = provinceData[i].Long
m["ricoverati_con_sintomi"] = provinceData[i].RicoveratiConSintomi
m["terapia_intensiva"] = provinceData[i].TerapiaIntensiva
m["totale_ospedalizzati"] = provinceData[i].TotaleOspedalizzati
m["isolamento_domiciliare"] = provinceData[i].IsolamentoDomiciliare
m["totale_attualmente_positivi"] = provinceData[i].TotaleAttualmentePositivi
m["nuovi_attualmente_positivi"] = provinceData[i].NuoviAttualmentePositivi
m["dimessi_guariti"] = provinceData[i].DimessiGuariti
m["deceduti"] = provinceData[i].Deceduti
m["totale_casi"] = provinceData[i].TotaleCasi
m["tamponi"] = provinceData[i].Tamponi
pts[i] = client.Point{
Measurement: dbName,
Tags: map[string]string{"regione": provinceData[i].DenominazioneRegione},
Time: provinceData[i].Datetime,
Fields: m}
}
bps := client.BatchPoints{Points: pts, Database: DB_NAME}
if dbResponse, err = con.Write(bps); err != nil {
panic(err)
}
return dbResponse
}
func retrieveWorldWideData(urlPath string) []WorldWideData {
var (
httpResponse *http.Response
err error
lines [][]string
v int64
)
if httpResponse, err = http.Get(urlPath); err != nil {
panic(err)
}
defer httpResponse.Body.Close()
// TODO: copy the response body in a buffer for perform multiple read
if lines, err = csv.NewReader(httpResponse.Body).ReadAll(); err != nil {
panic(err)
}
_ = httpResponse.Body.Close()
var wordWideData []WorldWideData = make([]WorldWideData, len(lines))
for i := 1; i < len(lines); i++ {
var t time.Time
// Parse the time into a standard one
if t, err = fmtdate.Parse("YYYY-MM-DD", lines[i][0]); err != nil {
panic(err)
}
wordWideData[i].Date = t
wordWideData[i].State = lines[i][1]
if lines[i][2] != "" {
if v, err = strconv.ParseInt(lines[i][2], 10, 32); err != nil {
panic(err)
}
wordWideData[i].NewCases = v
}
if lines[i][3] != "" {
if v, err = strconv.ParseInt(lines[i][3], 10, 32); err != nil {
panic(err)
}
wordWideData[i].NewDeaths = v
}
if lines[i][4] != "" {
if v, err = strconv.ParseInt(lines[i][4], 10, 32); err != nil {
panic(err)
}
wordWideData[i].TotalCases = v
}
if lines[i][5] != "" {
if v, err = strconv.ParseInt(lines[i][5], 10, 32); err != nil {
panic(err)
}
wordWideData[i].TotalDeaths = v
}
}
return wordWideData
}
func saveInfluxWordlData(worldData []WorldWideData, con *client.Client) *client.Response {
var dbResponse *client.Response // Response related to the data pushed into InfluxDB
var err error
// Initialize the list of event that have to be pushed into InfluxDB
pts := make([]client.Point, len(worldData))
for i := range worldData {
log.Println("Case: ", worldData[i])
var m map[string]interface{} = make(map[string]interface{})
m["total_deaths"] = worldData[i].TotalDeaths
m["total_cases"] = worldData[i].TotalCases
m["new_deaths"] = worldData[i].NewDeaths
m["new_cases"] = worldData[i].NewCases
pts[i] = client.Point{
Measurement: "all_world_data",
Tags: map[string]string{"nation": worldData[i].State},
Time: worldData[i].Date,
Fields: m}
}
bps := client.BatchPoints{Points: pts, Database: DB_NAME}
if dbResponse, err = con.Write(bps); err != nil {
panic(err)
}
return dbResponse
}
// The method is delegated to filter only the province that match the given input
func filterCasesForRegion(jsonData []ProvinceJsonData, regionName string) []ProvinceJsonData {
var provinceData []ProvinceJsonData
for i := range jsonData {
if jsonData[i].DenominazioneRegione == regionName {
provinceData = append(provinceData, jsonData[i])
}
}
return provinceData
}
// GetBody is delegated to retrieve the body from the given response
func getBody(body io.ReadCloser) (string, error) {
var sb strings.Builder
var err error
defer body.Close()
if _, err = io.Copy(&sb, body); err != nil {
return "", nil
}
return sb.String(), nil
}