Skip to content

Commit

Permalink
Bug/#377 - 그래프 데이터 버그 수정 (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
xjfcnfw3 authored Dec 5, 2024
2 parents 4a183b8 + d130e7a commit 6b7514b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 39 deletions.
22 changes: 22 additions & 0 deletions packages/backend/src/chat/like.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { DataSource, EntityManager } from 'typeorm';
import { Chat } from '@/chat/domain/chat.entity';
import { Like } from '@/chat/domain/like.entity';
import { LikeResponse } from '@/chat/dto/like.response';

@Injectable()
export class LikeService {
private readonly likedChats = new Set<number>();

constructor(private readonly dataSource: DataSource) {}

async toggleLike(userId: number, chatId: number) {
Expand All @@ -14,13 +17,32 @@ export class LikeService {
const like = await manager.findOne(Like, {
where: { user: { id: userId }, chat: { id: chatId } },
});
this.likedChats.add(chatId);
if (like) {
return await this.deleteLike(manager, chat, like);
}
return await this.saveLike(manager, chat, userId);
});
}

@Cron('*/30 * * * *')
async calculateLike() {
const tasks = [];
for (const chatId of this.likedChats) {
tasks.push(async () => {
const likeCount = await this.dataSource
.getRepository(Like)
.createQueryBuilder('like')
.where('like.chat_id = :chatId', { chatId })
.getCount();

await this.dataSource.getRepository(Chat).update(chatId, { likeCount });
});
}
await Promise.all(tasks.map((task) => task()));
this.likedChats.clear();
}

private async findChat(chatId: number, manager: EntityManager) {
const chat = await manager.findOne(Chat, {
where: { id: chatId },
Expand Down
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;

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

0 comments on commit 6b7514b

Please sign in to comment.