Add storage analytics page
This commit is contained in:
@@ -4,9 +4,11 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/shirou/gopsutil/v3/disk"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
@@ -209,3 +211,66 @@ func (s *Service) buildPath(id, filename string) string {
|
||||
func (s *Service) GetAllFiles() ([]FileRecord, error) {
|
||||
return s.repo.GetAll()
|
||||
}
|
||||
|
||||
func (s *Service) GetStorageStats() (*StorageStats, error) {
|
||||
files, err := s.repo.GetAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stats := &StorageStats{}
|
||||
|
||||
var total int64
|
||||
var active, deleted int
|
||||
|
||||
for _, f := range files {
|
||||
stats.TotalFiles++
|
||||
|
||||
if f.Deleted {
|
||||
deleted++
|
||||
} else {
|
||||
active++
|
||||
}
|
||||
|
||||
total += f.Size
|
||||
}
|
||||
|
||||
stats.TotalBytes = total
|
||||
stats.ActiveFiles = active
|
||||
stats.DeletedFiles = deleted
|
||||
|
||||
if stats.TotalFiles > 0 {
|
||||
stats.AverageFileSize = total / int64(stats.TotalFiles)
|
||||
}
|
||||
|
||||
// Biggest files
|
||||
sort.Slice(files, func(i, j int) bool {
|
||||
return files[i].Size > files[j].Size
|
||||
})
|
||||
|
||||
if len(files) > 10 {
|
||||
stats.LargestFiles = files[:10]
|
||||
} else {
|
||||
stats.LargestFiles = files
|
||||
}
|
||||
|
||||
usage, err := disk.Usage(s.storageDir)
|
||||
if err == nil {
|
||||
stats.DiskTotalBytes = int64(usage.Total)
|
||||
stats.DiskFreeBytes = int64(usage.Free)
|
||||
stats.DiskUsedBytes = int64(usage.Used)
|
||||
}
|
||||
|
||||
// tmp chunk usage
|
||||
var tmpTotal int64
|
||||
filepath.Walk("tmp", func(path string, info os.FileInfo, err error) error {
|
||||
if err == nil && !info.IsDir() {
|
||||
tmpTotal += info.Size()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
stats.TempBytes = tmpTotal
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
18
internal/file/stats.go
Normal file
18
internal/file/stats.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package file
|
||||
|
||||
type StorageStats struct {
|
||||
TotalFiles int
|
||||
ActiveFiles int
|
||||
DeletedFiles int
|
||||
|
||||
TotalBytes int64
|
||||
AverageFileSize int64
|
||||
|
||||
DiskTotalBytes int64
|
||||
DiskFreeBytes int64
|
||||
DiskUsedBytes int64
|
||||
|
||||
TempBytes int64
|
||||
|
||||
LargestFiles []FileRecord
|
||||
}
|
||||
Reference in New Issue
Block a user