Skip to content

Commit 0680a91

Browse files
committed
fix empty progress
1 parent 899f8e9 commit 0680a91

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
@@ -94,14 +94,15 @@ func (s *Service) UpgradePackages(ctx context.Context, names []string) (<-chan u
9494
}
9595

9696
eventsCh <- update.NewDataEvent(update.StartEvent, "apt cleaning cache is starting")
97+
eventsCh <- update.NewProgressEvent(80.0)
9798
for line, err := range runAptCleanCommand(ctx) {
9899
if err != nil {
99100
eventsCh <- update.NewErrorEvent(fmt.Errorf("error running apt clean command: %w", err))
100101
return
101102
}
102103
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, line)
103104
}
104-
105+
eventsCh <- update.NewProgressEvent(85.0)
105106
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, "Stop and destroy docker containers and images ....")
106107
streamCleanup := cleanupDockerContainers(ctx)
107108
for line, err := range streamCleanup {
@@ -113,6 +114,7 @@ func (s *Service) UpgradePackages(ctx context.Context, names []string) (<-chan u
113114
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, line)
114115
}
115116
}
117+
eventsCh <- update.NewProgressEvent(90.0)
116118

117119
// TODO: Remove this workaround once docker image versions are no longer hardcoded in arduino-app-cli.
118120
// 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
@@ -131,24 +131,44 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
131131
}
132132
eventsCh := make(chan update.Event, 100)
133133

134-
downloadProgressCB := func(curr *rpc.DownloadProgress) {
135-
data := helpers.ArduinoCLIDownloadProgressToString(curr)
136-
slog.Debug("Download progress", slog.String("download_progress", data))
137-
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, data)
138-
}
139-
taskProgressCB := func(msg *rpc.TaskProgress) {
140-
data := helpers.ArduinoCLITaskProgressToString(msg)
141-
slog.Debug("Task progress", slog.String("task_progress", data))
142-
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, data)
143-
}
144-
145134
go func() {
146135
defer a.lock.Unlock()
147136
defer close(eventsCh)
148137

138+
const indexWeight float32 = 30.0
139+
const indexBase float32 = 0.0
140+
const upgradeBase float32 = 30.0
141+
const upgradeWeight float32 = 60.0
142+
143+
makeDownloadProgressCallback := func(basePercentage, phaseWeight float32) func(*rpc.DownloadProgress) {
144+
return func(curr *rpc.DownloadProgress) {
145+
data := helpers.ArduinoCLIDownloadProgressToString(curr)
146+
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, data)
147+
if updateInfo := curr.GetUpdate(); updateInfo != nil {
148+
if updateInfo.GetTotalSize() <= 0 {
149+
return
150+
}
151+
localProgress := (float32(updateInfo.GetDownloaded()) / float32(updateInfo.GetTotalSize())) * 100.0
152+
totalArduinoProgress := basePercentage + (localProgress/100.0)*phaseWeight
153+
eventsCh <- update.NewProgressEvent(totalArduinoProgress)
154+
}
155+
}
156+
}
157+
makeTaskProgressCallback := func(basePercentage, phaseWeight float32) func(*rpc.TaskProgress) {
158+
return func(msg *rpc.TaskProgress) {
159+
data := helpers.ArduinoCLITaskProgressToString(msg)
160+
eventsCh <- update.NewDataEvent(update.UpgradeLineEvent, data)
161+
if !msg.GetCompleted() {
162+
localProgress := msg.GetPercent()
163+
totalArduinoProgress := basePercentage + (localProgress/100.0)*phaseWeight
164+
eventsCh <- update.NewProgressEvent(totalArduinoProgress)
165+
}
166+
}
167+
}
168+
149169
eventsCh <- update.NewDataEvent(update.StartEvent, "Upgrade is starting")
150170

151-
logrus.SetLevel(logrus.ErrorLevel) // Reduce the log level of arduino-cli
171+
logrus.SetLevel(logrus.ErrorLevel)
152172
srv := commands.NewArduinoCoreServer()
153173

154174
if err := setConfig(ctx, srv); err != nil {
@@ -172,21 +192,28 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
172192
}()
173193

174194
{
175-
stream, _ := commands.UpdateIndexStreamResponseToCallbackFunction(ctx, downloadProgressCB)
195+
updateIndexProgressCB := makeDownloadProgressCallback(indexBase, indexWeight)
196+
stream, _ := commands.UpdateIndexStreamResponseToCallbackFunction(ctx, updateIndexProgressCB)
176197
if err := srv.UpdateIndex(&rpc.UpdateIndexRequest{Instance: inst}, stream); err != nil {
177198
eventsCh <- update.NewErrorEvent(fmt.Errorf("error updating index: %w", err))
178199
return
179200
}
201+
202+
eventsCh <- update.NewProgressEvent(indexBase + indexWeight)
203+
180204
if err := srv.Init(&rpc.InitRequest{Instance: inst}, commands.InitStreamResponseToCallbackFunction(ctx, nil)); err != nil {
181205
eventsCh <- update.NewErrorEvent(fmt.Errorf("error initializing instance: %w", err))
182206
return
183207
}
184208
}
185209

210+
platformDownloadCB := makeDownloadProgressCallback(upgradeBase, upgradeWeight)
211+
platformTaskCB := makeTaskProgressCallback(upgradeBase, upgradeWeight)
212+
186213
stream, respCB := commands.PlatformUpgradeStreamResponseToCallbackFunction(
187214
ctx,
188-
downloadProgressCB,
189-
taskProgressCB,
215+
platformDownloadCB,
216+
platformTaskCB,
190217
)
191218
if err := srv.PlatformUpgrade(
192219
&rpc.PlatformUpgradeRequest{
@@ -218,8 +245,8 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
218245
},
219246
commands.PlatformInstallStreamResponseToCallbackFunction(
220247
ctx,
221-
downloadProgressCB,
222-
taskProgressCB,
248+
platformDownloadCB,
249+
platformTaskCB,
223250
),
224251
)
225252
if err != nil {
@@ -247,6 +274,7 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
247274
eventsCh <- update.NewErrorEvent(fmt.Errorf("error burning bootloader: %w", err))
248275
return
249276
}
277+
eventsCh <- update.NewProgressEvent(100.0)
250278
}()
251279

252280
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)