Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d02c1c1

Browse files
committedJun 26, 2024·
addressed comments
1 parent fa3deee commit d02c1c1

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed
 

‎internal/ota-api/dto.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"github.com/arduino/arduino-cli/table"
2828
)
2929

30+
const progressBarMultiplier = 2
31+
3032
type (
3133
OtaStatusResponse struct {
3234
FirmwareSize *int64 `json:"firmware_size,omitempty"`
@@ -157,8 +159,12 @@ func (r OtaStatusDetail) String() string {
157159
if len(r.Details) > 0 {
158160
t = table.New()
159161
t.SetHeader("Time", "Status", "Detail")
162+
fwSize := int64(0)
163+
if r.FirmwareSize != nil {
164+
fwSize = *r.FirmwareSize
165+
}
160166
for _, s := range r.Details {
161-
stateData := formatStateData(s.State, s.StateData, r.FirmwareSize, hasReachedFlashState(r.Details))
167+
stateData := formatStateData(s.State, s.StateData, fwSize, hasReachedFlashState(r.Details))
162168
t.AddRow(formatHumanReadableTs(s.Timestamp), upperCaseFirst(s.State), stateData)
163169
}
164170
output += "\nDetails:\n" + t.Render()
@@ -169,45 +175,50 @@ func (r OtaStatusDetail) String() string {
169175

170176
func hasReachedFlashState(states []State) bool {
171177
for _, s := range states {
172-
if s.State == "flash" {
178+
if s.State == "flash" || s.State == "reboot" {
173179
return true
174180
}
175181
}
176182
return false
177183
}
178184

179-
func formatStateData(state, data string, firmware_size *int64, hasReceivedFlashState bool) string {
185+
func formatStateData(state, data string, firmware_size int64, hasReceivedFlashState bool) string {
180186
if data == "" {
181187
return ""
182188
}
183189
if state == "fetch" {
184190
// This is the state 'fetch' of OTA progress. This contains a number that represents the number of bytes fetched
185-
if hasReceivedFlashState {
186-
return buildSimpleProgressBar(float64(100))
187-
}
188-
if strings.ToLower(data) == "unknown" {
191+
if data == "Unknown" {
189192
return ""
190193
}
191194
actualDownloadedData, err := strconv.Atoi(data)
192-
if err != nil || firmware_size == nil || actualDownloadedData <= 0 || *firmware_size <= 0 { // Sanitize and avoid division by zero
195+
if err != nil || actualDownloadedData <= 0 || firmware_size <= 0 { // Sanitize and avoid division by zero
193196
return data
194197
}
195-
percentage := (float64(actualDownloadedData) / float64(*firmware_size)) * 100
198+
if hasReceivedFlashState {
199+
return buildSimpleProgressBar(float64(100))
200+
}
201+
percentage := (float64(actualDownloadedData) / float64(firmware_size)) * 100
196202
return buildSimpleProgressBar(percentage)
197203
}
198-
if strings.ToLower(data) == "unknown" {
204+
if data == "Unknown" {
199205
return ""
200206
}
201207
return data
202208
}
203209

204210
func buildSimpleProgressBar(progress float64) string {
205211
progressInt := int(progress) / 10
206-
bar := "["
207-
bar = bar + strings.Repeat("=", progressInt)
208-
bar = bar + strings.Repeat(" ", 10-progressInt)
209-
bar = bar + "] " + strconv.FormatFloat(progress, 'f', 2, 64) + "%"
210-
return bar
212+
progressInt = progressInt * progressBarMultiplier
213+
maxProgress := 10 * progressBarMultiplier
214+
var bar strings.Builder
215+
bar.WriteString("[")
216+
bar.WriteString(strings.Repeat("=", progressInt))
217+
bar.WriteString(strings.Repeat(" ", maxProgress-progressInt))
218+
bar.WriteString("] ")
219+
bar.WriteString(strconv.FormatFloat(progress, 'f', 2, 64))
220+
bar.WriteString("%")
221+
return bar.String()
211222
}
212223

213224
func upperCaseFirst(s string) string {

‎internal/ota-api/dto_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,12 @@ import (
88

99
func TestProgressBar_notCompletePct(t *testing.T) {
1010
firmwareSize := int64(25665 * 2)
11-
bar := formatStateData("fetch", "25665", &firmwareSize, false)
12-
assert.Equal(t, "[===== ] 50.00%", bar)
11+
bar := formatStateData("fetch", "25665", firmwareSize, false)
12+
assert.Equal(t, "[========== ] 50.00%", bar)
1313
}
1414

1515
func TestProgressBar_ifFlashState_goTo100Pct(t *testing.T) {
1616
firmwareSize := int64(25665 * 2)
17-
bar := formatStateData("fetch", "25665", &firmwareSize, true) // If in flash status, go to 100%
18-
assert.Equal(t, "[==========] 100.00%", bar)
19-
}
20-
21-
func TestProgressBar_ifFlashStateAndUnknown_goTo100Pct(t *testing.T) {
22-
firmwareSize := int64(25665 * 2)
23-
bar := formatStateData("fetch", "Unknown", &firmwareSize, true) // If in flash status, go to 100%
24-
assert.Equal(t, "[==========] 100.00%", bar)
17+
bar := formatStateData("fetch", "25665", firmwareSize, true) // If in flash status, go to 100%
18+
assert.Equal(t, "[====================] 100.00%", bar)
2519
}

0 commit comments

Comments
 (0)
Please sign in to comment.