Skip to content

Commit 3a9b53b

Browse files
committed
fixup! Add clickhouseEngine
1 parent f0a2fb4 commit 3a9b53b

37 files changed

Lines changed: 98 additions & 74 deletions

src/clickhouseEngine.ts

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createClient } from '@clickhouse/client'
22
import nano from 'nano'
33

4+
import { clientConfig } from './bin/configure'
45
import { config } from './config'
56
import { initDbs } from './initDbs'
67
import { processBanxaTx } from './partners/banxa'
@@ -35,7 +36,7 @@ import { processThorchainTx } from './partners/thorchain'
3536
import { processTransakTx } from './partners/transak'
3637
import { processWyreTx } from './partners/wyre'
3738
import { processXanpoolTx } from './partners/xanpool'
38-
import { DbTx, StandardTx } from './types'
39+
import { DbTx, StandardTx, wasDbTx } from './types'
3940
import { datelog, snooze } from './util'
4041

4142
// Clickhouse recommends large batch inserts. We consider the couchdb
@@ -90,53 +91,53 @@ export async function clickhouseEngine(): Promise<void> {
9091

9192
const dbTransactions = nanoDb.db.use<StandardTx>('reports_transactions')
9293

94+
let afterTime = new Date(0).toISOString()
95+
let i = 0
96+
9397
while (true) {
94-
const response = await dbTransactions.view('versioning', 'indexVersion', {
95-
include_docs: true,
98+
const response = await dbTransactions.find({
99+
selector: {
100+
status: { $eq: 'complete' },
101+
updateTime: { $gt: afterTime }
102+
},
103+
sort: [{ updateTime: 'asc' }],
104+
use_index: 'status-updatetime',
96105
limit: PAGE_SIZE,
97-
start_key: 0,
98-
end_key: config.clickhouseIndexVersion,
99-
inclusive_end: false
106+
skip: PAGE_SIZE * i++
100107
})
101108

102-
const startDocId = response.rows[0]?.id
103-
const endDocId = response.rows[response.rows.length - 1]?.id
104-
datelog(
105-
`Processing ${response.rows.length} rows from ${startDocId} to ${endDocId}.`
106-
)
109+
const startDocId = response.docs[0]?._id
110+
const endDocId = response.docs[response.docs.length - 1]?._id
111+
112+
if (response.docs.length > 0) {
113+
datelog(
114+
`Processing ${response.docs.length} rows from ${startDocId} to ${endDocId}.`
115+
)
116+
} else {
117+
datelog(`Queried for new documents after ${afterTime}.`)
118+
}
107119

108120
const newDocs: DbTx[] = []
109121
const newRows: any[][] = []
122+
let lastDocUpdateTime: string | undefined
110123

111-
for (const row of response.rows) {
112-
if (row.doc == null) {
113-
throw new Error(`No doc for ${row.id}`)
114-
}
115-
116-
const { appId, partnerId } = getDocumentIdentifiers(row.id)
124+
for (const doc of response.docs) {
125+
const { appId, partnerId } = getDocumentIdentifiers(doc._id)
117126
const processor = processors[partnerId]
118127

119128
if (processor == null) {
120-
datelog(`Not found ${partnerId} for document ${row.id}`)
121-
newDocs.push({
122-
...row.doc,
123-
indexVersion: -1
124-
})
129+
datelog(`Not found ${partnerId} for document ${doc._id}`)
125130
continue
126131
}
127132

128133
let standardTx: StandardTx
129134
try {
130-
standardTx = processor(row.doc?.rawTx)
135+
standardTx = processor(doc.rawTx)
131136
} catch (error) {
132137
datelog(
133-
`Failed processing ${row.id} with '${partnerId}' processor`,
138+
`Failed processing ${doc._id} with '${partnerId}' processor`,
134139
String(error)
135140
)
136-
newDocs.push({
137-
...row.doc,
138-
indexVersion: -1
139-
})
140141
continue
141142
}
142143

@@ -159,14 +160,18 @@ export async function clickhouseEngine(): Promise<void> {
159160
standardTx.status,
160161
Math.round(standardTx.timestamp),
161162
standardTx.usdValue,
162-
standardTx.indexVersion
163+
config.clickhouseIndexVersion
163164
])
164165

165-
newDocs.push({
166-
_id: row.id,
167-
_rev: row.doc._rev,
168-
...standardTx
169-
})
166+
newDocs.push(
167+
wasDbTx({
168+
_id: doc._id,
169+
_rev: doc._rev,
170+
...standardTx
171+
})
172+
)
173+
174+
lastDocUpdateTime = standardTx.updateTime.toISOString()
170175
}
171176

172177
// Add the standardTx to the clickhouse database
@@ -200,7 +205,11 @@ export async function clickhouseEngine(): Promise<void> {
200205

201206
// We've reached the end of the view index, so we'll continue but with a
202207
// delay so as not to thrash the couchdb unnecessarily.
203-
if (response.rows.length !== PAGE_SIZE) {
208+
if (response.docs.length !== PAGE_SIZE) {
209+
i = 0
210+
if (lastDocUpdateTime != null) {
211+
afterTime = lastDocUpdateTime
212+
}
204213
await snooze(5000)
205214
}
206215
}

src/initDbs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const transactionIndexes: DesignDocumentMap = {
3838
...fieldsToDesign(['status', 'isoDate']),
3939
...fieldsToDesign(['status', 'payoutAmount', 'depositAmount']),
4040
...fieldsToDesign(['status', 'payoutCurrency', 'isoDate']),
41+
...fieldsToDesign(['status', 'updateTime']),
4142
...fieldsToDesign(['status', 'usdValue']),
4243
...fieldsToDesign(['status', 'usdValue', 'timestamp']),
4344
...fieldsToDesign(['usdValue']),

src/partners/banxa.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export function processBanxaTx(rawTx: unknown): StandardTx {
286286
payoutCurrency: outputCurrency,
287287
payoutAmount: outputAmount,
288288
timestamp,
289-
indexVersion: config.clickhouseIndexVersion,
289+
updateTime: new Date(),
290290
isoDate,
291291
usdValue: -1,
292292
rawTx

src/partners/bitaccess.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export function processBitaccessTx(rawTx: unknown): StandardTx {
154154
payoutCurrency: tx.withdrawal_currency.toUpperCase(),
155155
payoutAmount: tx.withdrawal_amount,
156156
timestamp,
157-
indexVersion: config.clickhouseIndexVersion,
157+
updateTime: new Date(),
158158
isoDate: tx.updated_at,
159159
usdValue: -1,
160160
rawTx

src/partners/bitrefill.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export function processBitrefillTx(rawTx: unknown): StandardTx {
161161
payoutCurrency: tx.currency,
162162
payoutAmount: parseInt(tx.value),
163163
timestamp,
164-
indexVersion: config.clickhouseIndexVersion,
164+
updateTime: new Date(),
165165
isoDate: new Date(tx.invoiceTime).toISOString(),
166166
usdValue: tx.usdPrice,
167167
rawTx

src/partners/bitsofgold.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export function processBitsOfGoldTx(rawTx: unknown): StandardTx {
130130
payoutCurrency,
131131
payoutAmount,
132132
timestamp: timestamp / 1000,
133-
indexVersion: config.clickhouseIndexVersion,
133+
updateTime: new Date(),
134134
isoDate: date.toISOString(),
135135
usdValue: -1,
136136
rawTx

src/partners/bity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export function processBityTx(rawTx: unknown): StandardTx {
187187
payoutCurrency: tx.output.currency.toUpperCase(),
188188
payoutAmount: safeParseFloat(tx.output.amount),
189189
timestamp: Date.parse(tx.timestamp_created.concat('Z')) / 1000,
190-
indexVersion: config.clickhouseIndexVersion,
190+
updateTime: new Date(),
191191
isoDate: new Date(tx.timestamp_created.concat('Z')).toISOString(),
192192
usdValue: -1,
193193
rawTx

src/partners/changehero.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export function processChangeHeroTx(rawTx: unknown): StandardTx {
178178
payoutCurrency: tx.currencyTo.toUpperCase(),
179179
payoutAmount: safeParseFloat(tx.amountTo),
180180
timestamp: tx.createdAt,
181-
indexVersion: config.clickhouseIndexVersion,
181+
updateTime: new Date(),
182182
isoDate: smartIsoDateFromTimestamp(tx.createdAt).isoDate,
183183
usdValue: -1,
184184
rawTx

src/partners/changelly.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export function processChangellyTx(rawTx: unknown): StandardTx {
190190
payoutCurrency: tx.currencyTo.toUpperCase(),
191191
payoutAmount: safeParseFloat(tx.amountTo),
192192
timestamp: tx.createdAt,
193-
indexVersion: config.clickhouseIndexVersion,
193+
updateTime: new Date(),
194194
isoDate: new Date(tx.createdAt * 1000).toISOString(),
195195
usdValue: -1,
196196
rawTx

src/partners/changenow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export function processChangeNowTx(rawTx: unknown): StandardTx {
161161
payoutCurrency: tx.payout.currency.toUpperCase(),
162162
payoutAmount: tx.payout.amount ?? tx.payout.expectedAmount ?? 0,
163163
timestamp,
164-
indexVersion: config.clickhouseIndexVersion,
164+
updateTime: new Date(),
165165
isoDate: date.toISOString(),
166166
usdValue: -1,
167167
rawTx

0 commit comments

Comments
 (0)