Add ImportResult.UpdateTimestamp method

This commit is contained in:
Philipp Wolfer 2023-11-15 18:37:36 +01:00
parent 298697dcfc
commit 729a3d0ed0
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
6 changed files with 39 additions and 30 deletions

View file

@ -48,9 +48,7 @@ func (b DumpBackend) ImportListens(results chan models.ListensResult, oldestTime
importResult.TotalCount += len(result.Listens)
for _, listen := range result.Listens {
if listen.ListenedAt.Unix() > importResult.LastTimestamp.Unix() {
importResult.LastTimestamp = listen.ListenedAt
}
importResult.UpdateTimestamp(listen.ListenedAt)
importResult.ImportCount += 1
fmt.Printf("🎶 %v: \"%v\" by %v (%v)\n",
listen.ListenedAt, listen.TrackName, listen.ArtistName(), listen.RecordingMbid)
@ -72,9 +70,7 @@ func (b DumpBackend) ImportLoves(results chan models.LovesResult, oldestTimestam
importResult.TotalCount += len(result.Loves)
for _, love := range result.Loves {
if love.Created.Unix() > importResult.LastTimestamp.Unix() {
importResult.LastTimestamp = love.Created
}
importResult.UpdateTimestamp(love.Created)
importResult.ImportCount += 1
fmt.Printf("❤️ %v: \"%v\" by %v (%v)\n",
love.Created, love.TrackName, love.ArtistName(), love.RecordingMbid)

View file

@ -55,10 +55,6 @@ func (b JspfBackend) ImportLoves(loves []models.Love, oldestTimestamp time.Time)
tracks := make([]Track, 0, result.TotalCount)
for _, love := range loves {
if love.Created.Unix() > result.LastTimestamp.Unix() {
result.LastTimestamp = love.Created
}
extension := MusicBrainzTrackExtension{
AddedAt: love.Created,
AddedBy: love.UserName,
@ -90,6 +86,7 @@ func (b JspfBackend) ImportLoves(loves []models.Love, oldestTimestamp time.Time)
tracks = append(tracks, track)
result.UpdateTimestamp(love.Created)
result.ImportCount += 1
}

View file

@ -158,10 +158,8 @@ func (b ListenBrainzApiBackend) ImportLoves(loves []models.Love, oldestTimestamp
}
if ok {
result.UpdateTimestamp(love.Created)
result.ImportCount += 1
if love.Created.Unix() > result.LastTimestamp.Unix() {
result.LastTimestamp = love.Created
}
} else {
msg := fmt.Sprintf("Failed import of \"%s\" by %s: %v",
love.TrackName, love.ArtistName(), errMsg)

View file

@ -60,20 +60,3 @@ type LovesImport interface {
// Imports the given list of loves.
ImportLoves(results chan LovesResult, oldestTimestamp time.Time) (ImportResult, error)
}
type ListensResult struct {
Error error
Listens ListensList
}
type LovesResult struct {
Error error
Loves LovesList
}
type ImportResult struct {
TotalCount int
ImportCount int
LastTimestamp time.Time
ImportErrors []string
}

View file

@ -92,3 +92,27 @@ func (l LovesList) Less(i, j int) bool {
func (l LovesList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
type ListensResult struct {
Error error
Listens ListensList
}
type LovesResult struct {
Error error
Loves LovesList
}
type ImportResult struct {
TotalCount int
ImportCount int
LastTimestamp time.Time
ImportErrors []string
}
// Sets LastTimestamp to newTime, if newTime is newer than LastTimestamp
func (i *ImportResult) UpdateTimestamp(newTime time.Time) {
if newTime.Unix() > i.LastTimestamp.Unix() {
i.LastTimestamp = newTime
}
}

View file

@ -62,3 +62,14 @@ func TestLovesListSort(t *testing.T) {
assert.Equal(t, love2, list[0])
assert.Equal(t, love3, list[1])
}
func TestImportResultUpdateTimestamp(t *testing.T) {
timestamp := time.Now()
i := models.ImportResult{LastTimestamp: timestamp}
newTimestamp := time.Now().Add(-time.Duration(2 * time.Second))
i.UpdateTimestamp(newTimestamp)
assert.Equalf(t, timestamp, i.LastTimestamp, "Expected older timestamp is kept")
newTimestamp = time.Now().Add(time.Duration(2 * time.Second))
i.UpdateTimestamp(newTimestamp)
assert.Equalf(t, newTimestamp, i.LastTimestamp, "Updated timestamp expected")
}