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

Feature/#332 - 누락된 최신 차트데이터가 존재하면 API 요청 후 차트데이터 전달 #344

Merged
merged 7 commits into from
Dec 3, 2024
74 changes: 50 additions & 24 deletions packages/backend/src/scraper/openapi/api/openapiPeriodData.api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Inject, Injectable } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { DataSource } from 'typeorm';
import { DataSource, QueryFailedError } from 'typeorm';
Copy link
Collaborator

Choose a reason for hiding this comment

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

오? 아니 왜 이런 걸 쿡북에 안 작성해주는거야

import { Logger } from 'winston';
import {
ChartData,
Expand All @@ -10,11 +10,7 @@ import {
} from '../type/openapiPeriodData.type';
import { TR_IDS } from '../type/openapiUtil.type';
import { NewDate } from '../util/newDate.util';
import {
getOpenApi,
getPreviousDate,
getTodayDate,
} from '../util/openapiUtil.api';
import { getPreviousDate, getTodayDate } from '../util/openapiUtil.api';
import { OpenapiTokenApi } from './openapiToken.api';
import { Json, OpenapiQueue } from '@/scraper/openapi/queue/openapi.queue';
import { Stock } from '@/stock/domain/stock.entity';
Expand All @@ -33,15 +29,13 @@ const DATE_TO_ENTITY = {
Y: StockYearly,
} as const;

const DATE_TO_MONTH = {
export const DATE_TO_MONTH = {
D: 1,
W: 6,
M: 24,
Y: 120,
} as const;

const INTERVALS = 10000;

@Injectable()
export class OpenapiPeriodData {
private readonly url: string =
Expand Down Expand Up @@ -69,6 +63,53 @@ export class OpenapiPeriodData {
await this.getChartData(stocks, 'D');
}

getInsertCartDataRequestCallback(
resolve: (value: StockData[]) => void,
stockId: string,
period: Period,
) {
return async (data: Json) => {
if (!data.output2 || !Array.isArray(data.output2)) return resolve([]);
const result = data.output2
.reduce((acc: StockData[], item: Record<string, string>) => {
if (!isChartData(item)) return acc;
const stockData = this.convertObjectToStockData(item, stockId);
acc.push(stockData);
this.insertChartData(stockData, period).catch((e) => {
if (
e instanceof QueryFailedError &&
e.driverError.message.includes('duplicate')
) {
this.logger.warn(
`when insert missing chart data, duplicate error :${stockId}`,
);
return;
}
this.logger.error(e);
});
return acc;
}, [])
.reverse();
resolve(result);
};
}

insertCartDataRequest(
resolve: (value: StockData[]) => void,
stockId: string,
period: Period,
): void {
const end = getTodayDate();
const start = getPreviousDate(end, DATE_TO_MONTH[period]);
const query = this.getItemChartPriceQuery(stockId, start, end, period);
this.openApiQueue.enqueue({
url: this.url,
query,
trId: TR_IDS.ITEM_CHART_PRICE,
callback: this.getInsertCartDataRequestCallback(resolve, stockId, period),
});
}

/**
* 월, 년의 경우 마지막 데이터를 업데이트 하는 형식으로 변경해야됨
*/
Expand Down Expand Up @@ -104,21 +145,6 @@ export class OpenapiPeriodData {
});
}

private async fetchChartData(query: ItemChartPriceQuery, configIdx: number) {
try {
const response = await getOpenApi(
this.url,
(await this.openApiToken.configs())[configIdx],
query,
TR_IDS.ITEM_CHART_PRICE,
);
return response.output2 as ChartData[];
} catch (error) {
this.logger.warn(error);
setTimeout(() => this.fetchChartData(query, configIdx), INTERVALS / 10);
}
}

private updateDates(
endDate: string,
period: Period,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import { StockLiveData } from '@/stock/domain/stockLiveData.entity';
],
controllers: [],
providers: [
LiveData,
OpenapiLiveData,
OpenapiTokenApi,
OpenapiPeriodData,
Expand All @@ -56,6 +55,6 @@ import { StockLiveData } from '@/stock/domain/stockLiveData.entity';
WebsocketClient,
LiveData,
],
exports: [LiveData],
exports: [LiveData, OpenapiPeriodData],
})
export class OpenapiScraperModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type ItemChartPriceQuery = {
fid_org_adj_prc: number;
};

