Skip to content

Commit

Permalink
Uptime: filter by date, address, port, notification
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed Jul 19, 2023
1 parent f269b9c commit 2f2e1b4
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 44 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
# Change Log
All notable changes to this project will be documented in this file.

## [1.0.5] - 2023-07-
### Added
- Uptime: split date and time
- Uptime: filter by date, address, port, notification

### Changed
- Connection timeout to 3 seconds

## [1.0.4] - 2023-07-14
### Added
- Uptime: color Date
Expand Down
14 changes: 14 additions & 0 deletions internal/check/in-slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package check

// InSlice - check if element is in slice
func InSlice(slice []string, elem string) bool {

for _, str := range slice {

if str == elem {
return true
}
}

return false
}
3 changes: 1 addition & 2 deletions internal/check/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ func State(host models.Host) bool {
host.Port = "80"
}

timeout := 1 * time.Second
timeout := 3 * time.Second
target := host.Addr + ":" + host.Port

conn, err := net.DialTimeout("tcp", target, timeout)
if err != nil {
// log.Println("ERROR:", err)
return false
}
conn.Close()
Expand Down
24 changes: 16 additions & 8 deletions internal/web/templates/uptime.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,32 @@
{{ range .UptimeMon }}
<tr>
<td>
<a href="/uptime/?filter=yes&panel={{ .Panel }}">{{ .Panel }}</a>
<a href="/uptime_filter/?panel={{ .Panel }}">{{ .Panel }}</a>
</td>
<td>
<a href="/uptime/?filter=yes&panel={{ .Panel }}&host={{ .Host }}">{{ .Host }}</a>
<a href="/uptime_filter/?panel={{ .Panel }}&host={{ .Host }}">{{ .Host }}</a>
</td>
<td>
<a href="/uptime_filter/?addr={{ .Addr }}">{{ .Addr }}</a>
</td>
<td>
<a href="/uptime_filter/?port={{ .Port }}">{{ .Port }}</a>
</td>
<td>
<a style="color: #{{ .Color }};" href="/uptime_filter/?date={{ .Date }}">{{ .Date }}</a>
</td>
<td>{{ .Addr }}</td>
<td>{{ .Port }}</td>
<td style="color: #{{ .Color }};">{{ .Date }}</td>
<td>{{ .Time }}</td>
<td>
{{ if .State }}
<a href="/uptime/?filter=yes&state=on"><i class="bi bi-circle-fill my-col-on"></i></a>
<a href="/uptime_filter/?state=on"><i class="bi bi-circle-fill my-col-on"></i></a>
{{ else }}
<a href="/uptime/?filter=yes&state=off"><i class="bi bi-circle-fill my-col-off"></i></a>
<a href="/uptime_filter/?state=off"><i class="bi bi-circle-fill my-col-off"></i></a>
{{ end }}
</td>
<td>
{{ range .Notify }}{{ . }} {{ end }}
{{ range .Notify }}
<a href="/uptime_filter/?notify={{ . }}">{{ . }}</a>
{{ end }}
</td>
</tr>
{{ end }}
Expand Down
57 changes: 44 additions & 13 deletions internal/web/uptime-filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,59 @@ package web

import (
// "log"
"net/http"
"sort"

"github.com/aceberg/miniboard/internal/check"
"github.com/aceberg/miniboard/internal/models"
)

