Skip to content

Commit

Permalink
Merge pull request #1049 from multiversx/ExtractElasticConstants
Browse files Browse the repository at this point in the history
Extract elastic constants
  • Loading branch information
danielailie authored Oct 26, 2023
2 parents eef4e03 + e9b08a7 commit 092d912
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 102 deletions.
5 changes: 3 additions & 2 deletions src/crons/elastic.updater/nsfw-queries.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { NftTypeEnum } from 'src/modules/assets/models';
import { ElasticQuery, QueryType } from '@multiversx/sdk-nestjs-elastic';
import { constants } from 'src/config';
import { ELASTIC_NFT_NSFW } from 'src/utils/constants';

export const getNsfwNotMarkedQuery = ElasticQuery.create()
.withMustNotExistCondition('nft_nsfw_mark')
.withMustNotExistCondition(ELASTIC_NFT_NSFW)
.withMustExistCondition('identifier')
.withMustMultiShouldCondition([NftTypeEnum.NonFungibleESDT, NftTypeEnum.SemiFungibleESDT], (type) => QueryType.Match('type', type))
.withMustCondition(QueryType.Nested('data', { 'data.nonEmptyURIs': true }))
.withPagination({ from: 0, size: constants.elasticMaxBatch });

export const getNsfwMarkedQuery = ElasticQuery.create()
.withFields(['nft_nsfw_mark'])
.withFields([ELASTIC_NFT_NSFW])
.withMustExistCondition('identifier')
.withMustMultiShouldCondition([NftTypeEnum.NonFungibleESDT, NftTypeEnum.SemiFungibleESDT], (type) => QueryType.Match('type', type))
.withMustCondition(QueryType.Nested('data', { 'data.nonEmptyURIs': true }))
Expand Down
15 changes: 8 additions & 7 deletions src/crons/elastic.updater/nsfw.updater.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CacheEventsPublisherService } from 'src/modules/rabbitmq/cache-invalida
import { ChangedEvent, CacheEventTypeEnum } from 'src/modules/rabbitmq/cache-invalidation/events/changed.event';
import { getNsfwMarkedQuery, getNsfwNotMarkedQuery } from './nsfw-queries';
import { constants } from 'src/config';
import { ELASTIC_NFT_NSFW, ELASTIC_TOKENS_INDEX } from 'src/utils/constants';

