1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Make datadog widget scrollable/interactive

This commit is contained in:
Sean Smith 2019-05-09 23:06:34 -04:00
parent 4e8c85bc34
commit 8c4bf5abc6
3 changed files with 120 additions and 20 deletions

View File

@ -90,7 +90,7 @@ func MakeWidget(
widget = cryptolive.NewWidget(app, settings) widget = cryptolive.NewWidget(app, settings)
case "datadog": case "datadog":
settings := datadog.NewSettingsFromYAML(widgetName, moduleConfig, globalConfig) settings := datadog.NewSettingsFromYAML(widgetName, moduleConfig, globalConfig)
widget = datadog.NewWidget(app, settings) widget = datadog.NewWidget(app, pages, settings)
case "gcal": case "gcal":
settings := gcal.NewSettingsFromYAML(widgetName, moduleConfig, globalConfig) settings := gcal.NewSettingsFromYAML(widgetName, moduleConfig, globalConfig)
widget = gcal.NewWidget(app, settings) widget = gcal.NewWidget(app, settings)

View File

@ -0,0 +1,17 @@
package datadog
import (
"github.com/gdamore/tcell"
)
func (widget *Widget) initializeKeyboardControls() {
widget.SetKeyboardChar("/", widget.ShowHelp)
widget.SetKeyboardChar("j", widget.next)
widget.SetKeyboardChar("k", widget.prev)
widget.SetKeyboardChar("o", widget.openItem)
widget.SetKeyboardKey(tcell.KeyDown, widget.next)
widget.SetKeyboardKey(tcell.KeyEnter, widget.openItem)
widget.SetKeyboardKey(tcell.KeyEsc, widget.unselect)
widget.SetKeyboardKey(tcell.KeyUp, widget.prev)
}

View File

@ -2,27 +2,56 @@ package datadog
import ( import (
"fmt" "fmt"
"strconv"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/wtf"
datadog "github.com/zorkian/go-datadog-api" datadog "github.com/zorkian/go-datadog-api"
) )
const HelpText = `
Keyboard commands for Datadog:
/: Show/hide this help window
j: Select the next item in the list
k: Select the previous item in the list
arrow down: Select the next item in the list
arrow up: Select the previous item in the list
return: Open the selected issue in a browser
`
type Widget struct { type Widget struct {
wtf.HelpfulWidget
wtf.TextWidget wtf.TextWidget
wtf.KeyboardWidget
app *tview.Application app *tview.Application
selected int
monitors []datadog.Monitor
settings *Settings settings *Settings
} }
func NewWidget(app *tview.Application, settings *Settings) *Widget { func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{ widget := Widget{
TextWidget: wtf.NewTextWidget(app, settings.common, false), HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
KeyboardWidget: wtf.NewKeyboardWidget(),
TextWidget: wtf.NewTextWidget(app, settings.common, true),
app: app, app: app,
settings: settings, settings: settings,
} }
widget.initializeKeyboardControls()
widget.View.SetInputCapture(widget.InputCapture)
widget.unselect()
widget.View.SetScrollable(true)
widget.View.SetRegions(true)
widget.HelpfulWidget.SetView(widget.View)
return &widget return &widget
} }
@ -35,22 +64,12 @@ func (widget *Widget) Refresh() {
if monitorErr != nil { if monitorErr != nil {
widget.View.SetWrap(true) widget.View.SetWrap(true)
content = monitorErr.Error() content = monitorErr.Error()
} else { widget.app.QueueUpdateDraw(func() {
widget.View.SetWrap(false) widget.View.SetTitle(widget.ContextualTitle(widget.CommonSettings.Title))
content = widget.contentFrom(monitors) widget.View.SetText(content)
})
return
} }
widget.app.QueueUpdateDraw(func() {
widget.View.SetTitle(widget.ContextualTitle(widget.CommonSettings.Title))
widget.View.Clear()
widget.View.SetText(content)
})
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) contentFrom(monitors []datadog.Monitor) string {
var str string
triggeredMonitors := []datadog.Monitor{} triggeredMonitors := []datadog.Monitor{}
for _, monitor := range monitors { for _, monitor := range monitors {
@ -60,13 +79,39 @@ func (widget *Widget) contentFrom(monitors []datadog.Monitor) string {
triggeredMonitors = append(triggeredMonitors, monitor) triggeredMonitors = append(triggeredMonitors, monitor)
} }
} }
widget.monitors = triggeredMonitors
widget.display()
}
func (widget *Widget) display() {
content := widget.contentFrom(widget.monitors)
widget.app.QueueUpdateDraw(func() {
widget.View.SetWrap(false)
widget.View.Clear()
widget.View.SetTitle(widget.ContextualTitle(widget.CommonSettings.Title))
widget.View.SetText(content)
widget.View.Highlight(strconv.Itoa(widget.selected)).ScrollToHighlight()
})
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) contentFrom(triggeredMonitors []datadog.Monitor) string {
var str string
if len(triggeredMonitors) > 0 { if len(triggeredMonitors) > 0 {
str = str + fmt.Sprintf( str = str + fmt.Sprintf(
" %s\n", " %s\n",
"[red]Triggered Monitors[white]", "[red]Triggered Monitors[white]",
) )
for _, triggeredMonitor := range triggeredMonitors { for idx, triggeredMonitor := range triggeredMonitors {
str = str + fmt.Sprintf("[red] %s\n", *triggeredMonitor.Name) str = str + fmt.Sprintf(`["%d"][%s][red] %s[%s][""]`,
idx,
widget.rowColor(idx),
*triggeredMonitor.Name,
widget.rowColor(idx),
) + "\n"
} }
} else { } else {
str = str + fmt.Sprintf( str = str + fmt.Sprintf(
@ -77,3 +122,41 @@ func (widget *Widget) contentFrom(monitors []datadog.Monitor) string {
return str return str
} }
func (widget *Widget) unselect() {
widget.selected = -1
widget.display()
}
func (widget *Widget) next() {
widget.selected++
if widget.monitors != nil && widget.selected >= len(widget.monitors) {
widget.selected = 0
}
widget.display()
}
func (widget *Widget) prev() {
widget.selected--
if widget.selected < 0 && widget.monitors != nil {
widget.selected = len(widget.monitors) - 1
}
widget.display()
}
func (widget *Widget) openItem() {
sel := widget.selected
if sel >= 0 && widget.monitors != nil && sel < len(widget.monitors) {
item := &widget.monitors[widget.selected]
wtf.OpenFile(fmt.Sprintf("https://app.datadoghq.com/monitors/%d?q=*", *item.Id))
}
}
func (widget *Widget) rowColor(idx int) string {
if widget.View.HasFocus() && (idx == widget.selected) {
widget.settings.common.DefaultFocussedRowColor()
}
return widget.settings.common.RowColor(idx)
}