summaryrefslogtreecommitdiff
path: root/api/backups/backups.go
diff options
context:
space:
mode:
Diffstat (limited to 'api/backups/backups.go')
-rw-r--r--api/backups/backups.go58
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)
+ }
+}