Skip to content

Commit

Permalink
Bug/#345 - 제약조건 에러 해결 및 각종 버그 수정 (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
xjfcnfw3 authored Dec 4, 2024
2 parents 67c5e21 + 57dd091 commit c035e0e
Show file tree
Hide file tree
Showing 23 changed files with 208 additions and 109 deletions.
4 changes: 2 additions & 2 deletions packages/backend/src/chat/chat.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('ChatService 테스트', () => {

await expect(() =>
chatService.scrollChat({
stockId: 'A005930',
stockId: '005930',
pageSize: 101,
}),
).rejects.toThrow('pageSize should be less than 100');
Expand All @@ -20,7 +20,7 @@ describe('ChatService 테스트', () => {
const chatService = new ChatService(dataSource as DataSource);

await expect(() =>
chatService.scrollChat({ stockId: 'A005930', pageSize: 101 }),
chatService.scrollChat({ stockId: '005930', pageSize: 101 }),
).rejects.toThrow('pageSize should be less than 100');
});
});
2 changes: 1 addition & 1 deletion packages/backend/src/chat/domain/chat.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
} from 'typeorm';
import { ChatType } from '@/chat/domain/chatType.enum';
import { Like } from '@/chat/domain/like.entity';
import { Mention } from '@/chat/domain/mention.entity';
import { DateEmbedded } from '@/common/dateEmbedded.entity';
import { Stock } from '@/stock/domain/stock.entity';
import { User } from '@/user/domain/user.entity';
import { Mention } from '@/chat/domain/mention.entity';

