updated qrz sources

This commit is contained in:
Paul 2020-08-16 16:56:03 +02:00
parent 55759f8e65
commit 4cf0aaeb58
4 changed files with 37 additions and 43 deletions

View File

@ -12,6 +12,7 @@ import (
func (config *Config) GetConfig() error { func (config *Config) GetConfig() error {
var configfile string var configfile string
var nofeed bool var nofeed bool
var debug bool
var port int var port int
flag.Usage = utils.Usage flag.Usage = utils.Usage
@ -19,6 +20,7 @@ func (config *Config) GetConfig() error {
flag.StringVar(&configfile, "configfile", "qrz.ini", "config file to use with qrz section") flag.StringVar(&configfile, "configfile", "qrz.ini", "config file to use with qrz section")
flag.IntVar(&port, "port", 8080, "web port to use") flag.IntVar(&port, "port", 8080, "web port to use")
flag.BoolVar(&nofeed, "nofeed", false, "no feed database table with entries at first launch") flag.BoolVar(&nofeed, "nofeed", false, "no feed database table with entries at first launch")
flag.BoolVar(&debug, "debug", false, "if debug logging must be enabled")
flag.Parse() flag.Parse()
cfg, err := ini.Load(configfile) cfg, err := ini.Load(configfile)
@ -30,6 +32,7 @@ func (config *Config) GetConfig() error {
config.Port = port config.Port = port
config.NoFeed = nofeed config.NoFeed = nofeed
config.Debug = debug
config.DbHostname = qrzsection.Key("db_hostname").MustString("localhost") config.DbHostname = qrzsection.Key("db_hostname").MustString("localhost")
config.DbName = qrzsection.Key("db_name").MustString("database") config.DbName = qrzsection.Key("db_name").MustString("database")
config.DbUsername = qrzsection.Key("db_username").MustString("username") config.DbUsername = qrzsection.Key("db_username").MustString("username")
@ -55,5 +58,6 @@ type Config struct {
Cron string Cron string
Port int Port int
NoFeed bool NoFeed bool
Debug bool
Version string Version string
} }

View File

@ -20,7 +20,7 @@ func Initialize(config *config.Config) (err error) {
config.Db.SetMapper(names.GonicMapper{}) config.Db.SetMapper(names.GonicMapper{})
config.Db.CreateTables(qrz.Qrz{}) config.Db.CreateTables(qrz.Qrz{})
config.Db.ShowSQL(true) config.Db.ShowSQL(config.Debug)
return return
} }

View File

@ -167,15 +167,32 @@ func insertFrsEntryToDB(config config.Config, frsPeople map[string]Qrz) (err err
return nil return nil
} }
// ToSlice converts struct to array
/* func ToSlice(qrz Qrz) (out []interface{}) {
val := reflect.ValueOf(qrz)
num := val.NumField()
for i := 1; i < num-1; i++ {
out = append(out, val.Field(i).Interface())
}
return
} */
// ToSlice converts struct to array
func ToSlice(qrz Qrz) (out []string) {
out = []string{qrz.QRZ, qrz.Name, qrz.City, qrz.Dept, qrz.Country, qrz.DMRID}
return
}
// Qrz describe FRS people // Qrz describe FRS people
type Qrz struct { type Qrz struct {
ID int `db:"id" xorm:"pk autoincr"` ID int `db:"id" xorm:"pk autoincr"`
QRZ string `db:"qrz" xorm:"varchar(25) notnull"` QRZ string `db:"qrz" xorm:"varchar(25) notnull"`
DMRID string `db:"dmrid" xorm:"varchar(25) notnull"`
Name string `db:"name" xorm:"varchar(25) notnull"` Name string `db:"name" xorm:"varchar(25) notnull"`
Address string `db:"address" xorm:"varchar(50) notnull"` Address string `db:"address" xorm:"varchar(50) notnull"`
City string `db:"city" xorm:"varchar(50) notnull"` City string `db:"city" xorm:"varchar(50) notnull"`
Zipcode string `db:"zipcode" xorm:"varchar(5) notnull"` Zipcode string `db:"zipcode" xorm:"varchar(5) notnull"`
Dept string `db:"dept" xorm:"varchar(25) notnull"` Dept string `db:"dept" xorm:"varchar(25) notnull"`
Country string `db:"country" xorm:"varchar(25) notnull"` Country string `db:"country" xorm:"varchar(25) notnull"`
DMRID string `db:"dmrid" xorm:"varchar(25) notnull"`
} }

View File

@ -2,6 +2,7 @@ package qrzws
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
@ -40,19 +41,12 @@ func RunServer(config config.Config) (err error) {
} }
return c.JSON(http.StatusOK, res) return c.JSON(http.StatusOK, res)
}) })
/*e.GET("/export_frs.csv", func(c echo.Context) (err error) { e.GET("/export_frs.csv", func(c echo.Context) (err error) {
data, mime, err := RunCSVExport(c, config) data, mime, err := RunCSVExport(c, config)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
return c.Blob(http.StatusOK, mime, data) return c.Blob(http.StatusOK, mime, data)
})*/
e.GET("/test", func(c echo.Context) (err error) {
res, err := Test(c, config)
if err != nil {
return c.String(http.StatusInternalServerError, fmt.Sprintf("Erreur 500 ta mère: %d", res))
}
return c.JSON(http.StatusOK, res)
}) })
if !config.NoFeed { if !config.NoFeed {
@ -62,14 +56,6 @@ func RunServer(config config.Config) (err error) {
return return
} }
// Test runs the main loop
func Test(c echo.Context, config config.Config) (cnt int64, err error) {
//config.Db.Where("qrz LIKE '%%' OR name LIKE '%%' OR city LIKE '%%' OR dept LIKE '%%' OR country LIKE '%%' OR dmrid LIKE '%%'").Limit(50, 2).Desc("id").Select("*").Find(&qrz)
cnt, err = config.Db.SQL("select count(*) from qrz").Count()
return
}
// Run runs the main loop // Run runs the main loop
func Run(c echo.Context, config config.Config) (res QrzDatatableOutput, err error) { func Run(c echo.Context, config config.Config) (res QrzDatatableOutput, err error) {
qrzinputjson := new(QrzDatatableInput) qrzinputjson := new(QrzDatatableInput)
@ -85,7 +71,7 @@ func Run(c echo.Context, config config.Config) (res QrzDatatableOutput, err erro
} }
for _, b := range rows { for _, b := range rows {
res.Data = append(res.Data, b) res.Data = append(res.Data, qrz.ToSlice(b))
} }
res.RecordsFiltered, err = BuildQueryCountFiltered(config, *qrzinputjson) res.RecordsFiltered, err = BuildQueryCountFiltered(config, *qrzinputjson)
@ -141,14 +127,14 @@ func BuildQuery(config config.Config, qrzdt QrzDatatableInput) (qrz []qrz.Qrz, e
// BuildQueryCountFiltered builds query for counting filtered // BuildQueryCountFiltered builds query for counting filtered
func BuildQueryCountFiltered(config config.Config, qrzdt QrzDatatableInput) (cnt int64, err error) { func BuildQueryCountFiltered(config config.Config, qrzdt QrzDatatableInput) (cnt int64, err error) {
searchstatement, err := SetSearchLike(config, qrzdt) searchstatement, err := SetSearchLike(config, qrzdt)
cnt, err = config.Db.SQL(fmt.Sprintf("select count(*) from qrz WHERE %s", searchstatement)).Count() cnt, err = config.Db.SQL(fmt.Sprintf("SELECT COUNT(*) FROM qrz WHERE %s", searchstatement)).Count()
return return
} }
// BuildQueryCountTotal builds query for counting totals // BuildQueryCountTotal builds query for counting totals
func BuildQueryCountTotal(config config.Config, qrzdt QrzDatatableInput) (cnt int64, err error) { func BuildQueryCountTotal(config config.Config, qrzdt QrzDatatableInput) (cnt int64, err error) {
cnt, err = config.Db.SQL("select count(*) from qrz").Count() cnt, err = config.Db.SQL("SELECT COUNT(*) FROM qrz").Count()
return return
} }
@ -233,33 +219,20 @@ func SetSearchLike(config config.Config, qrzdt QrzDatatableInput) (searchstmt st
return return
} }
/*
// RunCSVExport runs the main loop // RunCSVExport runs the main loop
func RunCSVExport(c echo.Context, config config.Config) (data []byte, mime string, err error) { func RunCSVExport(c echo.Context, config config.Config) (data []byte, mime string, err error) {
mime = "text/csv" mime = "text/csv"
res, err := config.Db.Exec(config.DbStatements.ExportCSV) var q []qrz.Qrz
var res []string var res []string
res = append(res, "QRZ,Name,City,Dept,Country") err = config.Db.Asc("qrz").Find(&q)
for rows.Next() { res = append(res, "QRZ,Name,City,Dept,Country,DMRID")
var l []string for _, item := range q {
results := make(map[string]interface{}) res = append(res, strings.Join(qrz.ToSlice(item), ","))
err = rows.MapScan(results)
colslice, _ := rows.Columns()
for _, column := range colslice {
if results[column] == nil {
l = append(l, "")
} else {
l = append(l, fmt.Sprintf("%s", results[column]))
}
}
line := strings.Join(l, ",")
res = append(res, line)
} }
data = []byte(strings.Join(res, "\n")) data = []byte(strings.Join(res, "\n"))
return return
} }
*/
// QrzDatatableInput describes datatable query // QrzDatatableInput describes datatable query
type QrzDatatableInput struct { type QrzDatatableInput struct {
@ -288,8 +261,8 @@ type QrzDatatableInput struct {
// QrzDatatableOutput describes datatable response // QrzDatatableOutput describes datatable response
type QrzDatatableOutput struct { type QrzDatatableOutput struct {
Data []qrz.Qrz `json:"data"` Data [][]string `json:"data"`
Draw int `json:"draw"` Draw int `json:"draw"`
RecordsTotal int64 `json:"recordsTotal"` RecordsTotal int64 `json:"recordsTotal"`
RecordsFiltered int64 `json:"recordsFiltered"` RecordsFiltered int64 `json:"recordsFiltered"`
} }