go_dreamfactory/cmd/v2/ui/app_monitor.go
2022-09-22 20:13:58 +08:00

147 lines
4.3 KiB
Go

package ui
import (
"fmt"
"go_dreamfactory/cmd/v2/lib/common"
"go_dreamfactory/cmd/v2/model"
"go_dreamfactory/cmd/v2/service/observer"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/anypb"
)
type appMonitor struct {
appAdapter
obs observer.Observer
logPanel *widget.Entry
monitorHeader *widget.List
monitorList *widget.List
monitorBinding binding.UntypedList
monitorData *model.PushModelList
}
func (this *appMonitor) LazyInit(obs observer.Observer) error {
this.obs = obs
this.tabItem = container.NewTabItemWithIcon(common.TOOLBAR_MONITOR, theme.MediaVideoIcon(), nil)
content := container.NewMax()
content.Objects = []fyne.CanvasObject{}
// panel for output log
this.logPanel = widget.NewMultiLineEntry()
this.logPanel.Wrapping = fyne.TextWrapWord
//clear button
clearBtn := widget.NewButtonWithIcon(common.APP_TESTCASE_BTN_CLEARLOG, theme.DeleteIcon(), func() {
this.logPanel.SetText("")
this.monitorBinding.Set([]interface{}{})
})
resPanel := container.NewBorder(container.NewHBox(clearBtn, layout.NewSpacer()), nil, nil, nil, this.logPanel)
this.monitorBinding = binding.NewUntypedList()
this.monitorData = model.NewPushModelList()
this.createMonitorList()
// layout
panel := container.NewVSplit(container.NewBorder(this.monitorHeader, nil, nil, nil, this.monitorList), resPanel)
content.Objects = append(content.Objects, panel)
this.tabItem.Content = content
this.Run()
return nil
}
func (a *appMonitor) OpenDefault() bool {
return true
}
func (a *appMonitor) GetAppName() string {
return common.TOOLBAR_MONITOR
}
func (a appMonitor) OnClose() bool {
return false
}
// monitor list data
func (this *appMonitor) Run() {
this.obs.AddListener(observer.EVENT_APP_MONI, observer.Listener{
OnNotify: func(d interface{}, args ...interface{}) {
time.Sleep(time.Millisecond * 20)
data := d.(*model.PushModel)
this.monitorData.DataList = append(this.monitorData.DataList, data)
this.reloadMonitorData()
showTip("收到新的数据推送,请打开[监控]页面")
},
})
}
func (this *appMonitor) reloadMonitorData() {
if this.monitorData != nil {
d := this.monitorData.AsInterfaceArray()
this.monitorBinding.Set(d)
}
this.monitorList.ScrollToBottom()
}
func (this *appMonitor) createMonitorList() {
// header
this.monitorHeader = widget.NewList(
func() int {
return 1
},
func() fyne.CanvasObject {
return container.NewGridWithColumns(3,
widget.NewLabelWithStyle("", fyne.TextAlignLeading, fyne.TextStyle{}),
widget.NewLabelWithStyle("", fyne.TextAlignCenter, fyne.TextStyle{}),
widget.NewLabelWithStyle("", fyne.TextAlignCenter, fyne.TextStyle{}))
},
func(id widget.ListItemID, item fyne.CanvasObject) {
item.(*fyne.Container).Objects[0].(*widget.Label).SetText(common.APP_MONITOR_TITLE_ID)
item.(*fyne.Container).Objects[1].(*widget.Label).SetText(common.APP_MONITOR_TITLE_DATA)
item.(*fyne.Container).Objects[2].(*widget.Label).SetText(common.APP_MONITOR_TITLE_DATE)
},
)
// data
this.monitorList = widget.NewListWithData(this.monitorBinding,
func() fyne.CanvasObject {
return container.NewGridWithColumns(3,
widget.NewLabelWithStyle("", fyne.TextAlignLeading, fyne.TextStyle{}),
widget.NewLabelWithStyle("", fyne.TextAlignCenter, fyne.TextStyle{}),
widget.NewLabelWithStyle("", fyne.TextAlignCenter, fyne.TextStyle{}),
)
},
func(data binding.DataItem, item fyne.CanvasObject) {
o, _ := data.(binding.Untyped).Get()
pd := o.(*model.PushModel)
item.(*fyne.Container).Objects[0].(*widget.Label).SetText(fmt.Sprintf("%s.%s", pd.Msg.MainType, pd.Msg.SubType))
item.(*fyne.Container).Objects[1].(*widget.Label).SetText(pd.MethodName)
item.(*fyne.Container).Objects[2].(*widget.Label).SetText(pd.DataTime)
},
)
this.monitorList.OnSelected = func(id widget.ListItemID) {
if di, err := this.monitorBinding.GetItem(id); err != nil {
logrus.Error(err)
} else {
o, _ := di.(binding.Untyped).Get()
pd := o.(*model.PushModel)
if res, err := anypb.New(pd.Msg.Data); err == nil {
this.logPanel.Text = res.String()
this.logPanel.Refresh()
} else {
logrus.WithField("err", err).Error("view detail")
}
}
}
}