(cli): Add redis view

This commit is contained in:
Ken Hibino
2022-05-15 13:34:39 -07:00
parent e52a4b97e5
commit a895145a43

View File

@@ -53,11 +53,19 @@ const (
) )
type dashState struct { type dashState struct {
queues []*asynq.QueueInfo queues []*asynq.QueueInfo
err error redisInfo redisInfo
rowIdx int // highlighted row err error
view viewType // current view type rowIdx int // highlighted row
prevView viewType // to support "go back" view viewType // current view type
prevView viewType // to support "go back"
}
type redisInfo struct {
version string
uptime string
memoryUsage int
peakMemoryUsage int
} }
func dash(cmd *cobra.Command, args []string) { func dash(cmd *cobra.Command, args []string) {
@@ -79,8 +87,9 @@ func dash(cmd *cobra.Command, args []string) {
// channels to send/receive data fetched asynchronously // channels to send/receive data fetched asynchronously
var ( var (
queuesCh = make(chan []*asynq.QueueInfo) errorCh = make(chan error)
errorCh = make(chan error) queuesCh = make(chan []*asynq.QueueInfo)
redisInfoCh = make(chan *redisInfo)
) )
go getQueueInfo(inspector, queuesCh, errorCh) go getQueueInfo(inspector, queuesCh, errorCh)
@@ -150,12 +159,16 @@ func dash(cmd *cobra.Command, args []string) {
state.view = viewTypeQueues state.view = viewTypeQueues
drawDash(s, baseStyle, &state) drawDash(s, baseStyle, &state)
} else if ev.Key() == tcell.KeyF2 && state.view != viewTypeServers { } else if ev.Key() == tcell.KeyF2 && state.view != viewTypeServers {
//TODO Start data fetch and reset ticker
state.view = viewTypeServers state.view = viewTypeServers
drawDash(s, baseStyle, &state) drawDash(s, baseStyle, &state)
} else if ev.Key() == tcell.KeyF3 && state.view != viewTypeSchedulers { } else if ev.Key() == tcell.KeyF3 && state.view != viewTypeSchedulers {
//TODO Start data fetch and reset ticker
state.view = viewTypeSchedulers state.view = viewTypeSchedulers
drawDash(s, baseStyle, &state) drawDash(s, baseStyle, &state)
} else if ev.Key() == tcell.KeyF4 && state.view != viewTypeRedis { } else if ev.Key() == tcell.KeyF4 && state.view != viewTypeRedis {
go getRedisInfo(redisInfoCh, errorCh)
ticker.Reset(interval)
state.view = viewTypeRedis state.view = viewTypeRedis
drawDash(s, baseStyle, &state) drawDash(s, baseStyle, &state)
} }
@@ -165,8 +178,8 @@ func dash(cmd *cobra.Command, args []string) {
switch state.view { switch state.view {
case viewTypeQueues: case viewTypeQueues:
go getQueueInfo(inspector, queuesCh, errorCh) go getQueueInfo(inspector, queuesCh, errorCh)
case viewTypeRedis:
// TODO: Add more cases for other type of data go getRedisInfo(redisInfoCh, errorCh)
} }
case queues := <-queuesCh: case queues := <-queuesCh:
@@ -174,6 +187,11 @@ func dash(cmd *cobra.Command, args []string) {
state.err = nil state.err = nil
drawDash(s, baseStyle, &state) drawDash(s, baseStyle, &state)
case redisInfo := <-redisInfoCh:
state.redisInfo = *redisInfo
state.err = nil
drawDash(s, baseStyle, &state)
case err := <-errorCh: case err := <-errorCh:
state.err = err state.err = err
drawDash(s, baseStyle, &state) drawDash(s, baseStyle, &state)
@@ -210,6 +228,16 @@ func getQueueInfo(i *asynq.Inspector, queuesCh chan<- []*asynq.QueueInfo, errorC
} }
func getRedisInfo(redisInfoCh chan<- *redisInfo, errorCh chan<- error) {
n := rand.Intn(1000)
redisInfoCh <- &redisInfo{
version: "6.2.6",
uptime: "9 days",
memoryUsage: n,
peakMemoryUsage: n + 123,
}
}
func drawDash(s tcell.Screen, style tcell.Style, state *dashState) { func drawDash(s tcell.Screen, style tcell.Style, state *dashState) {
s.Clear() s.Clear()
// Simulate data update on every render // Simulate data update on every render
@@ -232,7 +260,10 @@ func drawDash(s tcell.Screen, style tcell.Style, state *dashState) {
case viewTypeRedis: case viewTypeRedis:
d.Println("=== Redis Info === ", style.Bold(true)) d.Println("=== Redis Info === ", style.Bold(true))
d.NL() // empty line d.NL() // empty line
// TODO: Draw body d.Println(fmt.Sprintf("Version: %s", state.redisInfo.version), style)
d.Println(fmt.Sprintf("Uptime: %s", state.redisInfo.uptime), style)
d.Println(fmt.Sprintf("Memory Usage: %s", ByteCount(int64(state.redisInfo.memoryUsage))), style)
d.Println(fmt.Sprintf("Peak Memory Usage: %s", ByteCount(int64(state.redisInfo.peakMemoryUsage))), style)
case viewTypeHelp: case viewTypeHelp:
d.Println("=== HELP ===", style.Bold(true)) d.Println("=== HELP ===", style.Bold(true))
d.NL() // empty line d.NL() // empty line