Browse Source

feat: limit search with 50 results

Signed-off-by: Anatoly Popov <me@aensidhe.ru>
pull/4641/head
Anatoly Popov 4 weeks ago
parent
commit
63428af3fc
Failed to extract signature
  1. 22
      apps/api/src/services/data-provider/moex/moex.service.ts

22
apps/api/src/services/data-provider/moex/moex.service.ts

@ -153,7 +153,8 @@ async function getDividendsFromMoex({
async function readBatchedResponse<T>( async function readBatchedResponse<T>(
getNextBatch: (start: number) => Promise<Response<T>>, getNextBatch: (start: number) => Promise<Response<T>>,
extractor: (batch: Response<T>) => ResponseData extractor: (batch: Response<T>) => ResponseData,
max_items?: number
): Promise<ResponseData> { ): Promise<ResponseData> {
let batch: ResponseData; let batch: ResponseData;
const wholeResponse: ResponseData = { columns: [], data: [] }; const wholeResponse: ResponseData = { columns: [], data: [] };
@ -174,7 +175,10 @@ async function readBatchedResponse<T>(
} }
wholeResponse.data = [...wholeResponse.data, ...batch.data]; wholeResponse.data = [...wholeResponse.data, ...batch.data];
} while (batch.data.length > 0); } while (
batch.data.length > 0 &&
(!max_items || wholeResponse.data.length <= max_items)
);
return wholeResponse; return wholeResponse;
} }
@ -553,6 +557,7 @@ export class MoexService implements DataProviderInterface {
} }
async search({ query }: GetSearchParams): Promise<LookupResponse> { async search({ query }: GetSearchParams): Promise<LookupResponse> {
const MAX_SEARCH_ITEMS: number = 50;
// MOEX doesn't support search for queries less than 3 symbols // MOEX doesn't support search for queries less than 3 symbols
if (query.length < 3) { if (query.length < 3) {
return { items: [] }; return { items: [] };
@ -567,7 +572,8 @@ export class MoexService implements DataProviderInterface {
params['start'] = x; params['start'] = x;
return await moexClient.security.getSecurities(params); return await moexClient.security.getSecurities(params);
}, },
(x) => x.data.securities (x) => x.data.securities,
MAX_SEARCH_ITEMS
); );
const search = response_data_to_map(searchResponse, 'secid'); const search = response_data_to_map(searchResponse, 'secid');
@ -578,7 +584,7 @@ export class MoexService implements DataProviderInterface {
continue; continue;
} }
const profile = await this.getAssetProfile({ symbol: k }); const profile = await this.getAssetProfile({ symbol: k });
result.items.push({ const lookedup_profile = {
assetClass: profile.assetClass, assetClass: profile.assetClass,
assetSubClass: profile.assetSubClass, assetSubClass: profile.assetSubClass,
currency: profile.currency, currency: profile.currency,
@ -586,7 +592,13 @@ export class MoexService implements DataProviderInterface {
dataSource: this.getName(), dataSource: this.getName(),
name: profile.name, name: profile.name,
symbol: k symbol: k
}); };
if (k === query) {
result.items.unshift(lookedup_profile);
} else {
result.items.push(lookedup_profile);
}
} }
return result; return result;

Loading…
Cancel
Save