From c3d3b28c640189d91504509223de843284c224d4 Mon Sep 17 00:00:00 2001 From: Jamie Banks Date: Thu, 4 Jun 2026 18:25:13 +0000 Subject: [PATCH] refactor: consolidate backup inventory into sortable structs and collapse index UI sections --- backup/manager.go | 13 +++++-- web/server.go | 31 +++++++++++++---- web/templates/index.html | 75 +++++++++++++++++++++------------------- 3 files changed, 76 insertions(+), 43 deletions(-) diff --git a/backup/manager.go b/backup/manager.go index 5406ce6..bb2d29b 100644 --- a/backup/manager.go +++ b/backup/manager.go @@ -3,6 +3,7 @@ package backup import ( "os" "path/filepath" + "sort" "sync" "time" @@ -73,6 +74,10 @@ func (s *InstanceStatus) Snapshot() InstanceSnapshot { LastBackupResult: db.LastBackupResult, }) } + + sort.Slice(snap.Databases, func(i, j int) bool { + return snap.Databases[i].Name < snap.Databases[j].Name + }) return snap } @@ -128,8 +133,10 @@ func (m *Manager) GetInstances() []*InstanceStatus { defer m.mu.RUnlock() var result []*InstanceStatus - for _, status := range m.instances { - result = append(result, status) + for _, instCfg := range m.cfg.Instances { + if status, exists := m.instances[instCfg.Name]; exists { + result = append(result, status) + } } return result } @@ -251,6 +258,8 @@ func (m *Manager) RunInstance(name string) { inst.mu.Unlock() return } + + sort.Strings(dbs) inst.mu.Lock() for _, db := range dbs { diff --git a/web/server.go b/web/server.go index faa66ba..92d3545 100644 --- a/web/server.go +++ b/web/server.go @@ -9,6 +9,7 @@ import ( "net/http" "os" "path/filepath" + "sort" "strings" "time" @@ -32,7 +33,15 @@ type FileInfo struct { Timestamp time.Time } -type Inventory map[string]map[string][]FileInfo // Instance -> DB -> Files +type DBInventory struct { + Name string + Files []FileInfo +} + +type InstanceInventory struct { + Name string + Databases []DBInventory +} func NewServer(cfg *config.Config, manager *backup.Manager) *Server { s := &Server{ @@ -261,7 +270,7 @@ type TemplateData struct { AnyRunning bool AuthEnabled bool Instances []backup.InstanceSnapshot - Inventory Inventory + Inventory []InstanceInventory } func (s *Server) handleIndex(w http.ResponseWriter, r *http.Request) { @@ -347,14 +356,14 @@ func (s *Server) handleRunInstance(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } -func (s *Server) getInventory() (Inventory, int64) { - inv := make(Inventory) +func (s *Server) getInventory() ([]InstanceInventory, int64) { + var inv []InstanceInventory instances := s.manager.GetInstances() var totalSize int64 for _, inst := range instances { instName := inst.Config.Name - inv[instName] = make(map[string][]FileInfo) + var dbInvs []DBInventory entries, err := os.ReadDir(inst.Config.BackupDir) if err != nil { @@ -390,9 +399,19 @@ func (s *Server) getInventory() (Inventory, int64) { totalSize += info.Size() } if len(fileInfos) > 0 { - inv[instName][dbName] = fileInfos + sort.Slice(fileInfos, func(i, j int) bool { + return fileInfos[i].Timestamp.After(fileInfos[j].Timestamp) + }) + dbInvs = append(dbInvs, DBInventory{Name: dbName, Files: fileInfos}) } } + + if len(dbInvs) > 0 { + sort.Slice(dbInvs, func(i, j int) bool { + return dbInvs[i].Name < dbInvs[j].Name + }) + inv = append(inv, InstanceInventory{Name: instName, Databases: dbInvs}) + } } return inv, totalSize } diff --git a/web/templates/index.html b/web/templates/index.html index 32aa0e6..4922638 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -357,36 +357,39 @@ {{if .Databases}} -
- - - - - - - - - - - - {{range .Databases}} - - - - - - - - {{end}} - -
DatabaseFirst DiscoveredLast Backup TimeSizeResult
{{.Name}}{{formatTime .FirstDiscovered}}{{formatTime .LastBackupTime}}{{if gt .LastBackupSize 0}}{{formatBytes .LastBackupSize}}{{else}}-{{end}} - {{if .LastBackupResult}} - {{.LastBackupResult}} - {{else}} - Pending - {{end}} -
-
+
+ Show Databases ({{len .Databases}}) +
+ + + + + + + + + + + + {{range .Databases}} + + + + + + + + {{end}} + +
DatabaseFirst DiscoveredLast Backup TimeSizeResult
{{.Name}}{{formatTime .FirstDiscovered}}{{formatTime .LastBackupTime}}{{if gt .LastBackupSize 0}}{{formatBytes .LastBackupSize}}{{else}}-{{end}} + {{if .LastBackupResult}} + {{.LastBackupResult}} + {{else}} + Pending + {{end}} +
+
+
{{else}}

No databases discovered yet.

{{end}} @@ -395,16 +398,18 @@

Backup Inventory

- {{range $instName, $dbs := .Inventory}} + {{range .Inventory}}
- {{$instName}} + {{.Name}}
- {{range $dbName, $files := $dbs}} + {{$instName := .Name}} + {{range .Databases}} + {{$dbName := .Name}}
- {{$dbName}} ({{len $files}} files) + {{.Name}} ({{len .Files}} files)
@@ -415,7 +420,7 @@ - {{range $files}} + {{range .Files}}
{{.Name}} {{formatTime .Timestamp}}