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

Bug/#377 - 그래프 데이터 버그 수정 #379

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion packages/backend/src/common/cache/localCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class LocalCache<K, V> {
new PriorityQueue();

constructor(private readonly interval = 500) {
setInterval(() => this.clearExpired(), interval);
setInterval(() => this.clearExpired(), this.interval);
}

get(key: K) {
Expand Down
8 changes: 4 additions & 4 deletions packages/backend/src/stock/cache/stockData.cache.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Injectable } from '@nestjs/common';
import { LocalCache } from '@/common/cache/localCache';
import { StockDataResponse } from '@/stock/dto/stockData.response';
import { StockData } from '@/stock/domain/stockData.entity';

@Injectable()
export class StockDataCache {
private readonly localCache = new LocalCache<string, StockDataResponse>();
private readonly localCache = new LocalCache<string, StockData[]>();

set(key: string, value: StockDataResponse, ttl: number = 60000) {
set(key: string, value: StockData[], ttl: number = 60000) {
this.localCache.set(key, value, ttl);
}

get(key: string): StockDataResponse | null {
get(key: string): StockData[] | null {
return this.localCache.get(key);
}
}
50 changes: 33 additions & 17 deletions packages/backend/src/stock/dto/stockData.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,32 @@ export class PriceDto {
description: '시가',
example: '121.00',
})
open: number;
open: string;

@ApiProperty({
description: '고가',
example: '125.00',
})
high: number;
high: string;

@ApiProperty({
description: '저가',
example: '120.00',
})
low: number;
low: string;

@ApiProperty({
description: '종가',
example: '123.45',
})
close: number;
close: string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

굿굿


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

Expand Down Expand Up @@ -104,20 +104,36 @@ export class StockDataResponse {

renewLastData(stockLiveData: StockLiveData, entity: new () => StockData) {
const lastIndex = this.priceDtoList.length - 1;
this.priceDtoList[lastIndex].close = stockLiveData.currentPrice;
this.priceDtoList[lastIndex].close = String(stockLiveData.currentPrice);
this.renewHighLow(stockLiveData, lastIndex);
this.renewVolume(stockLiveData, lastIndex, entity);
this.renewStartTime(entity, lastIndex);
}

private renewStartTime(entity: new () => StockData, lastIndex: number) {
if (entity !== StockWeekly) {
this.priceDtoList[lastIndex].startTime = getToday();
this.volumeDtoList[lastIndex].startTime = getToday();
return;
}
}

private renewHighLow(stockLiveData: StockLiveData, lastIndex: number) {
this.priceDtoList[lastIndex].high =
Number(stockLiveData.high) > Number(this.priceDtoList[lastIndex].high)
? stockLiveData.high
: this.priceDtoList[lastIndex].high;
? String(stockLiveData.high)
: String(this.priceDtoList[lastIndex].high);
this.priceDtoList[lastIndex].low =
Number(stockLiveData.low) < Number(this.priceDtoList[lastIndex].low)
? stockLiveData.low
: this.priceDtoList[lastIndex].low;
? String(stockLiveData.low)
: String(this.priceDtoList[lastIndex].low);
}

this.priceDtoList[lastIndex].startTime =
entity !== StockWeekly
? getToday()
: this.priceDtoList[lastIndex].startTime;
private renewVolume(
stockLiveData: StockLiveData,
lastIndex: number,
entity: new () => StockData,
) {
this.volumeDtoList[lastIndex].volume =
entity === StockDaily
? String(stockLiveData.volume)
Expand Down
34 changes: 17 additions & 17 deletions packages/backend/src/stock/stockData.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,20 @@ export class StockDataService {
const cachedData = this.stockDataCache.get(cacheKey);

if (cachedData) {
return cachedData;
const time = new Date();
const response = this.convertResultsToResponse(cachedData);
if (!lastStartTime && time.getHours() < 16 && time.getHours() >= 9) {
return await this.renewResponse(response, entity, stockId);
}
return response;
}
const results = await this.getChartData(entity, stockId, lastStartTime);
this.stockDataCache.set(cacheKey, results);
const response = this.convertResultsToResponse(results);
const time = new Date();
if (!lastStartTime && time.getHours() < 16 && time.getHours() >= 9) {
return await this.renewResponse(response, entity, stockId);
}
const response = await this.getChartData(entity, stockId, lastStartTime);
this.stockDataCache.set(cacheKey, response);
return response;
}

Expand All @@ -69,7 +79,7 @@ export class StockDataService {
periodType: Period,
lastStartTime?: string,
) {
return new Promise<StockDataResponse>((resolve) => {
return new Promise<StockData[]>((resolve) => {
this.openapiPeriodData.insertCartDataRequest(
this.getHandleResponseCallback(entity, stockId, resolve, lastStartTime),
stockId,
Expand Down Expand Up @@ -109,16 +119,7 @@ export class StockDataService {
lastStartTime,
);
}
const response = await this.getChartDataFromDB(
entity,
stockId,
lastStartTime,
);
const time = new Date();
if (!lastStartTime && time.getHours() < 16 && time.getHours() >= 9) {
return await this.renewResponse(response, entity, stockId);
}
return response;
return await this.getChartDataFromDB(entity, stockId, lastStartTime);
}

private async renewResponse(
Expand Down Expand Up @@ -146,14 +147,13 @@ export class StockDataService {
this.dataSource.manager,
lastStartTime,
);
const results = await queryBuilder.getMany();
return this.convertResultsToResponse(results);
return await queryBuilder.getMany();
}

private getHandleResponseCallback(
entity: new () => StockData,
stockId: string,
resolve: (value: StockDataResponse) => void,
resolve: (value: StockData[]) => void,
lastStartTime?: string,
) {
return async () => {
Expand Down