You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

90 lines
2.9 KiB

4 years ago
package main
import (
log "github.com/sirupsen/logrus"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/denzs/wsjtx-dashboards/shared/wsjtx"
4 years ago
)
func handleMysql(result wsjtx.Result) {
db, dbDown := dbConn()
if dbDown {
log.Fatal("cant reach database..")
}
defer db.Close()
stmt, err := db.Prepare("INSERT INTO wsjtx_all_txt(ts, station, callsign, band, mode, report, dxcc, geohash, continent, cqzone, ituzone, rx) VALUES(FROM_UNIXTIME(?),?,?,?,?,?,?,?,?,?,?,?)")
4 years ago
defer stmt.Close()
_, err = stmt.Exec(result.Timestamp.Unix(), station, result.Call, result.Band, result.Mode, result.Signal, result.Ent.Entity, result.GeoHash, result.Ent.Continent, result.Ent.CQZone, result.Ent.ITUZone, result.Rx)
4 years ago
if err != nil {
log.WithFields(log.Fields{"err":err.Error()}).Warn("error while executing prepared statement..")
}
}
func init_db() {
var cnt int
log.Debug("init_db()")
db, _ := dbConn()
row := db.QueryRow("SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_NAME LIKE '%" + mysql_table + "%';")
switch err := row.Scan(&cnt); err {
case sql.ErrNoRows:
log.Error("something went wrong while scanning for existing table structure..")
panic(err.Error())
case nil:
default:
panic(err)
}
if cnt < 1 {
qry := "CREATE TABLE IF NOT EXISTS " + mysql_table + " (" +
"ts timestamp NOT NULL," +
"station VARCHAR(16) NOT NULL," +
"callsign VARCHAR(16) NOT NULL," +
"band VARCHAR(10) NOT NULL," +
"continent VARCHAR(32) NOT NULL," +
"mode VARCHAR(16) NOT NULL," +
"dxcc VARCHAR(128) NOT NULL," +
"geohash VARCHAR(16) NOT NULL," +
"report TINYINT NOT NULL," +
"cqzone INT NOT NULL," +
"ituzone INT NOT NULL," +
"rx TINYINT NOT NULL," +
4 years ago
"PRIMARY KEY UC_" + mysql_table + "(ts, station, callsign)," +
"INDEX idx_dxcc (dxcc));"
4 years ago
log.WithFields(log.Fields{"query":qry}).Debug("creating database..")
_, err := db.Exec(qry)
if err != nil {
log.Error("something went wrong while creating table structure..")
panic(err)
}
} else {
4 years ago
log.Info("found existing table..")
4 years ago
}
}
func dbConn() (db *sql.DB, err bool){
//db, er := sql.Open("mysql", mysql_user + ":" + mysql_pass + "@tcp(" + mysql_host + ")/" + mysql_db + "?parseTime=true&time_zone=%27UTC%27")
db, er := sql.Open("mysql", mysql_user + ":" + mysql_pass + "@tcp(" + mysql_host + ")/" + mysql_db)
4 years ago
if er != nil {
4 years ago
log.Error("db not reachable: %s",err)
4 years ago
return nil, true
}
pingerr := db.Ping()
if pingerr != nil {
4 years ago
log.Error("db not pingable..")
4 years ago
return nil, true
}
return db, false
}