-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway_stats.go
117 lines (102 loc) · 2.75 KB
/
gateway_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package collector
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
type Duration struct {
time.Duration
}
// Go seems to have issues parsing time.Duration from a JSON: https://github.com/golang/go/issues/10275
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case string:
var err error
d.Duration, err = time.ParseDuration(value)
if err != nil {
return err
}
return nil
default:
return errors.New("invalid duration")
}
}
type GatewayStatsResponse struct {
Code int `json:"code"`
Protocol string `json:"protocol"`
LastStatus struct {
Time time.Time `json:"time"`
Versions struct {
GatewayServerVersion *string `json:"ttn-lw-gateway-server"`
Firmware *string `json:"firmware"`
Package *string `json:"package"`
Platform *string `json:"platform"`
Station *string `json:"station"`
} `json:"versions"`
IP []string `json:"ip"`
Metrics struct {
Ackr float64 `json:"ackr"`
TxIn float64 `json:"txin"`
TxOk float64 `json:"txok"`
RxIn float64 `json:"rxin"`
RxOk float64 `json:"rxok"`
RxFw float64 `json:"rxfw"`
} `json:"metrics"`
} `json:"last_status"`
UplinkCount string `json:"uplink_count"`
DownlinkCount string `json:"downlink_count"`
TxAcknowledgementCount string `json:"tx_acknowledgement_count"`
RoundTripTimes *struct {
Min Duration `json:"min"`
Max Duration `json:"max"`
Median Duration `json:"median"`
Count int64 `json:"count"`
} `json:"round_trip_times"`
GatewayRemoteAddress struct {
IP string `json:"ip"`
} `json:"gateway_remote_address"`
}
func getGatewayStats(client *http.Client, uri string, apiKey string, gatewayID string) (*GatewayStatsResponse, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
u.Path = fmt.Sprintf("/api/v3/gs/gateways/%s/connection/stats", gatewayID)
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Authorization", "Bearer "+apiKey)
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer func() {
_ = res.Body.Close()
}()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP status %d on getting gateway stats", res.StatusCode)
}
var gatewayStats *GatewayStatsResponse
err = json.Unmarshal(body, &gatewayStats)
if err != nil {
return nil, err
}
if gatewayStats.Code != 0 {
return nil, fmt.Errorf("Gateway stats response code is %d", gatewayStats.Code)
}
return gatewayStats, nil
}