summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormatthieu <matthieu.cneude@gmail.com>2020-06-06 16:22:04 +0200
committermatthieu <matthieu.cneude@gmail.com>2020-06-06 16:22:04 +0200
commit70df0f0fc7b7611b6dac09486c705ccaf15a692d (patch)
tree99c35e32d17d9d78827704f7bc5014dd1567402c
parent705aae30c0fe99622ebab668bc3867215f483ab3 (diff)
Add gauge general widget
-rw-r--r--internal/host_widget.go66
-rw-r--r--internal/platform/host.go16
-rw-r--r--internal/platform/host_test.go98
-rw-r--r--internal/platform/termui.go1
-rw-r--r--internal/tui.go2
5 files changed, 144 insertions, 39 deletions
diff --git a/internal/host_widget.go b/internal/host_widget.go
index 2dce367..11de304 100644
--- a/internal/host_widget.go
+++ b/internal/host_widget.go
@@ -27,6 +27,7 @@ const (
rhTableDisk = "rh.table_disk"
rhTable = "rh.table"
rhBox = "rh.box"
+ rhGauge = "rh.gauge"
)
type HostWidget struct {
@@ -84,6 +85,8 @@ func (ms *HostWidget) CreateWidgets(widget Widget, tui *Tui) (f func() error, er
f, err = ms.table(widget)
case rhBox:
f, err = ms.box(widget)
+ case rhGauge:
+ f, err = ms.gauge(widget)
default:
return nil, errors.Errorf("can't find the widget %s", widget.Name)
}
@@ -144,6 +147,32 @@ func (ms *HostWidget) boxUptime(widget Widget) (f func() error, err error) {
return
}
+func formatSeconds(dur time.Duration) string {
+ dur = dur - (dur % time.Second)
+ var days int
+ for dur.Hours() > 24.0 {
+ days++
+ dur -= 24 * time.Hour
+ }
+ for dur.Hours() > 24.0 {
+ days++
+ dur -= 24 * time.Hour
+ }
+
+ s1 := dur.String()
+ s2 := ""
+ if days > 0 {
+ s2 = fmt.Sprintf("%dd ", days)
+ }
+ for _, ch := range s1 {
+ s2 += string(ch)
+ if ch == 'h' || ch == 'm' {
+ s2 += " "
+ }
+ }
+ return s2
+}
+
func (ms *HostWidget) boxCPURate(widget Widget) (f func() error, err error) {
title := " CPU usage "
if _, ok := widget.Options[optionTitle]; ok {
@@ -448,28 +477,27 @@ func (ms *HostWidget) box(widget Widget) (f func() error, err error) {
return
}
-func formatSeconds(dur time.Duration) string {
- dur = dur - (dur % time.Second)
- var days int
- for dur.Hours() > 24.0 {
- days++
- dur -= 24 * time.Hour
+
+func (ms *HostWidget) gauge(widget Widget) (f func() error, err error) {
+ title := fmt.Sprintf(" Gauge ")
+ if _, ok := widget.Options[optionTitle]; ok {
+ title = widget.Options[optionTitle]
}
- for dur.Hours() > 24.0 {
- days++
- dur -= 24 * time.Hour
+
+ cmd := "echo 50"
+ // cmd := "/bin/df -x devtmpfs -x tmpfs -x debugfs | sed -n '1!p'"
+ if _, ok := widget.Options[optionCommand]; ok {
+ cmd = widget.Options[optionCommand]
}
- s1 := dur.String()
- s2 := ""
- if days > 0 {
- s2 = fmt.Sprintf("%dd ", days)
+ data, err := platform.HostGauge(ms.service.Runner, cmd)
+ if err != nil {
+ return nil, err
}
- for _, ch := range s1 {
- s2 += string(ch)
- if ch == 'h' || ch == 'm' {
- s2 += " "
- }
+
+ f = func() error {
+ return ms.tui.AddGauge(data, title, widget.Options)
}
- return s2
+
+ return
}
diff --git a/internal/platform/host.go b/internal/platform/host.go
index 6d21448..df3b503 100644
--- a/internal/platform/host.go
+++ b/internal/platform/host.go
@@ -500,6 +500,22 @@ func HostBox(runner runnerFunc, command string) (string, error) {
return "", nil
}
+func HostGauge(runner runnerFunc, command string) (float64, error) {
+ lines, err := runner(command)
+ if err != nil {
+ return 0, err
+ }
+
+ scanner := bufio.NewScanner(strings.NewReader(lines))
+
+ for scanner.Scan() {
+ f, _ := strconv.ParseFloat(scanner.Text(), strconv.IntSize)
+ return f, nil
+ }
+
+ return 0, nil
+}
+
func formatToBar(data string) (val []uint64) {
data = strings.Trim(data, ",")
s := strings.Split(data, ",")
diff --git a/internal/platform/host_test.go b/internal/platform/host_test.go
index 8125c18..42c5a23 100644
--- a/internal/platform/host_test.go
+++ b/internal/platform/host_test.go
@@ -95,7 +95,7 @@ func Test_HostUptime(t *testing.T) {
wantErr: true,
},
{
- name: "Runner return impossible number",
+ name: "Runner return wrong number",
runner: func(cmd string) (string, error) { return "hello", nil },
wantErr: true,
},
@@ -140,9 +140,10 @@ func Test_HostLoad(t *testing.T) {
wantErr: true,
},
{
- name: "runner return impossible result",
- runner: func(cmd string) (string, error) { return "hello", nil },
- wantErr: true,
+ name: "runner return wrong result",
+ expected: "",
+ runner: func(cmd string) (string, error) { return "hello", nil },
+ wantErr: true,
},
}
@@ -184,7 +185,7 @@ func Test_HostProcesses(t *testing.T) {
wantErr: true,
},
{
- name: "runner return impossible result",
+ name: "runner return wrong result",
runner: func(cmd string) (string, error) { return "hello", nil },
wantErr: true,
},
@@ -244,7 +245,7 @@ func Test_HostMemory(t *testing.T) {
wantErr: true,
},
{
- name: "runner return impossible result",
+ name: "runner return wrong result",
runner: func(cmd string) (string, error) {
return string(ReadFixtureFile("./testdata/fixtures/ga_users.json", t)), errors.New("Error")
},
@@ -300,7 +301,7 @@ func Test_HostMemoryRate(t *testing.T) {
wantErr: true,
},
{
- name: "runner return impossible result",
+ name: "runner return wrong result",
runner: func(cmd string) (string, error) {
return string(ReadFixtureFile("./testdata/fixtures/ga_users.json", t)), errors.New("Error")
},
@@ -354,7 +355,7 @@ func Test_HostSwapRate(t *testing.T) {
wantErr: true,
},
{
- name: "runner return impossible result",
+ name: "runner return wrong result",
runner: func(cmd string) (string, error) {
return string(ReadFixtureFile("./testdata/fixtures/ga_users.json", t)), errors.New("Error")
},
@@ -407,7 +408,7 @@ func Test_HostCPURate(t *testing.T) {
wantErr: true,
},
{
- name: "runner return impossible result",
+ name: "runner return wrong result",
runner: func(cmd string) (string, error) {
return string(ReadFixtureFile("./testdata/fixtures/ga_users.json", t)), errors.New("Error")
},
@@ -465,7 +466,7 @@ func Test_HostNetIO(t *testing.T) {
wantErr: true,
},
{
- name: "runner return impossible result",
+ name: "runner return wrong result",
unit: "kb",
runner: func(cmd string) (string, error) {
return string(ReadFixtureFile("./testdata/fixtures/ga_users.json", t)), errors.New("Error")
@@ -532,13 +533,14 @@ func Test_HostDisk(t *testing.T) {
wantErr: true,
},
{
- name: "runner return impossible result",
- headers: []string{"Filesystem", "Size", "Used", "Available", "Use%", "Mount"},
- unit: "kb",
+ name: "runner return wrong result",
+ headers: []string{"Filesystem", "Size", "Used", "Available", "Use%", "Mount"},
+ expected: [][]string{{"Filesystem", "Size", "Used", "Available", "Use%", "Mount"}},
+ unit: "kb",
runner: func(cmd string) (string, error) {
- return string(ReadFixtureFile("./testdata/fixtures/ga_users.json", t)), errors.New("Error")
+ return "hello", nil
},
- wantErr: true,
+ wantErr: false,
},
}
@@ -592,12 +594,13 @@ func Test_HostDiskIO(t *testing.T) {
wantErr: true,
},
{
- name: "runner return impossible result",
- unit: "kb",
+ name: "runner return wrong result",
+ unit: "kb",
+ expected: "0.00 / 0.00",
runner: func(cmd string) (string, error) {
- return string(ReadFixtureFile("./testdata/fixtures/ga_users.json", t)), errors.New("Error")
+ return string(ReadFixtureFile("./testdata/fixtures/ga_users.json", t)), nil
},
- wantErr: true,
+ wantErr: false,
},
}
@@ -664,3 +667,60 @@ func Test_HostBox(t *testing.T) {
})
}
}
+
+func Test_HostGauge(t *testing.T) {
+ testCases := []struct {
+ name string
+ runner runnerFunc
+ expected float64
+ command string
+ wantErr bool
+ }{
+ {
+ name: "happy case",
+ expected: 60,
+ runner: func(cmd string) (string, error) {
+ return "60", nil
+ },
+ wantErr: false,
+ },
+ {
+ name: "empty result",
+ expected: 0,
+ runner: func(cmd string) (string, error) {
+ return "", nil
+ },
+ wantErr: false,
+ },
+ {
+ name: "runner return wrong result",
+ expected: 0,
+ runner: func(cmd string) (string, error) {
+ return "ldfsjsdf", nil
+ },
+ wantErr: false,
+ },
+ {
+ name: "runner return error",
+ command: "kb",
+ runner: func(cmd string) (string, error) {
+ return "60", errors.New("Error")
+ },
+ wantErr: true,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ actual, err := HostGauge(tc.runner, tc.command)
+ if (err != nil) != tc.wantErr {
+ t.Errorf("Error '%v' even if wantErr is %t", err, tc.wantErr)
+ return
+ }
+
+ if tc.wantErr == false && actual != tc.expected {
+ t.Errorf("Expected %v, actual %v", tc.expected, actual)
+ }
+ })
+ }
+}
diff --git a/internal/platform/termui.go b/internal/platform/termui.go
index 86de192..69a7869 100644
--- a/internal/platform/termui.go
+++ b/internal/platform/termui.go
@@ -92,6 +92,7 @@ func (t *termUI) Gauge(
gauge.PercentColor = termui.Attribute(textColor)
gauge.BorderLabel = title
gauge.Percent = data
+ gauge.Height = height
t.widgets = append(t.widgets, gauge)
}
diff --git a/internal/tui.go b/internal/tui.go
index 139fa3f..1c152e1 100644
--- a/internal/tui.go
+++ b/internal/tui.go
@@ -378,7 +378,7 @@ func (t *Tui) AddGauge(
options map[string]string,
) (err error) {
- var height int64 = 1
+ var height int64 = 3
if _, ok := options[optionHeight]; ok {
height, _ = strconv.ParseInt(options[optionHeight], 0, 0)
}