Skip to content

Commit

Permalink
search history
Browse files Browse the repository at this point in the history
  • Loading branch information
crossoverJie committed Dec 18, 2021
1 parent ed3056f commit bb46e7e
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Test the `gRPC` service like `postman`.


# Features
- [x] Cli performance test support.
- [x] CLI performance test support.
- [x] GUI support.
- [x] Metadata support.
- [x] Data persistence.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/golang/protobuf v1.5.2
github.com/jhump/protoreflect v1.10.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1 // indirect
github.com/urfave/cli/v2 v2.3.0
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.27.1
Expand Down
2 changes: 1 addition & 1 deletion gui/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const (
AppWeight = 1000
AppHeight = 800
HelpUrl = "https://github.com/crossoverJie/ptg"
SearchFormText = "Search"
SearchFormText = "SearchResult"
SearchFormPlaceHolder = "keyword"
TargetFormText = "Target:"
TargetFormHintText = "Input target url"
Expand Down
94 changes: 80 additions & 14 deletions gui/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/crossoverJie/ptg/gui/io"
"strings"
)

type (
History struct {
lruCache *LruCache
notifyChan chan struct{}
writeSearchChan chan struct{}
searchChan chan []*HistoryValue
historyButton *fyne.Container
alreadyButtonList []*widget.Button
targetInput *widget.Entry
Expand All @@ -21,36 +23,38 @@ type (
}

HistoryValue struct {
Id int
Value *io.Log
MethodInfo string
Id int `json:"id"`
Value *io.Log `json:"value"`
MethodInfo string `json:"method_info"`
}
)

func NewHistory(size int, historyButton *fyne.Container, targetInput, requestEntry, metadataEntry, responseEntry *widget.Entry) *History {
h := &History{
lruCache: NewLruList(size),
notifyChan: make(chan struct{}, size),
historyButton: historyButton,
targetInput: targetInput,
requestEntry: requestEntry,
metadataEntry: metadataEntry,
responseEntry: responseEntry,
lruCache: NewLruList(size),
writeSearchChan: make(chan struct{}, size),
searchChan: make(chan []*HistoryValue, size),
historyButton: historyButton,
targetInput: targetInput,
requestEntry: requestEntry,
metadataEntry: metadataEntry,
responseEntry: responseEntry,
}
go h.viewHistory()
go h.ViewSearch()
return h

}

func (h *History) Put(k, v interface{}) {
func (h *History) Put(k int, v *HistoryValue) {
h.lruCache.Put(k, v)
h.notifyChan <- struct{}{}
h.writeSearchChan <- struct{}{}
}

func (h *History) viewHistory() {
for {
select {
case <-h.notifyChan:
case <-h.writeSearchChan:

// Reset view.
for _, button := range h.alreadyButtonList {
Expand All @@ -76,3 +80,65 @@ func (h *History) viewHistory() {
}
}
}

func (h *History) SearchResult(kw string) []*HistoryValue {
var result []*HistoryValue
for _, v := range h.lruCache.List() {
historyValue := v.(*HistoryValue)
if kw == "" {
result = append(result, historyValue)
continue
}
if strings.Contains(strings.ToLower(historyValue.MethodInfo), kw) {
result = append(result, historyValue)
continue
}
if strings.Contains(strings.ToLower(historyValue.Value.Target), kw) {
result = append(result, historyValue)
continue
}
if strings.Contains(strings.ToLower(historyValue.Value.Request), kw) {
result = append(result, historyValue)
continue
}
if strings.Contains(strings.ToLower(historyValue.Value.Response), kw) {
result = append(result, historyValue)
continue
}
if strings.Contains(strings.ToLower(historyValue.Value.Metadata), kw) {
result = append(result, historyValue)
continue
}

}
h.searchChan <- result

return result
}

func (h *History) ViewSearch() {
for {
select {
case searchList := <-h.searchChan:
// Reset view.
for _, button := range h.alreadyButtonList {
h.historyButton.Remove(button)
}
h.alreadyButtonList = make([]*widget.Button, 0)
for _, v := range searchList {
historyValue := v
button := widget.NewButtonWithIcon(historyValue.MethodInfo, theme.HistoryIcon(), func() {
fmt.Println("Search tapped", historyValue.Id)
h.lruCache.Get(historyValue.Id)
h.targetInput.SetText(historyValue.Value.Target)
h.requestEntry.SetText(historyValue.Value.Request)
h.metadataEntry.SetText(historyValue.Value.Metadata)
h.responseEntry.SetText(historyValue.Value.Response)
})
h.historyButton.Add(button)
h.alreadyButtonList = append(h.alreadyButtonList, button)
}
}
}

}
55 changes: 55 additions & 0 deletions gui/history_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"encoding/json"
"fmt"
"github.com/crossoverJie/ptg/gui/io"
"github.com/stretchr/testify/assert"
"testing"
)

func TestHistory_Search(t *testing.T) {
history := NewHistory(10, nil, nil, nil, nil, nil)
history.Put(1, &HistoryValue{
Id: 1,
Value: &io.Log{
Target: "127.0.0.1:6001",
Request: "{\"order_id\":0,\"reason_id\":null,\"remark\":\"\",\"user_id\":null}",
Metadata: "{\"name\":\"abc\"}",
Response: "",
},
MethodInfo: "order.v1.OrderService.Create",
})
history.Put(2, &HistoryValue{
Id: 2,
Value: &io.Log{
Target: "127.0.0.1:6002",
Request: "{\"order_id\":99999,\"reason_id\":null,\"remark\":\"\",\"user_id\":null}",
Metadata: "{\"name\":\"zhangsan\"}",
Response: "",
},
MethodInfo: "order.v1.OrderService.Close",
})
history.Put(3, &HistoryValue{
Id: 3,
Value: &io.Log{
Target: "127.0.0.1:6003",
Request: "{\"order_id\":99999,\"reason_id\":null,\"remark\":\"\",\"user_id\":null}",
Metadata: "{\"name\":\"zhangsan\"}",
Response: "",
},
MethodInfo: "order.v1.OrderService.List",
})

search := history.SearchResult("6001")
for _, value := range search {
marshal, _ := json.Marshal(value)
fmt.Println(string(marshal))
assert.Equal(t, value.Id, 1)
}
search = history.SearchResult("9999")
for _, value := range search {
marshal, _ := json.Marshal(value)
fmt.Println(string(marshal))
}
}
7 changes: 5 additions & 2 deletions gui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {
searchAccordion := widget.NewAccordion()
searchEntry := widget.NewEntry()
historyButton := container.NewVBox()
history := NewHistory(3, historyButton, targetInput, requestEntry, metadataEntry, responseEntry)
history := NewHistory(10, historyButton, targetInput, requestEntry, metadataEntry, responseEntry)
historyId := 0

content := container.NewVBox()
Expand Down Expand Up @@ -139,13 +139,14 @@ func main() {
content.Add(searchAccordion)
content.Add(serviceAccordion)

// Search
searchEntry.SetPlaceHolder(ptgApp.SearchFormPlaceHolder)
searchForm := widget.NewForm(&widget.FormItem{
Widget: searchEntry,
HintText: ptgApp.SearchFormText,
}, &widget.FormItem{
Widget: widget.NewButtonWithIcon(ptgApp.SearchFormText, theme.SearchIcon(), func() {

history.SearchResult(strings.ToLower(searchEntry.Text))
}),
})

Expand Down Expand Up @@ -209,6 +210,8 @@ func main() {
processBar.Hide()
marshalIndent, _ := json.MarshalIndent(rpc, "", "\t")
responseEntry.SetText(string(marshalIndent))

// Write history
historyId++
history.Put(historyId, &HistoryValue{
Id: historyId,
Expand Down

0 comments on commit bb46e7e

Please sign in to comment.