Skip to content

Commit

Permalink
added q to quit and a license
Browse files Browse the repository at this point in the history
  • Loading branch information
Jahaja committed Sep 20, 2020
1 parent c8dfc5f commit ce591a0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 48 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ltt-tui
/.idea/
ltt-tui
19 changes: 19 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2020 Joakim Hamren

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 1 addition & 23 deletions tui/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ type LTTClient struct {
URI string
}

func (c *LTTClient) Start() error {
resp, err := http.Get(fmt.Sprintf("%s/start", c.URI))
if err != nil {
return fmt.Errorf("failed to start ttl: %w", err)
}

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("non-ok status code returned: %d", resp.StatusCode)
}

return nil
}

func (c *LTTClient) SetNumUsers(numUsers int) error {
resp, err := http.Get(fmt.Sprintf("%s/set-num-users?num-users=%d", c.URI, numUsers))
if err != nil {
Expand All @@ -41,16 +28,7 @@ func (c *LTTClient) SetNumUsers(numUsers int) error {
}

func (c *LTTClient) Stop() error {
resp, err := http.Get(fmt.Sprintf("%s/stop", c.URI))
if err != nil {
return fmt.Errorf("failed to stop ttl: %w", err)
}

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("non-ok status code returned: %d", resp.StatusCode)
}

return nil
return c.SetNumUsers(0)
}

func (c *LTTClient) Reset() error {
Expand Down
42 changes: 18 additions & 24 deletions tui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"log"
"os"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -33,6 +34,7 @@ type UIView struct {
LoadTest *ltt.LoadTest
Client *LTTClient
UpdateInterval int
Log *log.Logger
}

func NewUIView(client *LTTClient, updateInterval int) *UIView {
Expand All @@ -42,14 +44,15 @@ func NewUIView(client *LTTClient, updateInterval int) *UIView {
TableView: "tasks",
TableSortColumnIndex: 0,
TableSortDirection: SortAsc,
Log: log.New(os.Stdout, "", log.Ldate|log.Ltime),
}
return ui
}

func (ui *UIView) Setup() {
lt, err := ui.Client.GetLoadTestInfo()
if err != nil {
log.Fatalf("setup: %s\n", err.Error())
ui.Log.Fatalf("setup: %s\n", err.Error())
}
ui.LoadTest = lt

Expand Down Expand Up @@ -159,7 +162,7 @@ func (ui *UIView) Setup() {
numUsers := ui.LoadTest.Config.NumUsers
form.AddButton("Set", func() {
if err := ui.Client.SetNumUsers(numUsers); err != nil {
log.Fatal(err.Error())
ui.Log.Fatal(err.Error())
}
})

Expand All @@ -169,10 +172,7 @@ func (ui *UIView) Setup() {
SetDoneFunc(func(index int, label string) {
if label == "Stop" {
if err := ui.Client.Stop(); err != nil {
log.Printf("stop request failed: %s\n", err.Error())
} else {
idx := ui.Form.GetButtonIndex("Stop")
ui.Form.GetButton(idx).SetLabel("Start")
ui.Log.Printf("stop request failed: %s\n", err.Error())
}
}

Expand All @@ -186,22 +186,8 @@ func (ui *UIView) Setup() {
return event
})

onOffBtnLabel := "Start"
if lt.Status != ltt.StatusStopped {
onOffBtnLabel = "Stop"
}

form.AddButton(onOffBtnLabel, func() {
btn := ui.Form.GetButton(1)
if btn.GetLabel() == "Start" {
if err := ui.Client.Start(); err != nil {
log.Fatalf("start request failed: %s\n", err.Error())
} else {
btn.SetLabel("Stop")
}
} else if btn.GetLabel() == "Stop" {
ui.App.SetRoot(confirmStop, true).SetFocus(confirmStop)
}
form.AddButton("Stop", func() {
ui.App.SetRoot(confirmStop, true).SetFocus(confirmStop)
})

form.AddButton("Errors", func() {
Expand All @@ -223,7 +209,7 @@ func (ui *UIView) Setup() {
SetDoneFunc(func(index int, label string) {
if label == "Reset" {
if err := ui.Client.Reset(); err != nil {
log.Printf("reset request failed: %s\n", err.Error())
ui.Log.Printf("reset request failed: %s\n", err.Error())
}
}

Expand All @@ -241,6 +227,7 @@ func (ui *UIView) Setup() {
ui.App.SetRoot(confirmReset, true).SetFocus(confirmReset)
})

form.SetFieldBackgroundColor(tcell.ColorLightBlue)
form.AddInputField("Num users:", strconv.Itoa(ui.LoadTest.Config.NumUsers), 8, tview.InputFieldInteger, func(text string) {
numUsers, _ = strconv.Atoi(text)
})
Expand All @@ -257,6 +244,13 @@ func (ui *UIView) Setup() {
app.SetRoot(layout, true)
app.EnableMouse(true)
app.SetFocus(form)
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Rune() {
case 'q':
ui.App.Stop()
}
return event
})

ui.Form = form
ui.Layout = layout
Expand Down Expand Up @@ -432,7 +426,7 @@ func (ui *UIView) Run() {
for {
lt, err := ui.Client.GetLoadTestInfo()
if err != nil {
log.Printf("periodical update: %w", err.Error())
ui.Log.Printf("periodical update: %w", err.Error())
}
ui.LoadTest = lt

Expand Down

0 comments on commit ce591a0

Please sign in to comment.