From cca6c9662636bf0db951164a30f79dacac3bf141 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Sun, 27 Dec 2020 15:07:06 +0100 Subject: [PATCH] code cleaning and performances improvements --- cmd/qrz/qrz.go | 7 +++++-- src/qrz/main.go | 28 ++++++++++++++++------------ src/qrzws/main.go | 11 ++++++----- src/templates/main.go | 4 ++-- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/cmd/qrz/qrz.go b/cmd/qrz/qrz.go index 0abb7d6..173c390 100644 --- a/cmd/qrz/qrz.go +++ b/cmd/qrz/qrz.go @@ -19,8 +19,10 @@ var version string func main() { var cfg config.Config cfg.GetConfig() + cfg.Version = version + // Generate static files embedding PackrGen() err := database.Initialize(&cfg) @@ -31,15 +33,16 @@ func main() { err = qrz.InitCronConfig(cfg) if err != nil { - log.Fatalln(err) + log.Println(err) } err = qrzws.RunServer(cfg) if err != nil { - log.Fatalln(err) + log.Println(err) } } +// PackrGen act as an assets bundle maker func PackrGen() { packr.New("static", "../../static") packr.New("templates", "../../templates") diff --git a/src/qrz/main.go b/src/qrz/main.go index a24726c..388d50e 100644 --- a/src/qrz/main.go +++ b/src/qrz/main.go @@ -24,37 +24,41 @@ func InitCronConfig(config config.Config) (err error) { // Run runs the main loop func Run(config config.Config) (err error) { + existingQrz, err := getCurrentEntries(config) + if err != nil { + log.Fatalln(err) + } + for _, group := range config.QrzGroups { url := fmt.Sprintf(config.URLBase, group) - fmt.Println(fmt.Sprintf("Processing the %s group", group)) + log.Println(fmt.Sprintf("Processing the %s group with URL %s", group, url)) bodyStr, err := getBody(url) if err != nil { - log.Fatalln(err) + log.Println(err) + break } frsPeople, err := getFrsEntries(config, bodyStr) if err != nil { - log.Fatalln(err) - } - - existingQrz, err := getCurrentEntries(config) - if err != nil { - log.Fatalln(err) + log.Println(err) + break } err = discardExistingEntries(config, &frsPeople, existingQrz) if err != nil { - log.Fatalln(err) + log.Println(err) + continue } if len(frsPeople) > 0 { err = insertFrsEntryToDB(config, frsPeople) if err != nil { log.Println(err) + continue } } else { - fmt.Println( + log.Println( fmt.Sprintf("No entry to insert in database for group %s", group)) } @@ -153,7 +157,7 @@ func insertFrsEntryToDB(config config.Config, frsPeople map[string]Qrz) (err err var qrzNum int - fmt.Println( + log.Println( fmt.Sprintf("Starting insert of %d entries", len(frsPeople))) @@ -172,7 +176,7 @@ func insertFrsEntryToDB(config config.Config, frsPeople map[string]Qrz) (err err if err != nil { return err } - fmt.Println(fmt.Sprintf("Committed %d qrz entries", qrzNum)) + log.Println(fmt.Sprintf("Committed %d qrz entries", qrzNum)) return nil } diff --git a/src/qrzws/main.go b/src/qrzws/main.go index 2e1784b..3f3e41a 100644 --- a/src/qrzws/main.go +++ b/src/qrzws/main.go @@ -69,23 +69,24 @@ func Run(c echo.Context, config config.Config) (res QrzDatatableOutput, err erro rows, err := BuildQuery(config, qrzinputjson) if err != nil { - fmt.Println(err) return } - for _, b := range rows { - res.Data = append(res.Data, qrz.ToSlice(b)) + if len(rows) > 0 { + for _, b := range rows { + res.Data = append(res.Data, qrz.ToSlice(b)) + } + } else { + res.Data = append(res.Data, []string{"", "", "", "", "", ""}) } res.RecordsFiltered, err = BuildQCntFil(config, qrzinputjson) if err != nil { - fmt.Println(err, "filtered") return } res.RecordsTotal, err = BuildQCntTot(config, qrzinputjson) if err != nil { - fmt.Println(err, "total") return } diff --git a/src/templates/main.go b/src/templates/main.go index e9a8599..648bda6 100644 --- a/src/templates/main.go +++ b/src/templates/main.go @@ -3,10 +3,10 @@ package templates import ( - "fmt" "html/template" "io" "io/ioutil" + "log" "os" "strings" @@ -50,7 +50,7 @@ func BuildTemplatesDir(dir string) (builttemplates *Template, err error) { freader, _ := pkger.Open(path) tmplContent, err := ioutil.ReadAll(freader) tmpl.New(info.Name()).Parse(string(tmplContent)) - fmt.Println(info.Name(), tmplContent) + log.Println(info.Name(), tmplContent) return err })