Import result can report total and processed items

This commit is contained in:
Philipp Wolfer 2023-11-12 18:40:45 +01:00
parent 516de905bd
commit ead323eaed
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
5 changed files with 14 additions and 7 deletions

View file

@ -37,13 +37,15 @@ func (b DumpBackend) FromConfig(config *viper.Viper) models.Backend {
func (b DumpBackend) ImportListens(listens []models.Listen, oldestTimestamp time.Time) (models.ImportResult, error) {
result := models.ImportResult{
Count: len(listens),
TotalCount: len(listens),
ImportCount: 0,
LastTimestamp: oldestTimestamp,
}
for _, listen := range listens {
if listen.ListenedAt.Unix() > result.LastTimestamp.Unix() {
result.LastTimestamp = listen.ListenedAt
}
result.ImportCount += 1
fmt.Printf("🎶 %v: \"%s\" by %s\n", listen.ListenedAt, listen.TrackName, listen.ArtistName())
}
return result, nil
@ -51,13 +53,15 @@ func (b DumpBackend) ImportListens(listens []models.Listen, oldestTimestamp time
func (b DumpBackend) ImportLoves(loves []models.Love, oldestTimestamp time.Time) (models.ImportResult, error) {
result := models.ImportResult{
Count: len(loves),
TotalCount: len(loves),
ImportCount: 0,
LastTimestamp: oldestTimestamp,
}
for _, love := range loves {
if love.Created.Unix() > result.LastTimestamp.Unix() {
result.LastTimestamp = love.Created
}
result.ImportCount += 1
fmt.Printf("❤️ %v: \"%s\" by %s\n", love.Created, love.TrackName, love.ArtistName())
}
return result, nil

View file

@ -58,7 +58,7 @@ func (b ScrobblerLogBackend) ExportListens(oldestTimestamp time.Time) ([]models.
func (b ScrobblerLogBackend) ImportListens(listens []models.Listen, oldestTimestamp time.Time) (models.ImportResult, error) {
result := models.ImportResult{
Count: 0,
TotalCount: len(listens),
LastTimestamp: oldestTimestamp,
}
@ -82,6 +82,6 @@ func (b ScrobblerLogBackend) ImportListens(listens []models.Listen, oldestTimest
}
result.LastTimestamp = lastTimestamp
result.Count = len(listens)
result.ImportCount = len(listens)
return result, nil
}

View file

@ -48,7 +48,8 @@ var listensCmd = &cobra.Command{
cobra.CheckErr(err)
result, err := importBackend.ImportListens(listens, timestamp)
cobra.CheckErr(err)
fmt.Printf("Imported %v listens (last timestamp %v)\n", result.Count, result.LastTimestamp)
fmt.Printf("Imported %v of %v listens (last timestamp %v)\n",
result.ImportCount, result.TotalCount, result.LastTimestamp)
},
}

View file

@ -48,7 +48,8 @@ var lovesCmd = &cobra.Command{
cobra.CheckErr(err)
result, err := importBackend.ImportLoves(loves, timestamp)
cobra.CheckErr(err)
fmt.Printf("Imported %v loves (last timestamp %v)\n", result.Count, result.LastTimestamp)
fmt.Printf("Imported %v of %v loves (last timestamp %v)\n",
result.ImportCount, result.TotalCount, result.LastTimestamp)
},
}

View file

@ -48,6 +48,7 @@ type LovesImport interface {
}
type ImportResult struct {
Count int
TotalCount int
ImportCount int
LastTimestamp time.Time
}