func filterUptimeMon(panel, host, state string) []models.MonData {
func uptimeFilterHandler(w http.ResponseWriter, r *http.Request) {
var resultUptimeMon []models.MonData
var guiData models.GuiData
guiData.Config = AppConfig
guiData.CurrentTab = "Uptime Monitor"
guiData.Links = AllLinks

if panel != "" {
for _, mon := range UptimeMon {
if panel == mon.Panel && (host == "" || (host != "" && host == mon.Host)) {
resultUptimeMon = append(resultUptimeMon, mon)
}
panel := r.FormValue("panel")
host := r.FormValue("host")
state := r.FormValue("state")
date := r.FormValue("date")
addr := r.FormValue("addr")
port := r.FormValue("port")
notify := r.FormValue("notify")

for _, mon := range UptimeMon {

if panel == mon.Panel && (host == "" || (host != "" && host == mon.Host)) {
resultUptimeMon = append(resultUptimeMon, mon)
}

if (mon.State && state == "on") || (!mon.State && state == "off") {
resultUptimeMon = append(resultUptimeMon, mon)
}

if mon.Date == date || mon.Addr == addr || mon.Port == port {
resultUptimeMon = append(resultUptimeMon, mon)
}
}

if state != "" {
for _, mon := range UptimeMon {
if (mon.State && state == "on") || (!mon.State && state == "off") {
resultUptimeMon = append(resultUptimeMon, mon)
}
if check.InSlice(mon.Notify, notify) {
resultUptimeMon = append(resultUptimeMon, mon)
}
}

return resultUptimeMon
guiData.UptimeMon = resultUptimeMon

sort.Slice(guiData.UptimeMon, func(i, j int) bool {
return guiData.UptimeMon[i].Time > guiData.UptimeMon[j].Time
})

if AllLinks.Uptime.Show < 1 {
AllLinks.Uptime.Show = 20
}
if len(guiData.UptimeMon) > AllLinks.Uptime.Show {
guiData.UptimeMon = guiData.UptimeMon[0:AllLinks.Uptime.Show]
}

execTemplate(w, "uptime", guiData)
}
13 changes: 2 additions & 11 deletions internal/web/uptime.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,10 @@ func uptimeHandler(w http.ResponseWriter, r *http.Request) {
guiData.CurrentTab = "Uptime Monitor"
guiData.Links = AllLinks

filter := r.FormValue("filter")
panel := r.FormValue("panel")
host := r.FormValue("host")
state := r.FormValue("state")

if filter == "yes" {
guiData.UptimeMon = filterUptimeMon(panel, host, state) // uptime-filter.go
} else {
guiData.UptimeMon = UptimeMon
}
guiData.UptimeMon = UptimeMon

sort.Slice(guiData.UptimeMon, func(i, j int) bool {
return guiData.UptimeMon[i].Date > guiData.UptimeMon[j].Date
return guiData.UptimeMon[i].Time > guiData.UptimeMon[j].Time
})

if AllLinks.Uptime.Show < 1 {
Expand Down
21 changes: 11 additions & 10 deletions internal/web/webgui.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ func Gui(confPath, yamlPath, nodePath string) {
log.Printf("Web GUI at http://%s", address)
log.Println("=================================== ")

http.HandleFunc("/", indexHandler) // index.go
http.HandleFunc("/config/", configHandler) // config.go
http.HandleFunc("/config_save/", saveConfigHandler) // config.go
http.HandleFunc("/host/", hostHandler) // host.go
http.HandleFunc("/panels/", panelsHandler) // panels.go
http.HandleFunc("/panel_edit/", panelEditHandler) // panel-edit.go
http.HandleFunc("/tabs/", tabsHandler) // tabs.go
http.HandleFunc("/tab_edit/", tabEditHandler) // tab-edit.go
http.HandleFunc("/uptime/", uptimeHandler) // uptime.go
http.HandleFunc("/uptime_edit/", uptimeEditHandler) // uptime-edit.go
http.HandleFunc("/", indexHandler) // index.go
http.HandleFunc("/config/", configHandler) // config.go
http.HandleFunc("/config_save/", saveConfigHandler) // config.go
http.HandleFunc("/host/", hostHandler) // host.go
http.HandleFunc("/panels/", panelsHandler) // panels.go
http.HandleFunc("/panel_edit/", panelEditHandler) // panel-edit.go
http.HandleFunc("/tabs/", tabsHandler) // tabs.go
http.HandleFunc("/tab_edit/", tabEditHandler) // tab-edit.go
http.HandleFunc("/uptime/", uptimeHandler) // uptime.go
http.HandleFunc("/uptime_edit/", uptimeEditHandler) // uptime-edit.go
http.HandleFunc("/uptime_filter/", uptimeFilterHandler) // uptime-filter.go
err := http.ListenAndServe(address, nil)
check.IfError(err)
}

0 comments on commit 2f2e1b4

Please sign in to comment.