mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-04 10:42:57 +00:00
dev day #79
This commit is contained in:
@@ -23,6 +23,67 @@ type FilesController struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func (fc *FilesController) GetStorageUsage(c *gin.Context) {
|
||||
var usedBytes int64
|
||||
row := fc.DB.Model(&models.UploadedFile{}).Select("COALESCE(SUM(file_size), 0)").Row()
|
||||
_ = row.Scan(&usedBytes)
|
||||
var count int64
|
||||
fc.DB.Model(&models.UploadedFile{}).Count(&count)
|
||||
var settings models.Settings
|
||||
_ = fc.DB.First(&settings).Error
|
||||
quotaMB := settings.StorageQuotaMB
|
||||
if quotaMB <= 0 {
|
||||
quotaMB = 1024
|
||||
}
|
||||
warnPct := settings.StorageWarnThreshold
|
||||
if warnPct <= 0 {
|
||||
warnPct = 80
|
||||
}
|
||||
criticalPct := settings.StorageCriticalThreshold
|
||||
if criticalPct <= 0 {
|
||||
criticalPct = 95
|
||||
}
|
||||
if warnPct > criticalPct {
|
||||
warnPct = criticalPct - 5
|
||||
if warnPct < 0 {
|
||||
warnPct = 0
|
||||
}
|
||||
}
|
||||
quotaBytes := int64(quotaMB) * 1024 * 1024
|
||||
percent := 0.0
|
||||
if quotaBytes > 0 {
|
||||
percent = float64(usedBytes) * 100.0 / float64(quotaBytes)
|
||||
}
|
||||
status := "ok"
|
||||
if int(percent+0.5) >= criticalPct {
|
||||
status = "critical"
|
||||
} else if int(percent+0.5) >= warnPct {
|
||||
status = "warn"
|
||||
}
|
||||
resp := StorageUsageResponse{
|
||||
UsedBytes: usedBytes,
|
||||
UsedCount: count,
|
||||
QuotaMB: quotaMB,
|
||||
QuotaBytes: quotaBytes,
|
||||
Percent: percent,
|
||||
WarnPercent: warnPct,
|
||||
CriticalPercent: criticalPct,
|
||||
Status: status,
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
type StorageUsageResponse struct {
|
||||
UsedBytes int64 `json:"used_bytes"`
|
||||
UsedCount int64 `json:"used_count"`
|
||||
QuotaMB int `json:"quota_mb"`
|
||||
QuotaBytes int64 `json:"quota_bytes"`
|
||||
Percent float64 `json:"percent"`
|
||||
WarnPercent int `json:"warn_percent"`
|
||||
CriticalPercent int `json:"critical_percent"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// FileInfo represents detailed file information with usage tracking
|
||||
type FileInfo struct {
|
||||
ID uint `json:"id"`
|
||||
|
||||
Reference in New Issue
Block a user