Skip to content

Commit a603c1b

Browse files
committed
Merge remote-tracking branch 'remotes/stokito/uptime'
2 parents 8f0c9f5 + c984b27 commit a603c1b

File tree

7 files changed

+42
-3
lines changed

7 files changed

+42
-3
lines changed

config/columns.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ var defaultColumns = []Column{
7171
Label: "Container PID Count",
7272
Enabled: true,
7373
},
74+
Column{
75+
Name: "uptime",
76+
Label: "Running uptime duration",
77+
Enabled: true,
78+
},
7479
}
7580

7681
type Column struct {

config/param.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var defaultParams = []*Param{
1414
},
1515
&Param{
1616
Key: "columns",
17-
Val: "status,name,id,cpu,mem,net,io,pids",
17+
Val: "status,name,id,cpu,mem,net,io,pids,uptime",
1818
Label: "Enabled Columns",
1919
},
2020
}

connector/docker.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package connector
22

33
import (
44
"fmt"
5-
"github.com/op/go-logging"
65
"strings"
76
"sync"
7+
"time"
8+
9+
"github.com/op/go-logging"
810

911
"github.com/bcicen/ctop/connector/collector"
1012
"github.com/bcicen/ctop/connector/manager"
@@ -174,6 +176,7 @@ func (cm *Docker) refresh(c *container.Container) {
174176
c.SetMeta("IPs", ipsFormat(insp.NetworkSettings.Networks))
175177
c.SetMeta("ports", portsFormat(insp.NetworkSettings.Ports))
176178
c.SetMeta("created", insp.Created.Format("Mon Jan 2 15:04:05 2006"))
179+
c.SetMeta("uptime", calcUptime(insp))
177180
c.SetMeta("health", insp.State.Health.Status)
178181
c.SetMeta("[ENV-VAR]", strings.Join(insp.Config.Env, ";"))
179182
c.SetState(insp.State.Status)
@@ -192,6 +195,15 @@ func (cm *Docker) inspect(id string) (insp *api.Container, found bool, failed bo
192195
return c, true, false
193196
}
194197

198+
func calcUptime(insp *api.Container) string {
199+
endTime := insp.State.FinishedAt
200+
if endTime.IsZero() {
201+
endTime = time.Now()
202+
}
203+
uptime := endTime.Sub(insp.State.StartedAt)
204+
return uptime.Truncate(time.Second).String()
205+
}
206+
195207
// Mark all container IDs for refresh
196208
func (cm *Docker) refreshAll() {
197209
opts := api.ListContainersOptions{All: true}

container/sort.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ var Sorters = map[string]sortMethod{
7979
}
8080
return stateMap[c1state] > stateMap[c2state]
8181
},
82+
"uptime": func(c1, c2 *Container) bool {
83+
// Use secondary sort method if equal values
84+
c1Uptime := c1.GetMeta("uptime")
85+
c2Uptime := c2.GetMeta("uptime")
86+
if c1Uptime == c2Uptime {
87+
return nameSorter(c1, c2)
88+
}
89+
return c1Uptime > c2Uptime
90+
},
8291
}
8392

8493
func SortFields() (fields []string) {

cwidgets/compact/column.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var (
2222
"net": NewNetCol,
2323
"io": NewIOCol,
2424
"pids": NewPIDCol,
25+
"uptime": NewUptimeCol,
2526
}
2627
)
2728

cwidgets/compact/text.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ func (w *PIDCol) SetMetrics(m models.Metrics) {
8989
w.setText(fmt.Sprintf("%d", m.Pids))
9090
}
9191

92+
type UptimeCol struct {
93+
*TextCol
94+
}
95+
96+
func NewUptimeCol() CompactCol {
97+
return &UptimeCol{NewTextCol("UPTIME")}
98+
}
99+
100+
func (w *UptimeCol) SetMeta(m models.Meta) {
101+
w.Text = m.Get("uptime")
102+
}
103+
92104
type TextCol struct {
93105
*ui.Par
94106
header string

cwidgets/single/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
ui "github.com/gizak/termui"
77
)
88

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

1111
type Info struct {
1212
*ui.Table

0 commit comments

Comments
 (0)