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

Humanize object size #4766

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func (v bucketInfoMessage) String() string {
for _, tagName := range sortedTags {
val, ok := v.Usage.ObjectSizesHistogram[tagName]
if ok {
fmt.Fprintf(&b, " %*d object(s) %s\n", maxDigits, val, histogramTagsDesc[tagName].text)
fmt.Fprintf(&b, " %*s object(s) %s\n", maxDigits, humanize.Comma(int64(val)), histogramTagsDesc[tagName].text)
Cdaprod marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
98 changes: 92 additions & 6 deletions cmd/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package cmd

import (
"fmt"
"github.com/dustin/go-humanize"
"os"
"reflect"
"strings"
Expand All @@ -28,14 +30,82 @@ import (
func TestParseStat(t *testing.T) {
localTime := time.Unix(12001, 0).UTC()
testCases := []struct {
content ClientContent
targetAlias string
content ClientContent
targetAlias string
expectedHumanizedSize string
}{
{ClientContent{URL: *newClientURL("https://play.min.io/abc"), Size: 0, Time: localTime, Type: os.ModeDir, ETag: "blahblah", Metadata: map[string]string{"cusom-key": "custom-value"}, Expires: time.Now()}, "play"},
{ClientContent{URL: *newClientURL("https://play.min.io/testbucket"), Size: 500, Time: localTime, Type: os.ModeDir, ETag: "blahblah", Metadata: map[string]string{"cusom-key": "custom-value"}, Expires: time.Unix(0, 0).UTC()}, "play"},
{ClientContent{URL: *newClientURL("https://s3.amazonaws.com/yrdy"), Size: 0, Time: localTime, Type: 0o644, ETag: "abcdefasaas", Metadata: map[string]string{}}, "s3"},
{ClientContent{URL: *newClientURL("https://play.min.io/yrdy"), Size: 10000, Time: localTime, Type: 0o644, ETag: "blahblah", Metadata: map[string]string{"cusom-key": "custom-value"}}, "play"},
{
content: ClientContent{
URL: *newClientURL("https://play.min.io/abc"),
Size: 1000,
Time: localTime,
Type: os.ModeDir,
ETag: "blahblah",
Metadata: map[string]string{"custom-key": "custom-value"},
Expires: time.Now(),
},
targetAlias: "play",
expectedHumanizedSize: "1,000",
},
{
content: ClientContent{
URL: *newClientURL("https://play.min.io/abc"),
Size: 0,
Time: localTime,
Type: os.ModeDir,
ETag: "blahblah",
Metadata: map[string]string{"custom-key": "custom-value"},
Expires: time.Now(),
},
targetAlias: "play",
expectedHumanizedSize: "0",
},
{
content: ClientContent{
URL: *newClientURL("https://play.min.io/testbucket"),
Size: 500,
Time: localTime,
Type: os.ModeDir,
ETag: "blahblah",
Metadata: map[string]string{"custom-key": "custom-value"},
Expires: time.Unix(0, 0).UTC(),
},
targetAlias: "play",
expectedHumanizedSize: "500",
},
{
content: ClientContent{
URL: *newClientURL("https://s3.amazonaws.com/yrdy"),
Size: 0,
Time: localTime,
Type: 0o644,
ETag: "abcdefasaas",
Metadata: map[string]string{},
},
targetAlias: "s3",
expectedHumanizedSize: "0",
},
{
content: ClientContent{
URL: *newClientURL("https://play.min.io/yrdy"),
Size: 10000,
Time: localTime,
Type: 0o644,
ETag: "blahblah",
Metadata: map[string]string{"custom-key": "custom-value"},
},
targetAlias: "play",
expectedHumanizedSize: "10,000",
},
}

// }{
// {content: ClientContent{URL: *newClientURL("https://play.min.io/abc"), Size: 1000, Time: localTime, Type: os.ModeDir, ETag: "blahblah", Metadata: map[string]string{"custom-key": "custom-value"}, Expires: time.Now()}, "play", "1,000"},
// {content: ClientContent{URL: *newClientURL("https://play.min.io/abc"), Size: 0, Time: localTime, Type: os.ModeDir, ETag: "blahblah", Metadata: map[string]string{"cusom-key": "custom-value"}, Expires: time.Now()}, "play"},
// {content: ClientContent{URL: *newClientURL("https://play.min.io/testbucket"), Size: 500, Time: localTime, Type: os.ModeDir, ETag: "blahblah", Metadata: map[string]string{"cusom-key": "custom-value"}, Expires: time.Unix(0, 0).UTC()}, "play"},
// {content: ClientContent{URL: *newClientURL("https://s3.amazonaws.com/yrdy"), Size: 0, Time: localTime, Type: 0o644, ETag: "abcdefasaas", Metadata: map[string]string{}}, "s3"},
// {content: ClientContent{URL: *newClientURL("https://play.min.io/yrdy"), Size: 10000, Time: localTime, Type: 0o644, ETag: "blahblah", Metadata: map[string]string{"cusom-key": "custom-value"}}, "play"},
// }
for _, testCase := range testCases {
testCase := testCase
t.Run("", func(t *testing.T) {
Expand Down Expand Up @@ -65,6 +135,22 @@ func TestParseStat(t *testing.T) {
if etag != statMsg.ETag {
t.Errorf("Expecting %s, got %s", etag, statMsg.ETag)
}
humanizedSize := humanize.Comma(int64(testCase.content.Size))
if humanizedSize != testCase.expectedHumanizedSize {
t.Errorf("Expected humanized size %s, got %s for size %d", testCase.expectedHumanizedSize, humanizedSize, testCase.content.Size)
}
})
}
}

// Mock data representing a large object count
func TestHumanizedHistogramOutput(t *testing.T) {
largeObjectCount := uint64(1000000000) // 1 billion
humanizedCount := humanize.Comma(int64(largeObjectCount))
output := fmt.Sprintf("%12s objects", humanizedCount)
expected := "1,000,000,000 objects"

if output != expected {
t.Errorf("expected %s, got %s", expected, output)
}
}