glance/internal/widget/monitor.go

104 lines
2.2 KiB
Go
Raw Normal View History

2024-04-27 21:10:24 +02:00
package widget
import (
"context"
"html/template"
"strconv"
"time"
"github.com/glanceapp/glance/internal/assets"
"github.com/glanceapp/glance/internal/feed"
)
func statusCodeToText(status int) string {
if status == 200 {
return "OK"
}
if status == 404 {
return "Not Found"
}
if status == 403 {
return "Forbidden"
}
if status == 401 {
return "Unauthorized"
}
if status >= 400 {
return "Client Error"
}
if status >= 500 {
return "Server Error"
}
return strconv.Itoa(status)
}
func statusCodeToStyle(status int) string {
if status == 200 {
return "ok"
2024-04-27 21:10:24 +02:00
}
return "error"
2024-04-27 21:10:24 +02:00
}
type Monitor struct {
widgetBase `yaml:",inline"`
Sites []struct {
*feed.SiteStatusRequest `yaml:",inline"`
Status *feed.SiteStatus `yaml:"-"`
Title string `yaml:"title"`
IconUrl string `yaml:"icon"`
IsSimpleIcon bool `yaml:"-"`
SameTab bool `yaml:"same-tab"`
StatusText string `yaml:"-"`
StatusStyle string `yaml:"-"`
2024-04-27 21:10:24 +02:00
} `yaml:"sites"`
ShowFailingOnly bool `yaml:"show-failing-only"`
HasFailing bool `yaml:"-"`
2024-04-27 21:10:24 +02:00
}
func (widget *Monitor) Initialize() error {
widget.withTitle("Monitor").withCacheDuration(5 * time.Minute)
for i := range widget.Sites {
widget.Sites[i].IconUrl, widget.Sites[i].IsSimpleIcon = toSimpleIconIfPrefixed(widget.Sites[i].IconUrl)
}
2024-04-27 21:10:24 +02:00
return nil
}
func (widget *Monitor) Update(ctx context.Context) {
requests := make([]*feed.SiteStatusRequest, len(widget.Sites))
2024-04-27 21:10:24 +02:00
for i := range widget.Sites {
requests[i] = widget.Sites[i].SiteStatusRequest
2024-04-27 21:10:24 +02:00
}
statuses, err := feed.FetchStatusForSites(requests)
2024-04-27 21:10:24 +02:00
if !widget.canContinueUpdateAfterHandlingErr(err) {
return
}
widget.HasFailing = false
2024-04-27 21:10:24 +02:00
for i := range widget.Sites {
site := &widget.Sites[i]
status := &statuses[i]
site.Status = status
if status.Code >= 400 || status.TimedOut || status.Error != nil {
widget.HasFailing = true
}
2024-04-27 21:10:24 +02:00
if !status.TimedOut {
site.StatusText = statusCodeToText(status.Code)
site.StatusStyle = statusCodeToStyle(status.Code)
}
}
}
func (widget *Monitor) Render() template.HTML {
return widget.render(widget, assets.MonitorTemplate)
}