@@ -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
0 commit comments