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.
 
 
 
 

121 lines
3.8 KiB

package main
import (
log "github.com/sirupsen/logrus"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/denzs/wsjtx-exporter/shared/wsjtx"
"errors"
"fmt"
)
func getLast(station string, identifier string) (LastTs, error) {
var result LastTs
db, dbDown := dbConn()
if dbDown {
log.Fatal("cant reach database..")
}
defer db.Close()
log.Info("starting query...")
err := db.QueryRow("SELECT UNIX_TIMESTAMP(ts) FROM wsjtx_all_txt WHERE station = ? AND identifier = ? ORDER BY ts DESC LIMIT 1", station, identifier).Scan(&result.Last)
log.Info("finished query...")
if err != nil {
// FIXME
if err.Error() == "sql: no rows in result set" {
log.Infof("uuuh yeah! this is a nice one! ;)")
return result, errors.New("not found")
} else {
log.Infof("well.. shit happend while doing ts lookup: %s", err.Error())
return result, errors.New(fmt.Sprintf("some other weird shit happened: %s", err.Error()))
}
}
log.Infof("last timestamp in db is: %d",result.Last)
return result, nil
}
func handleMysql(result wsjtx.Result) {
log.Info("starting mysq stuff..")
db, dbDown := dbConn()
if dbDown {
log.Fatal("cant reach database..")
}
defer db.Close()
stmt, err := db.Prepare("INSERT INTO wsjtx_all_txt(ts, station, identifier, callsign, band, mode, report, dxcc, geohash, continent, cqzone, ituzone, rx) VALUES(FROM_UNIXTIME(?),?,?,?,?,?,?,?,?,?,?,?,?)")
defer stmt.Close()
_, err = stmt.Exec(result.Timestamp.Unix(), result.Station, result.Identifier, result.Call, result.Band, result.Mode, result.Signal, result.Ent.Entity, result.GeoHash, result.Ent.Continent, result.Ent.CQZone, result.Ent.ITUZone, result.Rx)
if err != nil {
log.WithFields(log.Fields{"warning":err.Error()}).Warn("error while executing prepared statement..")
} else {
log.Info("insert done! :)")
}
}
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 '%" + mysqlTable + "%';")
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 " + mysqlTable + " (" +
"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," +
"PRIMARY KEY UC_" + mysqlTable + "(ts, station, callsign)," +
"INDEX idx_dxcc (dxcc));"
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 {
log.Info("found existing table..")
}
}
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", mysqlUser + ":" + mysqlPass + "@tcp(" + mysqlHost + ")/" + mysqlDb)
if er != nil {
log.Error("db not reachable: %s",err)
return nil, true
}
pingerr := db.Ping()
if pingerr != nil {
log.Error("db not pingable..")
return nil, true
}
return db, false
}