Skip to content

Commit

Permalink
export uptime
Browse files Browse the repository at this point in the history
Summary: This will help us to estimate health of the device fleet

Differential Revision: D67601592
  • Loading branch information
leoleovich authored and facebook-github-bot committed Dec 23, 2024
1 parent c70c93c commit 9d4946b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
27 changes: 27 additions & 0 deletions calnex/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ type Version struct {
Firmware string
}

// Uptime is a struct representing Calnex uptime JSON response
type Uptime struct {
Uptime int64
}

// GNSS is a struct representing Calnex GNSS JSON response
type GNSS struct {
AntennaStatus string
Expand Down Expand Up @@ -461,6 +466,7 @@ const (
rebootURL = "https://%s/api/reboot?action=reboot"

versionURL = "https://%s/api/version"
uptimeURL = "https://%s/api/uptime"
firmwareURL = "https://%s/api/updatefirmware"
certificateURL = "https://%s/api/installcertificate"
licenseURL = "https://%s/api/option/load"
Expand Down Expand Up @@ -908,3 +914,24 @@ func (a *API) PowerSupplyStatus() (*PowerSupplyStatus, error) {

return p, nil
}

// FetchUptime returns uptime of the device
func (a *API) FetchUptime() (*Uptime, error) {
url := fmt.Sprintf(uptimeURL, a.source)
resp, err := a.Client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, errors.New(http.StatusText(resp.StatusCode))
}

u := &Uptime{}
if err = json.NewDecoder(resp.Body).Decode(u); err != nil {
return nil, err
}

return u, nil
}
21 changes: 21 additions & 0 deletions calnex/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,24 @@ func TestPowerSupplyStatusSentinel(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expected, g)
}

func TestFetchUptime(t *testing.T) {
sampleResp := "{\"uptime\": 42}"
expected := &Uptime{
Uptime: 42,
}

ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter,
r *http.Request) {
fmt.Fprintln(w, sampleResp)
}))
defer ts.Close()

parsed, _ := url.Parse(ts.URL)
calnexAPI := NewAPI(parsed.Host, true, time.Second)
calnexAPI.Client = ts.Client()

f, err := calnexAPI.FetchUptime()
require.NoError(t, err)
require.Equal(t, expected, f)
}

0 comments on commit 9d4946b

Please sign in to comment.