-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
212 lines (176 loc) · 5.71 KB
/
model.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
// special = lipgloss.AdaptiveColor{Light: "#43BF6D", Dark: "#73F59F"}
docStyle = lipgloss.NewStyle().Margin(1, 2)
detailHeaderStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FAFAFA")).
Padding(0, 1).
Width(100)
detailDataStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FAFAFA")).
PaddingTop(2).
PaddingLeft(2).
PaddingBottom(1).
Width(22)
titleStyle = lipgloss.NewStyle().
MarginBottom(1).
Align(lipgloss.Left).
Background(lipgloss.Color("#FF5F87")).
Width(100)
statusBarStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#343433", Dark: "#C1C6B2"}).
Background(lipgloss.AdaptiveColor{Light: "#D9DCCF", Dark: "#353533"})
)
// model is the bubbletea model
type model struct {
// applications is a list of applications to be monitored
applications []application
metadata metadata
healthcheckInterval time.Duration
viewport viewport.Model
// showPiHoleDetail is a flag to indicate if the pi hole detailed view should be shown
showPiHoleDetail bool
// piHoleSummary store Pi-hole DNS statistics
piHoleSummary PiHSummary
// client is the http client used for making calls to the applications
client *http.Client
applicationList list.Model
}
func (m model) Init() tea.Cmd {
m.viewport = viewport.Model{Width: width, Height: 10}
m.viewport.YPosition = 0
return m.checkApplications(10 * time.Millisecond)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.applicationList.SetSize(msg.Width-h, msg.Height-v)
return m, nil
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "enter":
log.Println("enter pressed")
if m.applicationList.SelectedItem().FilterValue() == "Pi-hole" {
m.showPiHoleDetail = true
m.piHoleSummary = m.fetchPiHoleStats()
}
case "esc":
if m.showPiHoleDetail {
m.showPiHoleDetail = false
}
return m, nil
}
case statusMsg:
m.metadata.status = "Looking good..."
for i, app := range m.applications {
m.applications[i].httpResp.status = msg[app.URL]
switch m.applications[i].httpResp.status {
case http.StatusOK:
// do nothing
default:
m.metadata.status = fmt.Sprintf("%s might be having issues...", app.Name)
}
}
m.applicationList.SetItems(appsToItems(m.applications))
return m, m.checkApplications(m.healthcheckInterval)
}
m.applicationList, cmd = m.applicationList.Update(msg)
return m, cmd
}
func (m model) View() string {
var b strings.Builder
// Apply titleStyle to the title and add it to the top of the view
title := titleStyle.Render(m.metadata.title)
b.WriteString(title + "\n\n")
if m.showPiHoleDetail {
// Render the detailed page for Pi-hole with actual statistics
var piHoleDetailBuilder strings.Builder
piHoleDetailBuilder.WriteString(detailHeaderStyle.Render("Pi-hole Detailed View") + "\n\n")
piHoleDetailBuilder.WriteString(fmt.Sprintf(detailHeaderStyle.Render("Total Queries: ") + detailDataStyle.Render(m.piHoleSummary.DNSQueries) + "\n"))
piHoleDetailBuilder.WriteString(fmt.Sprintf(detailHeaderStyle.Render("Queries Blocked: ") + detailDataStyle.Render(m.piHoleSummary.AdsBlocked) + "\n"))
piHoleDetailBuilder.WriteString(fmt.Sprintf(detailHeaderStyle.Render("Percentage Blocked: ") + detailDataStyle.Render(m.piHoleSummary.AdsPercentage+"%%") + "\n"))
piHoleDetailBuilder.WriteString(fmt.Sprintf(detailHeaderStyle.Render("Domains on Adlist: ") + detailDataStyle.Render(m.piHoleSummary.DomainsBlocked) + "\n"))
return piHoleDetailBuilder.String()
}
b.WriteString(m.applicationsView())
// Check if all applications are good
allGood := true
for _, app := range m.applications {
if app.httpResp.status != http.StatusOK {
allGood = false
break
}
}
// Create status bar
var statusBar string
if allGood {
statusBar = statusBarStyle.Render("All good..")
} else {
statusBar = statusBarStyle.Render(m.metadata.status)
}
// Append status bar to the view
b.WriteString("\n" + statusBar + "\n")
return b.String()
}
func (m *model) applicationsView() string {
return docStyle.Render(m.applicationList.View())
}
func (m model) checkApplications(d time.Duration) tea.Cmd {
return tea.Tick(d, func(t time.Time) tea.Msg {
msg := make(statusMsg)
for _, app := range m.applications {
req, err := http.NewRequest("GET", app.URL, nil)
if err != nil {
msg[app.URL] = 0
continue
}
if app.AuthHeader != "" && app.AuthKey != "" {
req.Header.Add(app.AuthHeader, app.AuthKey)
}
res, err := m.client.Do(req)
if err != nil {
msg[app.URL] = 0
continue
}
msg[app.URL] = res.StatusCode
}
return msg
})
}
// fetchPiHoleStats fetches statistics from the Pi-hole instance
func (m model) fetchPiHoleStats() PiHSummary {
// Find the Pi-hole application configuration
var piHoleApp application
for _, app := range m.applications {
if app.Name == "Pi-hole" {
piHoleApp = app
break
}
}
piholeURL := strings.TrimPrefix(piHoleApp.URL, "http://") // Remove "http://" prefix if present
piholeURL = strings.TrimSuffix(piholeURL, "/admin/") // Remove "/admin" suffix if present
// Set up the Pi-hole connector with the URL and AuthKey from the config
piHoleConnector := PiHConnector{
Host: piholeURL, // Remove "http://" prefix if present
Token: piHoleApp.AuthKey, // Use the AuthKey as the token
}
stats := piHoleConnector.Summary()
return stats
}