Skip to content

Commit

Permalink
refactor: migrate db package from fmt to structured logging (#2269)
Browse files Browse the repository at this point in the history
  • Loading branch information
saithsab877 authored Dec 23, 2024
1 parent 7d1fee9 commit 662bc0e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 31 deletions.
21 changes: 11 additions & 10 deletions db/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand All @@ -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{})
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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)
}
}

Expand Down
8 changes: 4 additions & 4 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -67,8 +68,7 @@ func InitDB() {
}

DB.db = db

fmt.Println("db connected")
utils.Log.Info("db connected")

// migrate table changes
db.AutoMigrate(&Tribe{})
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions db/config_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion db/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package db

import (
"context"
"fmt"
"os"
"time"

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions db/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package db
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -182,15 +182,15 @@ 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
}

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
}
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion db/test_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/rs/xid"
"github.com/stakwork/sphinx-tribes/utils"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -35,7 +36,7 @@ func InitTestDB() {

TestDB.db = db

fmt.Println("DB CONNECTED")
utils.Log.Info("DB CONNECTED")

// migrate table changes
db.AutoMigrate(&Tribe{})
Expand Down
3 changes: 2 additions & 1 deletion db/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/stakwork/sphinx-tribes/utils"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 662bc0e

Please sign in to comment.