updated data inserts

This commit is contained in:
Paul 2020-03-15 11:57:16 +01:00
parent fa64395c4d
commit dbe0564ec0
3 changed files with 64 additions and 31 deletions

View File

@ -30,16 +30,23 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
// Processes data for 'Global'
_, err = db.Exec(cfg.DbSchemaGlobal) _, err = db.Exec(cfg.DbSchemaGlobal)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
_, err = db.Exec(cfg.DbSchemaPays)
err = cr.InsertGlobalData(cfg, *db)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
err = cr.InsertData(cfg, *db) // Processes data for 'Pays'
_, err = db.Exec(cfg.DbSchemaPays)
if err != nil {
log.Fatalln(err)
}
err = cr.InsertPaysData(cfg, *db)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }

View File

@ -32,11 +32,11 @@ func (config *Config) GetConfig() (err error) {
config.DbSchemaGlobal = "CREATE TABLE IF NOT EXISTS coronaglobaldata (`id` int(8) NOT NULL AUTO_INCREMENT, `date` datetime NOT NULL, `infection` int(8) DEFAULT NULL, `deces` int(8) DEFAULT NULL, `guerisons` varchar(50) DEFAULT NULL, `tauxdeces` float(10) DEFAULT NULL, `tauxguerison` float(10) DEFAULT NULL, `tauxinfection` float(10) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `date` (`date`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8;" config.DbSchemaGlobal = "CREATE TABLE IF NOT EXISTS coronaglobaldata (`id` int(8) NOT NULL AUTO_INCREMENT, `date` datetime NOT NULL, `infection` int(8) DEFAULT NULL, `deces` int(8) DEFAULT NULL, `guerisons` varchar(50) DEFAULT NULL, `tauxdeces` float(10) DEFAULT NULL, `tauxguerison` float(10) DEFAULT NULL, `tauxinfection` float(10) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `date` (`date`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
config.DbInsertGlobal = "INSERT IGNORE INTO coronaglobaldata (date, infection, deces, guerisons, tauxdeces, tauxguerison, tauxinfection) VALUES ('%s', %d, %d , %d, %g, %g, %g);" config.DbInsertGlobal = "INSERT INTO coronaglobaldata (date, infection, deces, guerisons, tauxdeces, tauxguerison, tauxinfection) VALUES (:date, :infection, :deces, :guerisons, :tauxdeces, :tauxguerison, :tauxinfection) ON DUPLICATE KEY UPDATE date=:date,infection=:infection, deces=:deces, guerisons=:guerisons, tauxdeces=:tauxdeces, tauxguerison=:tauxguerison, tauxinfection=:tauxinfection;"
config.DbSchemaPays = "CREATE TABLE IF NOT EXISTS coronapaysdata (`id` int(8) NOT NULL AUTO_INCREMENT, `date` datetime NOT NULL, `pays` varchar(50) NOT NULL, `infection` int(8) DEFAULT NULL, `deces` int(8) DEFAULT NULL, `guerisons` varchar(50) DEFAULT NULL, `tauxdeces` float(10) DEFAULT NULL, `tauxguerison` float(10) DEFAULT NULL, `tauxinfection` float(10) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `date` (`date`,`pays`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8;" config.DbSchemaPays = "CREATE TABLE IF NOT EXISTS coronapaysdata (`id` int(8) NOT NULL AUTO_INCREMENT, `date` datetime NOT NULL, `pays` varchar(50) NOT NULL, `infection` int(8) DEFAULT NULL, `deces` int(8) DEFAULT NULL, `guerisons` varchar(50) DEFAULT NULL, `tauxdeces` float(10) DEFAULT NULL, `tauxguerison` float(10) DEFAULT NULL, `tauxinfection` float(10) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `date` (`date`,`pays`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
config.DbInsertPays = "INSERT IGNORE INTO coronapaysdata (date, pays, infection, deces, guerisons, tauxdeces, tauxguerison, tauxinfection) VALUES ('%s', '%s', %d, %d , %d, %g, %g, %g);" config.DbInsertPays = "INSERT INTO coronapaysdata (date, pays, infection, deces, guerisons, tauxdeces, tauxguerison, tauxinfection) VALUES (:date, :pays, :infection, :deces, :guerisons, :tauxdeces, :tauxguerison, :tauxinfection) ON DUPLICATE KEY UPDATE date=:date, pays=:pays,infection=:infection, deces=:deces, guerisons=:guerisons, tauxdeces=:tauxdeces, tauxguerison=:tauxguerison, tauxinfection=:tauxinfection;"
return return
} }

View File

