Skip to content

Commit ea7cfdc

Browse files
authored
Ota progress status details with progress bar fixes (#159)
1 parent 5483ab3 commit ea7cfdc

File tree

3 files changed

+57
-29
lines changed

3 files changed

+57
-29
lines changed

command/ota/status.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ func PrintOtaStatus(otaid, otaids, device string, cred *config.Credentials, limi
4444
res, err := otapi.GetOtaStatusByOtaID(otaid, limit, order)
4545
if err == nil && res != nil {
4646
feedback.PrintResult(otaapi.OtaStatusDetail{
47-
FirmwareSize: res.FirmwareSize,
47+
FirmwareSize: res.Ota.FirmwareSize,
4848
Ota: res.Ota,
4949
Details: res.States,
50+
MaxRetries: res.Ota.MaxRetries,
51+
RetryAttempt: res.Ota.RetryAttempt,
5052
})
5153
} else if err != nil {
5254
return err

internal/ota-api/dto.go

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,24 @@ const progressBarMultiplier = 2
3131

3232
type (
3333
OtaStatusResponse struct {
34-
FirmwareSize *int64 `json:"firmware_size,omitempty"`
35-
Ota Ota `json:"ota"`
36-
States []State `json:"states,omitempty"`
34+
Ota Ota `json:"ota"`
35+
States []State `json:"states,omitempty"`
3736
}
3837

3938
OtaStatusList struct {
4039
Ota []Ota `json:"ota"`
4140
}
4241

4342
Ota struct {
44-
ID string `json:"id,omitempty" yaml:"id,omitempty"`
45-
DeviceID string `json:"device_id,omitempty" yaml:"device_id,omitempty"`
46-
Status string `json:"status" yaml:"status"`
47-
StartedAt string `json:"started_at" yaml:"started_at"`
48-
EndedAt string `json:"ended_at,omitempty" yaml:"ended_at,omitempty"`
49-
ErrorReason string `json:"error_reason,omitempty" yaml:"error_reason,omitempty"`
43+
ID string `json:"id,omitempty" yaml:"id,omitempty"`
44+
DeviceID string `json:"device_id,omitempty" yaml:"device_id,omitempty"`
45+
Status string `json:"status" yaml:"status"`
46+
StartedAt string `json:"started_at" yaml:"started_at"`
47+
EndedAt string `json:"ended_at,omitempty" yaml:"ended_at,omitempty"`
48+
ErrorReason string `json:"error_reason,omitempty" yaml:"error_reason,omitempty"`
49+
FirmwareSize int64 `json:"firmware_size,omitempty"`
50+
MaxRetries int64 `json:"max_retries,omitempty"`
51+
RetryAttempt int64 `json:"retry_attempt,omitempty"`
5052
}
5153

5254
State struct {
@@ -57,7 +59,9 @@ type (
5759
}
5860

5961
OtaStatusDetail struct {
60-
FirmwareSize *int64 `json:"firmware_size,omitempty"`
62+
FirmwareSize int64 `json:"firmware_size,omitempty"`
63+
MaxRetries int64 `json:"max_retries,omitempty"`
64+
RetryAttempt int64 `json:"retry_attempt,omitempty"`
6165
Ota Ota `json:"ota"`
6266
Details []State `json:"details,omitempty"`
6367
}
@@ -81,7 +85,7 @@ func (r OtaStatusList) String() string {
8185
}
8286

8387
if hasErrorReason {
84-
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At", "Error Reason")
88+
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At", "Error Reason", "Retry Attempt")
8589
} else {
8690
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At")
8791
}
@@ -91,6 +95,7 @@ func (r OtaStatusList) String() string {
9195
line := []any{r.DeviceID, r.ID, r.MapStatus(), formatHumanReadableTs(r.StartedAt), formatHumanReadableTs(r.EndedAt)}
9296
if hasErrorReason {
9397
line = append(line, r.ErrorReason)
98+
line = append(line, strconv.FormatInt(r.RetryAttempt, 10))
9499
}
95100
t.AddRow(line...)
96101
}
@@ -114,7 +119,7 @@ func (r Ota) String() string {
114119
hasErrorReason := r.ErrorReason != ""
115120

116121
if hasErrorReason {
117-
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At", "Error Reason")
122+
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At", "Error Reason", "Retry Attempt")
118123
} else {
119124
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At")
120125
}
@@ -123,6 +128,7 @@ func (r Ota) String() string {
123128
line := []any{r.DeviceID, r.ID, r.MapStatus(), formatHumanReadableTs(r.StartedAt), formatHumanReadableTs(r.EndedAt)}
124129
if hasErrorReason {
125130
line = append(line, r.ErrorReason)
131+
line = append(line, strconv.FormatInt(r.RetryAttempt, 10))
126132
}
127133
t.AddRow(line...)
128134

@@ -138,18 +144,21 @@ func (r OtaStatusDetail) String() string {
138144
return "No OTA found"
139145
}
140146
t := table.New()
141-
hasErrorReason := r.Ota.ErrorReason != ""
142147

143-
if hasErrorReason {
144-
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At", "Error Reason")
148+
succeeded := strings.ToLower(r.Ota.Status) == "succeeded"
149+
hasError := r.Ota.ErrorReason != "" || !succeeded
150+
151+
if hasError {
152+
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At", "Error Reason", "Retry Attempt")
145153
} else {
146154
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At")
147155
}
148156

149157
// Now print the table
150158
line := []any{r.Ota.DeviceID, r.Ota.ID, r.Ota.MapStatus(), formatHumanReadableTs(r.Ota.StartedAt), formatHumanReadableTs(r.Ota.EndedAt)}
151-
if hasErrorReason {
159+
if hasError {
152160
line = append(line, r.Ota.ErrorReason)
161+
line = append(line, strconv.FormatInt(r.RetryAttempt, 10))
153162
}
154163
t.AddRow(line...)
155164

@@ -160,22 +169,37 @@ func (r OtaStatusDetail) String() string {
160169
t = table.New()
161170
t.SetHeader("Time", "Status", "Detail")
162171
fwSize := int64(0)
163-
if r.FirmwareSize != nil {
164-
fwSize = *r.FirmwareSize
172+
if r.FirmwareSize > 0 {
173+
fwSize = r.FirmwareSize
174+
}
175+
176+
firstTS := formatHumanReadableTs(r.Details[0].Timestamp)
177+
if !containsResetState(r.Details) && !hasError {
178+
t.AddRow(firstTS, "Flash", "")
165179
}
180+
181+
hasReachedFlashState := hasReachedFlashState(r.Details, succeeded)
166182
for _, s := range r.Details {
167-
stateData := formatStateData(s.State, s.StateData, fwSize, hasReachedFlashState(r.Details))
183+
stateData := formatStateData(s.State, s.StateData, fwSize, hasReachedFlashState)
168184
t.AddRow(formatHumanReadableTs(s.Timestamp), upperCaseFirst(s.State), stateData)
169185
}
186+
170187
output += "\nDetails:\n" + t.Render()
171188
}
172189

173190
return output
174191
}
175192

176-
func hasReachedFlashState(states []State) bool {
193+
func hasReachedFlashState(states []State, succeeded bool) bool {
194+
if succeeded {
195+
return true
196+
}
197+
return containsResetState(states)
198+
}
199+
200+
func containsResetState(states []State) bool {
177201
for _, s := range states {
178-
if s.State == "flash" || s.State == "reboot" {
202+
if strings.ToLower(s.State) == "flash" || strings.ToLower(s.State) == "reboot" {
179203
return true
180204
}
181205
}
@@ -193,15 +217,15 @@ func formatStateData(state, data string, firmware_size int64, hasReceivedFlashSt
193217
return data
194218
}
195219
if hasReceivedFlashState {
196-
return buildSimpleProgressBar(float64(100))
220+
return buildSimpleProgressBar(float64(100), firmware_size)
197221
}
198222
percentage := (float64(actualDownloadedData) / float64(firmware_size)) * 100
199-
return buildSimpleProgressBar(percentage)
223+
return buildSimpleProgressBar(percentage, firmware_size)
200224
}
201225
return data
202226
}
203227

204-
func buildSimpleProgressBar(progress float64) string {
228+
func buildSimpleProgressBar(progress float64, fw_size int64) string {
205229
progressInt := int(progress) / 10
206230
progressInt = progressInt * progressBarMultiplier
207231
maxProgress := 10 * progressBarMultiplier
@@ -210,8 +234,10 @@ func buildSimpleProgressBar(progress float64) string {
210234
bar.WriteString(strings.Repeat("=", progressInt))
211235
bar.WriteString(strings.Repeat(" ", maxProgress-progressInt))
212236
bar.WriteString("] ")
213-
bar.WriteString(strconv.FormatFloat(progress, 'f', 2, 64))
214-
bar.WriteString("%")
237+
bar.WriteString(strconv.FormatFloat(progress, 'f', 0, 64))
238+
bar.WriteString("% (firmware size: ")
239+
bar.WriteString(strconv.FormatInt(fw_size, 10))
240+
bar.WriteString(" bytes)")
215241
return bar.String()
216242
}
217243

internal/ota-api/dto_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
func TestProgressBar_notCompletePct(t *testing.T) {
1010
firmwareSize := int64(25665 * 2)
1111
bar := formatStateData("fetch", "25665", firmwareSize, false)
12-
assert.Equal(t, "[========== ] 50.00%", bar)
12+
assert.Equal(t, "[========== ] 50% (firmware size: 51330 bytes)", bar)
1313
}
1414

1515
func TestProgressBar_ifFlashState_goTo100Pct(t *testing.T) {
1616
firmwareSize := int64(25665 * 2)
1717
bar := formatStateData("fetch", "25665", firmwareSize, true) // If in flash status, go to 100%
18-
assert.Equal(t, "[====================] 100.00%", bar)
18+
assert.Equal(t, "[====================] 100% (firmware size: 51330 bytes)", bar)
1919
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy