monitor.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package widget
  2. import (
  3. "context"
  4. "html/template"
  5. "slices"
  6. "strconv"
  7. "time"
  8. "github.com/glanceapp/glance/internal/assets"
  9. "github.com/glanceapp/glance/internal/feed"
  10. )
  11. func statusCodeToText(status int, altStatusCodes []int) string {
  12. if status == 200 || slices.Contains(altStatusCodes, status) {
  13. return "OK"
  14. }
  15. if status == 404 {
  16. return "Not Found"
  17. }
  18. if status == 403 {
  19. return "Forbidden"
  20. }
  21. if status == 401 {
  22. return "Unauthorized"
  23. }
  24. if status >= 400 {
  25. return "Client Error"
  26. }
  27. if status >= 500 {
  28. return "Server Error"
  29. }
  30. return strconv.Itoa(status)
  31. }
  32. func statusCodeToStyle(status int, altStatusCodes []int) string {
  33. if status == 200 || slices.Contains(altStatusCodes, status) {
  34. return "ok"
  35. }
  36. return "error"
  37. }
  38. type Monitor struct {
  39. widgetBase `yaml:",inline"`
  40. Sites []struct {
  41. *feed.SiteStatusRequest `yaml:",inline"`
  42. Status *feed.SiteStatus `yaml:"-"`
  43. Title string `yaml:"title"`
  44. IconUrl string `yaml:"icon"`
  45. IsSimpleIcon bool `yaml:"-"`
  46. SameTab bool `yaml:"same-tab"`
  47. StatusText string `yaml:"-"`
  48. StatusStyle string `yaml:"-"`
  49. AltStatusCodes []int `yaml:"alt-status-codes"`
  50. } `yaml:"sites"`
  51. ShowFailingOnly bool `yaml:"show-failing-only"`
  52. HasFailing bool `yaml:"-"`
  53. }
  54. func (widget *Monitor) Initialize() error {
  55. widget.withTitle("Monitor").withCacheDuration(5 * time.Minute)
  56. for i := range widget.Sites {
  57. widget.Sites[i].IconUrl, widget.Sites[i].IsSimpleIcon = toSimpleIconIfPrefixed(widget.Sites[i].IconUrl)
  58. }
  59. return nil
  60. }
  61. func (widget *Monitor) Update(ctx context.Context) {
  62. requests := make([]*feed.SiteStatusRequest, len(widget.Sites))
  63. for i := range widget.Sites {
  64. requests[i] = widget.Sites[i].SiteStatusRequest
  65. }
  66. statuses, err := feed.FetchStatusForSites(requests)
  67. if !widget.canContinueUpdateAfterHandlingErr(err) {
  68. return
  69. }
  70. widget.HasFailing = false
  71. for i := range widget.Sites {
  72. site := &widget.Sites[i]
  73. status := &statuses[i]
  74. site.Status = status
  75. if !slices.Contains(site.AltStatusCodes, status.Code) && (status.Code >= 400 || status.TimedOut || status.Error != nil) {
  76. widget.HasFailing = true
  77. }
  78. if !status.TimedOut {
  79. site.StatusText = statusCodeToText(status.Code, site.AltStatusCodes)
  80. site.StatusStyle = statusCodeToStyle(status.Code, site.AltStatusCodes)
  81. }
  82. }
  83. }
  84. func (widget *Monitor) Render() template.HTML {
  85. return widget.render(widget, assets.MonitorTemplate)
  86. }