mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-29 13:27:03 +02:00
Implemented progressbar for export/import
This commit is contained in:
parent
ab04eb1123
commit
6e330daf06
24 changed files with 590 additions and 239 deletions
|
@ -33,16 +33,44 @@ import (
|
|||
type ScrobblerLogBackend struct {
|
||||
filePath string
|
||||
includeSkipped bool
|
||||
file *os.File
|
||||
log ScrobblerLog
|
||||
}
|
||||
|
||||
func (b ScrobblerLogBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
func (b *ScrobblerLogBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
b.filePath = config.GetString("file-path")
|
||||
b.includeSkipped = config.GetBool("include-skipped")
|
||||
return b
|
||||
}
|
||||
|
||||
func (b ScrobblerLogBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult) {
|
||||
func (b *ScrobblerLogBackend) Init() error {
|
||||
file, err := os.Create(b.filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b.log = ScrobblerLog{
|
||||
Timezone: "UNKNOWN",
|
||||
Client: "Rockbox unknown $Revision$",
|
||||
}
|
||||
|
||||
err = WriteHeader(file, &b.log)
|
||||
if err != nil {
|
||||
file.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
b.file = file
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *ScrobblerLogBackend) Finish() error {
|
||||
return b.file.Close()
|
||||
}
|
||||
|
||||
func (b *ScrobblerLogBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.Progress) {
|
||||
defer close(results)
|
||||
defer close(progress)
|
||||
file, err := os.Open(b.filePath)
|
||||
if err != nil {
|
||||
results <- models.ListensResult{Error: err}
|
||||
|
@ -60,45 +88,19 @@ func (b ScrobblerLogBackend) ExportListens(oldestTimestamp time.Time, results ch
|
|||
|
||||
listens := log.Listens.NewerThan(oldestTimestamp)
|
||||
sort.Sort(listens)
|
||||
progress <- models.Progress{Elapsed: int64(len(listens))}.Complete()
|
||||
results <- models.ListensResult{Listens: listens}
|
||||
}
|
||||
|
||||
func (b ScrobblerLogBackend) ImportListens(results chan models.ListensResult, oldestTimestamp time.Time) (models.ImportResult, error) {
|
||||
importResult := models.ImportResult{
|
||||
LastTimestamp: oldestTimestamp,
|
||||
}
|
||||
|
||||
file, err := os.Create(b.filePath)
|
||||
func (b *ScrobblerLogBackend) ImportListens(export models.ListensResult, importResult models.ImportResult, progress chan models.Progress) (models.ImportResult, error) {
|
||||
lastTimestamp, err := Write(b.file, export.Listens)
|
||||
if err != nil {
|
||||
return importResult, err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
log := ScrobblerLog{
|
||||
Timezone: "UNKNOWN",
|
||||
Client: "Rockbox unknown $Revision$",
|
||||
}
|
||||
|
||||
err = WriteHeader(file, &log)
|
||||
if err != nil {
|
||||
return importResult, err
|
||||
}
|
||||
|
||||
for result := range results {
|
||||
if result.Error != nil {
|
||||
return importResult, result.Error
|
||||
}
|
||||
|
||||
importResult.TotalCount += len(result.Listens)
|
||||
lastTimestamp, err := Write(file, result.Listens)
|
||||
if err != nil {
|
||||
return importResult, err
|
||||
}
|
||||
|
||||
importResult.UpdateTimestamp(lastTimestamp)
|
||||
importResult.ImportCount += len(result.Listens)
|
||||
}
|
||||
importResult.UpdateTimestamp(lastTimestamp)
|
||||
importResult.ImportCount = len(export.Listens)
|
||||
progress <- models.Progress{}.FromImportResult(importResult)
|
||||
|
||||
return importResult, nil
|
||||
}
|
||||
|
|
|
@ -32,6 +32,6 @@ import (
|
|||
func TestFromConfig(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("token", "thetoken")
|
||||
backend := scrobblerlog.ScrobblerLogBackend{}.FromConfig(config)
|
||||
assert.IsType(t, scrobblerlog.ScrobblerLogBackend{}, backend)
|
||||
backend := (&scrobblerlog.ScrobblerLogBackend{}).FromConfig(config)
|
||||
assert.IsType(t, &scrobblerlog.ScrobblerLogBackend{}, backend)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue