@@ -8,10 +8,12 @@ import (
8
8
"time"
9
9
10
10
"github.com/stellar/wallet-backend/internal/db"
11
+ "github.com/stellar/wallet-backend/internal/metrics"
11
12
)
12
13
13
14
type PaymentModel struct {
14
- DB db.ConnectionPool
15
+ DB db.ConnectionPool
16
+ MetricsService metrics.MetricsService
15
17
}
16
18
17
19
type Payment struct {
@@ -36,7 +38,11 @@ type Payment struct {
36
38
37
39
func (m * PaymentModel ) GetLatestLedgerSynced (ctx context.Context , cursorName string ) (uint32 , error ) {
38
40
var lastSyncedLedger uint32
41
+ start := time .Now ()
39
42
err := m .DB .GetContext (ctx , & lastSyncedLedger , `SELECT value FROM ingest_store WHERE key = $1` , cursorName )
43
+ duration := time .Since (start ).Seconds ()
44
+ m .MetricsService .ObserveDBQueryDuration ("SELECT" , "ingest_store" , duration )
45
+ m .MetricsService .IncDBQuery ("SELECT" , "ingest_store" )
40
46
// First run, key does not exist yet
41
47
if err == sql .ErrNoRows {
42
48
return 0 , nil
@@ -53,10 +59,14 @@ func (m *PaymentModel) UpdateLatestLedgerSynced(ctx context.Context, cursorName
53
59
INSERT INTO ingest_store (key, value) VALUES ($1, $2)
54
60
ON CONFLICT (key) DO UPDATE SET value = excluded.value
55
61
`
62
+ start := time .Now ()
56
63
_ , err := m .DB .ExecContext (ctx , query , cursorName , ledger )
64
+ duration := time .Since (start ).Seconds ()
65
+ m .MetricsService .ObserveDBQueryDuration ("INSERT" , "ingest_store" , duration )
57
66
if err != nil {
58
67
return fmt .Errorf ("updating last synced ledger to %d: %w" , ledger , err )
59
68
}
69
+ m .MetricsService .IncDBQuery ("INSERT" , "ingest_store" )
60
70
61
71
return nil
62
72
}
@@ -90,12 +100,15 @@ func (m *PaymentModel) AddPayment(ctx context.Context, tx db.Transaction, paymen
90
100
memo_type = EXCLUDED.memo_type
91
101
;
92
102
`
103
+ start := time .Now ()
93
104
_ , err := tx .ExecContext (ctx , query , payment .OperationID , payment .OperationType , payment .TransactionID , payment .TransactionHash , payment .FromAddress , payment .ToAddress , payment .SrcAssetCode , payment .SrcAssetIssuer , payment .SrcAssetType , payment .SrcAmount ,
94
105
payment .DestAssetCode , payment .DestAssetIssuer , payment .DestAssetType , payment .DestAmount , payment .CreatedAt , payment .Memo , payment .MemoType )
106
+ duration := time .Since (start ).Seconds ()
107
+ m .MetricsService .ObserveDBQueryDuration ("INSERT" , "ingest_payments" , duration )
95
108
if err != nil {
96
109
return fmt .Errorf ("inserting payment: %w" , err )
97
110
}
98
-
111
+ m . MetricsService . IncDBQuery ( "INSERT" , "ingest_payments" )
99
112
return nil
100
113
}
101
114
@@ -142,16 +155,19 @@ func (m *PaymentModel) GetPaymentsPaginated(ctx context.Context, address string,
142
155
if err != nil {
143
156
return nil , false , false , fmt .Errorf ("preparing named query: %w" , err )
144
157
}
158
+ start := time .Now ()
145
159
err = m .DB .SelectContext (ctx , & payments , query , args ... )
160
+ duration := time .Since (start ).Seconds ()
161
+ m .MetricsService .ObserveDBQueryDuration ("SELECT" , "ingest_payments" , duration )
146
162
if err != nil {
147
163
return nil , false , false , fmt .Errorf ("fetching payments: %w" , err )
148
164
}
165
+ m .MetricsService .IncDBQuery ("SELECT" , "ingest_payments" )
149
166
150
167
prevExists , nextExists , err := m .existsPrevNext (ctx , filteredSetCTE , address , sort , payments )
151
168
if err != nil {
152
169
return nil , false , false , fmt .Errorf ("checking prev and next pages: %w" , err )
153
170
}
154
-
155
171
return payments , prevExists , nextExists , nil
156
172
}
157
173
@@ -187,11 +203,14 @@ func (m *PaymentModel) existsPrevNext(ctx context.Context, filteredSetCTE string
187
203
}
188
204
189
205
var prevExists , nextExists bool
206
+ start := time .Now ()
190
207
err = m .DB .QueryRowxContext (ctx , query , args ... ).Scan (& prevExists , & nextExists )
208
+ duration := time .Since (start ).Seconds ()
209
+ m .MetricsService .ObserveDBQueryDuration ("SELECT" , "ingest_payments" , duration )
191
210
if err != nil {
192
211
return false , false , fmt .Errorf ("fetching prev and next exists: %w" , err )
193
212
}
194
-
213
+ m . MetricsService . IncDBQuery ( "SELECT" , "ingest_payments" )
195
214
return prevExists , nextExists , nil
196
215
}
197
216
0 commit comments