mirror of
https://git.sr.ht/~phw/scotty
synced 2025-05-11 02:27:05 +02:00
Initial implementation of unified export/import progress
Both export and import progress get updated over a unified channel. Most importantly this allows updating the import total from latest export results.
This commit is contained in:
parent
1f48abc284
commit
b8e6ccffdb
18 changed files with 369 additions and 194 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
||||
Copyright © 2023-2025 Philipp Wolfer <phw@uploadedlobster.com>
|
||||
|
||||
This file is part of Scotty.
|
||||
|
||||
|
@ -60,19 +60,26 @@ func (b *FunkwhaleApiBackend) InitConfig(config *config.ServiceConfig) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (b *FunkwhaleApiBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.Progress) {
|
||||
func (b *FunkwhaleApiBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.TransferProgress) {
|
||||
page := 1
|
||||
perPage := MaxItemsPerGet
|
||||
|
||||
// We need to gather the full list of listens in order to sort them
|
||||
listens := make(models.ListensList, 0, 2*perPage)
|
||||
p := models.Progress{Total: int64(perPage)}
|
||||
p := models.TransferProgress{
|
||||
Export: &models.Progress{
|
||||
Total: int64(perPage),
|
||||
},
|
||||
}
|
||||
|
||||
out:
|
||||
for {
|
||||
result, err := b.client.GetHistoryListenings(b.username, page, perPage)
|
||||
if err != nil {
|
||||
p.Export.Abort()
|
||||
progress <- p
|
||||
results <- models.ListensResult{Error: err}
|
||||
return
|
||||
}
|
||||
|
||||
count := len(result.Results)
|
||||
|
@ -83,7 +90,7 @@ out:
|
|||
for _, fwListen := range result.Results {
|
||||
listen := fwListen.AsListen()
|
||||
if listen.ListenedAt.After(oldestTimestamp) {
|
||||
p.Elapsed += 1
|
||||
p.Export.Elapsed += 1
|
||||
listens = append(listens, listen)
|
||||
} else {
|
||||
break out
|
||||
|
@ -92,34 +99,42 @@ out:
|
|||
|
||||
if result.Next == "" {
|
||||
// No further results
|
||||
p.Total = p.Elapsed
|
||||
p.Total -= int64(perPage - count)
|
||||
p.Export.Total = p.Export.Elapsed
|
||||
p.Export.Total -= int64(perPage - count)
|
||||
break out
|
||||
}
|
||||
|
||||
p.Total += int64(perPage)
|
||||
p.Export.TotalItems = len(listens)
|
||||
p.Export.Total += int64(perPage)
|
||||
progress <- p
|
||||
page += 1
|
||||
}
|
||||
|
||||
sort.Sort(listens)
|
||||
progress <- p.Complete()
|
||||
p.Export.TotalItems = len(listens)
|
||||
p.Export.Complete()
|
||||
progress <- p
|
||||
results <- models.ListensResult{Items: listens}
|
||||
}
|
||||
|
||||
func (b *FunkwhaleApiBackend) ExportLoves(oldestTimestamp time.Time, results chan models.LovesResult, progress chan models.Progress) {
|
||||
func (b *FunkwhaleApiBackend) ExportLoves(oldestTimestamp time.Time, results chan models.LovesResult, progress chan models.TransferProgress) {
|
||||
page := 1
|
||||
perPage := MaxItemsPerGet
|
||||
|
||||
// We need to gather the full list of listens in order to sort them
|
||||
loves := make(models.LovesList, 0, 2*perPage)
|
||||
p := models.Progress{Total: int64(perPage)}
|
||||
p := models.TransferProgress{
|
||||
Export: &models.Progress{
|
||||
Total: int64(perPage),
|
||||
},
|
||||
}
|
||||
|
||||
out:
|
||||
for {
|
||||
result, err := b.client.GetFavoriteTracks(page, perPage)
|
||||
if err != nil {
|
||||
progress <- p.Abort()
|
||||
p.Export.Abort()
|
||||
progress <- p
|
||||
results <- models.LovesResult{Error: err}
|
||||
return
|
||||
}
|
||||
|
@ -132,7 +147,7 @@ out:
|
|||
for _, favorite := range result.Results {
|
||||
love := favorite.AsLove()
|
||||
if love.Created.After(oldestTimestamp) {
|
||||
p.Elapsed += 1
|
||||
p.Export.Elapsed += 1
|
||||
loves = append(loves, love)
|
||||
} else {
|
||||
break out
|
||||
|
@ -144,13 +159,16 @@ out:
|
|||
break out
|
||||
}
|
||||
|
||||
p.Total += int64(perPage)
|
||||
p.Export.TotalItems = len(loves)
|
||||
p.Export.Total += int64(perPage)
|
||||
progress <- p
|
||||
page += 1
|
||||
}
|
||||
|
||||
sort.Sort(loves)
|
||||
progress <- p.Complete()
|
||||
p.Export.TotalItems = len(loves)
|
||||
p.Export.Complete()
|
||||
progress <- p
|
||||
results <- models.LovesResult{Items: loves}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue