summaryrefslogtreecommitdiff
path: root/cmd/util/table.go
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-09-29 23:37:16 +0300
committerGitHub <[email protected]>2018-09-29 23:37:16 +0300
commit6122fca108c20984732c969fb1ba53cce5b3c44a (patch)
tree40835f46a5c208ea2546b76e3bd9fa05429b405a /cmd/util/table.go
parentabe62c0675f839586312cff1c83d6a4cb31dd9d5 (diff)
parenta04b5b33eb30944007973067ec07e9c4a2c464ab (diff)
Merge pull request #39 from spiral/feature/1.3.0v1.2.3
Feature/1.3.0
Diffstat (limited to 'cmd/util/table.go')
-rw-r--r--cmd/util/table.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/cmd/util/table.go b/cmd/util/table.go
new file mode 100644
index 00000000..565c0679
--- /dev/null
+++ b/cmd/util/table.go
@@ -0,0 +1,58 @@
+package util
+
+import (
+ "github.com/dustin/go-humanize"
+ "github.com/olekukonko/tablewriter"
+ rrutil "github.com/spiral/roadrunner/util"
+ "os"
+ "strconv"
+ "time"
+)
+
+// WorkerTable renders table with information about rr server workers.
+func WorkerTable(workers []*rrutil.State) *tablewriter.Table {
+ tw := tablewriter.NewWriter(os.Stdout)
+ tw.SetHeader([]string{"PID", "Status", "Execs", "Memory", "Created"})
+ tw.SetColMinWidth(0, 7)
+ tw.SetColMinWidth(1, 9)
+ tw.SetColMinWidth(2, 7)
+ tw.SetColMinWidth(3, 7)
+ tw.SetColMinWidth(4, 18)
+
+ for _, w := range workers {
+ tw.Append([]string{
+ strconv.Itoa(w.Pid),
+ renderStatus(w.Status),
+ renderJobs(w.NumJobs),
+ humanize.Bytes(w.MemoryUsage),
+ renderAlive(time.Unix(0, w.Created)),
+ })
+ }
+
+ return tw
+}
+
+func renderStatus(status string) string {
+ switch status {
+ case "inactive":
+ return Sprintf("<yellow>inactive</reset>")
+ case "ready":
+ return Sprintf("<cyan>ready</reset>")
+ case "working":
+ return Sprintf("<green>working</reset>")
+ case "stopped":
+ return Sprintf("<red>stopped</reset>")
+ case "errored":
+ return Sprintf("<red>errored</reset>")
+ }
+
+ return status
+}
+
+func renderJobs(number int64) string {
+ return humanize.Comma(int64(number))
+}
+
+func renderAlive(t time.Time) string {
+ return humanize.RelTime(t, time.Now(), "ago", "")
+}