55 "fmt"
66 "io"
77 "os"
8+ "path/filepath"
89
910 "github.com/LumeraProtocol/supernode/gen/supernode/action/cascade"
1011 "github.com/LumeraProtocol/supernode/pkg/net"
@@ -168,12 +169,12 @@ func (a *cascadeAdapter) GetSupernodeStatus(ctx context.Context) (SupernodeStatu
168169 resp , err := a .client .HealthCheck (ctx , & cascade.HealthCheckRequest {})
169170 if err != nil {
170171 a .logger .Error (ctx , "Failed to get supernode status" , "error" , err )
171- return nil , fmt .Errorf ("failed to get supernode status: %w" , err )
172+ return SupernodeStatusresponse {} , fmt .Errorf ("failed to get supernode status: %w" , err )
172173 }
173174
174175 a .logger .Debug (ctx , "Supernode status retrieved" , "status" , resp )
175176
176- return resp , nil
177+ return * toSdkSupernodeStatus ( resp ) , nil
177178}
178179
179180// CascadeSupernodeDownload downloads a file from a supernode gRPC stream
@@ -190,16 +191,20 @@ func (a *cascadeAdapter) CascadeSupernodeDownload(
190191 ActionId : in .ActionID ,
191192 }, opts ... )
192193 if err != nil {
193- a .logger .Error (ctx , "failed to create download stream" ,
194- "action_id" , in .ActionID , "error" , err )
194+ a .logger .Error (ctx , "failed to create download stream" , "action_id" , in .ActionID , "error" , err )
195195 return nil , err
196196 }
197197
198198 // 2. Prepare destination file
199+ // Create directory structure if it doesn't exist
200+ if err := os .MkdirAll (filepath .Dir (in .OutputPath ), 0755 ); err != nil {
201+ a .logger .Error (ctx , "failed to create output directory" , "path" , filepath .Dir (in .OutputPath ), "error" , err )
202+ return nil , fmt .Errorf ("create output directory: %w" , err )
203+ }
204+
199205 outFile , err := os .Create (in .OutputPath )
200206 if err != nil {
201- a .logger .Error (ctx , "failed to create output file" ,
202- "path" , in .OutputPath , "error" , err )
207+ a .logger .Error (ctx , "failed to create output file" , "path" , in .OutputPath , "error" , err )
203208 return nil , fmt .Errorf ("create output file: %w" , err )
204209 }
205210 defer outFile .Close ()
@@ -223,10 +228,7 @@ func (a *cascadeAdapter) CascadeSupernodeDownload(
223228
224229 // 3a. Progress / event message
225230 case * cascade.DownloadResponse_Event :
226- a .logger .Info (ctx , "supernode event" ,
227- "event_type" , x .Event .EventType ,
228- "message" , x .Event .Message ,
229- "action_id" , in .ActionID )
231+ a .logger .Info (ctx , "supernode event" , "event_type" , x .Event .EventType , "message" , x .Event .Message , "action_id" , in .ActionID )
230232
231233 if in .EventLogger != nil {
232234 in .EventLogger (ctx , toSdkEvent (x .Event .EventType ), x .Event .Message , event.EventData {
@@ -249,17 +251,11 @@ func (a *cascadeAdapter) CascadeSupernodeDownload(
249251 bytesWritten += int64 (len (data ))
250252 chunkIndex ++
251253
252- a .logger .Debug (ctx , "received chunk" ,
253- "chunk_index" , chunkIndex ,
254- "chunk_size" , len (data ),
255- "bytes_written" , bytesWritten )
254+ a .logger .Debug (ctx , "received chunk" , "chunk_index" , chunkIndex , "chunk_size" , len (data ), "bytes_written" , bytesWritten )
256255 }
257256 }
258257
259- a .logger .Info (ctx , "download complete" ,
260- "bytes_written" , bytesWritten ,
261- "path" , in .OutputPath ,
262- "action_id" , in .ActionID )
258+ a .logger .Info (ctx , "download complete" , "bytes_written" , bytesWritten , "path" , in .OutputPath , "action_id" , in .ActionID )
263259
264260 return & CascadeSupernodeDownloadResponse {
265261 Success : true ,
@@ -299,3 +295,25 @@ func toSdkEvent(e cascade.SupernodeEventType) event.EventType {
299295 return event .SupernodeUnknown
300296 }
301297}
298+
299+ func toSdkSupernodeStatus (resp * cascade.HealthCheckResponse ) * SupernodeStatusresponse {
300+ result := & SupernodeStatusresponse {
301+ TasksInProgress : resp .TasksInProgress ,
302+ }
303+
304+ // Convert CPU data
305+ if resp .Cpu != nil {
306+ result .CPU .Usage = resp .Cpu .Usage
307+ result .CPU .Remaining = resp .Cpu .Remaining
308+ }
309+
310+ // Convert Memory data
311+ if resp .Memory != nil {
312+ result .Memory .Total = resp .Memory .Total
313+ result .Memory .Used = resp .Memory .Used
314+ result .Memory .Available = resp .Memory .Available
315+ result .Memory .UsedPerc = resp .Memory .UsedPerc
316+ }
317+
318+ return result
319+ }
0 commit comments