-
Notifications
You must be signed in to change notification settings - Fork 3
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
Feature/#253 - 가격 상승률 및 하락률 데이터 수집 #260
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
38c158b
✨ feat: 유저 서브네임 생성 로직 구현
xjfcnfw3 faa77af
✨ feat: 멘션 엔티티 구현
xjfcnfw3 8b80865
✨ feat: 특정 유저를 멘션하는 기능 구현
xjfcnfw3 3d82534
Merge branch 'dev-be' into feature/#110
xjfcnfw3 a2c49b0
🐛 fix: 중복된 닉네임 테스터 에러 수정
xjfcnfw3 d0fadf7
✨ feat: 채팅으로 멘션을 진행
xjfcnfw3 ba47d8b
✨ feat: 멘션 연관관계 설정
xjfcnfw3 2bc7699
🐛 fix: 잘못된 검증으로 멘션이 안되는 문제 해결
xjfcnfw3 299d3b7
✨ feat: 채팅스크롤에서 멘션 필드 추가
xjfcnfw3 52f830f
✨ feat: 닉네임과 서브네임으로 유저 닉네임 검색
xjfcnfw3 7621b73
✨ feat: 서브네임 like 적용
xjfcnfw3 0672bf4
🐛 fix: 주식 컨트롤러에서 잘못된 경로 매핑문제 해결
xjfcnfw3 a2cc91a
✨ feat: 변동률 랭킹 엔티티 구현
xjfcnfw3 2779b81
✨ feat: 변동률 랭킹 api 데이터 수집
xjfcnfw3 ee5e460
💄 style: 변동 랭킹 주식 관련 코드 eslint 형식으로 수정
xjfcnfw3 e1ceb5c
✨ feat: 랭킹 api 데이터를 스크롤
xjfcnfw3 2ab7a9c
Merge remote-tracking branch 'origin/dev-be' into feature/#253
xjfcnfw3 b68dce1
Merge branch 'dev-be' of https://github.com/boostcampwm-2024/web17-ju…
xjfcnfw3 a62b313
🐛 fix: openApiToken 타입 변경으로 인한 발생한 오류 수정
xjfcnfw3 d3b6d65
✨ feat: 구글 로그인 리다이렉트 주소 임시로 변경
xjfcnfw3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/backend/src/scraper/openapi/api/openapiFluctuationData.api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { Cron } from '@nestjs/schedule'; | ||
import { DataSource, EntityManager } from 'typeorm'; | ||
import { Logger } from 'winston'; | ||
import { OpenapiTokenApi } from '@/scraper/openapi/api/openapiToken.api'; | ||
import { | ||
DECREASE_STOCK_QUERY, | ||
INCREASE_STOCK_QUERY, | ||
} from '@/scraper/openapi/constants/query'; | ||
import { TR_IDS } from '@/scraper/openapi/type/openapiUtil.type'; | ||
import { getOpenApi } from '@/scraper/openapi/util/openapiUtil.api'; | ||
import { FluctuationRankStock } from '@/stock/domain/FluctuationRankStock.entity'; | ||
|
||
@Injectable() | ||
export class OpenapiFluctuationData { | ||
private readonly fluctuationUrl: string = | ||
'/uapi/domestic-stock/v1/ranking/fluctuation'; | ||
constructor( | ||
private readonly openApiToken: OpenapiTokenApi, | ||
private readonly datasource: DataSource, | ||
@Inject('winston') private readonly logger: Logger, | ||
) { | ||
setTimeout(() => this.getFluctuationRankStocks(), 1000); | ||
} | ||
|
||
@Cron('*/1 9-16 * * 1-5') | ||
async getFluctuationRankStocks() { | ||
await this.getDecreaseRankStocks(); | ||
await this.getIncreaseRankStocks(); | ||
} | ||
|
||
async getDecreaseRankStocks(count = 5) { | ||
try { | ||
if (count === 0) return; | ||
await this.datasource.transaction(async (manager) => { | ||
const result = await this.getFluctuationRankApiStocks(false); | ||
await this.datasource.manager.delete(FluctuationRankStock, { | ||
isRising: false, | ||
}); | ||
await this.saveFluctuationRankStocks(result, manager); | ||
this.logger.info('decrease rank stocks updated'); | ||
}); | ||
} catch (error) { | ||
this.logger.warn(error); | ||
this.getDecreaseRankStocks(--count); | ||
} | ||
} | ||
|
||
async getIncreaseRankStocks(count = 5) { | ||
try { | ||
if (count === 0) return; | ||
await this.datasource.transaction(async (manager) => { | ||
const result = await this.getFluctuationRankApiStocks(true); | ||
await this.datasource.manager.delete(FluctuationRankStock, { | ||
isRising: true, | ||
}); | ||
await this.saveFluctuationRankStocks(result, manager); | ||
this.logger.info('increase rank stocks updated'); | ||
}); | ||
} catch (error) { | ||
this.logger.warn(error); | ||
this.getIncreaseRankStocks(--count); | ||
} | ||
} | ||
|
||
private async saveFluctuationRankStocks( | ||
result: FluctuationRankStock[], | ||
manager: EntityManager, | ||
) { | ||
await manager | ||
.getRepository(FluctuationRankStock) | ||
.createQueryBuilder() | ||
.insert() | ||
.into(FluctuationRankStock) | ||
.values(result) | ||
.execute(); | ||
} | ||
|
||
private async getFluctuationRankApiStocks(isRising: boolean) { | ||
const query = isRising ? INCREASE_STOCK_QUERY : DECREASE_STOCK_QUERY; | ||
const result = await getOpenApi( | ||
this.fluctuationUrl, | ||
(await this.openApiToken.configs())[0], | ||
query, | ||
TR_IDS.FLUCTUATION_DATA, | ||
); | ||
|
||
return result.output.map((result: Record<string, string>) => ({ | ||
rank: result.data_rank, | ||
fluctuationRate: result.prdy_ctrt, | ||
stock: { id: result.stck_shrn_iscd }, | ||
isRising, | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const BASE_QUERY = { | ||
fid_cond_mrkt_div_code: 'J', | ||
fid_cond_scr_div_code: '20170', | ||
fid_input_iscd: '0000', | ||
fid_input_cnt_1: '0', | ||
fid_input_price_1: '', | ||
fid_input_price_2: '', | ||
fid_vol_cnt: '', | ||
fid_trgt_cls_code: '0', | ||
fid_trgt_exls_cls_code: '0', | ||
fid_div_cls_code: '0', | ||
fid_rsfl_rate1: '', | ||
fid_rsfl_rate2: '', | ||
}; | ||
|
||
export const DECREASE_STOCK_QUERY = { | ||
...BASE_QUERY, | ||
fid_rank_sort_cls_code: '1', | ||
fid_prc_cls_code: '1', | ||
}; | ||
|
||
export const INCREASE_STOCK_QUERY = { | ||
...BASE_QUERY, | ||
fid_rank_sort_cls_code: '0', | ||
fid_prc_cls_code: '1', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/backend/src/stock/domain/FluctuationRankStock.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
JoinColumn, | ||
ManyToOne, | ||
PrimaryGeneratedColumn, | ||
} from 'typeorm'; | ||
import { Stock } from '@/stock/domain/stock.entity'; | ||
|
||
@Entity() | ||
export class FluctuationRankStock { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@ManyToOne(() => Stock, (stock) => stock.id) | ||
@JoinColumn({ name: 'stock_id' }) | ||
stock: Stock; | ||
|
||
@Column({ name: 'fluctuation_rate', type: 'decimal', precision: 5, scale: 2 }) | ||
fluctuationRate: string; | ||
|
||
@Column() | ||
isRising: boolean; | ||
|
||
@Column() | ||
rank: number; | ||
|
||
@CreateDateColumn({ name: 'created_at', type: 'timestamp' }) | ||
createdAt: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,11 @@ import { plainToInstance } from 'class-transformer'; | |
import { DataSource, EntityManager } from 'typeorm'; | ||
import { Logger } from 'winston'; | ||
import { Stock } from './domain/stock.entity'; | ||
import { StockSearchResponse, StocksResponse } from './dto/stock.response'; | ||
import { | ||
StockRankResponses, | ||
StockSearchResponse, | ||
StocksResponse, | ||
} from './dto/stock.response'; | ||
import { UserStock } from '@/stock/domain/userStock.entity'; | ||
|
||
@Injectable() | ||
|
@@ -94,7 +98,7 @@ export class StockService { | |
} | ||
|
||
async getTopStocksByViews(limit: number) { | ||
const rawData = await this.StocksQuery() | ||
const rawData = await this.getStocksQuery() | ||
.orderBy('stock.views', 'DESC') | ||
.limit(limit) | ||
.getRawMany(); | ||
|
@@ -103,21 +107,17 @@ export class StockService { | |
} | ||
|
||
async getTopStocksByGainers(limit: number) { | ||
const rawData = await this.StocksQuery() | ||
.orderBy('stockLiveData.changeRate', 'DESC') | ||
.limit(limit) | ||
.getRawMany(); | ||
const rawData = await this.getStockRankQuery(true).take(limit).getRawMany(); | ||
|
||
return plainToInstance(StocksResponse, rawData); | ||
return new StockRankResponses(rawData); | ||
} | ||
|
||
async getTopStocksByLosers(limit: number) { | ||
const rawData = await this.StocksQuery() | ||
.orderBy('stockLiveData.changeRate', 'ASC') | ||
.limit(limit) | ||
const rawData = await this.getStockRankQuery(false) | ||
.take(limit) | ||
.getRawMany(); | ||
|
||
return plainToInstance(StocksResponse, rawData); | ||
return new StockRankResponses(rawData); | ||
} | ||
|
||
private async validateStockExists(stockId: string, manager: EntityManager) { | ||
|
@@ -153,7 +153,7 @@ export class StockService { | |
return await manager.exists(Stock, { where: { id: stockId } }); | ||
} | ||
|
||
private StocksQuery() { | ||
private getStocksQuery() { | ||
return this.datasource | ||
.getRepository(Stock) | ||
.createQueryBuilder('stock') | ||
|
@@ -176,4 +176,15 @@ export class StockService { | |
'stockDetail.marketCap AS marketCap', | ||
]); | ||
} | ||
|
||
private getStockRankQuery(isRising: boolean) { | ||
return this.getStocksQuery() | ||
.innerJoinAndSelect('stock.fluctuationRankStocks', 'FluctuationRankStock') | ||
.addSelect([ | ||
'fluctuationRankStock.rank AS stockRank', | ||
'fluctuationRankStock.isRising AS isRising', | ||
'fluctuationRankStock.fluctuation_rate AS fluctuationRate', | ||
]) | ||
.where('FluctuationRankStock.isRising = :isRising', { isRising }); | ||
} | ||
Comment on lines
+181
to
+189
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ㅗㅜㅑ; typeorm의 경지는 멀고 험난하군요.. |
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/1는 안 써도 될거 같아요!
https://docs.nestjs.com/techniques/task-scheduling