Skip to content

Commit 049a630

Browse files
committed
fix empty progress
1 parent 5deb69a commit 049a630

File tree

4 files changed

+87
-25
lines changed

4 files changed

+87
-25
lines changed

internal/update/apt/service.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,15 @@ func (s *Service) UpgradePackages(ctx context.Context, names []string) (<-chan u
106106
}
107107

108108
eventsCh <- update.NewDataEvent(update.StartEvent, "apt cleaning cache is starting")
109+
eventsCh <- update.NewProgressEvent(80.0)
109110
for line, err := range runAptCleanCommand(ctx) {
110111
if err != nil {
111112
eventsCh <- update.NewErrorEvent(fmt.Errorf("error running apt clean command: %w", err))
112113
return
113114
}
114115
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, line)
115116
}
116-
117+
eventsCh <- update.NewProgressEvent(85.0)
117118
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, "Stop and destroy docker containers and images ....")
118119
streamCleanup := cleanupDockerContainers(ctx)
119120
for line, err := range streamCleanup {
@@ -125,6 +126,7 @@ func (s *Service) UpgradePackages(ctx context.Context, names []string) (<-chan u
125126
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, line)
126127
}
127128
}
129+
eventsCh <- update.NewProgressEvent(90.0)
128130

129131
// TODO: Remove this workaround once docker image versions are no longer hardcoded in arduino-app-cli.
130132
// Tracking issue: https://github.com/arduino/arduino-app-cli/issues/600

internal/update/arduino/arduino.go

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,44 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
140140
}
141141
eventsCh := make(chan update.Event, 100)
142142

143-
downloadProgressCB := func(curr *rpc.DownloadProgress) {
144-
data := helpers.ArduinoCLIDownloadProgressToString(curr)
145-
slog.Debug("Download progress", slog.String("download_progress", data))
146-
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, data)
147-
}
148-
taskProgressCB := func(msg *rpc.TaskProgress) {
149-
data := helpers.ArduinoCLITaskProgressToString(msg)
150-
slog.Debug("Task progress", slog.String("task_progress", data))
151-
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, data)
152-
}
153-
154143
go func() {
155144
defer a.lock.Unlock()
156145
defer close(eventsCh)
157146

147+
const indexWeight float32 = 30.0
148+
const indexBase float32 = 0.0
149+
const upgradeBase float32 = 30.0
150+
const upgradeWeight float32 = 60.0
151+
152+
makeDownloadProgressCallback := func(basePercentage, phaseWeight float32) func(*rpc.DownloadProgress) {
153+
return func(curr *rpc.DownloadProgress) {
154+
data := helpers.ArduinoCLIDownloadProgressToString(curr)
155+
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, data)
156+
if updateInfo := curr.GetUpdate(); updateInfo != nil {
157+
if updateInfo.GetTotalSize() <= 0 {
158+
return
159+
}
160+
localProgress := (float32(updateInfo.GetDownloaded()) / float32(updateInfo.GetTotalSize())) * 100.0
161+
totalArduinoProgress := basePercentage + (localProgress/100.0)*phaseWeight
162+
eventsCh <- update.NewProgressEvent(totalArduinoProgress)
163+
}
164+
}
165+
}
166+
makeTaskProgressCallback := func(basePercentage, phaseWeight float32) func(*rpc.TaskProgress) {
167+
return func(msg *rpc.TaskProgress) {
168+
data := helpers.ArduinoCLITaskProgressToString(msg)
169+
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, data)
170+
if !msg.GetCompleted() {
171+
localProgress := msg.GetPercent()
172+
totalArduinoProgress := basePercentage + (localProgress/100.0)*phaseWeight
173+
eventsCh <- update.NewProgressEvent(totalArduinoProgress)
174+
}
175+
}
176+
}
177+
158178
eventsCh <- update.NewDataEvent(update.StartEvent, "Upgrade is starting")
159179

160-
logrus.SetLevel(logrus.ErrorLevel) // Reduce the log level of arduino-cli
180+
logrus.SetLevel(logrus.ErrorLevel)
161181
srv := commands.NewArduinoCoreServer()
162182

163183
if err := setConfig(ctx, srv); err != nil {
@@ -181,21 +201,28 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
181201
}()
182202

183203
{
184-
stream, _ := commands.UpdateIndexStreamResponseToCallbackFunction(ctx, downloadProgressCB)
204+
updateIndexProgressCB := makeDownloadProgressCallback(indexBase, indexWeight)
205+
stream, _ := commands.UpdateIndexStreamResponseToCallbackFunction(ctx, updateIndexProgressCB)
185206
if err := srv.UpdateIndex(&rpc.UpdateIndexRequest{Instance: inst}, stream); err != nil {
186207
eventsCh <- update.NewErrorEvent(fmt.Errorf("error updating index: %w", err))
187208
return
188209
}
210+
211+
eventsCh <- update.NewProgressEvent(indexBase + indexWeight)
212+
189213
if err := srv.Init(&rpc.InitRequest{Instance: inst}, commands.InitStreamResponseToCallbackFunction(ctx, nil)); err != nil {
190214
eventsCh <- update.NewErrorEvent(fmt.Errorf("error initializing instance: %w", err))
191215
return
192216
}
193217
}
194218

