Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/stokito/uptime'
Browse files Browse the repository at this point in the history
  • Loading branch information
bcicen committed Jun 11, 2021
2 parents 8f0c9f5 + c984b27 commit a603c1b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 3 deletions.
5 changes: 5 additions & 0 deletions config/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,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 @@ -14,7 +14,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
14 changes: 13 additions & 1 deletion connector/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package connector

import (
"fmt"
"github.com/op/go-logging"
"strings"
"sync"
"time"

"github.com/op/go-logging"

"github.com/bcicen/ctop/connector/collector"
"github.com/bcicen/ctop/connector/manager"
Expand Down Expand Up @@ -174,6 +176,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)
c.SetMeta("[ENV-VAR]", strings.Join(insp.Config.Env, ";"))
c.SetState(insp.State.Status)
Expand All @@ -192,6 +195,15 @@ func (cm *Docker) inspect(id string) (insp *api.Container, found bool, failed bo
return c, true, false
}

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 @@ -22,6 +22,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 @@ -89,6 +89,18 @@ func (w *PIDCol) SetMetrics(m models.Metrics) {
w.setText(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 a603c1b

Please sign in to comment.