cli/command: define some consts for repeated values

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-10-31 13:03:33 +01:00
parent 2f65cf7d1a
commit 3825d37923
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 14 additions and 9 deletions

View File

@ -22,6 +22,8 @@ const (
winMemUseHeader = "PRIV WORKING SET" // Used only on Windows winMemUseHeader = "PRIV WORKING SET" // Used only on Windows
memUseHeader = "MEM USAGE / LIMIT" // Used only on Linux memUseHeader = "MEM USAGE / LIMIT" // Used only on Linux
pidsHeader = "PIDS" // Used only on Linux pidsHeader = "PIDS" // Used only on Linux
noValue = "--"
) )
// StatsEntry represents the statistics data collected from a container // StatsEntry represents the statistics data collected from a container
@ -169,7 +171,7 @@ func (c *statsContext) Name() string {
if len(c.s.Name) > 1 { if len(c.s.Name) > 1 {
return c.s.Name[1:] return c.s.Name[1:]
} }
return "--" return noValue
} }
func (c *statsContext) ID() string { func (c *statsContext) ID() string {
@ -181,7 +183,7 @@ func (c *statsContext) ID() string {
func (c *statsContext) CPUPerc() string { func (c *statsContext) CPUPerc() string {
if c.s.IsInvalid { if c.s.IsInvalid {
return "--" return noValue
} }
return formatPercentage(c.s.CPUPercentage) return formatPercentage(c.s.CPUPercentage)
} }
@ -198,28 +200,28 @@ func (c *statsContext) MemUsage() string {
func (c *statsContext) MemPerc() string { func (c *statsContext) MemPerc() string {
if c.s.IsInvalid || c.os == winOSType { if c.s.IsInvalid || c.os == winOSType {
return "--" return noValue
} }
return formatPercentage(c.s.MemoryPercentage) return formatPercentage(c.s.MemoryPercentage)
} }
func (c *statsContext) NetIO() string { func (c *statsContext) NetIO() string {
if c.s.IsInvalid { if c.s.IsInvalid {
return "--" return noValue
} }
return units.HumanSizeWithPrecision(c.s.NetworkRx, 3) + " / " + units.HumanSizeWithPrecision(c.s.NetworkTx, 3) return units.HumanSizeWithPrecision(c.s.NetworkRx, 3) + " / " + units.HumanSizeWithPrecision(c.s.NetworkTx, 3)
} }
func (c *statsContext) BlockIO() string { func (c *statsContext) BlockIO() string {
if c.s.IsInvalid { if c.s.IsInvalid {
return "--" return noValue
} }
return units.HumanSizeWithPrecision(c.s.BlockRead, 3) + " / " + units.HumanSizeWithPrecision(c.s.BlockWrite, 3) return units.HumanSizeWithPrecision(c.s.BlockRead, 3) + " / " + units.HumanSizeWithPrecision(c.s.BlockWrite, 3)
} }
func (c *statsContext) PIDs() string { func (c *statsContext) PIDs() string {
if c.s.IsInvalid || c.os == winOSType { if c.s.IsInvalid || c.os == winOSType {
return "--" return noValue
} }
return strconv.FormatUint(c.s.PidsCurrent, 10) return strconv.FormatUint(c.s.PidsCurrent, 10)
} }

View File

@ -281,7 +281,8 @@ func (ctx *nodeInspectContext) ResourceNanoCPUs() int {
if ctx.Node.Description.Resources.NanoCPUs == 0 { if ctx.Node.Description.Resources.NanoCPUs == 0 {
return int(0) return int(0)
} }
return int(ctx.Node.Description.Resources.NanoCPUs) / 1e9 const nano = 1e9
return int(ctx.Node.Description.Resources.NanoCPUs) / nano
} }
func (ctx *nodeInspectContext) ResourceMemory() string { func (ctx *nodeInspectContext) ResourceMemory() string {

View File

@ -503,7 +503,8 @@ func (ctx *serviceInspectContext) ResourceReservationNanoCPUs() float64 {
if ctx.Service.Spec.TaskTemplate.Resources.Reservations.NanoCPUs == 0 { if ctx.Service.Spec.TaskTemplate.Resources.Reservations.NanoCPUs == 0 {
return float64(0) return float64(0)
} }
return float64(ctx.Service.Spec.TaskTemplate.Resources.Reservations.NanoCPUs) / 1e9 const nano = 1e9
return float64(ctx.Service.Spec.TaskTemplate.Resources.Reservations.NanoCPUs) / nano
} }
func (ctx *serviceInspectContext) ResourceReservationMemory() string { func (ctx *serviceInspectContext) ResourceReservationMemory() string {
@ -521,7 +522,8 @@ func (ctx *serviceInspectContext) HasResourceLimits() bool {
} }
func (ctx *serviceInspectContext) ResourceLimitsNanoCPUs() float64 { func (ctx *serviceInspectContext) ResourceLimitsNanoCPUs() float64 {
return float64(ctx.Service.Spec.TaskTemplate.Resources.Limits.NanoCPUs) / 1e9 const nano = 1e9
return float64(ctx.Service.Spec.TaskTemplate.Resources.Limits.NanoCPUs) / nano
} }
func (ctx *serviceInspectContext) ResourceLimitMemory() string { func (ctx *serviceInspectContext) ResourceLimitMemory() string {