Implemented progressbar for export/import

This commit is contained in:
Philipp Wolfer 2023-11-16 00:45:00 +01:00
parent ab04eb1123
commit 6e330daf06
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
24 changed files with 590 additions and 239 deletions

View file

@ -30,33 +30,54 @@ import (
// A listen service backend.
// All listen services must implement this interface.
type Backend interface {
// Initialize the backend from a config.
FromConfig(config *viper.Viper) Backend
}
type ImportBackend interface {
Backend
// If the backend needs to setup resources before starting to import,
// this can be done here.
Init() error
// The implementation can perform all steps here to finalize the
// export/import and free used resources.
Finish() error
}
// Must be implemented by services supporting the export of listens.
type ListensExport interface {
Backend
// Returns a list of all listens newer then oldestTimestamp.
// The returned list of listens is supposed to be ordered by the
// Listen.ListenedAt timestamp, with the oldest entry first.
ExportListens(oldestTimestamp time.Time, results chan ListensResult)
ExportListens(oldestTimestamp time.Time, results chan ListensResult, progress chan Progress)
}
// Must be implemented by services supporting the import of listens.
type ListensImport interface {
ImportBackend
// Imports the given list of listens.
ImportListens(results chan ListensResult, oldestTimestamp time.Time) (ImportResult, error)
ImportListens(export ListensResult, importResult ImportResult, progress chan Progress) (ImportResult, error)
}
// Must be implemented by services supporting the export of loves.
type LovesExport interface {
Backend
// Returns a list of all loves newer then oldestTimestamp.
// The returned list of listens is supposed to be ordered by the
// Love.Created timestamp, with the oldest entry first.
ExportLoves(oldestTimestamp time.Time, results chan LovesResult)
ExportLoves(oldestTimestamp time.Time, results chan LovesResult, progress chan Progress)
}
// Must be implemented by services supporting the import of loves.
type LovesImport interface {
ImportBackend
// Imports the given list of loves.
ImportLoves(results chan LovesResult, oldestTimestamp time.Time) (ImportResult, error)
ImportLoves(export LovesResult, importResult ImportResult, progress chan Progress) (ImportResult, error)
}

View file

@ -105,13 +105,14 @@ func (l LovesList) Swap(i, j int) {
}
type ListensResult struct {
Error error
Listens ListensList
Listens ListensList
OldestTimestamp time.Time
Error error
}
type LovesResult struct {
Error error
Loves LovesList
Error error
}
type ImportResult struct {
@ -119,6 +120,9 @@ type ImportResult struct {
ImportCount int
LastTimestamp time.Time
ImportErrors []string
// Error is only set if an unrecoverable import error occurred
Error error
}
// Sets LastTimestamp to newTime, if newTime is newer than LastTimestamp
@ -127,3 +131,27 @@ func (i *ImportResult) UpdateTimestamp(newTime time.Time) {
i.LastTimestamp = newTime
}
}
func (i *ImportResult) Update(from ImportResult) {
i.TotalCount = from.TotalCount
i.ImportCount += from.ImportCount
i.UpdateTimestamp(from.LastTimestamp)
}
type Progress struct {
Total int64
Elapsed int64
Completed bool
}
func (p Progress) FromImportResult(result ImportResult) Progress {
p.Total = int64(result.TotalCount)
p.Elapsed = int64(result.ImportCount)
return p
}
func (p Progress) Complete() Progress {
p.Total = p.Elapsed
p.Completed = true
return p
}

View file

@ -77,6 +77,23 @@ func TestLovesListSort(t *testing.T) {
assert.Equal(t, love3, list[1])
}
func TestImportResultUpdate(t *testing.T) {
result := models.ImportResult{
TotalCount: 100,
ImportCount: 20,
LastTimestamp: time.Now(),
}
newResult := models.ImportResult{
TotalCount: 120,
ImportCount: 50,
LastTimestamp: time.Now().Add(1 * time.Hour),
}
result.Update(newResult)
assert.Equal(t, 120, result.TotalCount)
assert.Equal(t, 70, result.ImportCount)
assert.Equal(t, newResult.LastTimestamp, result.LastTimestamp)
}
func TestImportResultUpdateTimestamp(t *testing.T) {
timestamp := time.Now()
i := models.ImportResult{LastTimestamp: timestamp}