-
-
Notifications
You must be signed in to change notification settings - Fork 802
/
widget.go
186 lines (148 loc) · 3.59 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package uptimerobot
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/view"
)
type Widget struct {
view.ScrollableWidget
monitors []Monitor
settings *Settings
err error
}
func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget {
widget := &Widget{
ScrollableWidget: view.NewScrollableWidget(tviewApp, redrawChan, pages, settings.Common),
settings: settings,
}
widget.SetRenderFunction(widget.Render)
widget.initializeKeyboardControls()
return widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
monitors, err := widget.getMonitors()
if widget.settings.offlineFirst {
var tmp Monitor
var next int
for i := 0; i < len(monitors); i++ {
if monitors[i].State != 2 {
tmp = monitors[i]
for j := i; j > next; j-- {
monitors[j] = monitors[j-1]
}
monitors[next] = tmp
next++
}
}
}
widget.monitors = monitors
widget.err = err
widget.SetItemCount(len(monitors))
widget.Render()
}
// Render sets up the widget data for redrawing to the screen
func (widget *Widget) Render() {
widget.Redraw(widget.content)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) content() (string, string, bool) {
numUp := 0
for _, monitor := range widget.monitors {
if monitor.State == 2 {
numUp++
}
}
title := fmt.Sprintf("%s (%d/%d)", widget.CommonSettings().Title, numUp, len(widget.monitors))
if widget.err != nil {
return title, widget.err.Error(), true
}
if widget.monitors == nil {
return title, "No monitors to display", false
}
str := widget.contentFrom(widget.monitors)
return title, str, false
}
func (widget *Widget) contentFrom(monitors []Monitor) string {
var str string
for _, monitor := range monitors {
prefix := ""
switch monitor.State {
case 2:
prefix += "[green] + "
case 8:
case 9:
prefix += "[red] - "
default:
prefix += "[yellow] ~ "
}
str += fmt.Sprintf(`%s%s [gray](%s)[white]
`,
prefix,
monitor.Name,
formatUptimes(monitor.Uptime),
)
}
return str
}
func formatUptimes(str string) string {
splits := strings.Split(str, "-")
str = ""
for i, s := range splits {
if i != 0 {
str += "|"
}
s = s[:5]
s = strings.TrimRight(s, "0")
s = strings.TrimRight(s, ".") + "%"
str += s
}
return str
}
type Monitor struct {
Name string `json:"friendly_name"`
// Monitor state, see: https://uptimerobot.com/api/#parameters
State int8 `json:"status"`
// Uptime ratio, preformatted, e.g.: 100.000-97.233-96.975
Uptime string `json:"custom_uptime_ratio"`
}
func (widget *Widget) getMonitors() ([]Monitor, error) {
// See: https://uptimerobot.com/api/#getMonitorsWrap
resp, errh := http.PostForm("https://api.uptimerobot.com/v2/getMonitors",
url.Values{
"api_key": {widget.settings.apiKey},
"format": {"json"},
"custom_uptime_ratios": {widget.settings.uptimePeriods},
},
)
if errh != nil {
return nil, errh
}
defer func() { _ = resp.Body.Close() }()
body, _ := io.ReadAll(resp.Body)
// First pass to read the status
c := make(map[string]json.RawMessage)
errj1 := json.Unmarshal(body, &c)
if errj1 != nil {
return nil, errj1
}
if string(c["stat"]) != `"ok"` {
return nil, errors.New(string(body))
}
// Second pass to get the actual info
var monitors []Monitor
errj2 := json.Unmarshal(c["monitors"], &monitors)
if errj2 != nil {
return nil, errj2
}
return monitors, nil
}