Skip to content

Commit

Permalink
Refactor multipart request handling and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ShocOne committed Jun 5, 2024
1 parent a937cd2 commit dcc324d
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions httpclient/multipartrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/base64"
"fmt"
"io"
"math"
"mime/multipart"
"net/http"
"net/textproto"
Expand Down Expand Up @@ -52,10 +51,8 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
if err != nil {
return nil, err
}

// Log the constructed request body for debugging
logMultiPartRequestBody(body, log)

// Construct the full URL for the API endpoint.
url := c.APIHandler.ConstructAPIResourceEndpoint(endpoint, log)

Expand All @@ -80,7 +77,9 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
headerHandler.SetRequestHeaders(endpoint)
headerHandler.LogHeaders(c.clientConfig.ClientOptions.Logging.HideSensitiveData)

// Start tracking upload time
startTime := time.Now()

resp, err := c.httpClient.Do(req)
if err != nil {
log.Error("Failed to send request", zap.String("method", method), zap.String("endpoint", endpoint), zap.Error(err))
Expand Down Expand Up @@ -232,30 +231,19 @@ func chunkFileUpload(file *os.File, writer io.Writer, log logger.Logger, updateP
return nil
}

// logUploadProgress logs the upload progress based on the percentage of the total upload.
func logUploadProgress(totalSize int64, log logger.Logger) func(int64) {
var uploadedSize int64
var lastLoggedPercentage float64
startTime := time.Now()
// logUploadProgress logs the upload progress based on the percentage of the total file size.
func logUploadProgress(fileSize int64, log logger.Logger) func(int64) {
var uploaded int64 = 0
const logInterval = 5 // Log every 5% increment
lastLoggedPercentage := int64(0)

return func(bytesWritten int64) {
uploadedSize += bytesWritten
percentage := math.Floor(float64(uploadedSize) / float64(totalSize) * 100)
uploadedMB := float64(uploadedSize) / (1024 * 1024)

if percentage != lastLoggedPercentage {
log.Info("File upload progress",
zap.String("completed", fmt.Sprintf("%.0f%%", percentage)),
zap.Float64("uploaded_megabytes", uploadedMB),
zap.Duration("elapsed_time", time.Since(startTime)))
lastLoggedPercentage = percentage
}
uploaded += bytesWritten
percentage := (uploaded * 100) / fileSize

if uploadedSize == totalSize {
totalTime := time.Since(startTime)
log.Info("File upload completed",
zap.Float64("total_uploaded_megabytes", float64(uploadedSize)/(1024*1024)),
zap.Duration("total_upload_time", totalTime))
if percentage >= lastLoggedPercentage+logInterval {
log.Debug("Upload progress", zap.Int64("uploaded_bytes", uploaded), zap.Int64("total_bytes", fileSize), zap.Int64("percentage", percentage))
lastLoggedPercentage = percentage
}
}
}
Expand Down

0 comments on commit dcc324d

Please sign in to comment.