Run exporter in goroutine

Use channel to pass data from exporter to importer
This commit is contained in:
Philipp Wolfer 2023-11-15 18:16:00 +01:00
parent 1ba165a631
commit 298697dcfc
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
7 changed files with 124 additions and 42 deletions

View file

@ -38,13 +38,13 @@ type ListensExport interface {
// 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) ([]Listen, error)
ExportListens(oldestTimestamp time.Time, results chan ListensResult)
}
// Must be implemented by services supporting the import of listens.
type ListensImport interface {
// Imports the given list of listens.
ImportListens(listens []Listen, oldestTimestamp time.Time) (ImportResult, error)
ImportListens(results chan ListensResult, oldestTimestamp time.Time) (ImportResult, error)
}
// Must be implemented by services supporting the export of loves.
@ -52,13 +52,23 @@ type LovesExport interface {
// 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) ([]Love, error)
ExportLoves(oldestTimestamp time.Time, results chan LovesResult)
}
// Must be implemented by services supporting the import of loves.
type LovesImport interface {
// Imports the given list of loves.
ImportLoves(loves []Love, oldestTimestamp time.Time) (ImportResult, error)
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 {

View file

@ -64,3 +64,31 @@ type Love struct {
RecordingMbid MBID
RecordingMsid MBID
}
type ListensList []Listen
func (l ListensList) Len() int {
return len(l)
}
func (l ListensList) Less(i, j int) bool {
return l[i].ListenedAt.Unix() < l[j].ListenedAt.Unix()
}
func (l ListensList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
type LovesList []Love
func (l LovesList) Len() int {
return len(l)
}
func (l LovesList) Less(i, j int) bool {
return l[i].Created.Unix() < l[j].Created.Unix()
}
func (l LovesList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}

View file

@ -22,7 +22,9 @@ THE SOFTWARE.
package models_test
import (
"sort"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uploadedlobster.com/scotty/models"
@ -38,3 +40,25 @@ func TestTrackArtistName(t *testing.T) {
}
assert.Equal(t, "Foo, Bar, Baz", track.ArtistName())
}
func TestListensListSort(t *testing.T) {
listen1 := models.Listen{ListenedAt: time.Unix(3, 0)}
listen2 := models.Listen{ListenedAt: time.Unix(0, 0)}
listen3 := models.Listen{ListenedAt: time.Unix(2, 0)}
list := models.ListensList{listen1, listen2, listen3}
sort.Sort(list)
assert.Equal(t, listen1, list[2])
assert.Equal(t, listen2, list[0])
assert.Equal(t, listen3, list[1])
}
func TestLovesListSort(t *testing.T) {
love1 := models.Love{Created: time.Unix(3, 0)}
love2 := models.Love{Created: time.Unix(0, 0)}
love3 := models.Love{Created: time.Unix(2, 0)}
list := models.LovesList{love1, love2, love3}
sort.Sort(list)
assert.Equal(t, love1, list[2])
assert.Equal(t, love2, list[0])
assert.Equal(t, love3, list[1])
}