This commit is contained in:
Tomas Dvorak
2026-04-29 11:32:39 +02:00
parent 193839bd27
commit 67254f89a9
20 changed files with 1792 additions and 1094 deletions
+64 -17
View File
@@ -426,13 +426,17 @@ func (h *APIHandler) getCalendarEvents(e *core.RequestEvent) error {
}
events := []map[string]interface{}{}
from, to := calendarRange(e)
// Domain expirations
domains, _ := h.app.FindAllRecords("domains",
dbx.NewExp("user = {:user} && expiry_date != ''", dbx.Params{"user": authRecord.Id}),
dbx.NewExp("user = {:user}", dbx.Params{"user": authRecord.Id}),
)
for _, d := range domains {
expiryDate := d.GetDateTime("expiry_date").Time()
if expiryDate.IsZero() || !dateInRange(expiryDate, from, to) {
continue
}
domainName := d.GetString("domain_name")
daysUntil := int(expiryDate.Sub(time.Now()).Hours() / 24)
@@ -446,18 +450,23 @@ func (h *APIHandler) getCalendarEvents(e *core.RequestEvent) error {
}
events = append(events, map[string]interface{}{
"id": "domain-" + d.Id,
"title": "🌐 " + domainName + " expires",
"date": expiryDate.Format("2006-01-02"),
"type": "domain_expiry",
"color": color,
"id": "domain-" + d.Id,
"title": domainName + " expires",
"date": expiryDate.Format("2006-01-02"),
"type": "domain_expiry",
"color": color,
"domain_id": d.Id,
"entity_id": d.Id,
"entity_name": domainName,
"link": "/domain/" + d.Id,
"days_until": daysUntil,
})
}
// SSL expirations
for _, d := range domains {
sslExpiry := d.GetDateTime("ssl_valid_to").Time()
if sslExpiry.IsZero() {
if sslExpiry.IsZero() || !dateInRange(sslExpiry, from, to) {
continue
}
domainName := d.GetString("domain_name")
@@ -473,11 +482,16 @@ func (h *APIHandler) getCalendarEvents(e *core.RequestEvent) error {
}
events = append(events, map[string]interface{}{
"id": "ssl-" + d.Id,
"title": "🔒 " + domainName + " SSL expires",
"date": sslExpiry.Format("2006-01-02"),
"type": "ssl_expiry",
"color": color,
"id": "ssl-" + d.Id,
"title": domainName + " SSL expires",
"date": sslExpiry.Format("2006-01-02"),
"type": "ssl_expiry",
"color": color,
"domain_id": d.Id,
"entity_id": d.Id,
"entity_name": domainName,
"link": "/domain/" + d.Id,
"days_until": daysUntil,
})
}
@@ -487,6 +501,9 @@ func (h *APIHandler) getCalendarEvents(e *core.RequestEvent) error {
)
for _, i := range incidents {
startedAt := i.GetDateTime("started_at").Time()
if startedAt.IsZero() || !dateInRange(startedAt, from, to) {
continue
}
title := i.GetString("title")
severity := i.GetString("severity")
@@ -501,17 +518,47 @@ func (h *APIHandler) getCalendarEvents(e *core.RequestEvent) error {
}
events = append(events, map[string]interface{}{
"id": "incident-" + i.Id,
"title": "⚠️ " + title,
"date": startedAt.Format("2006-01-02"),
"type": "incident",
"color": color,
"id": "incident-" + i.Id,
"title": title,
"date": startedAt.Format("2006-01-02"),
"type": "incident",
"color": color,
"incident_id": i.Id,
"entity_id": i.Id,
"entity_name": title,
"link": "/incidents",
})
}
return e.JSON(http.StatusOK, events)
}
func calendarRange(e *core.RequestEvent) (*time.Time, *time.Time) {
var from, to *time.Time
if value := e.Request.URL.Query().Get("from"); value != "" {
if parsed, err := time.Parse("2006-01-02", value); err == nil {
from = &parsed
}
}
if value := e.Request.URL.Query().Get("to"); value != "" {
if parsed, err := time.Parse("2006-01-02", value); err == nil {
end := parsed.Add(24*time.Hour - time.Nanosecond)
to = &end
}
}
return from, to
}
func dateInRange(value time.Time, from, to *time.Time) bool {
if from != nil && value.Before(*from) {
return false
}
if to != nil && value.After(*to) {
return false
}
return true
}
// addUpdate adds an update record
func (h *APIHandler) addUpdate(incidentID, message, updateType string, oldStatus, newStatus *string, createdBy string) {
collection, err := h.app.FindCollectionByNameOrId("incident_updates")
+60
View File
@@ -0,0 +1,60 @@
package incidents
import (
"net/http/httptest"
"testing"
"time"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/router"
)
func TestDateInRange(t *testing.T) {
from := mustDate(t, "2026-04-01")
to := mustDate(t, "2026-04-30")
tests := []struct {
name string
date string
want bool
}{
{name: "before range", date: "2026-03-31", want: false},
{name: "start boundary", date: "2026-04-01", want: true},
{name: "inside range", date: "2026-04-15", want: true},
{name: "end boundary", date: "2026-04-30", want: true},
{name: "after range", date: "2026-05-01", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := dateInRange(mustDate(t, tt.date), &from, &to)
if got != tt.want {
t.Fatalf("dateInRange(%s) = %v, want %v", tt.date, got, tt.want)
}
})
}
}
func TestCalendarRangeParsesDateBounds(t *testing.T) {
request := httptest.NewRequest("GET", "/api/beszel/incidents/calendar?from=2026-04-01&to=2026-04-30", nil)
from, to := calendarRange(&core.RequestEvent{Event: router.Event{Request: request}})
if from == nil || from.Format("2006-01-02") != "2026-04-01" {
t.Fatalf("unexpected from date: %v", from)
}
if to == nil || !dateInRange(mustDate(t, "2026-04-30").Add(23*time.Hour), from, to) {
t.Fatalf("expected to date to include the whole end day, got %v", to)
}
if dateInRange(mustDate(t, "2026-05-01"), from, to) {
t.Fatal("expected range to exclude day after the to bound")
}
}
func mustDate(t *testing.T, value string) time.Time {
t.Helper()
parsed, err := time.Parse("2006-01-02", value)
if err != nil {
t.Fatal(err)
}
return parsed
}