11import { createClient } from '@clickhouse/client'
22import nano from 'nano'
33
4+ import { clientConfig } from './bin/configure'
45import { config } from './config'
56import { initDbs } from './initDbs'
67import { processBanxaTx } from './partners/banxa'
@@ -35,7 +36,7 @@ import { processThorchainTx } from './partners/thorchain'
3536import { processTransakTx } from './partners/transak'
3637import { processWyreTx } from './partners/wyre'
3738import { processXanpoolTx } from './partners/xanpool'
38- import { DbTx , StandardTx } from './types'
39+ import { DbTx , StandardTx , wasDbTx } from './types'
3940import { 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 }
0 commit comments