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 12 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
12 changes: 10 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest --detectOpenHandles",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
Expand Down Expand Up @@ -70,7 +70,15 @@
"json",
"ts"
],
"rootDir": "src",
"roots": [
"<rootDir>"
],
"modulePaths": [
"<rootDir>"
],
"moduleDirectories": [
"node_modules"
],
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
Expand Down
20 changes: 19 additions & 1 deletion backend/src/search/entities/crossRef.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,32 @@ export class PaperInfo {
title?: string;
authors?: string[];
doi?: string;

constructor(body: PaperInfo) {
this.title = body.title;
this.authors = body.authors;
this.doi = body.doi;
}
}
export class PaperInfoExtended extends PaperInfo {
publishedAt?: string;
citations?: number;
references?: number;

constructor(body: PaperInfoExtended) {
super(body);
this.publishedAt = body.publishedAt;
this.citations = body.citations;
this.references = body.references;
}
}
export class PaperInfoDetail extends PaperInfoExtended {
referenceList?: ReferenceInfo[];

constructor(body: PaperInfoDetail) {
super(body);
this.referenceList = body.referenceList;
}
}
export interface ReferenceInfo {
issn?: string;
Expand All @@ -21,7 +39,7 @@ export interface ReferenceInfo {
'doi-asserted-by'?: string;
'first-page'?: string;
isbn?: string;
doi?: string;
DOI?: string;
component?: string;
'article-title'?: string;
'volume-title'?: string;
Expand Down
15 changes: 11 additions & 4 deletions backend/src/search/search.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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 { AutoCompleteDto, GetPaperDto, SearchDto } from './entities/search.dto';
import { SearchTotalHits } from '@elastic/elasticsearch/lib/api/types';
Expand All @@ -17,7 +17,8 @@ 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.parsePaperInfo);
this.searchService.crawlAllCrossRefData(keyword, totalItems, 1000);
return papers;
Expand All @@ -27,14 +28,20 @@ export class SearchController {
@UsePipes(new ValidationPipe({ transform: true }))
async getPapers(@Query() query: SearchDto) {
const { keyword, rows, page } = query;
const { items, totalItems } = await this.searchService.getCrossRefData(keyword, rows, page);
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(items, this.searchService.parsePaperInfoExtended);
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
91 changes: 43 additions & 48 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,
Expand All @@ -15,22 +15,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 @@ -42,64 +32,69 @@ 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<T extends PaperInfo>(items: CrossRefItem[], parser: (item: CrossRefItem) => T) {
return items.map(parser).filter((info) => info.title || info.authors?.length > 0);
return items.map(parser).filter((info) => info.title);
}
parsePaperInfo = (item: CrossRefItem) => {
const paperInfo = {
const data = {
title: item.title?.[0],
authors: item.author?.reduce((acc, cur) => {
const authorName = `${cur.name ? cur.name : cur.given ? cur.given + ' ' : ''}${cur.family || ''}`;
authorName && acc.push(authorName);
return acc;
}, []),
doi: item.DOI,
} as PaperInfo;
};

return paperInfo;
return new PaperInfo(data);
};
parsePaperInfoExtended = (item: CrossRefItem) => {
const paperInfo = {
const data = {
...this.parsePaperInfo(item),
publishedAt: item.created?.['date-time'],
citations: item['is-referenced-by-count'],
references: item['references-count'],
} as PaperInfoExtended;
};

return paperInfo;
return new PaperInfoExtended(data);
};
parsePaperInfoDetail = (item: CrossRefItem) => {
const referenceList = item['reference'].map((reference) => {
return {
title:
reference['article-title'] ||
reference['journal-title'] ||
reference['series-title'] ||
reference['volume-title'],
doi: reference['doi'],
// TODO: 현재 원하는 정보를 얻기 위해서는 해당 reference에 대한 정보를 crossref에 다시 요청해야함
author: reference['author'],
publishedAt: reference['year'],
citations: 0,
references: 0,
};
});
const paperInfo = {
const referenceList =
item['reference']?.map((reference) => {
return {
key: reference['DOI'] || reference.key || reference.unstructured,
title:
reference['article-title'] ||
reference['journal-title'] ||
reference['series-title'] ||
reference['volume-title'] ||
reference.unstructured,
doi: reference['DOI'],
// TODO: 현재 원하는 정보를 얻기 위해서는 해당 reference에 대한 정보를 crossref에 다시 요청해야함
author: reference['author'],
publishedAt: reference['year'],
citations: 0,
references: 0,
};
}) || [];
const data = {
...this.parsePaperInfoExtended(item),
referenceList,
} as PaperInfoDetail;
};

return paperInfo;
return new PaperInfoDetail(data);
};

async getPaper(doi: string) {
Expand Down
Loading