Skip to content

Commit 4497ff9

Browse files
authored
Merge pull request #219 from deploymenttheory/v0.1.50-dev
chore: Refactor multipart request handling and logging
2 parents 216f967 + d8e5b05 commit 4497ff9

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

httpclient/multipartrequest.go

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/base64"
77
"fmt"
88
"io"
9-
"math"
109
"mime/multipart"
1110
"net/http"
1211
"net/textproto"
@@ -52,10 +51,8 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
5251
if err != nil {
5352
return nil, err
5453
}
55-
5654
// Log the constructed request body for debugging
5755
logMultiPartRequestBody(body, log)
58-
5956
// Construct the full URL for the API endpoint.
6057
url := c.APIHandler.ConstructAPIResourceEndpoint(endpoint, log)
6158

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

80+
// Start tracking upload time
8381
startTime := time.Now()
82+
8483
resp, err := c.httpClient.Do(req)
8584
if err != nil {
8685
log.Error("Failed to send request", zap.String("method", method), zap.String("endpoint", endpoint), zap.Error(err))
@@ -232,30 +231,19 @@ func chunkFileUpload(file *os.File, writer io.Writer, log logger.Logger, updateP
232231
return nil
233232
}
234233

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()
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)
240239

241240
return func(bytesWritten int64) {
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)))
251-
lastLoggedPercentage = percentage
252-
}
241+
uploaded += bytesWritten
242+
percentage := (uploaded * 100) / fileSize
253243

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))
244+
if percentage >= lastLoggedPercentage+logInterval {
245+
log.Debug("Upload progress", zap.Int64("uploaded_bytes", uploaded), zap.Int64("total_bytes", fileSize), zap.Int64("percentage", percentage))
246+
lastLoggedPercentage = percentage
259247
}
260248
}
261249
}

0 commit comments

Comments
 (0)