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