Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: json output not using speed format #220

Closed
wants to merge 1 commit into from
Closed

fix: json output not using speed format #220

wants to merge 1 commit into from

Conversation

luevano
Copy link

@luevano luevano commented Jun 15, 2024

Closes #219

Implemented Marshaler for ByteRate, Added extra DL/UL fields to Server so that the json output uses the specified format and scaling for the speed values.

Example output (string) (for default format/unit):

...
"dl_speed": "341.80 Mbps",
"ul_speed": "298.76 Mbps",
...

Example output (string) (for binary-bytes unit option):

...
"dl_speed": "40.79 MiB/s",
"ul_speed": "31.11 MiB/s",
...

Before output (float) (always the raw float value):

...
"dl_speed": 42814913.17295695,
"ul_speed": 37902142.46883963,
...

@r3inbowari r3inbowari self-requested a review June 15, 2024 07:09
@@ -45,17 +46,25 @@ type ByteRate float64

var globalByteRateUnit UnitType

func (r ByteRate) MarshalJSON() ([]byte, error) {
if rStr := r.String(); rStr != naString {
return []byte(strings.Split(rStr, " ")[0]), nil
Copy link
Collaborator

@r3inbowari r3inbowari Jun 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the unit is auto-scaled, the user cannot know what the unit is.

eg: when use speedtest.UnitTypeDecimalBits
the json output may be "bps", "Kbps", "Mbps" or "Gbps". This is hard to confirm.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I tried including the unit but it errors out, probably because it expects a float instead of a string, that's why I did the split to get rid of the unit. I could add a new field to the server struct for DLString and ULString, and make the original ones not appear in the JSON. I'll get an example to get your feedback.

@r3inbowari
Copy link
Collaborator

r3inbowari commented Jun 15, 2024

Thank you very much @luevano, I left a comment.

@luevano
Copy link
Author

luevano commented Jun 15, 2024

Thank you very much @luevano, I left a comment.

Thanks for the feedback, I changed the fix to add new fields and use those for the json, it now outputs as string value and contains the autoscaled value and the correct unit.

Added example outputs in the OP.

Closing reason: #219 (comment)

@r3inbowari r3inbowari closed this Jun 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

json output never uses Mbps format for speeds
2 participants