Skip to content

Commit 0bbe190

Browse files
Reset transferLast24h to 0 for accounts that have no transactions in the last 24h (#1355)
* reset transferLast24h to 0 for accounts that have no transactions in the last 24h * fix lint errors --------- Co-authored-by: cfaur09 <[email protected]>
1 parent 9ff2fa6 commit 0bbe190

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

src/common/indexer/elastic/elastic.indexer.service.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ export class ElasticIndexerService implements IndexerInterface {
404404
return await this.elasticService.getList('operations', 'hash', elasticQuery);
405405
}
406406

407-
async getAccounts(queryPagination: QueryPagination, filter: AccountQueryOptions): Promise<any[]> {
407+
async getAccounts(queryPagination: QueryPagination, filter: AccountQueryOptions, fields?: string[]): Promise<any[]> {
408408
let elasticQuery = this.indexerHelper.buildAccountFilterQuery(filter);
409409
const sortOrder: ElasticSortOrder = !filter.order || filter.order === SortOrder.desc ? ElasticSortOrder.descending : ElasticSortOrder.ascending;
410410
const sort: AccountSort = filter.sort ?? AccountSort.balance;
@@ -429,6 +429,10 @@ export class ElasticIndexerService implements IndexerInterface {
429429

430430
elasticQuery = elasticQuery.withPagination(queryPagination);
431431

432+
if (fields && fields.length > 0) {
433+
elasticQuery = elasticQuery.withFields(fields);
434+
}
435+
432436
return await this.elasticService.getList('accounts', 'address', elasticQuery);
433437
}
434438

@@ -976,6 +980,16 @@ export class ElasticIndexerService implements IndexerInterface {
976980
return await this.elasticService.getCount('scdeploys', elasticQuery);
977981
}
978982

983+
async getAddressesWithTransfersLast24h(): Promise<string[]> {
984+
const elasticQuery = ElasticQuery.create()
985+
.withFields(['address'])
986+
.withPagination({ from: 0, size: 10000 })
987+
.withMustExistCondition('api_transfersLast24h');
988+
989+
const result = await this.elasticService.getList('accounts', 'address', elasticQuery);
990+
return result.map(x => x.address);
991+
}
992+
979993
async getEvents(pagination: QueryPagination, filter: EventsFilter): Promise<Events[]> {
980994
const elasticQuery = this.indexerHelper.buildEventsFilter(filter)
981995
.withPagination(pagination)

src/common/indexer/indexer.interface.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export interface IndexerInterface {
104104

105105
getAccount(address: string): Promise<Account>
106106

107-
getAccounts(queryPagination: QueryPagination, filter: AccountQueryOptions): Promise<Account[]>
107+
getAccounts(queryPagination: QueryPagination, filter: AccountQueryOptions, fields?: string[]): Promise<Account[]>
108108

109109
getAccountDeploys(pagination: QueryPagination, address: string): Promise<ScDeploy[]>
110110

@@ -188,6 +188,8 @@ export interface IndexerInterface {
188188

189189
getApplicationCount(filter: ApplicationFilter): Promise<number>
190190

191+
getAddressesWithTransfersLast24h(): Promise<string[]>
192+
191193
getEvents(pagination: QueryPagination, filter: EventsFilter): Promise<Events[]>
192194

193195
getEvent(txHash: string): Promise<Events>

src/common/indexer/indexer.service.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ export class IndexerService implements IndexerInterface {
227227
}
228228

229229
@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
230-
async getAccounts(queryPagination: QueryPagination, filter: AccountQueryOptions): Promise<Account[]> {
231-
return await this.indexerInterface.getAccounts(queryPagination, filter);
230+
async getAccounts(queryPagination: QueryPagination, filter: AccountQueryOptions, fields?: string[]): Promise<Account[]> {
231+
return await this.indexerInterface.getAccounts(queryPagination, filter, fields);
232232
}
233233

234234
@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
@@ -452,6 +452,10 @@ export class IndexerService implements IndexerInterface {
452452
}
453453

454454
@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
455+
async getAddressesWithTransfersLast24h(): Promise<string[]> {
456+
return await this.indexerInterface.getAddressesWithTransfersLast24h();
457+
}
458+
455459
async getEvents(pagination: QueryPagination, filter: EventsFilter): Promise<Events[]> {
456460
return await this.indexerInterface.getEvents(pagination, filter);
457461
}

src/crons/cache.warmer/cache.warmer.service.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { PoolService } from "src/endpoints/pool/pool.service";
3333
import * as JsonDiff from "json-diff";
3434
import { QueryPagination } from "src/common/entities/query.pagination";
3535
import { StakeService } from "src/endpoints/stake/stake.service";
36+
import { ApplicationMostUsed } from "src/endpoints/accounts/entities/application.most.used";
3637

3738
@Injectable()
3839
export class CacheWarmerService {
@@ -377,21 +378,28 @@ export class CacheWarmerService {
377378
async handleUpdateAccountTransfersLast24h() {
378379
const batchSize = 100;
379380
const mostUsed = await this.accountService.getApplicationMostUsedRaw();
381+
const mostUsedIndexedAccounts = await this.indexerService.getAddressesWithTransfersLast24h();
380382

381-
const batches = BatchUtils.splitArrayIntoChunks(mostUsed, batchSize);
383+
const allAddressesToUpdate = [...mostUsed.map(item => item.address), ...mostUsedIndexedAccounts].distinct();
384+
const mostUsedDictionary = mostUsed.toRecord<ApplicationMostUsed>(item => item.address);
385+
386+
const batches = BatchUtils.splitArrayIntoChunks(allAddressesToUpdate, batchSize);
382387
for (const batch of batches) {
383388
const accounts = await this.indexerService.getAccounts(
384389
new QueryPagination({ from: 0, size: batchSize }),
385-
new AccountQueryOptions({ addresses: batch.map(item => item.address) }),
390+
new AccountQueryOptions({ addresses: batch }),
391+
['address', 'api_transfersLast24h'],
386392
);
387393

388-
const accountsDictionary = accounts.toRecord<Account>(account => account.address);
394+
const accountsDictionary = accounts.toRecord<Pick<Account, 'address' | 'api_transfersLast24h'>>(account => account.address);
395+
396+
for (const address of batch) {
397+
const account = accountsDictionary[address];
398+
const newTransfersLast24h = mostUsedDictionary[address]?.transfers24H ?? 0;
389399

390-
for (const item of batch) {
391-
const account = accountsDictionary[item.address];
392-
if (account && account.api_transfersLast24h !== item.transfers24H) {
393-
this.logger.log(`Setting transferLast24h to ${item.transfers24H} for account with address '${item.address}'`);
394-
await this.indexerService.setAccountTransfersLast24h(item.address, item.transfers24H);
400+
if (account && account.api_transfersLast24h !== newTransfersLast24h) {
401+
this.logger.log(`Setting transferLast24h to ${newTransfersLast24h} for account with address '${address}'`);
402+
await this.indexerService.setAccountTransfersLast24h(address, newTransfersLast24h);
395403
}
396404
}
397405
}

0 commit comments

Comments
 (0)