forked from elastic/fleet-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] Support download rate in string with unit format (elastic#3677)
- Loading branch information
Showing
7 changed files
with
484 additions
and
277 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Kind can be one of: | ||
# - breaking-change: a change to previously-documented behavior | ||
# - deprecation: functionality that is being removed in a later release | ||
# - bug-fix: fixes a problem in a previous version | ||
# - enhancement: extends functionality but does not break or fix existing behavior | ||
# - feature: new functionality | ||
# - known-issue: problems that we are aware of in a given version | ||
# - security: impacts on the security of a product or a user’s deployment. | ||
# - upgrade: important information for someone upgrading from a prior version | ||
# - other: does not fit into any of the other categories | ||
kind: bug-fix | ||
|
||
# Change summary; a 80ish characters long description of the change. | ||
summary: Fix Download rate parsing | ||
|
||
# Long description; in case the summary is not enough to describe the change | ||
# this field accommodate a description without length limits. | ||
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment. | ||
description: Fix checkin API to API to temporarly support download_rate as string with unit per second (example 123MBps) as elastic-agent is currently sending this. | ||
|
||
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc. | ||
component: fleet-server | ||
|
||
# PR URL; optional; the PR number that added the changeset. | ||
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added. | ||
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number. | ||
# Please provide it if you are adding a fragment for a different PR. | ||
pr: https://github.com/elastic/fleet-server/pull/3677 | ||
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of). | ||
# If not present is automatically filled by the tooling with the issue linked to the PR number. | ||
#issue: https://github.com/owner/repo/1234 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/docker/go-units" | ||
) | ||
|
||
func parseDownloadRate(jsonDownloadRate json.RawMessage) (*float64, error) { | ||
var fDownloadRate float64 | ||
err := json.Unmarshal(jsonDownloadRate, &fDownloadRate) | ||
if err == nil { | ||
return &fDownloadRate, nil | ||
} | ||
|
||
// Handle string download_rate with format human_unitps | ||
var rawDownloadRate string | ||
err = json.Unmarshal(jsonDownloadRate, &rawDownloadRate) | ||
if err != nil { | ||
return nil, fmt.Errorf("error unmarshaling download_rate: %w", err) | ||
} | ||
if rawDownloadRate != "" { | ||
downloadRate, err := units.FromHumanSize(strings.TrimSuffix(rawDownloadRate, "ps")) | ||
if err != nil { | ||
return nil, fmt.Errorf("error converting download_rate from human size: %w", err) | ||
} | ||
fDownloadRate := float64(downloadRate) | ||
return &fDownloadRate, nil | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func (t *UpgradeMetadataDownloading) UnmarshalJSON(b []byte) error { | ||
object := make(map[string]json.RawMessage) | ||
err := json.Unmarshal(b, &object) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if raw, found := object["download_rate"]; found { | ||
downloadRate, err := parseDownloadRate(raw) | ||
if err != nil { | ||
return err | ||
} | ||
t.DownloadRate = downloadRate | ||
delete(object, "download_rate") | ||
} | ||
|
||
if raw, found := object["download_percent"]; found { | ||
err = json.Unmarshal(raw, &t.DownloadPercent) | ||
if err != nil { | ||
return fmt.Errorf("error reading 'download_percent': %w", err) | ||
} | ||
delete(object, "download_percent") | ||
} | ||
|
||
if raw, found := object["retry_error_msg"]; found { | ||
err = json.Unmarshal(raw, &t.RetryErrorMsg) | ||
if err != nil { | ||
return fmt.Errorf("error reading 'retry_error_msg': %w", err) | ||
} | ||
delete(object, "retry_error_msg") | ||
} | ||
|
||
if raw, found := object["retry_until"]; found { | ||
err = json.Unmarshal(raw, &t.RetryUntil) | ||
if err != nil { | ||
return fmt.Errorf("error reading 'retry_until': %w", err) | ||
} | ||
delete(object, "retry_until") | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
//go:build !integration | ||
|
||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_ParseDownloadRate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
raw json.RawMessage | ||
expectedErrMsg string | ||
expectedValue float64 | ||
}{{ | ||
name: "download rate as float", | ||
raw: json.RawMessage(`1000000`), | ||
expectedValue: 1000000.00, | ||
}, { | ||
name: "download rate as MBps", | ||
raw: json.RawMessage(`"1MBps"`), | ||
expectedValue: 1000000.00, | ||
}, { | ||
name: "download only MBps", | ||
raw: json.RawMessage(`"MBps"`), | ||
expectedErrMsg: "error converting download_rate from human size: invalid size: 'MB'", | ||
}, { | ||
name: "download rate random string", | ||
raw: json.RawMessage(`"toto"`), | ||
expectedErrMsg: "error converting download_rate from human size: invalid size: 'toto'", | ||
}} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
|
||
res, err := parseDownloadRate(tc.raw) | ||
if tc.expectedErrMsg != "" { | ||
fmt.Printf("TEST %v+", err) | ||
require.ErrorContains(t, err, tc.expectedErrMsg) | ||
} else { | ||
require.NoError(t, err) | ||
require.NotNil(t, res) | ||
require.Equal(t, tc.expectedValue, *res) | ||
} | ||
|
||
}) | ||
} | ||
} |