-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enrich output of immuadmin status command
Signed-off-by: Stefano Scafiti <[email protected]>
- Loading branch information
Showing
15 changed files
with
1,832 additions
and
1,583 deletions.
There are no files selected for viewing
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,55 @@ | ||
package helper | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const ( | ||
Byte = 1 | ||
KiloByte = 1 << (10 * iota) | ||
MegaByte | ||
GigaByte | ||
TeraByte | ||
PetaByte | ||
ExaByte | ||
) | ||
|
||
var unitMap = map[int]string{ | ||
Byte: "B", | ||
KiloByte: "KB", | ||
MegaByte: "MB", | ||
GigaByte: "GB", | ||
TeraByte: "TB", | ||
PetaByte: "PB", | ||
ExaByte: "EB", | ||
} | ||
|
||
func FormatByteSize(size uint64) string { | ||
u := getUnit(size) | ||
|
||
fsize := float64(size) / float64(u) | ||
if rounded := uint64(fsize + 0.05); rounded == 1024 { | ||
u = getUnit(1024 * uint64(u)) | ||
fsize = 1 | ||
} | ||
return strings.TrimSuffix(strconv.FormatFloat(fsize, 'f', 1, 64), ".0") + " " + unitMap[u] | ||
} | ||
|
||
func getUnit(size uint64) int { | ||
switch { | ||
case size >= ExaByte: | ||
return ExaByte | ||
case size >= PetaByte: | ||
return PetaByte | ||
case size >= TeraByte: | ||
return TeraByte | ||
case size >= GigaByte: | ||
return GigaByte | ||
case size >= MegaByte: | ||
return MegaByte | ||
case size >= KiloByte: | ||
return KiloByte | ||
} | ||
return Byte | ||
} |
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,23 @@ | ||
package helper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/codenotary/immudb/cmd/helper" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFormatByteSize(t *testing.T) { | ||
require.Equal(t, "25 B", helper.FormatByteSize(25)) | ||
require.Equal(t, "10 KB", helper.FormatByteSize(size(10.0, helper.KiloByte))) | ||
require.Equal(t, "5.4 MB", helper.FormatByteSize(size(5.4, helper.MegaByte))) | ||
require.Equal(t, "11.8 GB", helper.FormatByteSize(size(11.8, helper.GigaByte))) | ||
require.Equal(t, "99.3 PB", helper.FormatByteSize(size(99.27, helper.PetaByte))) | ||
require.Equal(t, "1 EB", helper.FormatByteSize(size(1023.95, helper.PetaByte))) | ||
require.Equal(t, "2.3 EB", helper.FormatByteSize(size(2.3, helper.ExaByte))) | ||
} | ||
|
||
func size(fraction float64, unit int) uint64 { | ||
s := fraction * float64(unit) | ||
return uint64(s) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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