backup-notify/api/backups/backups.go

59 lines
1.7 KiB
Go
Raw Normal View History

2024-04-21 21:46:40 -04:00
package backups
import (
"log"
"net/http"
"time"
"git.simponic.xyz/simponic/backup-notify/api/types"
"git.simponic.xyz/simponic/backup-notify/database"
)
func getHostStatusOverTime(backups []database.Backup) map[string][]bool {
hostnameBackups := make(map[string][]database.Backup)
statusOverTime := make(map[string][]bool)
if len(backups) == 0 {
return statusOverTime
}
firstReceivedBackup := backups[0].ReceivedOn
for _, backup := range backups {
if backup.ReceivedOn.Before(firstReceivedBackup) {
firstReceivedBackup = backup.ReceivedOn
}
if _, ok := hostnameBackups[backup.Hostname]; !ok {
hostnameBackups[backup.Hostname] = []database.Backup{}
}
hostnameBackups[backup.Hostname] = append(hostnameBackups[backup.Hostname], backup)
}
daysSinceFirstBackup := int(time.Since(firstReceivedBackup).Hours()/24) + 1
for hostname := range hostnameBackups {
statusOverTime[hostname] = make([]bool, daysSinceFirstBackup)
for _, backup := range hostnameBackups[hostname] {
dayReceivedOn := int(time.Since(backup.ReceivedOn).Hours() / 24)
statusOverTime[hostname][dayReceivedOn] = true
}
}
return statusOverTime
}
func ListBackupsContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
backups, err := database.ListBackups(context.DBConn)
if err != nil {
log.Println(err)
resp.WriteHeader(http.StatusInternalServerError)
return failure(context, req, resp)
}
hostStatusOverTime := getHostStatusOverTime(backups)
(*context.TemplateData)["HostStatusOverTime"] = hostStatusOverTime
return success(context, req, resp)
}
}