-
-
Notifications
You must be signed in to change notification settings - Fork 805
/
widget.go
118 lines (94 loc) · 2.68 KB
/
widget.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package datadog
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view"
datadog "github.com/zorkian/go-datadog-api"
)
type Widget struct {
view.KeyboardWidget
view.ScrollableWidget
monitors []datadog.Monitor
settings *Settings
err error
}
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
KeyboardWidget: view.NewKeyboardWidget(app, pages, settings.common),
ScrollableWidget: view.NewScrollableWidget(app, settings.common),
settings: settings,
}
widget.SetRenderFunction(widget.Render)
widget.initializeKeyboardControls()
widget.View.SetInputCapture(widget.InputCapture)
widget.KeyboardWidget.SetView(widget.View)
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
widget.err = nil
monitors, monitorErr := widget.Monitors()
if monitorErr != nil {
widget.monitors = nil
widget.err = monitorErr
widget.SetItemCount(0)
widget.Redraw(func() (string, string, bool) { return widget.CommonSettings().Title, monitorErr.Error(), true })
return
}
triggeredMonitors := []datadog.Monitor{}
for _, monitor := range monitors {
state := *monitor.OverallState
if state == "Alert" {
triggeredMonitors = append(triggeredMonitors, monitor)
}
}
widget.monitors = triggeredMonitors
widget.SetItemCount(len(widget.monitors))
widget.Render()
}
func (widget *Widget) Render() {
widget.Redraw(widget.content)
}
func (widget *Widget) HelpText() string {
return widget.KeyboardWidget.HelpText()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) content() (string, string, bool) {
triggeredMonitors := widget.monitors
var str string
title := widget.CommonSettings().Title
if widget.err != nil {
return title, widget.err.Error(), true
}
if len(triggeredMonitors) > 0 {
str += fmt.Sprintf(
" %s\n",
fmt.Sprintf(
"[%s]Triggered Monitors[white]",
widget.settings.common.Colors.Subheading,
),
)
for idx, triggeredMonitor := range triggeredMonitors {
row := fmt.Sprintf(`[%s][red] %s[%s]`,
widget.RowColor(idx),
*triggeredMonitor.Name,
widget.RowColor(idx),
)
str += utils.HighlightableHelper(widget.View, row, idx, len(*triggeredMonitor.Name))
}
} else {
str += fmt.Sprintf(
" %s\n",
"[green]No Triggered Monitors[white]",
)
}
return title, str, false
}
func (widget *Widget) openItem() {
sel := widget.GetSelected()
if sel >= 0 && widget.monitors != nil && sel < len(widget.monitors) {
item := &widget.monitors[sel]
utils.OpenFile(fmt.Sprintf("https://app.datadoghq.com/monitors/%d?q=*", *item.Id))
}
}