Skip to content

Commit

Permalink
fix: task 521; long response time of filters
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulgalimov committed May 17, 2024
1 parent 3cb3a6a commit 58b8367
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "unique-marketplace-backend",
"version": "3.0.242",
"version": "3.0.243",
"description": "Backend project for unique marketplace",
"author": "Unique Network",
"private": true,
Expand Down
7 changes: 7 additions & 0 deletions packages/common/modules/config/load.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export function loadFileStorageConfig(): FileStorageConfig {
};
}

const createAppCacheConfig = () => {
return {
offersAttributesTtlMs: +process.env.OFFERS_ATTRIBUTES_TTL_MS || 3_600_000,
};
};

export const loadConfig = (): Config => ({
environment: process.env.ENVIRONMENT || 'development',
port: parseInt(process.env.PORT, 10) || 3000,
Expand All @@ -30,6 +36,7 @@ export const loadConfig = (): Config => ({
signatureKey: process.env.SIGNATURE_KEY || '', // Sign and Verify key (sign the following data)

cache: createCacheConfig(process.env),
appCache: createAppCacheConfig(),

releaseVersion: process.env.npm_package_version,

Expand Down
5 changes: 5 additions & 0 deletions packages/common/modules/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export interface FileStorageConfig {
secretKey: string;
}

export type AppCacheConfig = {
offersAttributesTtlMs: number;
};

export type Config = {
environment: string;
port: number;
Expand All @@ -16,6 +20,7 @@ export type Config = {

market: MarketSwaggerOptions;
cache: CacheConfig;
appCache: AppCacheConfig;

signer?: SignerConfig;
signatureKey?: string;
Expand Down
37 changes: 28 additions & 9 deletions packages/market/src/offers/view-offers.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BadRequestException, HttpStatus, Injectable, Logger } from '@nestjs/common';
import { BadRequestException, CACHE_MANAGER, HttpStatus, Inject, Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { OfferEntity, ViewOffers } from '@app/common/modules/database';
import { DataSource, Repository, SelectQueryBuilder, ValueTransformer } from 'typeorm';
Expand All @@ -11,6 +11,9 @@ import { GetOneFilter, SortingOrder, SortingParameter } from './interfaces/offer
import { HelperService } from '@app/common/src/lib/helper.service';
import { PaginationRouting } from '@app/common/src/lib/base.constants';
import { SortingOfferRequest } from '@app/common/modules/types/requests';
import { Cache } from 'cache-manager';
import { ConfigService } from '@nestjs/config';
import { AppCacheConfig } from '@app/common/modules/config/types';

const offersMapping = {
priceRaw: 'price_raw',
Expand All @@ -36,13 +39,18 @@ const priceTransformer: ValueTransformer = {
export class ViewOffersService {
private logger = new Logger(ViewOffersService.name);
private readonly offersSorts: Record<string, string>;
private appCache: AppCacheConfig;

constructor(
configService: ConfigService,
private connection: DataSource,
@InjectRepository(ViewOffers) private viewOffersRepository: Repository<ViewOffers>,
private readonly bundleService: BundleService,
@Inject(CACHE_MANAGER)
private readonly cache: Cache,
) {
this.offersSorts = this.prepareMapping(offersMapping, connection.getMetadata(OfferEntity).columns);
this.appCache = configService.getOrThrow<AppCacheConfig>('appCache');
}

prepareMapping = (input: Record<string, string>, columnMetadata: ColumnMetadata[]): Record<string, string> => {
Expand Down Expand Up @@ -167,14 +175,25 @@ export class ViewOffersService {
}

public async getAttributes(): Promise<any> {
const queryFilter = this.viewOffersRepository.createQueryBuilder('view_offers');
const counts = await this.byAttributesCount(queryFilter);
const attributes = await this.byAttributes(queryFilter).getRawMany();
const attributesParsed = this.parseAttributes(attributes);
return {
counts,
attributes: attributesParsed,
};
let response = await this.cache.get('offers-attributes');

if (response) {
return response;
} else {
const queryFilter = this.viewOffersRepository.createQueryBuilder('view_offers');
const counts = await this.byAttributesCount(queryFilter);
const attributes = await this.byAttributes(queryFilter).getRawMany();
const attributesParsed = this.parseAttributes(attributes);

response = {
counts,
attributes: attributesParsed,
};

await this.cache.set('offers-attributes', response, this.appCache.offersAttributesTtlMs);

return response;
}
}

public async filter(offersFilter: OffersFilter, pagination: PaginationRouting, sort: SortingOfferRequest): Promise<any> {
Expand Down

0 comments on commit 58b8367

Please sign in to comment.