From dcc324dbb6d7a5d2476e2f17420a5ffc99360b45 Mon Sep 17 00:00:00 2001 From: ShocOne <62835948+ShocOne@users.noreply.github.com> Date: Wed, 5 Jun 2024 19:06:11 +0100 Subject: [PATCH] Refactor multipart request handling and logging --- httpclient/multipartrequest.go | 36 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/httpclient/multipartrequest.go b/httpclient/multipartrequest.go index 4117a06..5d6567e 100644 --- a/httpclient/multipartrequest.go +++ b/httpclient/multipartrequest.go @@ -6,7 +6,6 @@ import ( "encoding/base64" "fmt" "io" - "math" "mime/multipart" "net/http" "net/textproto" @@ -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) @@ -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)) @@ -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 } } }