1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/datadog/widget.go
Chris Cummer aa313bdaa4 WTF-389 Log an exception and terminate if widget config is invalid
If, on startup, a widget's positional configuration is invalid (ie:
cannot be displayed onscreen) then terminate the app and inform about
which widget is erroring
2019-04-12 05:29:44 -07:00

74 lines
1.5 KiB
Go

package datadog
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
datadog "github.com/zorkian/go-datadog-api"
)
type Widget struct {
wtf.TextWidget
}
func NewWidget(app *tview.Application) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(app, "Datadog", "datadog", false),
}
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
monitors, monitorErr := Monitors()
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s", widget.Name())))
widget.View.Clear()
var content string
if monitorErr != nil {
widget.View.SetWrap(true)
content = monitorErr.Error()
} else {
widget.View.SetWrap(false)
content = widget.contentFrom(monitors)
}
widget.View.SetText(content)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) contentFrom(monitors []datadog.Monitor) string {
var str string
triggeredMonitors := []datadog.Monitor{}
for _, monitor := range monitors {
state := *monitor.OverallState
switch state {
case "Alert":
triggeredMonitors = append(triggeredMonitors, monitor)
}
}
if len(triggeredMonitors) > 0 {
str = str + fmt.Sprintf(
" %s\n",
"[red]Triggered Monitors[white]",
)
for _, triggeredMonitor := range triggeredMonitors {
str = str + fmt.Sprintf("[red] %s\n", *triggeredMonitor.Name)
}
} else {
str = str + fmt.Sprintf(
" %s\n",
"[green]No Triggered Monitors[white]",
)
}
return str
}