Skip to content

Commit

Permalink
Merge pull request #1660 from mlafeldt/optional-stats
Browse files Browse the repository at this point in the history
Make alloc-status and node-status work without access to stats
  • Loading branch information
dadgar committed Aug 29, 2016
2 parents b813362 + 464e1c8 commit 152c11d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
15 changes: 7 additions & 8 deletions command/alloc_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,6 @@ func (c *AllocStatusCommand) Run(args []string) int {
return 0
}

var statsErr error
var stats *api.AllocResourceUsage
stats, statsErr = client.Allocations().Stats(alloc, nil)
if statsErr != nil {
c.Ui.Output("")
c.Ui.Error(fmt.Sprintf("couldn't retrieve stats (HINT: ensure Client.Advertise.HTTP is set): %v", statsErr))
}

// Format the allocation data
basic := []string{
fmt.Sprintf("ID|%s", limit(alloc.ID, length)),
Expand All @@ -236,6 +228,13 @@ func (c *AllocStatusCommand) Run(args []string) int {
if short {
c.shortTaskStatus(alloc)
} else {
var statsErr error
var stats *api.AllocResourceUsage
stats, statsErr = client.Allocations().Stats(alloc, nil)
if statsErr != nil {
c.Ui.Output("")
c.Ui.Error(fmt.Sprintf("couldn't retrieve stats (HINT: ensure Client.Advertise.HTTP is set): %v", statsErr))
}
c.outputTaskDetails(alloc, stats, displayStats)
}

Expand Down
28 changes: 15 additions & 13 deletions command/node_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,6 @@ func (c *NodeStatusCommand) Run(args []string) int {
}

func (c *NodeStatusCommand) formatNode(client *api.Client, node *api.Node) int {
// Get the host stats
hostStats, nodeStatsErr := client.Nodes().Stats(node.ID, nil)
if nodeStatsErr != nil {
c.Ui.Output("")
c.Ui.Error(fmt.Sprintf("error fetching node stats (HINT: ensure Client.Advertise.HTTP is set): %v", nodeStatsErr))
}

// Format the header output
basic := []string{
fmt.Sprintf("ID|%s", limit(node.ID, c.length)),
Expand All @@ -300,13 +293,22 @@ func (c *NodeStatusCommand) formatNode(client *api.Client, node *api.Node) int {
fmt.Sprintf("Drain|%v", node.Drain),
fmt.Sprintf("Status|%s", node.Status),
}
if hostStats != nil {
uptime := time.Duration(hostStats.Uptime * uint64(time.Second))
basic = append(basic, fmt.Sprintf("Uptime|%s", uptime.String()))
}
c.Ui.Output(c.Colorize().Color(formatKV(basic)))

if !c.short {
if c.short {
c.Ui.Output(c.Colorize().Color(formatKV(basic)))
} else {
// Get the host stats
hostStats, nodeStatsErr := client.Nodes().Stats(node.ID, nil)
if nodeStatsErr != nil {
c.Ui.Output("")
c.Ui.Error(fmt.Sprintf("error fetching node stats (HINT: ensure Client.Advertise.HTTP is set): %v", nodeStatsErr))
}
if hostStats != nil {
uptime := time.Duration(hostStats.Uptime * uint64(time.Second))
basic = append(basic, fmt.Sprintf("Uptime|%s", uptime.String()))
}
c.Ui.Output(c.Colorize().Color(formatKV(basic)))

// Get list of running allocations on the node
runningAllocs, err := getRunningAllocs(client, node.ID)
if err != nil {
Expand Down

0 comments on commit 152c11d

Please sign in to comment.