From 662bc0e5080b8d77d5e20ece56da04d5ad4aed2d Mon Sep 17 00:00:00 2001 From: "M.Abubakar" <156602406+saithsab877@users.noreply.github.com> Date: Mon, 23 Dec 2024 19:27:13 +0500 Subject: [PATCH] refactor: migrate db package from fmt to structured logging (#2269) --- db/alerts.go | 21 +++++++++++---------- db/config.go | 8 ++++---- db/config_test.go | 4 ++-- db/metrics.go | 2 +- db/redis.go | 11 +++++------ db/store.go | 12 ++++++------ db/test_config.go | 3 ++- db/tickets.go | 3 ++- 8 files changed, 33 insertions(+), 31 deletions(-) diff --git a/db/alerts.go b/db/alerts.go index c1f6915ab..99e260de1 100644 --- a/db/alerts.go +++ b/db/alerts.go @@ -6,10 +6,11 @@ import ( "crypto/sha256" "encoding/hex" "encoding/json" - "fmt" "net/http" "os" "strconv" + + "github.com/stakwork/sphinx-tribes/utils" ) type Action struct { @@ -36,7 +37,7 @@ func (db database) ProcessAlerts(p Person) { alertTribeUuid := os.Getenv("ALERT_TRIBE_UUID") botId := os.Getenv("ALERT_BOT_ID") if relayUrl == "" || alertSecret == "" || alertTribeUuid == "" || botId == "" { - fmt.Println("Ticket alerts: ENV information not found") + utils.Log.Info("Ticket alerts: ENV information not found") return } @@ -50,14 +51,14 @@ func (db database) ProcessAlerts(p Person) { // Check that new ticket time exists if p.NewTicketTime == 0 { - fmt.Println("Ticket alerts: New ticket time not found") + utils.Log.Info("Ticket alerts: New ticket time not found") return } var issue PropertyMap = nil wanteds, ok := p.Extras["wanted"].([]interface{}) if !ok { - fmt.Println("Ticket alerts: No tickets found for person") + utils.Log.Info("Ticket alerts: No tickets found for person") } for _, wanted := range wanteds { w, ok2 := wanted.(map[string]interface{}) @@ -76,19 +77,19 @@ func (db database) ProcessAlerts(p Person) { } if issue == nil { - fmt.Println("Ticket alerts: No ticket identified with the correct timestamp") + utils.Log.Info("Ticket alerts: No ticket identified with the correct timestamp") } languages, ok4 := issue["codingLanguage"].([]interface{}) if !ok4 { - fmt.Println("Ticket alerts: No languages found in ticket") + utils.Log.Info("Ticket alerts: No languages found in ticket") return } var err error people, err := db.GetPeopleForNewTicket(languages) if err != nil { - fmt.Println("Ticket alerts: DB query to get interested people failed", err) + utils.Log.Error("Ticket alerts: DB query to get interested people failed: %v", err) return } @@ -98,12 +99,12 @@ func (db database) ProcessAlerts(p Person) { action.Pubkey = per.OwnerPubKey buf, err := json.Marshal(action) if err != nil { - fmt.Println("Ticket alerts: Unable to parse message into byte buffer", err) + utils.Log.Error("Ticket alerts: Unable to parse message into byte buffer: %v", err) return } request, err := http.NewRequest("POST", relayUrl, bytes.NewReader(buf)) if err != nil { - fmt.Println("Ticket alerts: Unable to create a request to send to relay", err) + utils.Log.Error("Ticket alerts: Unable to create a request to send to relay: %v", err) return } @@ -115,7 +116,7 @@ func (db database) ProcessAlerts(p Person) { request.Header.Set("Content-Type", "application/json") _, err = client.Do(request) if err != nil { - fmt.Println("Ticket alerts: Unable to communicate request to relay", err) + utils.Log.Error("Ticket alerts: Unable to communicate request to relay: %v", err) } } diff --git a/db/config.go b/db/config.go index 6b42289e7..df53d3f13 100644 --- a/db/config.go +++ b/db/config.go @@ -6,6 +6,7 @@ import ( "os" "github.com/rs/xid" + "github.com/stakwork/sphinx-tribes/utils" "gopkg.in/go-playground/validator.v9" "gorm.io/driver/postgres" "gorm.io/gorm" @@ -40,7 +41,7 @@ var DB database func InitDB() { dbURL := os.Getenv("DATABASE_URL") - fmt.Printf("db url : %v", dbURL) + utils.Log.Info("db url : %v", dbURL) if dbURL == "" { rdsHost := os.Getenv("RDS_HOSTNAME") @@ -67,8 +68,7 @@ func InitDB() { } DB.db = db - - fmt.Println("db connected") + utils.Log.Info("db connected") // migrate table changes db.AutoMigrate(&Tribe{}) @@ -472,7 +472,7 @@ func (db database) ProcessUpdateTicketsWithoutGroup() { // update each ticket with group uuid for _, ticket := range tickets { - fmt.Println("ticket from process", ticket) + utils.Log.Info("ticket from process: %v", ticket) err := db.UpdateTicketsWithoutGroup(ticket) if err != nil { log.Printf("Error updating ticket: %v", err) diff --git a/db/config_test.go b/db/config_test.go index cf69243ba..2b3f385f1 100644 --- a/db/config_test.go +++ b/db/config_test.go @@ -1,11 +1,11 @@ package db import ( - "fmt" "testing" "time" "github.com/google/uuid" + "github.com/stakwork/sphinx-tribes/utils" "github.com/stretchr/testify/assert" "gorm.io/gorm" ) @@ -237,7 +237,7 @@ func TestProcessUpdateTicketsWithoutGroup(t *testing.T) { // get ticket and assert that the ticket group is the same as the ticket uuid ticket, err = TestDB.GetTicket(ticket.UUID.String()) - fmt.Println("tickets", tickets) + utils.Log.Info("tickets: %v", tickets) ticketUuid := ticket.UUID ticketAuthorID := "12345" diff --git a/db/metrics.go b/db/metrics.go index 997de887a..90e46f466 100644 --- a/db/metrics.go +++ b/db/metrics.go @@ -24,7 +24,7 @@ func (db database) TotalPeopleByPeriod(r PaymentDateRange) int64 { // convert timestamp string to int64 timestamp, err := utils.ConvertStringToInt(r.StartDate) if err != nil { - fmt.Println("Error parsing date:", err) + utils.Log.Error("Error parsing date: %v", err) } // Convert the timestamp to a time.Time object diff --git a/db/redis.go b/db/redis.go index 7171ff071..4f5ca0e40 100644 --- a/db/redis.go +++ b/db/redis.go @@ -2,7 +2,6 @@ package db import ( "context" - "fmt" "os" "time" @@ -30,27 +29,27 @@ func InitRedis() { opt, err := redis.ParseURL(redisURL) if err != nil { RedisError = err - fmt.Println("REDIS URL CONNECTION ERROR ===", err) + utils.Log.Error("REDIS URL CONNECTION ERROR === %v", err) } RedisClient = redis.NewClient(opt) } if err := RedisClient.Ping(ctx).Err(); err != nil { RedisError = err - fmt.Println("Could Not Connect To Redis", err) + utils.Log.Error("Could Not Connect To Redis: %v", err) } } func SetValue(key string, value interface{}) { err := RedisClient.Set(ctx, key, value, expireTime).Err() if err != nil { - fmt.Println("REDIS SET ERROR :", err) + utils.Log.Error("REDIS SET ERROR: %v", err) } } func GetValue(key string) string { val, err := RedisClient.Get(ctx, key).Result() if err != nil { - fmt.Println("REDIS GET ERROR :", err) + utils.Log.Error("REDIS GET ERROR: %v", err) } return val @@ -60,7 +59,7 @@ func SetMap(key string, values map[string]interface{}) { for k, v := range values { err := RedisClient.HSet(ctx, key, k, v).Err() if err != nil { - fmt.Println("REDIS SET MAP ERROR :", err) + utils.Log.Error("REDIS SET MAP ERROR: %v", err) } } RedisClient.Expire(ctx, key, expireTime) diff --git a/db/store.go b/db/store.go index b9461af5e..116d235ee 100644 --- a/db/store.go +++ b/db/store.go @@ -3,7 +3,6 @@ package db import ( "encoding/json" "errors" - "fmt" "io" "net/http" "strconv" @@ -15,6 +14,7 @@ import ( "github.com/rs/xid" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/config" + "github.com/stakwork/sphinx-tribes/utils" ) type StoreData struct { @@ -172,7 +172,7 @@ func Verify(w http.ResponseWriter, r *http.Request) { challenge := chi.URLParam(r, "challenge") _, err := Store.GetChallengeCache(challenge) if err != nil { - fmt.Println("challenge not found", err) + utils.Log.Error("challenge not found: %v", err) w.WriteHeader(http.StatusUnauthorized) return } @@ -182,7 +182,7 @@ func Verify(w http.ResponseWriter, r *http.Request) { r.Body.Close() err = json.Unmarshal(body, &payload) if err != nil { - fmt.Println(err) + utils.Log.Error("%v", err) w.WriteHeader(http.StatusNotAcceptable) return } @@ -190,7 +190,7 @@ func Verify(w http.ResponseWriter, r *http.Request) { payload.Pubkey = pubKeyFromAuth marshalled, err := json.Marshal(payload) if err != nil { - fmt.Println("payload unparseable", err) + utils.Log.Error("payload unparseable: %v", err) w.WriteHeader(http.StatusUnauthorized) return } @@ -270,14 +270,14 @@ func PostSave(w http.ResponseWriter, r *http.Request) { r.Body.Close() err = json.Unmarshal(body, &save) if err != nil { - fmt.Println(err) + utils.Log.Error("%v", err) w.WriteHeader(http.StatusNotAcceptable) return } s, err := json.Marshal(save) if err != nil { - fmt.Println("save payload unparseable", err) + utils.Log.Error("save payload unparseable: %v", err) w.WriteHeader(http.StatusUnauthorized) return } diff --git a/db/test_config.go b/db/test_config.go index 58901199b..9b74a03b9 100644 --- a/db/test_config.go +++ b/db/test_config.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/rs/xid" + "github.com/stakwork/sphinx-tribes/utils" "gorm.io/driver/postgres" "gorm.io/gorm" ) @@ -35,7 +36,7 @@ func InitTestDB() { TestDB.db = db - fmt.Println("DB CONNECTED") + utils.Log.Info("DB CONNECTED") // migrate table changes db.AutoMigrate(&Tribe{}) diff --git a/db/tickets.go b/db/tickets.go index b67197520..5c1fd5b98 100644 --- a/db/tickets.go +++ b/db/tickets.go @@ -6,6 +6,7 @@ import ( "time" "github.com/google/uuid" + "github.com/stakwork/sphinx-tribes/utils" "gorm.io/gorm" ) @@ -181,7 +182,7 @@ func (db database) UpdateTicketsWithoutGroup(ticket Tickets) error { data["author"] = "HUMAN" } - fmt.Println("data ===", data) + utils.Log.Info("data === %v", data) result := db.db.Model(&Tickets{}).Where("uuid = ?", ticket.UUID).Updates(data)