Skip to content

Commit d472177

Browse files
committed
Added temp limit/sort client side for ota states
1 parent 6dcc079 commit d472177

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

command/ota/status.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func PrintOtaStatus(otaid, device string, cred *config.Credentials, limit int, o
6262
otapi := otaapi.NewClient(cred)
6363

6464
if otaid != "" {
65-
res, err := otapi.GetOtaStatusByOtaID(otaid)
65+
res, err := otapi.GetOtaStatusByOtaID(otaid, limit, order)
6666
if err == nil && res != nil {
6767
formatOtaByIdOutput(&otaapi.OtaStatusPrint{
6868
Ota: res.Ota,

internal/ota-api/client.go

+25-18
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323
"fmt"
2424
"io"
2525
"net/http"
26+
"sort"
2627
"strings"
28+
"time"
2729

2830
"github.com/arduino/arduino-cloud-cli/config"
2931
"github.com/arduino/arduino-cloud-cli/internal/iot"
@@ -65,7 +67,7 @@ func (c *OtaApiClient) performGetRequest(endpoint, token string) (*http.Response
6567
return res, nil
6668
}
6769

68-
func (c *OtaApiClient) GetOtaStatusByOtaID(otaid string) (*OtaStatusResponse, error) {
70+
func (c *OtaApiClient) GetOtaStatusByOtaID(otaid string, limit int, order string) (*OtaStatusResponse, error) {
6971

7072
if otaid == "" {
7173
return nil, fmt.Errorf("invalid ota-id: empty")
@@ -95,6 +97,28 @@ func (c *OtaApiClient) GetOtaStatusByOtaID(otaid string) (*OtaStatusResponse, er
9597
return nil, err
9698
}
9799
}
100+
101+
if len(otaResponse.States) > 0 {
102+
// Sort output by StartedAt
103+
sort.Slice(otaResponse.States, func(i, j int) bool {
104+
t1, err := time.Parse(time.RFC3339, otaResponse.States[i].Timestamp)
105+
if err != nil {
106+
return false
107+
}
108+
t2, err := time.Parse(time.RFC3339, otaResponse.States[j].Timestamp)
109+
if err != nil {
110+
return false
111+
}
112+
if order == "asc" {
113+
return t1.Before(t2)
114+
}
115+
return t1.After(t2)
116+
})
117+
if limit > 0 && len(otaResponse.States) > limit {
118+
otaResponse.States = otaResponse.States[:limit]
119+
}
120+
}
121+
98122
return &otaResponse, nil
99123
} else if res.StatusCode == 404 || res.StatusCode == 400 {
100124
return nil, fmt.Errorf("ota-id %s not found", otaid)
@@ -139,23 +163,6 @@ func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string, limit int, order
139163
return nil, err
140164
}
141165
}
142-
143-
/*
144-
if len(otaResponse.Ota) > 0 {
145-
// Sort output by StartedAt
146-
sort.Slice(otaResponse.Ota, func(i, j int) bool {
147-
t1, err := time.Parse(time.RFC3339, otaResponse.Ota[i].StartedAt)
148-
if err != nil {
149-
return false
150-
}
151-
t2, err := time.Parse(time.RFC3339, otaResponse.Ota[j].StartedAt)
152-
if err != nil {
153-
return false
154-
}
155-
return t1.After(t2)
156-
})
157-
}
158-
*/
159166
return &otaResponse, nil
160167
} else if res.StatusCode == 404 || res.StatusCode == 400 {
161168
return nil, fmt.Errorf("device-id %s not found", deviceID)

0 commit comments

Comments
 (0)