zdravko/internal/handlers/index.go

183 lines
4 KiB
Go
Raw Normal View History

package handlers
import (
2024-02-22 16:29:17 +00:00
"context"
"net/http"
2024-02-22 16:29:17 +00:00
"time"
2024-02-22 16:29:17 +00:00
"code.tjo.space/mentos1386/zdravko/internal/models"
"code.tjo.space/mentos1386/zdravko/internal/services"
"code.tjo.space/mentos1386/zdravko/web/templates/components"
"github.com/labstack/echo/v4"
)
type IndexData struct {
*components.Base
2024-02-22 16:29:17 +00:00
HealthChecks []*HealthCheck
MonitorsLength int
2024-02-22 16:29:17 +00:00
TimeRange string
Status string
}
type HealthCheck struct {
2024-02-22 16:29:17 +00:00
Name string
Status string
HistoryDaily *History
HistoryHourly *History
}
2024-02-22 16:29:17 +00:00
type History struct {
History []string
Uptime int
}
func getDay(date time.Time) string {
return date.Format("2006-01-02")
}
func getHour(date time.Time) string {
return date.Format("2006-01-02T15:04")
}
func getDailyHistory(history []models.MonitorHistory) *History {
2024-02-22 16:29:17 +00:00
numDays := 90
historyDailyMap := map[string]string{}
numOfSuccess := 0
2024-02-22 19:20:34 +00:00
numTotal := 0
2024-02-22 16:29:17 +00:00
for i := 0; i < numDays; i++ {
day := getDay(time.Now().AddDate(0, 0, -i).Truncate(time.Hour * 24))
historyDailyMap[day] = models.MonitorUnknown
}
2024-02-22 16:29:17 +00:00
for _, _history := range history {
day := getDay(_history.CreatedAt.Truncate(time.Hour * 24))
// skip if day is not in the last 90 days
if _, ok := historyDailyMap[day]; !ok {
continue
}
numTotal++
if _history.Status == models.MonitorSuccess {
2024-02-22 16:29:17 +00:00
numOfSuccess++
}
// skip if day is already set to failure
if historyDailyMap[day] == models.MonitorFailure {
2024-02-22 16:29:17 +00:00
continue
}
historyDailyMap[day] = _history.Status
}
historyDaily := make([]string, numDays)
for i := 0; i < numDays; i++ {
day := getDay(time.Now().AddDate(0, 0, -numDays+i+1).Truncate(time.Hour * 24))
historyDaily[i] = historyDailyMap[day]
}
2024-02-22 19:20:34 +00:00
uptime := 0
if numTotal > 0 {
uptime = 100 * numOfSuccess / numTotal
}
2024-02-22 16:29:17 +00:00
return &History{
History: historyDaily,
2024-02-22 19:20:34 +00:00
Uptime: uptime,
2024-02-22 16:29:17 +00:00
}
}
func getHourlyHistory(history []models.MonitorHistory) *History {
2024-02-22 16:29:17 +00:00
numHours := 48
historyHourlyMap := map[string]string{}
numOfSuccess := 0
2024-02-22 19:20:34 +00:00
numTotal := 0
2024-02-22 16:29:17 +00:00
for i := 0; i < numHours; i++ {
hour := getHour(time.Now().Add(time.Hour * time.Duration(-i)).Truncate(time.Hour))
historyHourlyMap[hour] = models.MonitorUnknown
2024-02-22 16:29:17 +00:00
}
for _, _history := range history {
hour := getHour(_history.CreatedAt.Truncate(time.Hour))
// skip if day is not in the last 90 days
if _, ok := historyHourlyMap[hour]; !ok {
continue
}
numTotal++
if _history.Status == models.MonitorSuccess {
2024-02-22 16:29:17 +00:00
numOfSuccess++
}
// skip if day is already set to failure
if historyHourlyMap[hour] == models.MonitorFailure {
2024-02-22 16:29:17 +00:00
continue
}
historyHourlyMap[hour] = _history.Status
}
historyHourly := make([]string, numHours)
for i := 0; i < numHours; i++ {
hour := getHour(time.Now().Add(time.Hour * time.Duration(-numHours+i+1)).Truncate(time.Hour))
historyHourly[i] = historyHourlyMap[hour]
}
2024-02-22 19:20:34 +00:00
uptime := 0
if numTotal > 0 {
uptime = 100 * numOfSuccess / numTotal
}
2024-02-22 16:29:17 +00:00
return &History{
History: historyHourly,
2024-02-22 19:20:34 +00:00
Uptime: uptime,
}
}
func (h *BaseHandler) Index(c echo.Context) error {
2024-02-22 16:29:17 +00:00
ctx := context.Background()
monitors, err := services.GetMonitors(ctx, h.query)
2024-02-22 16:29:17 +00:00
if err != nil {
return err
}
timeRange := c.QueryParam("time-range")
if timeRange != "48hours" && timeRange != "90days" {
timeRange = "90days"
}
overallStatus := "SUCCESS"
monitorsWithHistory := make([]*HealthCheck, len(monitors))
for i, monitor := range monitors {
historyDaily := getDailyHistory(monitor.History)
historyHourly := getHourlyHistory(monitor.History)
2024-02-22 16:29:17 +00:00
status := historyDaily.History[89]
if status != models.MonitorSuccess {
2024-02-22 16:29:17 +00:00
overallStatus = status
}
monitorsWithHistory[i] = &HealthCheck{
Name: monitor.Name,
2024-02-22 16:29:17 +00:00
Status: status,
HistoryDaily: historyDaily,
HistoryHourly: historyHourly,
}
}
return c.Render(http.StatusOK, "index.tmpl", &IndexData{
Base: &components.Base{
NavbarActive: GetPageByTitle(Pages, "Status"),
Navbar: Pages,
},
HealthChecks: monitorsWithHistory,
MonitorsLength: len(monitors),
2024-02-22 16:29:17 +00:00
TimeRange: timeRange,
Status: overallStatus,
})
}