Skip to content

Commit

Permalink
bcicen#135 Add uptime column
Browse files Browse the repository at this point in the history
  • Loading branch information
stokito committed Oct 26, 2020
1 parent c5038e2 commit c984b27
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 5 deletions.
5 changes: 5 additions & 0 deletions config/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ var defaultColumns = []Column{
Label: "Container PID Count",
Enabled: true,
},
Column{
Name: "uptime",
Label: "Running uptime duration",
Enabled: true,
},
}

type Column struct {
Expand Down
2 changes: 1 addition & 1 deletion config/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var defaultParams = []*Param{
},
&Param{
Key: "columns",
Val: "status,name,id,cpu,mem,net,io,pids",
Val: "status,name,id,cpu,mem,net,io,pids,uptime",
Label: "Enabled Columns",
},
}
Expand Down
16 changes: 13 additions & 3 deletions connector/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package connector

import (
"fmt"
"strings"
"sync"

"github.com/bcicen/ctop/connector/collector"
"github.com/bcicen/ctop/connector/manager"
"github.com/bcicen/ctop/container"
api "github.com/fsouza/go-dockerclient"
"strings"
"sync"
"time"
)

func init() { enabled["docker"] = NewDocker }
Expand Down Expand Up @@ -123,6 +123,7 @@ func (cm *Docker) refresh(c *container.Container) {
c.SetMeta("IPs", ipsFormat(insp.NetworkSettings.Networks))
c.SetMeta("ports", portsFormat(insp.NetworkSettings.Ports))
c.SetMeta("created", insp.Created.Format("Mon Jan 2 15:04:05 2006"))
c.SetMeta("uptime", calcUptime(insp))
c.SetMeta("health", insp.State.Health.Status)
for _, env := range insp.Config.Env {
c.SetMeta("[ENV-VAR]", env)
Expand All @@ -140,6 +141,15 @@ func (cm *Docker) inspect(id string) *api.Container {
return c
}

func calcUptime(insp *api.Container) string {
endTime := insp.State.FinishedAt
if endTime.IsZero() {
endTime = time.Now()
}
uptime := endTime.Sub(insp.State.StartedAt)
return uptime.Truncate(time.Second).String()
}

// Mark all container IDs for refresh
func (cm *Docker) refreshAll() {
opts := api.ListContainersOptions{All: true}
Expand Down
9 changes: 9 additions & 0 deletions container/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ var Sorters = map[string]sortMethod{
}
return stateMap[c1state] > stateMap[c2state]
},
"uptime": func(c1, c2 *Container) bool {
// Use secondary sort method if equal values
c1Uptime := c1.GetMeta("uptime")
c2Uptime := c2.GetMeta("uptime")
if c1Uptime == c2Uptime {
return nameSorter(c1, c2)
}
return c1Uptime > c2Uptime
},
}

func SortFields() (fields []string) {
Expand Down
1 change: 1 addition & 0 deletions cwidgets/compact/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
"net": NewNetCol,
"io": NewIOCol,
"pids": NewPIDCol,
"uptime": NewUptimeCol,
}
)

Expand Down
12 changes: 12 additions & 0 deletions cwidgets/compact/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ func (w *PIDCol) SetMetrics(m models.Metrics) {
w.Text = fmt.Sprintf("%d", m.Pids)
}

type UptimeCol struct {
*TextCol
}

func NewUptimeCol() CompactCol {
return &UptimeCol{NewTextCol("UPTIME")}
}

func (w *UptimeCol) SetMeta(m models.Meta) {
w.Text = m.Get("uptime")
}

type TextCol struct {
*ui.Par
header string
Expand Down
2 changes: 1 addition & 1 deletion cwidgets/single/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
ui "github.com/gizak/termui"
)

var displayInfo = []string{"id", "name", "image", "ports", "IPs", "state", "created", "health"}
var displayInfo = []string{"id", "name", "image", "ports", "IPs", "state", "created", "uptime", "health"}

type Info struct {
*ui.Table
Expand Down

0 comments on commit c984b27

Please sign in to comment.