refactor(backend): 설정 통합 및 N+1 쿼리 최적화
- 카테고리 ID 상수를 config/index.js에 CATEGORY_IDS로 통합 - youtube.js, x.js, schedules/index.js에서 하드코딩된 ID를 상수로 교체 - 앨범 목록 조회 시 N+1 쿼리 문제 해결 (트랙 한 번에 조회) - 리팩토링 작업 문서 추가 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
2b24bfe0a7
commit
b61bfe93b4
6 changed files with 121 additions and 17 deletions
|
|
@ -1,3 +1,10 @@
|
|||
// 카테고리 ID 상수
|
||||
export const CATEGORY_IDS = {
|
||||
YOUTUBE: 2,
|
||||
X: 3,
|
||||
BIRTHDAY: 8,
|
||||
};
|
||||
|
||||
export default {
|
||||
server: {
|
||||
port: parseInt(process.env.PORT) || 80,
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { fetchSingleTweet, extractTitle } from '../../services/x/scraper.js';
|
||||
import { addOrUpdateSchedule } from '../../services/meilisearch/index.js';
|
||||
import { formatDate, formatTime } from '../../utils/date.js';
|
||||
import config from '../../config/index.js';
|
||||
import config, { CATEGORY_IDS } from '../../config/index.js';
|
||||
|
||||
const X_CATEGORY_ID = 3;
|
||||
const X_CATEGORY_ID = CATEGORY_IDS.X;
|
||||
const NITTER_URL = config.nitter?.url || process.env.NITTER_URL || 'http://nitter:8080';
|
||||
const DEFAULT_USERNAME = 'realfromis_9';
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import { fetchVideoInfo } from '../../services/youtube/api.js';
|
||||
import { addOrUpdateSchedule } from '../../services/meilisearch/index.js';
|
||||
import { CATEGORY_IDS } from '../../config/index.js';
|
||||
|
||||
const YOUTUBE_CATEGORY_ID = 2;
|
||||
const YOUTUBE_CATEGORY_ID = CATEGORY_IDS.YOUTUBE;
|
||||
|
||||
/**
|
||||
* YouTube 관련 관리자 라우트
|
||||
|
|
|
|||
|
|
@ -88,13 +88,28 @@ export default async function albumsRoutes(fastify) {
|
|||
ORDER BY release_date DESC
|
||||
`);
|
||||
|
||||
if (albums.length === 0) return albums;
|
||||
|
||||
// N+1 쿼리 최적화: 모든 트랙을 한 번에 조회
|
||||
const albumIds = albums.map(a => a.id);
|
||||
const [allTracks] = await db.query(
|
||||
`SELECT id, album_id, track_number, title, is_title_track, duration, lyricist, composer, arranger
|
||||
FROM album_tracks WHERE album_id IN (?) ORDER BY album_id, track_number`,
|
||||
[albumIds]
|
||||
);
|
||||
|
||||
// 앨범 ID별로 트랙 그룹화
|
||||
const tracksByAlbum = {};
|
||||
for (const track of allTracks) {
|
||||
if (!tracksByAlbum[track.album_id]) {
|
||||
tracksByAlbum[track.album_id] = [];
|
||||
}
|
||||
tracksByAlbum[track.album_id].push(track);
|
||||
}
|
||||
|
||||
// 각 앨범에 트랙 할당
|
||||
for (const album of albums) {
|
||||
const [tracks] = await db.query(
|
||||
`SELECT id, track_number, title, is_title_track, duration, lyricist, composer, arranger
|
||||
FROM album_tracks WHERE album_id = ? ORDER BY track_number`,
|
||||
[album.id]
|
||||
);
|
||||
album.tracks = tracks;
|
||||
album.tracks = tracksByAlbum[album.id] || [];
|
||||
}
|
||||
|
||||
return albums;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
*/
|
||||
import suggestionsRoutes from './suggestions.js';
|
||||
import { searchSchedules, syncAllSchedules } from '../../services/meilisearch/index.js';
|
||||
import { CATEGORY_IDS } from '../../config/index.js';
|
||||
|
||||
export default async function schedulesRoutes(fastify) {
|
||||
const { db, meilisearch, redis } = fastify;
|
||||
|
|
@ -151,7 +152,7 @@ export default async function schedulesRoutes(fastify) {
|
|||
};
|
||||
|
||||
// 카테고리별 추가 필드
|
||||
if (s.category_id === 2 && s.youtube_video_id) {
|
||||
if (s.category_id === CATEGORY_IDS.YOUTUBE && s.youtube_video_id) {
|
||||
// YouTube
|
||||
result.videoId = s.youtube_video_id;
|
||||
result.videoType = s.youtube_video_type;
|
||||
|
|
@ -159,7 +160,7 @@ export default async function schedulesRoutes(fastify) {
|
|||
result.videoUrl = s.youtube_video_type === 'shorts'
|
||||
? `https://www.youtube.com/shorts/${s.youtube_video_id}`
|
||||
: `https://www.youtube.com/watch?v=${s.youtube_video_id}`;
|
||||
} else if (s.category_id === 3 && s.x_post_id) {
|
||||
} else if (s.category_id === CATEGORY_IDS.X && s.x_post_id) {
|
||||
// X (Twitter)
|
||||
const username = 'realfromis_9';
|
||||
result.postId = s.x_post_id;
|
||||
|
|
@ -357,8 +358,8 @@ async function handleMonthlySchedules(db, year, month) {
|
|||
members,
|
||||
};
|
||||
|
||||
// source 정보 추가 (YouTube: 2, X: 3)
|
||||
if (s.category_id === 2 && s.youtube_video_id) {
|
||||
// source 정보 추가
|
||||
if (s.category_id === CATEGORY_IDS.YOUTUBE && s.youtube_video_id) {
|
||||
const videoUrl = s.youtube_video_type === 'shorts'
|
||||
? `https://www.youtube.com/shorts/${s.youtube_video_id}`
|
||||
: `https://www.youtube.com/watch?v=${s.youtube_video_id}`;
|
||||
|
|
@ -366,7 +367,7 @@ async function handleMonthlySchedules(db, year, month) {
|
|||
name: s.youtube_channel || 'YouTube',
|
||||
url: videoUrl,
|
||||
};
|
||||
} else if (s.category_id === 3 && s.x_post_id) {
|
||||
} else if (s.category_id === CATEGORY_IDS.X && s.x_post_id) {
|
||||
schedule.source = {
|
||||
name: '',
|
||||
url: `https://x.com/realfromis_9/status/${s.x_post_id}`,
|
||||
|
|
@ -407,9 +408,9 @@ async function handleMonthlySchedules(db, year, month) {
|
|||
};
|
||||
}
|
||||
|
||||
// 생일 카테고리 (id: 8)
|
||||
// 생일 카테고리
|
||||
const BIRTHDAY_CATEGORY = {
|
||||
id: 8,
|
||||
id: CATEGORY_IDS.BIRTHDAY,
|
||||
name: '생일',
|
||||
color: '#f472b6',
|
||||
};
|
||||
|
|
@ -427,7 +428,7 @@ async function handleMonthlySchedules(db, year, month) {
|
|||
grouped[dateKey].schedules.push(birthdaySchedule);
|
||||
|
||||
// 생일 카테고리 카운트
|
||||
const existingBirthdayCategory = grouped[dateKey].categories.find(c => c.id === 8);
|
||||
const existingBirthdayCategory = grouped[dateKey].categories.find(c => c.id === CATEGORY_IDS.BIRTHDAY);
|
||||
if (existingBirthdayCategory) {
|
||||
existingBirthdayCategory.count++;
|
||||
} else {
|
||||
|
|
|
|||
80
docs/refactoring.md
Normal file
80
docs/refactoring.md
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
# Backend Refactoring Plan
|
||||
|
||||
백엔드 코드 품질 개선을 위한 리팩토링 계획서
|
||||
|
||||
## 작업 목록
|
||||
|
||||
### 1단계: 설정 통합 (config 정리) ✅ 완료
|
||||
- [x] 카테고리 ID 상수 통합 (`CATEGORY_IDS`)
|
||||
- [ ] 매직 넘버 설정 파일로 이동 (추후)
|
||||
- [ ] JWT 기본값 제거 (추후)
|
||||
|
||||
**수정된 파일:**
|
||||
- `src/config/index.js` - `CATEGORY_IDS` 상수 추가
|
||||
- `src/routes/admin/youtube.js` - config에서 import
|
||||
- `src/routes/admin/x.js` - config에서 import
|
||||
- `src/routes/schedules/index.js` - 하드코딩된 2, 3, 8 → 상수로 변경
|
||||
|
||||
---
|
||||
|
||||
### 2단계: N+1 쿼리 최적화 ✅ 완료
|
||||
- [x] 앨범 목록 조회 시 트랙 한 번에 조회로 변경
|
||||
- [x] 스케줄 멤버 조회 - 이미 최적화됨 (확인 완료)
|
||||
|
||||
**수정된 파일:**
|
||||
- `src/routes/albums/index.js` - GET /api/albums에서 트랙 조회 최적화
|
||||
- 변경 전: 앨범마다 트랙 조회 (N+1 쿼리)
|
||||
- 변경 후: 모든 트랙을 한 번에 조회 후 JavaScript에서 그룹화 (2 쿼리)
|
||||
|
||||
---
|
||||
|
||||
### 3단계: 서비스 레이어 분리
|
||||
- [ ] `src/services/album.js` 생성 - 앨범 관련 비즈니스 로직
|
||||
- [ ] `src/services/schedule.js` 생성 - 스케줄 관련 비즈니스 로직
|
||||
- [ ] 라우트에서 서비스 호출로 변경
|
||||
|
||||
**관련 파일:**
|
||||
- `src/routes/albums/index.js`
|
||||
- `src/routes/schedules/index.js`
|
||||
|
||||
---
|
||||
|
||||
### 4단계: 에러 처리 통일
|
||||
- [ ] 에러 응답 유틸리티 생성 (`src/utils/error.js`)
|
||||
- [ ] `reply.status()` vs `reply.code()` 통일
|
||||
- [ ] `console.error` → `fastify.log.error` 변경
|
||||
|
||||
**관련 파일:**
|
||||
- 모든 라우트 파일
|
||||
|
||||
---
|
||||
|
||||
### 5단계: 중복 코드 제거
|
||||
- [ ] 멤버 이름 매핑 유틸리티 생성
|
||||
- [ ] 이미지 업로드 공통 함수 생성
|
||||
- [ ] 스케줄러 상태 업데이트 로직 통합
|
||||
|
||||
**관련 파일:**
|
||||
- `src/services/image.js`
|
||||
- `src/services/youtube/index.js`
|
||||
- `src/plugins/scheduler.js`
|
||||
|
||||
---
|
||||
|
||||
## 진행 상황
|
||||
|
||||
| 단계 | 작업 | 상태 |
|
||||
|------|------|------|
|
||||
| 1단계 | 설정 통합 | ✅ 완료 |
|
||||
| 2단계 | N+1 쿼리 최적화 | ✅ 완료 |
|
||||
| 3단계 | 서비스 레이어 분리 | 대기 |
|
||||
| 4단계 | 에러 처리 통일 | 대기 |
|
||||
| 5단계 | 중복 코드 제거 | 대기 |
|
||||
|
||||
---
|
||||
|
||||
## 참고사항
|
||||
|
||||
- 각 단계별로 테스트 후 다음 단계 진행
|
||||
- 기존 API 응답 형식은 유지
|
||||
- 프론트엔드 수정 불필요하도록 진행
|
||||
Loading…
Add table
Reference in a new issue