Skip to content

Commit

Permalink
Merge pull request #66 from lungria/feature/add-logs
Browse files Browse the repository at this point in the history
logs and bugfix for IntervalProvider
  • Loading branch information
SuddenGunter authored May 11, 2022
2 parents 9d57704 + e5a8ff5 commit fa55eb5
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 62 deletions.
2 changes: 1 addition & 1 deletion app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func initImporter(cfg config.Config, cl *mono.Client, a *account.Repository, t *
gen := interval.NewGenerator(t)
acIm := account.NewImporter(cl, a)
txIm := transaction.NewImporter(cl, t, gen)
globalIm := importer.NewImporter(acIm, txIm)
globalIm := importer.NewImporter(txIm, acIm)

return importer.NewWorker(globalIm, cfg.MonoAccountID)
}
19 changes: 14 additions & 5 deletions importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package importer

import (
"context"
"time"

"github.com/rs/zerolog/log"
)
Expand All @@ -15,7 +16,7 @@ type AccountImporter interface {
// TransactionsImporter abstracts transactions import logic implementation.
type TransactionsImporter interface {
// Import loads transactions in specified interval for specified accountID and saves them to storage.
Import(ctx context.Context, accountID string) error
Import(ctx context.Context, accountID string) (time.Time, time.Time, error)
}

// Importer loads latest data from bank for specified accountID.
Expand All @@ -32,17 +33,25 @@ func NewImporter(transactions TransactionsImporter, accounts AccountImporter) *I
}
}

// Import latest data from bank for specified accountID.
// Import the latest data from bank for specified accountID.
func (i *Importer) Import(ctx context.Context, accountID string) {
err := i.accounts.Import(ctx, accountID)
if err != nil {
log.Err(err).Msg("failed import")
log.Err(err).Msg("failed account import")
return
}

err = i.transactions.Import(ctx, accountID)
from, to, err := i.transactions.Import(ctx, accountID)
if err != nil {
log.Err(err).Msg("failed import")
log.Err(err).
Time("from", from).
Time("to", to).
Msg("failed transactions import")
return
}

log.Debug().
Time("from", from).
Time("to", to).
Msg("import finished")
}
5 changes: 3 additions & 2 deletions importer/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"io/ioutil"
"testing"
"time"

"github.com/lungria/spendshelf-backend/importer"
"github.com/lungria/spendshelf-backend/importer/mock"
Expand All @@ -18,8 +19,8 @@ func TestImport_WhenCalled_ImportsAccountsAndTransactions(t *testing.T) {
return nil
}
transactions := &mock.TransactionsImporterMock{}
transactions.ImportFunc = func(ctx context.Context, accountID string) error {
return nil
transactions.ImportFunc = func(ctx context.Context, accountID string) (time.Time, time.Time, error) {
return time.Time{}, time.Time{}, nil
}
log.Logger = zerolog.New(ioutil.Discard)
svc := importer.NewImporter(transactions, accounts)
Expand Down
78 changes: 39 additions & 39 deletions importer/mock/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions importer/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"sync"
"time"

"github.com/rs/zerolog/log"
)

const (
Expand Down Expand Up @@ -40,7 +38,6 @@ func (w *Worker) Start() {
select {
case _ = <-ticker.C:
w.executeWithTimeout(w.ctx)
log.Debug().Msg("import finished")
case _ = <-w.ctx.Done():
return
}
Expand Down
16 changes: 10 additions & 6 deletions transaction/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func NewImporter(
}

// Import loads transactions in specified interval for specified accountID and saves them to storage.
func (i *Importer) Import(ctx context.Context, accountID string) error {
func (i *Importer) Import(ctx context.Context, accountID string) (time.Time, time.Time, error) {
from, to, err := i.intervalGen.GetInterval(ctx, accountID)
if err != nil {
return fmt.Errorf("failed import transaction for account '%s': %w", accountID, err)
return i.resultWithErr(fmt.Errorf("failed import transaction for account '%s': %w", accountID, err))
}

query := mono.GetTransactionsQuery{
Expand All @@ -66,21 +66,25 @@ func (i *Importer) Import(ctx context.Context, accountID string) error {

monoTransactions, err := i.api.GetTransactions(ctx, query)
if err != nil {
return fmt.Errorf("failed import transaction for account '%s': %w", accountID, err)
return i.resultWithErr(fmt.Errorf("failed import transaction for account '%s': %w", accountID, err))
}

if len(monoTransactions) == 0 {
return nil
return from, to, nil
}

transactions := i.mapTransactions(accountID, monoTransactions)

err = i.transactions.Save(ctx, transactions)
if err != nil {
return fmt.Errorf("failed import transaction for account '%s': %w", accountID, err)
return i.resultWithErr(fmt.Errorf("failed import transaction for account '%s': %w", accountID, err))
}

return nil
return from, to, nil
}

func (i *Importer) resultWithErr(err error) (time.Time, time.Time, error) {
return time.Time{}, time.Time{}, err
}

func (i *Importer) mapTransactions(accountID string, src []mono.Transaction) []Transaction {
Expand Down
10 changes: 5 additions & 5 deletions transaction/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestImport_WhenGetIntervalFails_ReturnsError(t *testing.T) {
}
svc := transaction.NewImporter(api, storage, gen)

err := svc.Import(context.Background(), "acc")
_, _, err := svc.Import(context.Background(), "acc")

assert.True(t, errors.Is(err, testError))
assert.Zero(t, len(storage.SaveCalls()))
Expand All @@ -43,7 +43,7 @@ func TestImport_WhenApiGetTransactionsFails_ReturnsError(t *testing.T) {
}
svc := transaction.NewImporter(api, storage, gen)

err := svc.Import(context.Background(), "acc")
_, _, err := svc.Import(context.Background(), "acc")

assert.True(t, errors.Is(err, testError))
assert.Zero(t, len(storage.SaveCalls()))
Expand All @@ -63,7 +63,7 @@ func TestImport_WhenApiGetTransactionsReturnsNothing_StorageNotCalled(t *testing
}
svc := transaction.NewImporter(api, storage, gen)

err := svc.Import(context.Background(), "acc")
_, _, err := svc.Import(context.Background(), "acc")

assert.Nil(t, err)
assert.Equal(t, 0, len(storage.SaveCalls()))
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestImport_WhenStorageSaveReturnsError_ReturnsError(t *testing.T) {
}
svc := transaction.NewImporter(api, db, gen)

err := svc.Import(context.Background(), "acc")
_, _, err := svc.Import(context.Background(), "acc")
saveCalls := db.SaveCalls()

assert.Error(t, err)
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestImport_WhenDataIsSaved_ReturnsNil(t *testing.T) {
}
svc := transaction.NewImporter(api, db, gen)

err := svc.Import(context.Background(), "acc")
_, _, err := svc.Import(context.Background(), "acc")
saveCalls := db.SaveCalls()

assert.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion transaction/interval/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (gen *Generator) GetInterval(ctx context.Context, accountID string) (from,

diffSecs := nowUtc.Sub(lastKnownTransactionDate.UTC()).Seconds()
if diffSecs > maxAllowedIntervalDuration {
return lastKnownTransactionDate, lastKnownTransactionDate.Add(maxAllowedIntervalDuration / 2), nil
return lastKnownTransactionDate, lastKnownTransactionDate.Add(time.Second * maxAllowedIntervalDuration / 2), nil
}

return lastKnownTransactionDate.UTC(), nowUtc, nil
Expand Down

0 comments on commit fa55eb5

Please sign in to comment.