Add backup diagnostics snapshots tasks and homelab map
This commit is contained in:
35
backups.go
35
backups.go
@@ -12,12 +12,15 @@ import (
|
||||
)
|
||||
|
||||
type BackupItem struct {
|
||||
VMID int `json:"vmid"`
|
||||
Storage string `json:"storage"`
|
||||
VolumeID string `json:"volid"`
|
||||
Format string `json:"format"`
|
||||
SizeBytes uint64 `json:"size"`
|
||||
CreatedAt int64 `json:"ctime"`
|
||||
VMID int `json:"vmid"`
|
||||
Storage string `json:"storage"`
|
||||
VolumeID string `json:"volid"`
|
||||
Format string `json:"format"`
|
||||
SizeBytes uint64 `json:"size"`
|
||||
CreatedAt int64 `json:"ctime"`
|
||||
PreviousSizeBytes uint64 `json:"previousSize,omitempty"`
|
||||
SizeChangePercent *float64 `json:"sizeChangePercent,omitempty"`
|
||||
PreviousCreatedAt int64 `json:"previousCtime,omitempty"`
|
||||
}
|
||||
|
||||
type BackupMetrics struct {
|
||||
@@ -89,17 +92,25 @@ func readBackups() BackupMetrics {
|
||||
}
|
||||
|
||||
func latestBackups(items []BackupItem) []BackupItem {
|
||||
latest := make(map[int]BackupItem)
|
||||
byGuest := make(map[int][]BackupItem)
|
||||
for _, item := range items {
|
||||
if item.VMID == 0 {
|
||||
continue
|
||||
}
|
||||
if previous, ok := latest[item.VMID]; !ok || item.CreatedAt > previous.CreatedAt {
|
||||
latest[item.VMID] = item
|
||||
}
|
||||
byGuest[item.VMID] = append(byGuest[item.VMID], item)
|
||||
}
|
||||
result := make([]BackupItem, 0, len(latest))
|
||||
for _, item := range latest {
|
||||
result := make([]BackupItem, 0, len(byGuest))
|
||||
for _, history := range byGuest {
|
||||
sort.Slice(history, func(i, j int) bool { return history[i].CreatedAt > history[j].CreatedAt })
|
||||
item := history[0]
|
||||
if len(history) > 1 {
|
||||
item.PreviousSizeBytes = history[1].SizeBytes
|
||||
item.PreviousCreatedAt = history[1].CreatedAt
|
||||
if history[1].SizeBytes > 0 {
|
||||
change := (float64(item.SizeBytes) - float64(history[1].SizeBytes)) / float64(history[1].SizeBytes) * 100
|
||||
item.SizeChangePercent = &change
|
||||
}
|
||||
}
|
||||
result = append(result, item)
|
||||
}
|
||||
sort.Slice(result, func(i, j int) bool { return result[i].VMID < result[j].VMID })
|
||||
|
||||
Reference in New Issue
Block a user