type NsfwType = {
identifier: string;
Expand All @@ -24,7 +25,7 @@ export class NsfwUpdaterService {
) {}

public async cleanReindexing() {
await this.elasticService.getScrollableList('tokens', 'identifier', getNsfwNotMarkedQuery, async (items) => {
await this.elasticService.getScrollableList(ELASTIC_TOKENS_INDEX, 'identifier', getNsfwNotMarkedQuery, async (items) => {
const nsfwItems = items.map((item) => ({
identifier: item.identifier,
nsfw: item.nft_nsfw_mark,
Expand All @@ -35,7 +36,7 @@ export class NsfwUpdaterService {
}

public async validateNsfwFlags() {
await this.elasticService.getScrollableList('tokens', 'identifier', getNsfwMarkedQuery, async (items) => {
await this.elasticService.getScrollableList(ELASTIC_TOKENS_INDEX, 'identifier', getNsfwMarkedQuery, async (items) => {
const nsfwItems = items.map((item) => ({
identifier: item.identifier,
nsfw: item.nft_nsfw_mark,
Expand All @@ -46,7 +47,7 @@ export class NsfwUpdaterService {
}

public async updateNsfwWhereNone() {
await this.elasticService.getScrollableList('tokens', 'identifier', getNsfwNotMarkedQuery, async (items) => {
await this.elasticService.getScrollableList(ELASTIC_TOKENS_INDEX, 'identifier', getNsfwNotMarkedQuery, async (items) => {
const nsfwItems = items.map((item) => ({
identifier: item.identifier,
nsfw: item.nft_nsfw_mark,
Expand Down Expand Up @@ -118,9 +119,9 @@ export class NsfwUpdaterService {
try {
this.logger.log(`Setting nsfw for '${identifier}' with value ${nsfw}`);
await this.elasticService.setCustomValue(
'tokens',
ELASTIC_TOKENS_INDEX,
identifier,
this.elasticService.buildUpdateBody<number>('nft_nsfw_mark', nsfw),
this.elasticService.buildUpdateBody<number>(ELASTIC_NFT_NSFW, nsfw),
'?retry_on_conflict=2',
);
} catch (error) {
Expand All @@ -136,7 +137,7 @@ export class NsfwUpdaterService {
try {
if (items && items.length > 0) {
this.logger.log(`Updating NSFW flag`);
await this.elasticService.bulkRequest('tokens', this.buildNsfwBulkUpdate(items));
await this.elasticService.bulkRequest(ELASTIC_TOKENS_INDEX, this.buildNsfwBulkUpdate(items));
}
} catch (error) {
this.logger.error('Unexpected error when updating nsfw with bulk request', {
Expand All @@ -149,7 +150,7 @@ export class NsfwUpdaterService {
private buildNsfwBulkUpdate(items: { identifier: string; nsfw: number }[]): string[] {
let updates: string[] = [];
items.forEach((r) => {
updates.push(this.buildBulkUpdate('tokens', r.identifier, 'nft_nsfw_mark', parseFloat(r.nsfw.toString())));
updates.push(this.buildBulkUpdate(ELASTIC_TOKENS_INDEX, r.identifier, ELASTIC_NFT_NSFW, parseFloat(r.nsfw.toString())));
});
return updates;
}
Expand Down
3 changes: 2 additions & 1 deletion src/crons/elastic.updater/rarity.updater.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { generateCacheKeyFromParams } from 'src/utils/generate-cache-key';
import { NftRarityElasticService } from 'src/modules/nft-rarity/nft-rarity.elastic.service';
import { RedisCacheService } from '@multiversx/sdk-nestjs-cache';
import { Locker } from '@multiversx/sdk-nestjs-common';
import { ELASTIC_TOKENS_INDEX } from 'src/utils/constants';

@Injectable()
export class RarityUpdaterService {
Expand Down Expand Up @@ -33,7 +34,7 @@ export class RarityUpdaterService {

const query = this.nftRarityElasticService.getAllNftsWhereRarityNotComputedFromElasticQuery();

await this.elasticService.getScrollableList('tokens', 'token', query, async (items) => {
await this.elasticService.getScrollableList(ELASTIC_TOKENS_INDEX, 'token', query, async (items) => {
const collections = [...new Set(items.map((i) => i.token))];
collectionsToUpdate = collectionsToUpdate.concat(collections.filter((c) => collectionsToUpdate.indexOf(c) === -1));
if (collectionsToUpdate.length >= maxCollectionsToUpdate) {
Expand Down
5 changes: 3 additions & 2 deletions src/crons/elastic.updater/traits.updater.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getCollectionsWhereTraitsFlagNotSetFromElasticQuery,
getCollectionsWithTraitSummaryFromElasticQuery,
} from 'src/modules/nft-traits/nft-traits.elastic.queries';
import { ELASTIC_TOKENS_INDEX } from 'src/utils/constants';

@Injectable()
export class TraitsUpdaterService {
Expand All @@ -30,7 +31,7 @@ export class TraitsUpdaterService {
const lastIndex = await this.getLastValidatedCollectionIndex();
let collections: string[] = [];

await this.elasticService.getScrollableList('tokens', 'token', query, async (items) => {
await this.elasticService.getScrollableList(ELASTIC_TOKENS_INDEX, 'token', query, async (items) => {
collections = collections.concat([...new Set(items.map((i) => i.token))]);
if (collections.length > lastIndex + maxCollectionsToValidate) {
return undefined;
Expand Down Expand Up @@ -65,7 +66,7 @@ export class TraitsUpdaterService {

const query = getCollectionsWhereTraitsFlagNotSetFromElasticQuery(maxCollectionsToUpdate);

await this.elasticService.getScrollableList('tokens', 'token', query, async (items) => {
await this.elasticService.getScrollableList(ELASTIC_TOKENS_INDEX, 'token', query, async (items) => {
const collections = [...new Set(items.map((i) => i.token))];
collectionsToUpdate = collectionsToUpdate.concat(collections.filter((c) => collectionsToUpdate.indexOf(c) === -1));
if (collectionsToUpdate.length >= maxCollectionsToUpdate) {
Expand Down
15 changes: 8 additions & 7 deletions src/modules/admins/flag-nft.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Asset, NftTypeEnum } from '../assets/models';
import { VerifyContentService } from '../assets/verify-content.service';
import { CacheEventsPublisherService } from '../rabbitmq/cache-invalidation/cache-invalidation-publisher/change-events-publisher.service';
import { CacheEventTypeEnum, ChangedEvent } from '../rabbitmq/cache-invalidation/events/changed.event';
import { ELASTIC_NFT_NSFW, ELASTIC_TOKENS_INDEX } from 'src/utils/constants';
type NsfwType = {
identifier: string;
nsfw: any;
Expand Down Expand Up @@ -68,7 +69,7 @@ export class FlagNftService {
.withMustMultiShouldCondition([NftTypeEnum.NonFungibleESDT, NftTypeEnum.SemiFungibleESDT], (type) => QueryType.Match('type', type))
.withMustCondition(QueryType.Nested('data', { 'data.nonEmptyURIs': true }))
.withPagination({ from: 0, size: 10000 });
await this.elasticUpdater.getScrollableList('tokens', 'identifier', query, async (items) => {
await this.elasticUpdater.getScrollableList(ELASTIC_TOKENS_INDEX, 'identifier', query, async (items) => {
const nsfwItems = items.map((item) => ({
identifier: item.identifier,
nsfw: item.nft_nsfw_mark,
Expand Down Expand Up @@ -124,9 +125,9 @@ export class FlagNftService {
}),
);
await this.elasticUpdater.setCustomValue(
'tokens',
ELASTIC_TOKENS_INDEX,
identifier,
this.elasticUpdater.buildUpdateBody<number>('nft_nsfw_mark', savedValue),
this.elasticUpdater.buildUpdateBody<number>(ELASTIC_NFT_NSFW, savedValue),
'?retry_on_conflict=2',
);
}
Expand Down Expand Up @@ -158,9 +159,9 @@ export class FlagNftService {
}),
);
await this.elasticUpdater.setCustomValue(
'tokens',
ELASTIC_TOKENS_INDEX,
identifier,
this.elasticUpdater.buildUpdateBody<number>('nft_nsfw_mark', value.toRounded(2)),
this.elasticUpdater.buildUpdateBody<number>(ELASTIC_NFT_NSFW, value.toRounded(2)),
'?retry_on_conflict=2',
);

Expand Down Expand Up @@ -219,10 +220,10 @@ export class FlagNftService {
async () => {
try {
await this.elasticUpdater.putMappings(
'tokens',
ELASTIC_TOKENS_INDEX,
this.elasticUpdater.buildPutMultipleMappingsBody([
{
key: 'nft_nsfw_mark',
key: ELASTIC_NFT_NSFW,
value: 'float',
},
]),
Expand Down
3 changes: 2 additions & 1 deletion src/modules/assets/assets-getter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { FeaturedService } from '../featured/featured.service';
import { FeaturedCollectionsFilter } from '../featured/Featured-Collections.Filter';
import { FeaturedCollectionTypeEnum } from '../featured/FeatureCollectionType.enum';
import { constants } from 'src/config';
import { ELASTIC_TOKENS_INDEX } from 'src/utils/constants';

@Injectable()
export class AssetsGetterService {
Expand Down Expand Up @@ -200,7 +201,7 @@ export class AssetsGetterService {
if (artistCollections) {
const batch = artistCollections?.collections?.slice(0, 100);
let elasticQuery = this.getCollectionsElasticQuery(batch, offset, size);
let elasticNfts = await this.elasticService.getList('tokens', 'identifier', elasticQuery);
let elasticNfts = await this.elasticService.getList(ELASTIC_TOKENS_INDEX, 'identifier', elasticQuery);

const returnAssets = await this.mapElasticNfts(elasticNfts);
return new CollectionType({
Expand Down
Loading

0 comments on commit 092d912

Please sign in to comment.