Database support to store the last processed timestamp

This commit is contained in:
Philipp Wolfer 2023-11-14 16:32:21 +01:00
parent 503449c43d
commit c8146355c1
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
8 changed files with 268 additions and 5 deletions

View file

@ -26,8 +26,10 @@ import (
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uploadedlobster.com/scotty/backends"
"go.uploadedlobster.com/scotty/models"
"go.uploadedlobster.com/scotty/storage"
)
// listensCmd represents the listens command
@ -39,18 +41,42 @@ var listensCmd = &cobra.Command{
sourceName, sourceConfig := getConfigFromFlag(cmd, "from")
targetName, targetConfig := getConfigFromFlag(cmd, "to")
fmt.Printf("Transferring listens from %s to %s...\n", sourceName, targetName)
// Initialize backends
exportBackend, err := backends.ResolveBackend[models.ListensExport](sourceConfig)
cobra.CheckErr(err)
importBackend, err := backends.ResolveBackend[models.ListensImport](targetConfig)
cobra.CheckErr(err)
// Setup database
db, err := storage.New(viper.GetString("database"))
cobra.CheckErr(err)
// Read timestamp
timestamp := time.Unix(getInt64FromFlag(cmd, "timestamp"), 0)
if timestamp == time.Unix(0, 0) {
timestamp, err = db.GetImportTimestamp(sourceName, targetName, "listens")
cobra.CheckErr(err)
}
fmt.Printf("From timestamp: %v (%v)\n", timestamp, timestamp.Unix())
// Export from source
listens, err := exportBackend.ExportListens(timestamp)
cobra.CheckErr(err)
fmt.Printf("Loaded %v listens from %v.\n", len(listens), sourceName)
// Import into target
result, err := importBackend.ImportListens(listens, timestamp)
cobra.CheckErr(err)
fmt.Printf("Imported %v of %v listens (last timestamp %v)\n",
result.ImportCount, result.TotalCount, result.LastTimestamp)
fmt.Printf("Imported %v of %v listens into %v.\n",
result.ImportCount, result.TotalCount, targetName)
// Update timestamp
fmt.Printf("Latest timestamp: %v (%v)\n", result.LastTimestamp, result.LastTimestamp.Unix())
err = db.SetImportTimestamp(sourceName, targetName, "listens", result.LastTimestamp)
cobra.CheckErr(err)
// Print errors
if len(result.ImportErrors) > 0 {
fmt.Printf("\nDuring the import the following errors occurred:\n")
for _, err := range result.ImportErrors {

View file

@ -26,8 +26,10 @@ import (
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uploadedlobster.com/scotty/backends"
"go.uploadedlobster.com/scotty/models"
"go.uploadedlobster.com/scotty/storage"
)
// lovesCmd represents the loves command
@ -39,18 +41,42 @@ var lovesCmd = &cobra.Command{
sourceName, sourceConfig := getConfigFromFlag(cmd, "from")
targetName, targetConfig := getConfigFromFlag(cmd, "to")
fmt.Printf("Transferring loves from %s to %s...\n", sourceName, targetName)
// Initialize backends
exportBackend, err := backends.ResolveBackend[models.LovesExport](sourceConfig)
cobra.CheckErr(err)
importBackend, err := backends.ResolveBackend[models.LovesImport](targetConfig)
cobra.CheckErr(err)
// Setup database
db, err := storage.New(viper.GetString("database"))
cobra.CheckErr(err)
// Read timestamp
timestamp := time.Unix(getInt64FromFlag(cmd, "timestamp"), 0)
if timestamp == time.Unix(0, 0) {
timestamp, err = db.GetImportTimestamp(sourceName, targetName, "loves")
cobra.CheckErr(err)
}
fmt.Printf("From timestamp: %v (%v)\n", timestamp, timestamp.Unix())
// Export from source
loves, err := exportBackend.ExportLoves(timestamp)
cobra.CheckErr(err)
fmt.Printf("Loaded %v loves from %v.\n", len(loves), sourceName)
// Import into target
result, err := importBackend.ImportLoves(loves, timestamp)
cobra.CheckErr(err)
fmt.Printf("Imported %v of %v loves (last timestamp %v)\n",
result.ImportCount, result.TotalCount, result.LastTimestamp)
fmt.Printf("Imported %v of %v loves into %v.\n",
result.ImportCount, result.TotalCount, targetName)
// Update timestamp
fmt.Printf("Latest timestamp: %v (%v)\n", result.LastTimestamp, result.LastTimestamp.Unix())
err = db.SetImportTimestamp(sourceName, targetName, "loves", result.LastTimestamp)
cobra.CheckErr(err)
// Print errors
if len(result.ImportErrors) > 0 {
fmt.Printf("\nDuring the import the following errors occurred:\n")
for _, err := range result.ImportErrors {