@Entity()
export class Chat {
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/chat/dto/chat.request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IsNumber, IsOptional, IsString } from 'class-validator';
export class ChatScrollQuery {
@ApiProperty({
description: '종목 주식 id(종목방 id)',
example: 'A005930',
example: '005930',
})
@IsString()
stockId: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/chat/dto/like.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class LikeResponse {
@ApiProperty({
type: 'string',
description: '참여 중인 좀목 id',
example: 'A005930',
example: '005930',
})
stockId: string;

Expand Down Expand Up @@ -66,4 +66,4 @@ export class LikeResponse {

function isStockId(stockId?: string): stockId is string {
return stockId !== undefined;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Test, TestingModule } from '@nestjs/testing';
import { KoreaStockInfoService } from './korea-stock-info.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Stock } from '@/stock/domain/stock.entity';
import { WinstonModule } from 'nest-winston';
import { KoreaStockInfoService } from './korea-stock-info.service';
import { logger } from '@/configs/logger.config';
import { Stock } from '@/stock/domain/stock.entity';

xdescribe('KoreaStockInfoService', () => {
let service: KoreaStockInfoService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class OpenapiDetailData extends Openapi {
super(datasource, config, 100);
}

@Cron('35 0 * * 1-5')
@Cron('35 0 * * 2-6')
async start() {
super.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class OpenapiPeriodData {
//this.getItemChartPriceCheck();
}

@Cron('0 1 * * 1-5')
@Cron('0 1 * * 2-6')
async getItemChartPriceCheck() {
if (process.env.NODE_ENV !== 'production') return;
const stocks = await this.datasource.manager.find(Stock, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class OpenapiTokenApi {
return this.config;
}

@Cron('30 0 * * 1-5')
@Cron('30 0 * * *')
async init() {
const expired_config = this.config.filter(
(val) =>
Expand Down
43 changes: 27 additions & 16 deletions packages/backend/src/scraper/openapi/util/priorityQueue.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
export class PriorityQueue<T> {
private heap: { value: T; priority: number }[];
type HeapNode<T> = { value: T; priority: number; order: number };

constructor() {
this.heap = [];
}
export class PriorityQueue<T> {
private heap: HeapNode<T>[] = [];
private count: number = 0;

enqueue(value: T, priority: number) {
this.heap.push({ value, priority });
this.heap.push({ value, priority, order: this.count++ });
this.heapifyUp();
}

Expand All @@ -31,11 +30,17 @@ export class PriorityQueue<T> {
}

isEmpty(): boolean {
return this.heap.length === 0;
const result = this.heap.length === 0;
if (!result) {
return result;
}
this.count = 0;
return result;
}

clear() {
this.heap = [];
this.count = 0;
}

private getParentIndex(index: number): number {
Expand All @@ -56,13 +61,20 @@ export class PriorityQueue<T> {

private heapifyUp() {
let index = this.heap.length - 1;
while (
index > 0 &&
this.heap[index].priority < this.heap[this.getParentIndex(index)].priority
) {
this.swap(index, this.getParentIndex(index));
index = this.getParentIndex(index);

while (index > 0) {
const parentIndex = this.getParentIndex(index);
if (!this.compare(this.heap[index], this.heap[parentIndex])) break;
this.swap(index, parentIndex);
index = parentIndex;
}
}

private compare(a: HeapNode<T>, b: HeapNode<T>) {
if (a.priority === b.priority) {
return a.order < b.order;
}
return a.priority < b.priority;
}

private heapifyDown() {
Expand All @@ -73,13 +85,12 @@ export class PriorityQueue<T> {

if (
rightChildIndex < this.heap.length &&
this.heap[rightChildIndex].priority <
this.heap[smallerChildIndex].priority
this.compare(this.heap[rightChildIndex], this.heap[smallerChildIndex])
) {
smallerChildIndex = rightChildIndex;
}

if (this.heap[index].priority <= this.heap[smallerChildIndex].priority) {
if (this.compare(this.heap[index], this.heap[smallerChildIndex])) {
break;
}

Expand Down
38 changes: 38 additions & 0 deletions packages/backend/src/scraper/openapi/util/queue.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { PriorityQueue } from '@/scraper/openapi/util/priorityQueue';

describe('priorityQueue', () => {
let priorityQueue: PriorityQueue<number>;

beforeEach(() => {
priorityQueue = new PriorityQueue();
});

test('대량의 데이터 셋', () => {
const size = 1000;
const priorities: number[] = [];
for (let i = 0; i < size; i++) {
const priority = Math.floor(Math.random() * 10);
priorities.push(priority);
priorityQueue.enqueue(i, priority);
}
let lastPriority = -1;
while (!priorityQueue.isEmpty()) {
const current = priorityQueue.dequeue();
const currentPriority = priorities[current!];
expect(currentPriority).toBeGreaterThanOrEqual(lastPriority);
if (currentPriority > lastPriority) {
lastPriority = currentPriority;
}
}
});

test('동일 우선순위일 경우 FIFO', () => {
const size = 1000;
for (let i = 0; i < size; i++) {
priorityQueue.enqueue(i, 0);
}
for (let i = 0; i < size; i++) {
expect(priorityQueue.dequeue()).toBe(i);
}
});
});
2 changes: 1 addition & 1 deletion packages/backend/src/stock/cache/stockData.cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ export class StockDataCache {
get(key: string): StockDataResponse | null {
return this.localCache.get(key);
}
}
}
34 changes: 32 additions & 2 deletions packages/backend/src/stock/decorator/stock.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import {
ParseIntPipe,
Query,
} from '@nestjs/common';
import { ApiOperation, ApiQuery, ApiResponse } from '@nestjs/swagger';
import { StocksResponse } from '../dto/stock.response';
import {
ApiOkResponse,
ApiOperation,
ApiQuery,
ApiResponse,
} from '@nestjs/swagger';
import { StockRankResponses, StocksResponse } from '../dto/stock.response';

export function LimitQuery(defaultValue = 5): ParameterDecorator {
return Query('limit', new DefaultValuePipe(defaultValue), ParseIntPipe);
Expand All @@ -30,3 +35,28 @@ export function ApiGetStocks(summary: string) {
}),
);
}

export function ApiFluctuationQuery() {
return applyDecorators(
ApiOperation({
summary: '등가, 등락률 기반 주식 리스트 조회 API',
description: '등가, 등락률 기반 주식 리스트를 조회합니다',
}),
ApiQuery({
name: 'limit',
required: false,
description:
'조회할 리스트 수(기본값: 20, 등가, 등락 모두 받으면 모든 데이터 전송)',
}),
ApiQuery({
name: 'type',
required: false,
description: '데이터 타입(기본값: increase, all, increase, decrease)',
enum: ['increase', 'decrease', 'all'],
}),
ApiOkResponse({
description: '',
type: [StockRankResponses],
}),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function ApiGetStockData(summary: string, type: string) {
name: 'stockId',
type: String,
description: '주식 ID',
example: 'A005930',
example: '005930',
}),
ApiQuery({
name: 'lastStartTime',
Expand Down
8 changes: 4 additions & 4 deletions packages/backend/src/stock/dto/stock.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Stock } from '@/stock/domain/stock.entity';
export class StockViewsResponse {
@ApiProperty({
description: '응답 메시지',
example: 'A005930',
example: '005930',
})
id: string;

Expand All @@ -31,7 +31,7 @@ export class StockViewsResponse {
export class StocksResponse {
@ApiProperty({
description: '주식 종목 코드',
example: 'A005930',
example: '005930',
})
id: string;

Expand Down Expand Up @@ -72,7 +72,7 @@ export class StocksResponse {
class StockSearchResult {
@ApiProperty({
description: '주식 종목 코드',
example: 'A005930',
example: '005930',
})
id: string;

Expand Down Expand Up @@ -105,7 +105,7 @@ export class StockSearchResponse {
export class StockRankResponse {
@ApiProperty({
description: '주식 종목 코드',
example: 'A005930',
example: '005930',
})
id: string;

Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/stock/dto/stockView.request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IsString } from 'class-validator';

export class StockViewRequest {
@ApiProperty({
example: 'A005930',
example: '005930',
description: '개별 주식 id',
})
@IsString()
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/stock/dto/userStock.request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IsString } from 'class-validator';

export class UserStockRequest {
@ApiProperty({
example: 'A005930',
example: '005930',
description: '주식 종목 id',
})
@IsString()
Expand Down
Loading

0 comments on commit c035e0e

Please sign in to comment.