From a5471d34571251d32c33e4fe03aa0bf26f55bca5 Mon Sep 17 00:00:00 2001 From: danielailie Date: Wed, 13 Sep 2023 11:06:11 +0300 Subject: [PATCH] Upgrade holders queries --- .../mx-communication/mx-elastic.service.ts | 127 ++++++------------ 1 file changed, 44 insertions(+), 83 deletions(-) diff --git a/src/common/services/mx-communication/mx-elastic.service.ts b/src/common/services/mx-communication/mx-elastic.service.ts index 7617bcf8c..5fe2f22bb 100644 --- a/src/common/services/mx-communication/mx-elastic.service.ts +++ b/src/common/services/mx-communication/mx-elastic.service.ts @@ -3,11 +3,12 @@ import { HitResponse, SearchResponse } from './models/elastic-search'; import { ApiService } from './api.service'; import { PerformanceProfiler } from 'src/modules/metrics/performance.profiler'; import { MetricsCollector } from 'src/modules/metrics/metrics.collector'; -import { ElasticQuery } from '@multiversx/sdk-nestjs-elastic'; +import { ElasticQuery, QueryType } from '@multiversx/sdk-nestjs-elastic'; import { ElasticMetricType } from '@multiversx/sdk-nestjs-monitoring'; import { ApiSettings } from './models/api-settings'; import { constants } from 'src/config'; import { HoldersCount } from 'src/modules/analytics/models/general-stats.model'; +import { NftTypeEnum } from 'src/modules/assets/models'; export interface AddressTransactionCount { contractAddress: string; transactionCount: number; @@ -276,32 +277,19 @@ export class MxElasticService { } async getHoldersCount(): Promise { - const query = { - size: 0, - query: { - bool: { - should: [ - { - match: { - type: 'NonFungibleESDT', - }, + const query = ElasticQuery.create() + .withPagination({ from: 0, size: 0 }) + .withShouldCondition([NftTypeEnum.NonFungibleESDT, NftTypeEnum.SemiFungibleESDT].map((type) => QueryType.Match('type', type))) + .withExtra({ + aggs: { + unique_addresses: { + cardinality: { + field: 'address', }, - { - match: { - type: 'SemiFungibleESDT', - }, - }, - ], - }, - }, - aggs: { - unique_addresses: { - cardinality: { - field: 'address', }, }, - }, - }; + }) + .toJson(); const resultRaw = await this.apiService.post(`${this.url}/accountsesdt/_search`, query, { timeout: 120000, @@ -311,27 +299,20 @@ export class MxElasticService { } async getHoldersCountForCollection(collectionIdentifier: string): Promise { - const query = { - size: 0, - query: { - bool: { - should: [ - { - match: { - token: `${collectionIdentifier}`, - }, + const query = ElasticQuery.create() + .withPagination({ from: 0, size: 0 }) + .withShouldCondition([NftTypeEnum.NonFungibleESDT, NftTypeEnum.SemiFungibleESDT].map((type) => QueryType.Match('type', type))) + .withShouldCondition(QueryType.Match('token', collectionIdentifier)) + .withExtra({ + aggs: { + unique_addresses: { + cardinality: { + field: 'address', }, - ], - }, - }, - aggs: { - unique_addresses: { - cardinality: { - field: 'address', }, }, - }, - }; + }) + .toJson(); const resultRaw = await this.apiService.post(`${this.url}/accountsesdt/_search`, query, { timeout: 120000, @@ -341,32 +322,19 @@ export class MxElasticService { } async getTopHoldersCountForCollections(): Promise { - const query = { - size: 0, - query: { - bool: { - should: [ - { - match: { - type: 'NonFungibleESDT', - }, + const query = ElasticQuery.create() + .withPagination({ from: 0, size: 0 }) + .withShouldCondition([NftTypeEnum.NonFungibleESDT, NftTypeEnum.SemiFungibleESDT].map((type) => QueryType.Match('type', type))) + .withExtra({ + aggs: { + first_by_address: { + terms: { + field: 'address', }, - { - match: { - type: 'SemiFungibleESDT', - }, - }, - ], - }, - }, - aggs: { - first_by_address: { - terms: { - field: 'address', }, }, - }, - }; + }) + .toJson(); const resultRaw = await this.apiService.post(`${this.url}/accountsesdt/_search`, query, { timeout: 120000, @@ -382,27 +350,20 @@ export class MxElasticService { } async getTopHoldersCountForCollection(collectionIdentifier: string): Promise { - const query = { - size: 0, - query: { - bool: { - should: [ - { - match: { - token: `${collectionIdentifier}`, - }, + const query = ElasticQuery.create() + .withPagination({ from: 0, size: 0 }) + .withShouldCondition([NftTypeEnum.NonFungibleESDT, NftTypeEnum.SemiFungibleESDT].map((type) => QueryType.Match('type', type))) + .withShouldCondition(QueryType.Match('token', collectionIdentifier)) + .withExtra({ + aggs: { + first_by_address: { + terms: { + field: 'address', }, - ], - }, - }, - aggs: { - first_by_address: { - terms: { - field: 'address', }, }, - }, - }; + }) + .toJson(); const resultRaw = await this.apiService.post(`${this.url}/accountsesdt/_search`, query, { timeout: 120000,