Skip to content

Commit 216f967

Browse files
authored
Merge pull request #218 from deploymenttheory/v0.1.50-dev
Refactor multipart request handling and logging
2 parents 3263593 + 5ed238f commit 216f967

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

httpclient/multipartrequest.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/base64"
77
"fmt"
88
"io"
9+
"math"
910
"mime/multipart"
1011
"net/http"
1112
"net/textproto"
@@ -51,8 +52,10 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
5152
if err != nil {
5253
return nil, err
5354
}
55+
5456
// Log the constructed request body for debugging
5557
logMultiPartRequestBody(body, log)
58+
5659
// Construct the full URL for the API endpoint.
5760
url := c.APIHandler.ConstructAPIResourceEndpoint(endpoint, log)
5861

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

80-
// Start tracking upload time
8183
startTime := time.Now()
82-
8384
resp, err := c.httpClient.Do(req)
8485
if err != nil {
8586
log.Error("Failed to send request", zap.String("method", method), zap.String("endpoint", endpoint), zap.Error(err))
@@ -231,20 +232,31 @@ func chunkFileUpload(file *os.File, writer io.Writer, log logger.Logger, updateP
231232
return nil
232233
}
233234

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

240241
return func(bytesWritten int64) {
241-
uploaded += bytesWritten
242-
percentage := (uploaded * 100) / fileSize
243-
244-
if percentage >= lastLoggedPercentage+logInterval {
245-
log.Debug("Upload progress", zap.Int64("uploaded_bytes", uploaded), zap.Int64("total_bytes", fileSize), zap.Int64("percentage", percentage))
242+
uploadedSize += bytesWritten
243+
percentage := math.Floor(float64(uploadedSize) / float64(totalSize) * 100)
244+
uploadedMB := float64(uploadedSize) / (1024 * 1024)
245+
246+
if percentage != lastLoggedPercentage {
247+
log.Info("File upload progress",
248+
zap.String("completed", fmt.Sprintf("%.0f%%", percentage)),
249+
zap.Float64("uploaded_megabytes", uploadedMB),
250+
zap.Duration("elapsed_time", time.Since(startTime)))
246251
lastLoggedPercentage = percentage
247252
}
253+
254+
if uploadedSize == totalSize {
255+
totalTime := time.Since(startTime)
256+
log.Info("File upload completed",
257+
zap.Float64("total_uploaded_megabytes", float64(uploadedSize)/(1024*1024)),
258+
zap.Duration("total_upload_time", totalTime))
259+
}
248260
}
249261
}
250262

0 commit comments

Comments
 (0)