Skip to content

Commit fa55eb5

Browse files
authored
Merge pull request #66 from lungria/feature/add-logs
logs and bugfix for IntervalProvider
2 parents 9d57704 + e5a8ff5 commit fa55eb5

File tree

8 files changed

+73
-62
lines changed

8 files changed

+73
-62
lines changed

app/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func initImporter(cfg config.Config, cl *mono.Client, a *account.Repository, t *
6060
gen := interval.NewGenerator(t)
6161
acIm := account.NewImporter(cl, a)
6262
txIm := transaction.NewImporter(cl, t, gen)
63-
globalIm := importer.NewImporter(acIm, txIm)
63+
globalIm := importer.NewImporter(txIm, acIm)
6464

6565
return importer.NewWorker(globalIm, cfg.MonoAccountID)
6666
}

importer/importer.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package importer
22

33
import (
44
"context"
5+
"time"
56

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

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

35-
// Import latest data from bank for specified accountID.
36+
// Import the latest data from bank for specified accountID.
3637
func (i *Importer) Import(ctx context.Context, accountID string) {
3738
err := i.accounts.Import(ctx, accountID)
3839
if err != nil {
39-
log.Err(err).Msg("failed import")
40+
log.Err(err).Msg("failed account import")
4041
return
4142
}
4243

43-
err = i.transactions.Import(ctx, accountID)
44+
from, to, err := i.transactions.Import(ctx, accountID)
4445
if err != nil {
45-
log.Err(err).Msg("failed import")
46+
log.Err(err).
47+
Time("from", from).
48+
Time("to", to).
49+
Msg("failed transactions import")
4650
return
4751
}
52+
53+
log.Debug().
54+
Time("from", from).
55+
Time("to", to).
56+
Msg("import finished")
4857
}

importer/importer_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"io/ioutil"
66
"testing"
7+
"time"
78

89
"github.com/lungria/spendshelf-backend/importer"
910
"github.com/lungria/spendshelf-backend/importer/mock"
@@ -18,8 +19,8 @@ func TestImport_WhenCalled_ImportsAccountsAndTransactions(t *testing.T) {
1819
return nil
1920
}
2021
transactions := &mock.TransactionsImporterMock{}
21-
transactions.ImportFunc = func(ctx context.Context, accountID string) error {
22-
return nil
22+
transactions.ImportFunc = func(ctx context.Context, accountID string) (time.Time, time.Time, error) {
23+
return time.Time{}, time.Time{}, nil
2324
}
2425
log.Logger = zerolog.New(ioutil.Discard)
2526
svc := importer.NewImporter(transactions, accounts)

importer/mock/mock.go

Lines changed: 39 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

importer/worker.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"sync"
66
"time"
7-
8-
"github.com/rs/zerolog/log"
97
)
108

119
const (
@@ -40,7 +38,6 @@ func (w *Worker) Start() {
4038
select {
4139
case _ = <-ticker.C:
4240
w.executeWithTimeout(w.ctx)
43-
log.Debug().Msg("import finished")
4441
case _ = <-w.ctx.Done():
4542
return
4643
}

transaction/importer.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ func NewImporter(
5252
}
5353

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

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

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

7272
if len(monoTransactions) == 0 {
73-
return nil
73+
return from, to, nil
7474
}
7575

7676
transactions := i.mapTransactions(accountID, monoTransactions)
7777

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

83-
return nil
83+
return from, to, nil
84+
}
85+
86+
func (i *Importer) resultWithErr(err error) (time.Time, time.Time, error) {
87+
return time.Time{}, time.Time{}, err
8488
}
8589

8690
func (i *Importer) mapTransactions(accountID string, src []mono.Transaction) []Transaction {

transaction/importer_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestImport_WhenGetIntervalFails_ReturnsError(t *testing.T) {
2222
}
2323
svc := transaction.NewImporter(api, storage, gen)
2424

25-
err := svc.Import(context.Background(), "acc")
25+
_, _, err := svc.Import(context.Background(), "acc")
2626

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

46-
err := svc.Import(context.Background(), "acc")
46+
_, _, err := svc.Import(context.Background(), "acc")
4747

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

66-
err := svc.Import(context.Background(), "acc")
66+
_, _, err := svc.Import(context.Background(), "acc")
6767

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

96-
err := svc.Import(context.Background(), "acc")
96+
_, _, err := svc.Import(context.Background(), "acc")
9797
saveCalls := db.SaveCalls()
9898

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

127-
err := svc.Import(context.Background(), "acc")
127+
_, _, err := svc.Import(context.Background(), "acc")
128128
saveCalls := db.SaveCalls()
129129

130130
assert.Nil(t, err)

transaction/interval/interval.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (gen *Generator) GetInterval(ctx context.Context, accountID string) (from,
4949

5050
diffSecs := nowUtc.Sub(lastKnownTransactionDate.UTC()).Seconds()
5151
if diffSecs > maxAllowedIntervalDuration {
52-
return lastKnownTransactionDate, lastKnownTransactionDate.Add(maxAllowedIntervalDuration / 2), nil
52+
return lastKnownTransactionDate, lastKnownTransactionDate.Add(time.Second * maxAllowedIntervalDuration / 2), nil
5353
}
5454

5555
return lastKnownTransactionDate.UTC(), nowUtc, nil

0 commit comments

Comments
 (0)