summaryrefslogtreecommitdiff
path: root/cmd/util/table.go
blob: 565c06797d94adf9445416f022e60d5288d7a1f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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", "")
}