Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10-6 [BE] [검색리스트] pagination 범위 초과시 error 처리 #63

Merged
merged 14 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions backend/src/search/search.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Controller, Get, Query, UsePipes, ValidationPipe } from '@nestjs/common';
import { Controller, Get, NotFoundException, Query, UsePipes, ValidationPipe } from '@nestjs/common';
import { SearchService } from './search.service';

import { KeywordValidationPipe } from './pipe/search.pipe';
import { SearchDto } from './pipe/search.dto';
import { SearchTotalHits } from '@elastic/elasticsearch/lib/api/types';
import { PaperInfoExtended } from './entities/crossRef.entity';
import { CROSSREF_CACHE_QUEUE } from 'src/util';
import { Interval } from '@nestjs/schedule';
import { RankingService } from 'src/ranking/ranking.service';
Expand All @@ -18,7 +19,9 @@ export class SearchController {
if (elasticDataCount > 0) {
return elastic.hits.hits.map((paper) => paper._source);
}
const { items, totalItems } = await this.searchService.getCrossRefAutoCompleteData(keyword);

const selects = ['title', 'author', 'DOI'];
const { items, totalItems } = await this.searchService.getCrossRefData(keyword, 5, 1, selects);
const papers = this.searchService.parseCrossRefData(items);
this.searchService.crawlAllCrossRefData(keyword, totalItems, 1000);
return papers;
Expand All @@ -28,14 +31,22 @@ export class SearchController {
@UsePipes(new ValidationPipe({ transform: true }))
async getPapers(@Query('keyword', KeywordValidationPipe) keyword: string, @Query() query: SearchDto) {
const { rows, page } = query;
const { items, totalItems } = await this.searchService.getCrossRefData(keyword, rows, page);
const papers = this.searchService.parseCrossRefData(items);
const selects = ['title', 'author', 'created', 'is-referenced-by-count', 'references-count', 'DOI'];
const { items, totalItems } = await this.searchService.getCrossRefData(keyword, rows, page, selects);

const totalPages = Math.ceil(totalItems / rows);
if (page > totalPages) {
throw new NotFoundException(`page(${page})는 ${totalPages} 보다 클 수 없습니다.`);
}

const papers = this.searchService.parseCrossRefData<PaperInfoExtended>(items);
this.rankingService.insertRedis(keyword);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트도 추가해주시면 좋겠네요.

return {
papers,
pageInfo: {
totalItems,
totalPages: Math.ceil(totalItems / rows),
totalPages,
},
};
}
Expand Down
7 changes: 6 additions & 1 deletion backend/src/search/search.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import { ScheduleModule } from '@nestjs/schedule';
import { RankingService } from 'src/ranking/ranking.service';
@Module({
imports: [
HttpModule,
HttpModule.register({
timeout: 20000,
headers: {
'User-Agent': `Axios/1.1.3(mailto:${process.env.MAIL_TO})`,
},
}),
ElasticsearchModule.registerAsync({
useFactory: () => ({
node: process.env.ELASTIC_HOST,
Expand Down
44 changes: 18 additions & 26 deletions backend/src/search/search.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, RequestTimeoutException } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { CrossRefResponse, CrossRefItem, PaperInfoExtended, PaperInfo } from './entities/crossRef.entity';
import { CROSSREF_API_URL, CROSSREF_CACHE_QUEUE } from '../util';
Expand All @@ -8,22 +8,12 @@ import { SearchHit } from '@elastic/elasticsearch/lib/api/types';
@Injectable()
export class SearchService {
constructor(private readonly httpService: HttpService, private readonly esService: ElasticsearchService) {}
async getCrossRefAutoCompleteData(keyword: string) {
const crossRefdata = await this.httpService.axiosRef.get<CrossRefResponse>(CROSSREF_API_URL(keyword));
const items = crossRefdata.data.message.items;
const totalItems = crossRefdata.data.message['total-results'];
return { items, totalItems };
}

async getCrossRefData(keyword: string, rows: number, page: number) {
const crossRefdata = await this.httpService.axiosRef.get<CrossRefResponse>(
CROSSREF_API_URL(
keyword,
rows,
['title', 'author', 'created', 'is-referenced-by-count', 'references-count', 'DOI'],
page,
),
);
async getCrossRefData(keyword: string, rows: number, page: number, selects?: string[]) {
const crossRefdata = await this.httpService.axiosRef
.get<CrossRefResponse>(CROSSREF_API_URL(keyword, rows, page, selects))
.catch((err) => {
throw new RequestTimeoutException(err.message);
});
const items = crossRefdata.data.message.items;
const totalItems = crossRefdata.data.message['total-results'];
return { items, totalItems };
Expand All @@ -35,17 +25,19 @@ export class SearchService {
.fill(0)
.map((v, i) => {
CROSSREF_CACHE_QUEUE.push(
CROSSREF_API_URL(
keyword,
rows,
['title', 'author', 'created', 'is-referenced-by-count', 'references-count', 'DOI'],
i + 1,
),
CROSSREF_API_URL(keyword, rows, i + 1, [
'title',
'author',
'created',
'is-referenced-by-count',
'references-count',
'DOI',
]),
);
}),
);
}
parseCrossRefData(items: CrossRefItem[]) {
parseCrossRefData<T extends PaperInfo>(items: CrossRefItem[]) {
return items
.map((item) => {
const paperInfo = new PaperInfoExtended();
Expand All @@ -59,9 +51,9 @@ export class SearchService {
paperInfo.publishedAt = item.created?.['date-time'];
paperInfo.citations = item['is-referenced-by-count'];
paperInfo.references = item['references-count'];
return paperInfo;
return paperInfo as T;
})
.filter((info) => info.title || info.authors?.length > 0);
.filter((info) => info.title);
}
async putElasticSearch(paper: PaperInfoExtended) {
return await this.esService.index({
Expand Down
4 changes: 2 additions & 2 deletions backend/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const CROSSREF_API_URL = (keyword: string, rows = 5, selects: string[] = ['author', 'title', 'DOI'], page = 1) =>
export const CROSSREF_API_URL = (keyword: string, rows = 5, page = 1, selects: string[] = ['author', 'title', 'DOI']) =>
`https://api.crossref.org/works?query=${keyword}&rows=${rows}&select=${selects.join(',')}&offset=${
rows * (page - 1)
}`;
}&mailto=${process.env.MAIL_TO}&sort=is-referenced-by-count`;
export const CROSSREF_CACHE_QUEUE = [];