misc updates on coronafana/xorm
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Paul 2021-11-06 14:14:58 +01:00
parent a102b58576
commit f0aa63c7a6
4 changed files with 43 additions and 13 deletions

View File

@ -2,6 +2,9 @@ package main
import (
"log"
"os"
"os/signal"
"syscall"
"git.paulbsd.com/paulbsd/coronafana/src/config"
"git.paulbsd.com/paulbsd/coronafana/src/coronafana"
@ -15,6 +18,14 @@ func main() {
var cr coronafana.Coronafana
var err error
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
network.CleanUp()
}()
defer network.CleanUp()
err = cfg.GetConfig()
if err != nil {
log.Fatalln(err)
@ -30,6 +41,7 @@ func main() {
if err != nil {
log.Fatalln(err)
}
defer dc()
defer t.Close()

View File

@ -16,10 +16,10 @@ import (
// GetData fetch data from open data portal
func GetData(cfg config.Config, d *tor.Dialer) (cr Coronafana, err error) {
var client = &http.Client{Transport: &http.Transport{DialContext: d.DialContext}}
log.Println("Getting JSON file ...")
var client = &http.Client{Transport: &http.Transport{DialContext: d.DialContext}}
if cfg.CoronaURL == "" {
err = fmt.Errorf("CoronaURL is not defined")
return
@ -136,9 +136,9 @@ type Coronafana struct {
type Coronaglobaldata struct {
ID int `xorm:"not null pk autoincr INT(8)"`
Date time.Time `xorm:"not null unique(date) DATETIME"`
Infection int `json:"Infection" xorm:"default NULL INT(8)"`
Deces int `json:"Deces" xorm:"default NULL INT(8)"`
Guerisons int `json:"Guerisons" xorm:"default NULL INT(8)"`
Infection int `json:"Infection" xorm:"INT(8)"`
Deces int `json:"Deces" xorm:"INT(8)"`
Guerisons int `json:"Guerisons" xorm:"INT(8)"`
Tauxdeces float64 `json:"TauxDeces"`
Tauxguerison float64 `json:"TauxGuerison"`
Tauxinfection float64 `json:"TauxInfection"`
@ -150,12 +150,12 @@ type Coronapaysdata struct {
ID int `xorm:"not null pk autoincr INT(8)"`
Date time.Time `xorm:"not null unique(date) DATETIME"`
Pays string `json:"Pays" xorm:"not null unique(date) VARCHAR(50)"`
Infection int `json:"Infection" xorm:"default NULL INT(8)"`
Deces int `json:"Deces" xorm:"default NULL INT(8)"`
Guerisons int `json:"Guerisons" xorm:"default NULL INT(8)"`
Tauxdeces float64 `json:"TauxDeces" xorm:"default NULL FLOAT"`
Tauxguerison float64 `json:"TauxGuerison" xorm:"default NULL FLOAT"`
Tauxinfection float64 `json:"TauxInfection" xorm:"default NULL FLOAT"`
Infection int `json:"Infection" xorm:"INT(8)"`
Deces int `json:"Deces" xorm:"INT(8)"`
Guerisons int `json:"Guerisons" xorm:"INT(8)"`
Tauxdeces float64 `json:"TauxDeces" xorm:"FLOAT"`
Tauxguerison float64 `json:"TauxGuerison" xorm:"FLOAT"`
Tauxinfection float64 `json:"TauxInfection" xorm:"FLOAT"`
DateString string `json:"Date" xorm:"-"`
}

View File

@ -2,6 +2,7 @@ package database
import (
"fmt"
"log"
"git.paulbsd.com/paulbsd/coronafana/src/config"
"git.paulbsd.com/paulbsd/coronafana/src/coronafana"
@ -11,6 +12,7 @@ import (
// InitDatabase ...
func InitDatabase(cfg *config.Config) (err error) {
log.Println("Initializing database ...")
var tables = []interface{}{
new(coronafana.Coronaglobaldata),
new(coronafana.Coronapaysdata),

View File

@ -2,7 +2,10 @@ package network
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/cretz/bine/tor"
@ -10,12 +13,14 @@ import (
// InitTorSession initialize a tor session
func InitTorSession() (t *tor.Tor, dialer *tor.Dialer, dialCancel context.CancelFunc, err error) {
log.Println("Initializing hidden network ...")
t, err = tor.Start(nil, nil)
if err != nil {
log.Panicf("Failed to start tor: %v", err)
}
// Wait at most a minute to start network and get
dialCtx, dialCancel := context.WithTimeout(context.Background(), time.Minute)
// Wait at most 30 seconds to start network and get
log.Println("Wait for network to initilize for 30 seconds ...")
dialCtx, dialCancel := context.WithTimeout(context.Background(), time.Second*30)
dialer, err = t.Dialer(dialCtx, nil)
if err != nil {
@ -23,3 +28,14 @@ func InitTorSession() (t *tor.Tor, dialer *tor.Dialer, dialCancel context.Cancel
}
return
}
func CleanUp() {
rm, err := filepath.Glob("data-dir*")
if err != nil {
log.Println(err)
}
for _, f := range rm {
fmt.Printf("Removing %s\n", f)
os.RemoveAll(f)
}
}