Skip to content

Commit

Permalink
fix: unmarshal error (#35)
Browse files Browse the repository at this point in the history
* fix: unmarshal error

* fix: downgrade go version

* fix: tier field

* fix: add back error log
  • Loading branch information
martabal authored Mar 13, 2024
1 parent b4b8a7d commit 2bb14bb
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: "1.22"
go-version: "1.22.1"

- name: Build
run: go build .
8 changes: 4 additions & 4 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ on:
pull_request:
branches: ["main"]

permissions:
packages: write

jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: "main"
fetch-depth: 0

- name: Docker meta
id: meta
Expand Down Expand Up @@ -57,7 +57,7 @@ jobs:
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GH_TOKEN }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v5
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: "1.22"
go-version: "1.22.1"

- name: Install dependencies
run: go get .
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22-alpine3.19 AS builder
FROM golang:1.22.1-alpine3.19 AS builder

ARG BUILD_VERSION

Expand Down
18 changes: 10 additions & 8 deletions src/api/qbittorrent.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package API

import "encoding/json"

type Info []struct {
AmountLeft int `json:"amount_left"`
Category string `json:"category"`
Expand Down Expand Up @@ -55,14 +57,14 @@ type Category struct {
}

type Trackers []struct {
Message string `json:"msg"`
NumDownloaded int `json:"num_downloaded"`
NumLeeches int `json:"num_leeches"`
NumPeers int `json:"num_peers"`
NumSeeds int `json:"num_seeds"`
Status int `json:"status"`
Tier int `json:"tier"`
URL string `json:"url"`
Message string `json:"msg"`
NumDownloaded int `json:"num_downloaded"`
NumLeeches int `json:"num_leeches"`
NumPeers int `json:"num_peers"`
NumSeeds int `json:"num_seeds"`
Status int `json:"status"`
Tier json.RawMessage `json:"tier"`
URL string `json:"url"`
}

type Transfer struct {
Expand Down
12 changes: 6 additions & 6 deletions src/go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module qbit-exp

go 1.22
go 1.22.1

require (
github.com/joho/godotenv v1.5.1
github.com/prometheus/client_golang v1.18.0
github.com/prometheus/client_golang v1.19.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/prometheus/client_model v0.6.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
golang.org/x/sys v0.17.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
github.com/prometheus/common v0.50.0 // indirect
github.com/prometheus/procfs v0.13.0 // indirect
golang.org/x/sys v0.18.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
20 changes: 10 additions & 10 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
github.com/prometheus/client_model v0.6.0 h1:k1v3CzpSRUTrKMppY35TLwPvxHqBu0bYgxZzqGIgaos=
github.com/prometheus/client_model v0.6.0/go.mod h1:NTQHnmxFpouOD0DpvP4XujX3CdOAGQPoaGhyTchlyt8=
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
github.com/prometheus/common v0.50.0 h1:YSZE6aa9+luNa2da6/Tik0q0A5AbR+U003TItK57CPQ=
github.com/prometheus/common v0.50.0/go.mod h1:wHFBCEVWVmHMUpg7pYcOm2QUR/ocQdYSJVQJKnHc3xQ=
github.com/prometheus/procfs v0.13.0 h1:GqzLlQyfsPbaEHaQkO7tbDlriv/4o5Hudv6OXHGKX7o=
github.com/prometheus/procfs v0.13.0/go.mod h1:cd4PFCR54QLnGKPaKGA6l+cfuNXtht43ZKY6tow0Y1g=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
2 changes: 1 addition & 1 deletion src/logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type PrettyHandler struct {

func (h *PrettyHandler) Handle(ctx context.Context, r slog.Record) error {
level := r.Level.String()
timeStr := r.Time.Format("[02/01/2023 15:04:05]")
timeStr := fmt.Sprintf("[%02d-%02d-%04d %02d:%02d:%02d]", r.Time.Year(), r.Time.Month(), r.Time.Day(), r.Time.Hour(), r.Time.Minute(), r.Time.Second())

switch r.Level {
case slog.LevelDebug:
Expand Down
6 changes: 5 additions & 1 deletion src/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,18 @@ func Sendbackmessagetrackers(result []*API.Trackers, r *prometheus.Registry) {
for _, listOfTracker := range result {
for _, tracker := range *listOfTracker {
if IsValidURL(tracker.URL) {
tier, err := strconv.Atoi(string(tracker.Tier))
if err != nil {
tier = 0
}
qbittorrent_tracker_info.With(prometheus.Labels{
"message": tracker.Message,
"downloaded": strconv.Itoa(tracker.NumDownloaded),
"leeches": strconv.Itoa(tracker.NumLeeches),
"peers": strconv.Itoa(tracker.NumPeers),
"seeders": strconv.Itoa(int(tracker.NumSeeds)),
"status": strconv.Itoa((tracker.Status)),
"tier": strconv.Itoa((tracker.Tier)),
"tier": strconv.Itoa(tier),
"url": tracker.URL}).Set(1)
}
}
Expand Down
26 changes: 19 additions & 7 deletions src/qbit/qbit.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Data struct {
QueryParams *[]QueryParams
}

const UnmarshError = "Can not unmarshal JSON for preferences"
const UnmarshError = "Can not unmarshal JSON for "

var info = []Data{
{
Expand Down Expand Up @@ -84,11 +84,14 @@ func getData(r *prometheus.Registry, data Data, goroutine bool) bool {
if err != nil {
return false
}
unmarshErr := UnmarshError + data.Ref
switch data.Ref {
case "info":
result := new(API.Info)
if err := json.Unmarshal(body, &result); err != nil {
logger.Log.Error(UnmarshError)
logger.Log.Debug(string(body))
logger.Log.Debug(err.Error())
logger.Log.Error(unmarshErr)
} else {
prom.Sendbackmessagetorrent(result, r)
if !models.GetFeatureFlag() {
Expand All @@ -99,14 +102,18 @@ func getData(r *prometheus.Registry, data Data, goroutine bool) bool {
case "maindata":
result := new(API.Maindata)
if err := json.Unmarshal(body, &result); err != nil {
logger.Log.Error(UnmarshError)
logger.Log.Debug(string(body))
logger.Log.Debug(err.Error())
logger.Log.Error(unmarshErr)
} else {
prom.Sendbackmessagemaindata(result, r)
}
case "preference":
result := new(API.Preferences)
if err := json.Unmarshal(body, &result); err != nil {
logger.Log.Error(UnmarshError)
logger.Log.Debug(string(body))
logger.Log.Debug(err.Error())
logger.Log.Error(unmarshErr)
} else {
prom.Sendbackmessagepreference(result, r)
}
Expand All @@ -123,7 +130,9 @@ func getData(r *prometheus.Registry, data Data, goroutine bool) bool {
case "transfer":
result := new(API.Transfer)
if err := json.Unmarshal(body, &result); err != nil {
logger.Log.Error(UnmarshError)
logger.Log.Debug(string(body))
logger.Log.Debug(err.Error())
logger.Log.Error(unmarshErr)
} else {
prom.Sendbackmessagetransfer(result, r)
}
Expand All @@ -147,8 +156,11 @@ func getTrackersInfo(data Data, c chan func() (*API.Trackers, error)) {
c <- (func() (*API.Trackers, error) { return nil, err })
}
result := new(API.Trackers)
unmarshErr := UnmarshError + "tracker"
if err := json.Unmarshal(body, &result); err != nil {
logger.Log.Error("Can not unmarshal JSON for preferences")
logger.Log.Debug(string(body))
logger.Log.Debug(err.Error())
logger.Log.Error(unmarshErr)
} else {
c <- (func() (*API.Trackers, error) { return result, err })
}
Expand Down Expand Up @@ -231,7 +243,7 @@ func Apirequest(uri string, method string, queryParams *[]QueryParams) (*http.Re
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
err := fmt.Errorf("Can't connect to server")
err := fmt.Errorf("can't connect to server")
if !models.GetPromptError() {
logger.Log.Debug(err.Error())
models.SetPromptError(true)
Expand Down

0 comments on commit 2bb14bb

Please sign in to comment.