Skip to content

Commit

Permalink
create own function for creating list of heartbeats for template
Browse files Browse the repository at this point in the history
  • Loading branch information
gi8lino committed Jun 19, 2024
1 parent 60a8c25 commit 5893711
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 23 deletions.
53 changes: 30 additions & 23 deletions pkg/handlers/heartbeats.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,12 @@ func Heartbeats(logger logger.Logger, staticFS fs.FS, version, siteRoot string,
return
}

var heartbeatDataList []*HeartbeatData
for _, h := range heartbeatStore.GetAll() {
var notifications []NotificationState
for _, notificationName := range h.Notifications {
n := notificationStore.Get(notificationName)
if n != nil {
notifications = append(notifications, NotificationState{
Name: n.Name,
Enabled: *n.Enabled,
Type: n.Type,
})
}
}
heartbeatDataList = append(heartbeatDataList, &HeartbeatData{
Name: h.Name,
Status: h.Status,
Interval: h.Interval,
Grace: h.Grace,
LastPing: h.LastPing,
Notifications: notifications,
})
}
heartbeatList := createHeartbeatList(heartbeatStore, notificationStore)

data := HeartbeatPageData{
Version: version,
SiteRoot: siteRoot,
Heartbeats: heartbeatDataList,
Heartbeats: heartbeatList,
}

if err := tmpl.ExecuteTemplate(w, "heartbeat", data); err != nil {
Expand All @@ -94,3 +73,31 @@ func Heartbeats(logger logger.Logger, staticFS fs.FS, version, siteRoot string,
}
}
}

// createHeartbeatList formats the heartbeats from the heartbeatStore for the template
func createHeartbeatList(heartbeatStore *heartbeat.Store, notificationStore *notify.Store) []*HeartbeatData {
var heartbeatDataList []*HeartbeatData
for _, h := range heartbeatStore.GetAll() {
var notifications []NotificationState
for _, notificationName := range h.Notifications {
n := notificationStore.Get(notificationName)
if n != nil {
notifications = append(notifications, NotificationState{
Name: n.Name,
Enabled: *n.Enabled,
Type: n.Type,
})
}
}
heartbeatDataList = append(heartbeatDataList, &HeartbeatData{
Name: h.Name,
Status: h.Status,
Interval: h.Interval,
Grace: h.Grace,
LastPing: h.LastPing,
Notifications: notifications,
})
}

return heartbeatDataList
}
42 changes: 42 additions & 0 deletions pkg/handlers/heartbeats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,45 @@ func TestHeartbeatsHandler(t *testing.T) {
assert.Contains(t, rec.Body.String(), "Internal Server Error", "Expected internal server error message")
})
}

func TestCreateHeartbeatList(t *testing.T) {
heartbeatStore := heartbeat.NewStore()
notificationStore := notify.NewStore()

interval := time.Minute
grace := time.Minute

h := &heartbeat.Heartbeat{
Name: "test",
Enabled: new(bool),
Interval: &timer.Timer{Interval: &interval},
Grace: &timer.Timer{Interval: &grace},
LastPing: time.Now(),
Notifications: []string{"test"},
}
*h.Enabled = true

err := heartbeatStore.Add("test", h)
assert.NoError(t, err)

ns := &notify.Notification{
Name: "test",
Type: "email",
Enabled: new(bool),
MailConfig: &notifier.MailConfig{},
}
*ns.Enabled = true

err = notificationStore.Add("test", ns)
assert.NoError(t, err)

t.Run("CreateHeartbeatList", func(t *testing.T) {
heartbeatList := createHeartbeatList(heartbeatStore, notificationStore)
assert.Len(t, heartbeatList, 1, "Expected one heartbeat in list")
assert.Equal(t, "test", heartbeatList[0].Name, "Expected heartbeat name to be 'test'")
assert.Equal(t, "email", heartbeatList[0].Notifications[0].Type, "Expected notification type to be 'email'")
assert.Equal(t, true, heartbeatList[0].Notifications[0].Enabled, "Expected notification to be enabled")
assert.Equal(t, interval, *heartbeatList[0].Interval.Interval, "Expected interval duration to match")
assert.Equal(t, grace, *heartbeatList[0].Grace.Interval, "Expected grace duration to match")
})
}

0 comments on commit 5893711

Please sign in to comment.