-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
9 changed files
with
252 additions
and
14 deletions.
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