219+
platformDownloadCB := makeDownloadProgressCallback(upgradeBase, upgradeWeight)
220+
platformTaskCB := makeTaskProgressCallback(upgradeBase, upgradeWeight)
221+
195222
stream, respCB := commands.PlatformUpgradeStreamResponseToCallbackFunction(
196223
ctx,
197-
downloadProgressCB,
198-
taskProgressCB,
224+
platformDownloadCB,
225+
platformTaskCB,
199226
)
200227
if err := srv.PlatformUpgrade(
201228
&rpc.PlatformUpgradeRequest{
@@ -227,8 +254,8 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
227254
},
228255
commands.PlatformInstallStreamResponseToCallbackFunction(
229256
ctx,
230-
downloadProgressCB,
231-
taskProgressCB,
257+
platformDownloadCB,
258+
platformTaskCB,
232259
),
233260
)
234261
if err != nil {
@@ -256,6 +283,7 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
256283
eventsCh <- update.NewErrorEvent(fmt.Errorf("error burning bootloader: %w", err))
257284
return
258285
}
286+
eventsCh <- update.NewProgressEvent(100.0)
259287
}()
260288

261289
return eventsCh, nil

internal/update/event.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

1616
package update
1717

18-
import "go.bug.st/f"
18+
import (
19+
"fmt"
20+
21+
"go.bug.st/f"
22+
)
1923

2024
// EventType defines the type of upgrade event.
2125
type EventType int
@@ -24,16 +28,17 @@ const (
2428
UpgradeLineEvent EventType = iota
2529
StartEvent
2630
RestartEvent
31+
ProgressEvent
2732
DoneEvent
2833
ErrorEvent
2934
)
3035

3136
// Event represents a single event in the upgrade process.
3237
type Event struct {
33-
Type EventType
34-
35-
data string
36-
err error // error field for error events
38+
Type EventType
39+
Progress float32
40+
data string
41+
err error // error field for error events
3742
}
3843

3944
func (t EventType) String() string {
@@ -44,6 +49,8 @@ func (t EventType) String() string {
4449
return "restarting"
4550
case StartEvent:
4651
return "starting"
52+
case ProgressEvent:
53+
return "progress"
4754
case DoneEvent:
4855
return "done"
4956
case ErrorEvent:
@@ -60,6 +67,13 @@ func NewDataEvent(t EventType, data string) Event {
6067
}
6168
}
6269

70+
func NewProgressEvent(progress float32) Event {
71+
return Event{
72+
Type: ProgressEvent,
73+
data: fmt.Sprintf("%.2f", progress),
74+
}
75+
}
76+
6377
func NewErrorEvent(err error) Event {
6478
return Event{
6579
Type: ErrorEvent,

internal/update/update.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,24 @@ func (m *Manager) UpgradePackages(ctx context.Context, pkgs []UpgradablePackage)
136136
// update of the cores we will end up with inconsistent state, or
137137
// we need to re run the upgrade because the orchestrator interrupted
138138
// in the middle the upgrade of the cores.
139+
140+
const arduinoWeight float32 = 20.0
141+
const aptWeight float32 = 80.0
142+
139143
arduinoEvents, err := m.arduinoPlatformUpdateService.UpgradePackages(ctx, arduinoPlatform)
140144
if err != nil {
141145
m.broadcast(NewErrorEvent(fmt.Errorf("failed to upgrade Arduino packages: %w", err)))
142146
return
143147
}
144148
for e := range arduinoEvents {
145-
m.broadcast(e)
149+
if e.Type == ProgressEvent {
150+
globalProgress := (e.Progress / 100.0) * arduinoWeight
151+
fmt.Println("++++++++++++++++++++++++ globalProgress arduinoEvents:", globalProgress)
152+
slog.Debug("Arduino upgrade progress", slog.Float64("globalProgress", float64(globalProgress)))
153+
m.broadcast(NewProgressEvent(globalProgress))
154+
} else {
155+
m.broadcast(e)
156+
}
146157
}
147158

148159
aptEvents, err := m.debUpdateService.UpgradePackages(ctx, debPkgs)
@@ -151,7 +162,14 @@ func (m *Manager) UpgradePackages(ctx context.Context, pkgs []UpgradablePackage)
151162
return
152163
}
153164
for e := range aptEvents {
154-
m.broadcast(e)
165+
if e.Type == ProgressEvent {
166+
globalProgress := arduinoWeight + (e.Progress/100.0)*aptWeight
167+
fmt.Println("++++++++++++++++++++++++ globalProgress APT:", globalProgress)
168+
slog.Debug("APT upgrade progress", slog.Float64("globalProgress", float64(globalProgress)))
169+
m.broadcast(NewProgressEvent(globalProgress))
170+
} else {
171+
m.broadcast(e)
172+
}
155173
}
156174

157175
m.broadcast(NewDataEvent(DoneEvent, "Update completed"))

0 commit comments

Comments
 (0)