export const isChartData = (data?: any) => {
export const isChartData = (data?: any): data is ChartData => {
return (
data &&
typeof data === 'object' &&
Expand Down
56 changes: 32 additions & 24 deletions packages/backend/src/scraper/openapi/util/newDate.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,6 @@ export class NewDate extends Date {
);
}

private resetTime(): NewDate {
this.setHours(0, 0, 0, 0);
return this;
}

private getWeek(): number {
const date = this.resetTime();
const firstOne = new Date(date.getFullYear(), 0, 1);
return Math.ceil(
((date.getTime() - firstOne.getTime()) / 86400000 +
firstOne.getDay() +
1) /
7,
);
}

private isLastWeekOfTheYear(): boolean {
return this.getWeek() === 53;
}

private isFirstWeekOfTheYear(): boolean {
return this.getWeek() === 1;
}

dateToNewDate(date: Date): NewDate {
return new NewDate(date);
}
Expand Down Expand Up @@ -77,4 +53,36 @@ export class NewDate extends Date {
isSameYear(dateToCompare: NewDate | Date): boolean {
return this.getFullYear() === dateToCompare.getFullYear();
}

isSameDate(dateToCompare: NewDate | Date): boolean {
return (
this.getFullYear() === dateToCompare.getFullYear() &&
this.getMonth() === dateToCompare.getMonth() &&
this.getDate() === dateToCompare.getDate()
);
}
Comment on lines +57 to +63
Copy link
Collaborator

Choose a reason for hiding this comment

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

아 이걸 추가하셨군요!


private resetTime(): NewDate {
this.setHours(0, 0, 0, 0);
return this;
}

private getWeek(): number {
const date = this.resetTime();
const firstOne = new Date(date.getFullYear(), 0, 1);
return Math.ceil(
((date.getTime() - firstOne.getTime()) / 86400000 +
firstOne.getDay() +
1) /
7,
);
}

private isLastWeekOfTheYear(): boolean {
return this.getWeek() === 53;
}

private isFirstWeekOfTheYear(): boolean {
return this.getWeek() === 1;
}
}
9 changes: 9 additions & 0 deletions packages/backend/src/stock/constants/timeunit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const TIME_UNIT = {
MINUTE: 'minute',
DAY: 'day',
WEEK: 'week',
MONTH: 'month',
YEAR: 'year',
} as const;

export type TIME_UNIT = (typeof TIME_UNIT)[keyof typeof TIME_UNIT];
24 changes: 24 additions & 0 deletions packages/backend/src/stock/dto/stockData.response.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { StockData } from '@/stock/domain/stockData.entity';

export class PriceDto {
@ApiProperty({
Expand Down Expand Up @@ -33,6 +34,14 @@ export class PriceDto {
example: '123.45',
})
close: number;

constructor(stockData: StockData) {
this.startTime = stockData.startTime;
this.open = stockData.open;
this.high = stockData.high;
this.low = stockData.low;
this.close = stockData.close;
}
}

export class VolumeDto {
Expand All @@ -49,6 +58,11 @@ export class VolumeDto {
example: 1000,
})
volume: number;

constructor(stockData: StockData) {
this.startTime = stockData.startTime;
this.volume = stockData.volume;
}
}

export class StockDataResponse {
Expand All @@ -71,4 +85,14 @@ export class StockDataResponse {
example: true,
})
hasMore: boolean;

constructor(
priceDtoList: PriceDto[],
volumeDtoList: VolumeDto[],
hasMore: boolean,
) {
this.priceDtoList = priceDtoList;
this.volumeDtoList = volumeDtoList;
this.hasMore = hasMore;
}
}
53 changes: 19 additions & 34 deletions packages/backend/src/stock/stock.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,12 @@ import { ApiGetStockData } from './decorator/stockData.decorator';
import { StockDetailResponse } from './dto/stockDetail.response';
import { StockIndexRateResponse } from './dto/stockIndexRate.response';
import { StockService } from './stock.service';
import {
StockDataDailyService,
StockDataMinutelyService,
StockDataMonthlyService,
StockDataWeeklyService,
StockDataYearlyService,
} from './stockData.service';
import { StockDetailService } from './stockDetail.service';
import { StockRateIndexService } from './stockRateIndex.service';
import SessionGuard from '@/auth/session/session.guard';
import { GetUser } from '@/common/decorator/user.decorator';
import { sessionConfig } from '@/configs/session.config';
import { TIME_UNIT } from '@/stock/constants/timeunit';
import { StockSearchRequest } from '@/stock/dto/stock.request';
import {
StockRankResponses,
Expand All @@ -52,16 +46,13 @@ import {
UserStocksResponse,
} from '@/stock/dto/userStock.response';
import { User } from '@/user/domain/user.entity';

const TIME_UNIT = {
MINUTE: 'minute',
DAY: 'day',
WEEK: 'week',
MONTH: 'month',
YEAR: 'year',
} as const;

type TIME_UNIT = (typeof TIME_UNIT)[keyof typeof TIME_UNIT];
import { StockDataService } from '@/stock/stockData.service';
import {
StockDaily,
StockMonthly,
StockWeekly,
StockYearly,
} from '@/stock/domain/stockData.entity';

const FLUCTUATION_TYPE = {
INCREASE: 'increase',
Expand All @@ -76,13 +67,9 @@ type FLUCTUATION_TYPE =
export class StockController {
constructor(
private readonly stockService: StockService,
private readonly stockDataMinutelyService: StockDataMinutelyService,
private readonly stockDataDailyService: StockDataDailyService,
private readonly stockDataWeeklyService: StockDataWeeklyService,
private readonly stockDataMonthlyService: StockDataMonthlyService,
private readonly stockDataYearlyService: StockDataYearlyService,
private readonly stockDetailService: StockDetailService,
private readonly stockRateIndexService: StockRateIndexService,
private readonly stockDataService: StockDataService,
) {}

@HttpCode(200)
Expand Down Expand Up @@ -289,11 +276,9 @@ export class StockController {
async getStockDataDaily(
@Param('stockId') stockId: string,
@Query('lastStartTime') lastStartTime?: string,
@Query('timeunit') timeunit: TIME_UNIT = TIME_UNIT.MINUTE,
@Query('timeunit') timeunit: TIME_UNIT = TIME_UNIT.DAY,
) {
switch (timeunit) {
case TIME_UNIT.MINUTE:
return this.getMinutelyData(stockId, lastStartTime);
case TIME_UNIT.DAY:
return this.getDailyData(stockId, lastStartTime);
case TIME_UNIT.MONTH:
Expand All @@ -309,7 +294,8 @@ export class StockController {
stockId: string,
lastStartTime: string | undefined,
) {
return this.stockDataYearlyService.getStockDataYearly(
return this.stockDataService.getPaginated(
StockYearly,
stockId,
lastStartTime,
);
Expand All @@ -319,7 +305,8 @@ export class StockController {
stockId: string,
lastStartTime: string | undefined,
) {
return this.stockDataWeeklyService.getStockDataWeekly(
return this.stockDataService.getPaginated(
StockWeekly,
stockId,
lastStartTime,
);
Expand All @@ -329,20 +316,18 @@ export class StockController {
stockId: string,
lastStartTime: string | undefined,
) {
return this.stockDataMonthlyService.getStockDataMonthly(
return this.stockDataService.getPaginated(
StockMonthly,
stockId,
lastStartTime,
);
}

private getMinutelyData(stockId: string, lastStartTime?: string) {
return this.stockDataMinutelyService.getStockDataMinutely(
private getDailyData(stockId: string, lastStartTime?: string) {
return this.stockDataService.getPaginated(
StockDaily,
stockId,
lastStartTime,
);
}

private getDailyData(stockId: string, lastStartTime?: string) {
return this.stockDataDailyService.getStockDataDaily(stockId, lastStartTime);
}
}
Loading
Loading