feat: image variants cache panel
This commit is contained in:
1 parent
951fef18ba
commit
d768668014
53 files changed
+5809
-46
No files matched your search
@@ -15,6 +15,7 @@ type VariantFileRepository struct {
|
||||
}
|
||||
|
||||
var _ domain.VariantFileRepository = &VariantFileRepository{}
|
||||
var _ domain.VariantCacheRepository = &VariantFileRepository{}
|
||||
|
||||
func NewVariantFileRepository(db *gorm.DB) *VariantFileRepository {
|
||||
return &VariantFileRepository{db}
|
||||
@@ -90,6 +91,76 @@ func (v *VariantFileRepository) TotalsBySource() (map[string]domain.VariantFileT
|
||||
return totals, nil
|
||||
}
|
||||
|
||||
const (
|
||||
unusedCondition = "variant_name IN ? AND COALESCE(last_requested_at, created_at) < ?"
|
||||
topSourcesOrder = "bytes DESC, source_name ASC"
|
||||
)
|
||||
|
||||
func (v *VariantFileRepository) UnusedTotals(filter domain.UnusedFilter) (domain.VariantFileTotals, error) {
|
||||
if len(filter.VariantNames) == 0 {
|
||||
return domain.VariantFileTotals{}, nil
|
||||
}
|
||||
var row struct {
|
||||
Files int
|
||||
Bytes int64
|
||||
}
|
||||
err := v.db.Model(&entity.ImageVariantFile{}).
|
||||
Select("COUNT(*) AS files, COALESCE(SUM(size), 0) AS bytes").
|
||||
Where(unusedCondition, filter.VariantNames, filter.Cutoff.UTC()).
|
||||
Scan(&row).Error
|
||||
if err != nil {
|
||||
return domain.VariantFileTotals{}, err
|
||||
}
|
||||
return domain.VariantFileTotals{Files: row.Files, Bytes: row.Bytes}, nil
|
||||
}
|
||||
|
||||
func (v *VariantFileRepository) TopSources(limit int) ([]domain.SourceTotals, error) {
|
||||
var rows []struct {
|
||||
SourceName string
|
||||
Files int
|
||||
Bytes int64
|
||||
}
|
||||
err := v.db.Model(&entity.ImageVariantFile{}).
|
||||
Select("source_name, COUNT(*) AS files, COALESCE(SUM(size), 0) AS bytes").
|
||||
Group("source_name").
|
||||
Order(topSourcesOrder).
|
||||
Limit(limit).
|
||||
Scan(&rows).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sources := make([]domain.SourceTotals, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
sources = append(sources, domain.SourceTotals{SourceName: row.SourceName, Files: row.Files, Bytes: row.Bytes})
|
||||
}
|
||||
return sources, nil
|
||||
}
|
||||
|
||||
func (v *VariantFileRepository) ListPage(filter domain.VariantFileFilter, afterID uint32, limit int) ([]domain.VariantFile, error) {
|
||||
statement := v.db.Where("id > ?", afterID)
|
||||
if filter.VariantName != "" {
|
||||
statement = statement.Where("variant_name = ?", filter.VariantName)
|
||||
}
|
||||
if filter.SourceName != "" {
|
||||
statement = statement.Where("source_name = ?", filter.SourceName)
|
||||
}
|
||||
if filter.Unused != nil {
|
||||
if len(filter.Unused.VariantNames) == 0 {
|
||||
return []domain.VariantFile{}, nil
|
||||
}
|
||||
statement = statement.Where(unusedCondition, filter.Unused.VariantNames, filter.Unused.Cutoff.UTC())
|
||||
}
|
||||
var found []entity.ImageVariantFile
|
||||
if err := statement.Order("id asc").Limit(limit).Find(&found).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files := make([]domain.VariantFile, 0, len(found))
|
||||
for _, model := range found {
|
||||
files = append(files, mappers.VariantFileToDomain(model))
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (v *VariantFileRepository) Totals() (map[string]domain.VariantFileTotals, error) {
|
||||
var rows []struct {
|
||||
VariantName string
|
||||
|
||||
Reference in new issue
Block a user