2024-02-11 19:28:00 +00:00
|
|
|
package handlers
|
2024-02-10 11:59:58 +00:00
|
|
|
|
|
|
|
import (
|
2024-02-22 16:29:17 +00:00
|
|
|
"context"
|
2024-02-10 11:59:58 +00:00
|
|
|
"net/http"
|
2024-02-22 16:29:17 +00:00
|
|
|
"time"
|
2024-02-10 11:59:58 +00:00
|
|
|
|
2024-02-22 16:29:17 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/models"
|
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/services"
|
2024-02-12 10:22:14 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/web/templates/components"
|
2024-02-20 10:24:04 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-02-10 11:59:58 +00:00
|
|
|
)
|
|
|
|
|
2024-02-12 13:20:38 +00:00
|
|
|
type IndexData struct {
|
|
|
|
*components.Base
|
2024-02-22 16:29:17 +00:00
|
|
|
HealthChecks []*HealthCheck
|
|
|
|
HealthchecksLength int
|
|
|
|
TimeRange string
|
|
|
|
Status string
|
2024-02-12 13:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HealthCheck struct {
|
2024-02-22 16:29:17 +00:00
|
|
|
Name string
|
|
|
|
Status string
|
|
|
|
HistoryDaily *History
|
|
|
|
HistoryHourly *History
|
2024-02-12 13:20:38 +00:00
|
|
|
}
|
|
|
|
|
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.HealthcheckHistory) *History {
|
|
|
|
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.HealthcheckUnknown
|
2024-02-12 13:20:38 +00:00
|
|
|
}
|
|
|
|
|
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.HealthcheckSuccess {
|
|
|
|
numOfSuccess++
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip if day is already set to failure
|
|
|
|
if historyDailyMap[day] == models.HealthcheckFailure {
|
|
|
|
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-12 13:20:38 +00:00
|
|
|
}
|
|
|
|
|
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.HealthcheckHistory) *History {
|
|
|
|
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.HealthcheckUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
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.HealthcheckSuccess {
|
|
|
|
numOfSuccess++
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip if day is already set to failure
|
|
|
|
if historyHourlyMap[hour] == models.HealthcheckFailure {
|
|
|
|
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,
|
2024-02-12 13:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
func (h *BaseHandler) Index(c echo.Context) error {
|
2024-02-22 16:29:17 +00:00
|
|
|
ctx := context.Background()
|
2024-02-22 19:20:34 +00:00
|
|
|
healthchecks, err := services.GetHealthchecks(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"
|
|
|
|
|
|
|
|
healthchecksWithHistory := make([]*HealthCheck, len(healthchecks))
|
|
|
|
for i, healthcheck := range healthchecks {
|
|
|
|
historyDaily := getDailyHistory(healthcheck.History)
|
|
|
|
historyHourly := getHourlyHistory(healthcheck.History)
|
|
|
|
|
|
|
|
status := historyDaily.History[89]
|
|
|
|
if status != models.HealthcheckSuccess {
|
|
|
|
overallStatus = status
|
|
|
|
}
|
|
|
|
|
|
|
|
healthchecksWithHistory[i] = &HealthCheck{
|
|
|
|
Name: healthcheck.Name,
|
|
|
|
Status: status,
|
|
|
|
HistoryDaily: historyDaily,
|
|
|
|
HistoryHourly: historyHourly,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
return c.Render(http.StatusOK, "index.tmpl", &IndexData{
|
2024-02-12 13:20:38 +00:00
|
|
|
Base: &components.Base{
|
2024-02-15 22:47:56 +00:00
|
|
|
NavbarActive: GetPageByTitle(Pages, "Status"),
|
|
|
|
Navbar: Pages,
|
2024-02-12 13:20:38 +00:00
|
|
|
},
|
2024-02-22 16:29:17 +00:00
|
|
|
HealthChecks: healthchecksWithHistory,
|
|
|
|
HealthchecksLength: len(healthchecks),
|
|
|
|
TimeRange: timeRange,
|
|
|
|
Status: overallStatus,
|
2024-02-12 10:22:14 +00:00
|
|
|
})
|
2024-02-10 11:59:58 +00:00
|
|
|
}
|