Skip to content

Commit

Permalink
profile: Add progress bar for download and upload to Subnet
Browse files Browse the repository at this point in the history
  • Loading branch information
Anis Eleuch committed Sep 24, 2024
1 parent 3dac3d3 commit 0070d0a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 35 deletions.
20 changes: 16 additions & 4 deletions cmd/subnet-file-uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ import (

// SubnetFileUploader - struct to upload files to SUBNET
type SubnetFileUploader struct {
alias string // used for saving api-key and license from response
filename string // filename passed in the SUBNET request
alias string // used for saving api-key and license from response
filename string // filename passed in the SUBNET request
showProgressBar bool
FilePath string // file to upload
ReqURL string // SUBNET upload URL
Params url.Values // query params to be sent in the request
Expand Down Expand Up @@ -113,12 +114,23 @@ func (i *SubnetFileUploader) subnetUploadReq() (*http.Request, error) {
}
defer file.Close()

rd := io.Reader(file)

if i.showProgressBar {
var st os.FileInfo
st, e = file.Stat()
if e != nil {
return
}
rd = newProgressReader(rd, "", st.Size())
}

if i.AutoCompress {
z, _ := zstd.NewWriter(part, zstd.WithEncoderConcurrency(2))
defer z.Close()
_, e = z.ReadFrom(file)
_, e = z.ReadFrom(rd)
} else {
_, e = io.Copy(part, file)
_, e = io.Copy(part, rd)
}
}()

Expand Down
74 changes: 43 additions & 31 deletions cmd/support-profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func moveFile(sourcePath, destPath string) error {
return os.Remove(sourcePath)
}

func saveProfileFile(data io.ReadCloser) {
func saveProfileFile(data io.Reader) {
// Create profile zip file
tmpFile, e := os.CreateTemp("", "mc-profile-")
fatalIf(probe.NewError(e), "Unable to download profile data.")
Expand All @@ -176,7 +176,6 @@ func saveProfileFile(data io.ReadCloser) {
fatalIf(probe.NewError(e), "Unable to download profile data.")

// Close everything
data.Close()
tmpFile.Close()

downloadedFile := profileFile + "." + time.Now().Format(dateTimeFormatFilename)
Expand Down Expand Up @@ -218,49 +217,62 @@ func mainSupportProfile(ctx *cli.Context) error {
}

func execSupportProfile(ctx *cli.Context, client *madmin.AdminClient, alias, apiKey string) {
var reqURL string
var headers map[string]string
profilers := ctx.String("type")
duration := ctx.Int("duration")

if !globalAirgapped {
// Retrieve subnet credentials (login/license) beforehand as
// it can take a long time to fetch the profile data
uploadURL := SubnetUploadURL("profile")
reqURL, headers = prepareSubnetUploadURL(uploadURL, alias, apiKey)
}

if !globalJSON {
console.Infof("Profiling '%s' for %d seconds... \n", alias, duration)
}

// Start profiling and download data locally
data, e := client.Profile(globalContext, madmin.ProfilerType(profilers), time.Second*time.Duration(duration))
fatalIf(probe.NewError(e), "Unable to save profile data")
rd := io.Reader(data)
if !globalJSON {
console.Info("Downloading profiling data..\n")
rd = newProgressReader(rd, "", 0)
}
saveProfileFile(rd)
data.Close()

saveProfileFile(data)

if !globalAirgapped {
_, e = (&SubnetFileUploader{
alias: alias,
FilePath: profileFile,
ReqURL: reqURL,
Headers: headers,
DeleteAfterUpload: true,
}).UploadFileToSubnet()
if e != nil {
printMsg(supportProfileMessage{
Status: "error",
Error: e.Error(),
File: profileFile,
})
return
}
if globalAirgapped {
printMsg(supportProfileMessage{
Status: "success",
File: profileFile,
})
} else {
return
}

// --airgap is not passed, upload the downloaded profile to Subnet

// Retrieve subnet credentials (login/license) beforehand as
// it can take a long time to fetch the profile data
uploadURL := SubnetUploadURL("profile")
reqURL, headers := prepareSubnetUploadURL(uploadURL, alias, apiKey)

uploader := SubnetFileUploader{
alias: alias,
FilePath: profileFile,
ReqURL: reqURL,
Headers: headers,
DeleteAfterUpload: true,
}

if !globalJSON {
console.Info("Uploading profiling data to Subnet..\n")
uploader.showProgressBar = true
}

_, e = uploader.UploadFileToSubnet()
if e != nil {
printMsg(supportProfileMessage{
Status: "success",
Status: "error",
Error: e.Error(),
File: profileFile,
})
return
}
printMsg(supportProfileMessage{
Status: "success",
})
}

0 comments on commit 0070d0a

Please sign in to comment.