@ -2,7 +2,6 @@ package coronafana
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
@ -34,48 +33,75 @@ func GetData(cfg config.Config) (cr Coronafana, err error) {
return return
} }
// InsertData insert data to MySQL / MariaDB // InsertGlobalData insert data to MySQL / MariaDB
func (cr Coronafana) InsertData(cfg config.Config, db sqlx.DB) (err error) { func (cr Coronafana) InsertGlobalData(cfg config.Config, db sqlx.DB) (err error) {
tx := db.MustBegin() var tx *sqlx.Tx
tx = db.MustBegin()
txStmtglobal, err := tx.PrepareNamed(cfg.DbInsertGlobal)
for _, dt := range cr.GlobalData { for _, dt := range cr.GlobalData {
query := fmt.Sprintf(cfg.DbInsertGlobal, dt.Date, dt.Infection, dt.Deces, dt.Guerisons, dt.TauxDeces, dt.TauxGuerison, dt.TauxInfection) _, err = txStmtglobal.Exec(&dt)
tx.MustExec(query) if err != nil {
return
} }
for _, dt := range cr.PaysData {
query := fmt.Sprintf(cfg.DbInsertPays, dt.Date, strings.Replace(dt.Pays, `'`, `\'`, -1), dt.Infection, dt.Deces, dt.Guerisons, dt.TauxDeces, dt.TauxGuerison, dt.TauxInfection)
tx.MustExec(query)
} }
err = tx.Commit() err = tx.Commit()
if err != nil { if err != nil {
return err return
}
return
}
// InsertPaysData insert data to MySQL / MariaDB
func (cr Coronafana) InsertPaysData(cfg config.Config, db sqlx.DB) (err error) {
var tx *sqlx.Tx
tx = db.MustBegin()
cr.ReplacePaysQuotes()
txStmtpays, err := tx.PrepareNamed(cfg.DbInsertPays)
for _, dt := range cr.PaysData {
_, err = txStmtpays.Exec(&dt)
if err != nil {
return
}
}
err = tx.Commit()
if err != nil {
return
} }
return return
} }
// ReplacePaysQuotes escapes quotes in Pays names
func (cr Coronafana) ReplacePaysQuotes() (err error) {
for _, pad := range cr.PaysData {
pad.Pays = strings.Replace(pad.Pays, `'`, `\'`, -1)
}
return err
}
// Coronafana is the main struct // Coronafana is the main struct
type Coronafana struct { type Coronafana struct {
Source string `json:"Source"` Source string `json:"Source"`
Information string `json:"Information"` Information string `json:"Information"`
Utilisation string `json:"Utilisation"` Utilisation string `json:"Utilisation"`
GlobalData []struct { GlobalData []struct {
Date string `json:"Date" gorm:"primary_key"` Date string `json:"Date" gorm:"primary_key" db:"date"`
Infection int `json:"Infection"` Infection int `json:"Infection" db:"infection"`
Deces int `json:"Deces"` Deces int `json:"Deces" db:"deces"`
Guerisons int `json:"Guerisons"` Guerisons int `json:"Guerisons" db:"guerisons"`
TauxDeces float64 `json:"TauxDeces"` TauxDeces float64 `json:"TauxDeces" db:"tauxdeces"`
TauxGuerison float64 `json:"TauxGuerison"` TauxGuerison float64 `json:"TauxGuerison" db:"tauxguerison"`
TauxInfection float64 `json:"TauxInfection"` TauxInfection float64 `json:"TauxInfection" db:"tauxinfection"`
} `json:"GlobalData"` } `json:"GlobalData"`
PaysData []struct { PaysData []struct {
Date string `json:"Date" gorm:"primary_key"` Date string `json:"Date" gorm:"primary_key" db:"date"`
Pays string `json:"Pays"` Pays string `json:"Pays" db:"pays"`
Infection int `json:"Infection"` Infection int `json:"Infection" db:"infection"`
Deces int `json:"Deces"` Deces int `json:"Deces" db:"deces"`
Guerisons int `json:"Guerisons"` Guerisons int `json:"Guerisons" db:"guerisons"`
TauxDeces float64 `json:"TauxDeces"` TauxDeces float64 `json:"TauxDeces" db:"tauxdeces"`
TauxGuerison float64 `json:"TauxGuerison"` TauxGuerison float64 `json:"TauxGuerison" db:"tauxguerison"`
TauxInfection float64 `json:"TauxInfection"` TauxInfection float64 `json:"TauxInfection" db:"tauxinfection"`
} `json:"PaysData"` } `json:"PaysData"`
} }