diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-04-21 18:46:40 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-04-21 18:46:40 -0700 |
commit | d14605d1388aaa7cc9ef1c230eae5ba14c9cef44 (patch) | |
tree | 59fcda0fae7899ca577eed1f72d89bff17d5ad5d /api/backups | |
download | backup-notify-d14605d1388aaa7cc9ef1c230eae5ba14c9cef44.tar.gz backup-notify-d14605d1388aaa7cc9ef1c230eae5ba14c9cef44.zip |
initial commit
Diffstat (limited to 'api/backups')
-rw-r--r-- | api/backups/backups.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/api/backups/backups.go b/api/backups/backups.go new file mode 100644 index 0000000..d389582 --- /dev/null +++ b/api/backups/backups.go @@ -0,0 +1,58 @@ +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) + } +} |