Skip to content

Commit

Permalink
Merge pull request #1022 from multiversx/SERVICES-1833-add-query-for-…
Browse files Browse the repository at this point in the history
…top-holders-for-list-of-collections

Update top holders queries
  • Loading branch information
danielailie authored Sep 13, 2023
2 parents dca7c50 + a5471d3 commit 05fc0d2
Showing 1 changed file with 44 additions and 83 deletions.
127 changes: 44 additions & 83 deletions src/common/services/mx-communication/mx-elastic.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -276,32 +277,19 @@ export class MxElasticService {
}

async getHoldersCount(): Promise<number> {
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,
Expand All @@ -311,27 +299,20 @@ export class MxElasticService {
}

async getHoldersCountForCollection(collectionIdentifier: string): Promise<number> {
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,
Expand All @@ -341,32 +322,19 @@ export class MxElasticService {
}

async getTopHoldersCountForCollections(): Promise<HoldersCount[]> {
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,
Expand All @@ -382,27 +350,20 @@ export class MxElasticService {
}

async getTopHoldersCountForCollection(collectionIdentifier: string): Promise<HoldersCount[]> {
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,
Expand Down

0 comments on commit 05fc0d2

Please sign in to comment.