split size and unit
Signed-off-by: S.Çağlar Onur <caglar@10ur.org>
This commit is contained in:
parent
632af98db8
commit
27cf0c9963
@ -18,6 +18,7 @@ package pages
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
@ -55,26 +56,35 @@ const (
|
|||||||
YB
|
YB
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b ByteSize) String() string {
|
func (b ByteSize) Size() string {
|
||||||
|
for _, i := range [...]ByteSize{YB, ZB, EB, PB, TB, GB, MB, KB} {
|
||||||
|
if b >= i {
|
||||||
|
return fmt.Sprintf("%.2f", b/i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%.2f", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b ByteSize) Unit() string {
|
||||||
switch {
|
switch {
|
||||||
case b >= YB:
|
case b >= YB:
|
||||||
return fmt.Sprintf("%.2fYB", b/YB)
|
return "YB"
|
||||||
case b >= ZB:
|
case b >= ZB:
|
||||||
return fmt.Sprintf("%.2fZB", b/ZB)
|
return "ZB"
|
||||||
case b >= EB:
|
case b >= EB:
|
||||||
return fmt.Sprintf("%.2fEB", b/EB)
|
return "EB"
|
||||||
case b >= PB:
|
case b >= PB:
|
||||||
return fmt.Sprintf("%.2fPB", b/PB)
|
return "PB"
|
||||||
case b >= TB:
|
case b >= TB:
|
||||||
return fmt.Sprintf("%.2fTB", b/TB)
|
return "TB"
|
||||||
case b >= GB:
|
case b >= GB:
|
||||||
return fmt.Sprintf("%.2fGB", b/GB)
|
return "GB"
|
||||||
case b >= MB:
|
case b >= MB:
|
||||||
return fmt.Sprintf("%.2fMB", b/MB)
|
return "MB"
|
||||||
case b >= KB:
|
case b >= KB:
|
||||||
return fmt.Sprintf("%.2fKB", b/KB)
|
return "KB"
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%.2fB", b)
|
return "B"
|
||||||
}
|
}
|
||||||
|
|
||||||
var funcMap = template.FuncMap{
|
var funcMap = template.FuncMap{
|
||||||
@ -83,6 +93,7 @@ var funcMap = template.FuncMap{
|
|||||||
"printCores": printCores,
|
"printCores": printCores,
|
||||||
"printShares": printShares,
|
"printShares": printShares,
|
||||||
"printSize": printSize,
|
"printSize": printSize,
|
||||||
|
"printUnit": printUnit,
|
||||||
"getMemoryUsage": getMemoryUsage,
|
"getMemoryUsage": getMemoryUsage,
|
||||||
"getMemoryUsagePercent": getMemoryUsagePercent,
|
"getMemoryUsagePercent": getMemoryUsagePercent,
|
||||||
"getHotMemoryPercent": getHotMemoryPercent,
|
"getHotMemoryPercent": getHotMemoryPercent,
|
||||||
@ -189,7 +200,17 @@ func toMegabytes(bytes uint64) float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func printSize(bytes uint64) string {
|
func printSize(bytes uint64) string {
|
||||||
return ByteSize(bytes).String()
|
if bytes >= math.MaxInt64 {
|
||||||
|
return "unlimited"
|
||||||
|
}
|
||||||
|
return ByteSize(bytes).Size()
|
||||||
|
}
|
||||||
|
|
||||||
|
func printUnit(bytes uint64) string {
|
||||||
|
if bytes >= math.MaxInt64 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return ByteSize(bytes).Unit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func toMemoryPercent(usage uint64, spec *info.ContainerSpec, machine *info.MachineInfo) int {
|
func toMemoryPercent(usage uint64, spec *info.ContainerSpec, machine *info.MachineInfo) int {
|
||||||
|
@ -82,13 +82,13 @@ const containersHtmlTemplate = `
|
|||||||
<ul class="list-group">
|
<ul class="list-group">
|
||||||
<li class="list-group-item active isolation-title panel-title">Memory</li>
|
<li class="list-group-item active isolation-title panel-title">Memory</li>
|
||||||
{{if .Spec.Memory.Reservation}}
|
{{if .Spec.Memory.Reservation}}
|
||||||
<li class="list-group-item"><span class="stat-label">Reservation</span> <span class="unit-label">{{printSize .Spec.Memory.Reservation}}</span></li>
|
<li class="list-group-item"><span class="stat-label">Reservation</span> {{printSize .Spec.Memory.Reservation}} <span class="unit-label">{{printUnit .Spec.Memory.Reservation}}</span></li>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if .Spec.Memory.Limit}}
|
{{if .Spec.Memory.Limit}}
|
||||||
<li class="list-group-item"><span class="stat-label">Limit</span> <span class="unit-label">{{printSize .Spec.Memory.Limit}}</span></li>
|
<li class="list-group-item"><span class="stat-label">Limit</span> {{printSize .Spec.Memory.Limit}} <span class="unit-label">{{printUnit .Spec.Memory.Limit}}</span></li>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if .Spec.Memory.SwapLimit}}
|
{{if .Spec.Memory.SwapLimit}}
|
||||||
<li class="list-group-item"><span class="stat-label">Swap Limit</span> <span class="unit-label">{{printSize .Spec.Memory.SwapLimit}}</span></li>
|
<li class="list-group-item"><span class="stat-label">Swap Limit</span> {{printSize .Spec.Memory.SwapLimit}} <span class="unit-label">{{printUnit .Spec.Memory.SwapLimit}}</span></li>
|
||||||
{{end}}
|
{{end}}
|
||||||
</ul>
|
</ul>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
Loading…
Reference in New Issue
Block a user