-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.ts
78 lines (69 loc) · 2.2 KB
/
worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {
AccountIndexerConfigWithMeta,
AccountIndexerRequestArgs,
EntityDateRangeResponse,
IndexerDomainContext,
IndexerWorkerDomainI,
} from '../../../services/indexer/src/types.js'
import { Blockchain, ParsedEntity } from '../../../types.js'
import {
AccountTimeSeriesStats,
AccountStatsFilters,
AccountStats,
} from '../../stats/index.js'
import { WorkerKind } from '../../workers.js'
import { importBlockchainWorkerIndexerDomain } from '../common.js'
/**
* Describes an indexer worker domain, capable of stats processing.
*/
export type IndexerWorkerDomainWithStats = {
updateStats(account: string, now: number): Promise<void>
getTimeSeriesStats(
account: string,
type: string,
filters: AccountStatsFilters,
): Promise<AccountTimeSeriesStats>
getStats(account: string): Promise<AccountStats>
}
export interface BlockchainIndexerWorkerI<T extends ParsedEntity<unknown>> {
onEntityDateRange(response: EntityDateRangeResponse<T>): Promise<void>
}
/**
* Describes an indexer worker domain, implements some common methods for any instance
*/
export abstract class IndexerWorkerDomain<
T extends ParsedEntity<unknown> = ParsedEntity<unknown>,
> implements IndexerWorkerDomainI
{
protected instance!: number
protected blockchainInstances!: Record<
Blockchain,
BlockchainIndexerWorkerI<T>
>
constructor(protected context: IndexerDomainContext) {
this.instance = Number(context.instanceName.split('-')[1])
}
async init(): Promise<void> {
// @note: Preload supported blockchains
this.blockchainInstances = await importBlockchainWorkerIndexerDomain(
WorkerKind.Indexer,
this.context.supportedBlockchains,
this.context,
this,
)
}
abstract onNewAccount(
config: AccountIndexerConfigWithMeta<unknown> | AccountIndexerRequestArgs,
): Promise<void>
async onEntityDateRange(response: EntityDateRangeResponse<T>): Promise<void> {
const { blockchainId, type, account, startDate, endDate } = response
console.log(
`${blockchainId} ${type} | processing entities`,
account,
startDate,
endDate,
)
const worker = this.blockchainInstances[blockchainId]
await worker.onEntityDateRange(response)
}
}