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
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -117,12 +118,88 @@ type mockVariantFileRepo struct {
|
||||
calls int
|
||||
|
||||
totalsBySourceCalls int
|
||||
|
||||
references map[uint32]time.Time
|
||||
failTotals error
|
||||
failPage error
|
||||
totalsCalls int
|
||||
unusedCalls int
|
||||
topCalls int
|
||||
pageLimits []int
|
||||
}
|
||||
|
||||
var _ domain.VariantFileRepository = &mockVariantFileRepo{}
|
||||
var _ domain.VariantCacheRepository = &mockVariantFileRepo{}
|
||||
|
||||
func newMockVariantFileRepo() *mockVariantFileRepo {
|
||||
return &mockVariantFileRepo{rows: map[uint32]domain.VariantFile{}}
|
||||
return &mockVariantFileRepo{rows: map[uint32]domain.VariantFile{}, references: map[uint32]time.Time{}}
|
||||
}
|
||||
|
||||
func (m *mockVariantFileRepo) setReference(id uint32, at time.Time) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.references[id] = at
|
||||
}
|
||||
|
||||
func (m *mockVariantFileRepo) isUnused(row domain.VariantFile, filter domain.UnusedFilter) bool {
|
||||
return slices.Contains(filter.VariantNames, row.VariantName) && m.references[row.ID].Before(filter.Cutoff)
|
||||
}
|
||||
|
||||
func (m *mockVariantFileRepo) UnusedTotals(filter domain.UnusedFilter) (domain.VariantFileTotals, error) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.unusedCalls++
|
||||
var totals domain.VariantFileTotals
|
||||
for _, row := range m.rows {
|
||||
if m.isUnused(row, filter) {
|
||||
totals.Files++
|
||||
totals.Bytes += row.Size
|
||||
}
|
||||
}
|
||||
return totals, nil
|
||||
}
|
||||
|
||||
func (m *mockVariantFileRepo) TopSources(limit int) ([]domain.SourceTotals, error) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.topCalls++
|
||||
bySource := map[string]domain.SourceTotals{}
|
||||
for _, row := range m.rows {
|
||||
current := bySource[row.SourceName]
|
||||
bySource[row.SourceName] = domain.SourceTotals{SourceName: row.SourceName, Files: current.Files + 1, Bytes: current.Bytes + row.Size}
|
||||
}
|
||||
sources := make([]domain.SourceTotals, 0, len(bySource))
|
||||
for _, source := range bySource {
|
||||
sources = append(sources, source)
|
||||
}
|
||||
sort.Slice(sources, func(i, j int) bool {
|
||||
if sources[i].Bytes != sources[j].Bytes {
|
||||
return sources[i].Bytes > sources[j].Bytes
|
||||
}
|
||||
return sources[i].SourceName < sources[j].SourceName
|
||||
})
|
||||
return sources[:min(limit, len(sources))], nil
|
||||
}
|
||||
|
||||
func (m *mockVariantFileRepo) ListPage(filter domain.VariantFileFilter, afterID uint32, limit int) ([]domain.VariantFile, error) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.pageLimits = append(m.pageLimits, limit)
|
||||
if m.failPage != nil {
|
||||
return nil, m.failPage
|
||||
}
|
||||
page := []domain.VariantFile{}
|
||||
for _, row := range m.rows {
|
||||
if row.ID <= afterID || (filter.VariantName != "" && row.VariantName != filter.VariantName) || (filter.SourceName != "" && row.SourceName != filter.SourceName) {
|
||||
continue
|
||||
}
|
||||
if filter.Unused != nil && !m.isUnused(row, *filter.Unused) {
|
||||
continue
|
||||
}
|
||||
page = append(page, row)
|
||||
}
|
||||
sort.Slice(page, func(i, j int) bool { return page[i].ID < page[j].ID })
|
||||
return page[:min(limit, len(page))], nil
|
||||
}
|
||||
|
||||
func (m *mockVariantFileRepo) Find(variantName string, sourceName string) (domain.VariantFile, bool, error) {
|
||||
@@ -190,6 +267,10 @@ func (m *mockVariantFileRepo) Totals() (map[string]domain.VariantFileTotals, err
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.calls++
|
||||
m.totalsCalls++
|
||||
if m.failTotals != nil {
|
||||
return nil, m.failTotals
|
||||
}
|
||||
totals := map[string]domain.VariantFileTotals{}
|
||||
for _, row := range m.rows {
|
||||
current := totals[row.VariantName]
|
||||
|
||||
Reference in new issue
Block a user