Make (memory) limits human readable (16.00PB v.s 16000000000MB)

Signed-off-by: S.Çağlar Onur <caglar@10ur.org>
This commit is contained in:
S.Çağlar Onur 2014-08-24 17:57:58 -04:00
parent 6443943aa1
commit 632af98db8
2 changed files with 51 additions and 11 deletions

View File

@ -18,7 +18,6 @@ package pages
import (
"fmt"
"html/template"
"math"
"net/http"
"net/url"
"path"
@ -33,12 +32,57 @@ import (
const ContainersPage = "/containers/"
// from http://golang.org/doc/effective_go.html#constants
type ByteSize float64
const (
_ = iota
// KB - kilobyte
KB ByteSize = 1 << (10 * iota)
// MB - megabyte
MB
// GB - gigabyte
GB
// TB - terabyte
TB
// PB - petabyte
PB
// EB - exabyte
EB
// ZB - zettabyte
ZB
// YB - yottabyte
YB
)
func (b ByteSize) String() string {
switch {
case b >= YB:
return fmt.Sprintf("%.2fYB", b/YB)
case b >= ZB:
return fmt.Sprintf("%.2fZB", b/ZB)
case b >= EB:
return fmt.Sprintf("%.2fEB", b/EB)
case b >= PB:
return fmt.Sprintf("%.2fPB", b/PB)
case b >= TB:
return fmt.Sprintf("%.2fTB", b/TB)
case b >= GB:
return fmt.Sprintf("%.2fGB", b/GB)
case b >= MB:
return fmt.Sprintf("%.2fMB", b/MB)
case b >= KB:
return fmt.Sprintf("%.2fKB", b/KB)
}
return fmt.Sprintf("%.2fB", b)
}
var funcMap = template.FuncMap{
"containerLink": containerLink,
"printMask": printMask,
"printCores": printCores,
"printShares": printShares,
"printMegabytes": printMegabytes,
"printSize": printSize,
"getMemoryUsage": getMemoryUsage,
"getMemoryUsagePercent": getMemoryUsagePercent,
"getHotMemoryPercent": getHotMemoryPercent,
@ -144,12 +188,8 @@ func toMegabytes(bytes uint64) float64 {
return float64(bytes) / (1 << 20)
}
func printMegabytes(bytes uint64) string {
if bytes >= math.MaxInt64 {
return "unlimited"
}
megabytes := toMegabytes(bytes)
return strconv.FormatFloat(megabytes, 'f', 3, 64)
func printSize(bytes uint64) string {
return ByteSize(bytes).String()
}
func toMemoryPercent(usage uint64, spec *info.ContainerSpec, machine *info.MachineInfo) int {

View File

@ -82,13 +82,13 @@ const containersHtmlTemplate = `
<ul class="list-group">
<li class="list-group-item active isolation-title panel-title">Memory</li>
{{if .Spec.Memory.Reservation}}
<li class="list-group-item"><span class="stat-label">Reservation</span> {{printMegabytes .Spec.Memory.Reservation}} <span class="unit-label">MB</span></li>
<li class="list-group-item"><span class="stat-label">Reservation</span> <span class="unit-label">{{printSize .Spec.Memory.Reservation}}</span></li>
{{end}}
{{if .Spec.Memory.Limit}}
<li class="list-group-item"><span class="stat-label">Limit</span> {{printMegabytes .Spec.Memory.Limit}} <span class="unit-label">MB</span></li>
<li class="list-group-item"><span class="stat-label">Limit</span> <span class="unit-label">{{printSize .Spec.Memory.Limit}}</span></li>
{{end}}
{{if .Spec.Memory.SwapLimit}}
<li class="list-group-item"><span class="stat-label">Swap Limit</span> {{printMegabytes .Spec.Memory.SwapLimit}} <span class="unit-label">MB</span></li>
<li class="list-group-item"><span class="stat-label">Swap Limit</span> <span class="unit-label">{{printSize .Spec.Memory.SwapLimit}}</span></li>
{{end}}
</ul>
{{end}}