feat: add CDN cache hit rate for accurate bandwidth cost estimation

- Add cdn_enabled and cdn_cache_hit_rate API parameters
- Use case별 기본 캐시 히트율 자동 적용 (video: 92%, blog: 90%, etc.)
- 원본 서버 트래픽(origin_monthly_tb)과 절감 비용(cdn_savings_cost) 계산
- 응답에 CDN breakdown 필드 추가 (bandwidth_estimate, bandwidth_info)
- 캐시 키에 CDN 옵션 포함하여 정확한 캐시 분리
- 4개 CDN 관련 테스트 추가 (총 59 tests)
- CLAUDE.md 문서 업데이트

Cost impact example (10K video streaming):
- Without CDN: $18,370 → With CDN 92%: $1,464 (92% savings)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
kappa
2026-01-26 11:34:53 +09:00
parent ba939ceff3
commit 23abd0e64e
8 changed files with 257 additions and 34 deletions

View File

@@ -56,7 +56,7 @@ src/
│ └── report.ts # GET /api/recommend/report
└── __tests__/
├── utils.test.ts # Validation & security tests (27 tests)
└── bandwidth.test.ts # Bandwidth estimation tests (28 tests)
└── bandwidth.test.ts # Bandwidth estimation tests (32 tests, including CDN)
```
### Key Data Flow
@@ -115,6 +115,44 @@ Estimates monthly bandwidth based on use_case patterns:
Heavy bandwidth (>1TB/month) triggers warning about overage costs.
### CDN Cache Hit Rate (`bandwidth.ts`)
CDN 사용 시 원본 서버 트래픽을 자동 계산하여 실제 비용을 추정합니다.
**API Parameters**:
- `cdn_enabled`: CDN 사용 여부 (기본: `true`)
- `cdn_cache_hit_rate`: 캐시 히트율 override (0.0-1.0, 기본: use_case별 자동)
**Use Case별 기본 캐시 히트율**:
| Use Case | 캐시 히트율 | 설명 |
|----------|-------------|------|
| video/streaming | 92% | 동일 영상 반복 시청 |
| blog/static | 90% | 대부분 정적 콘텐츠 |
| file/download | 85% | 소프트웨어/에셋 다운로드 |
| ecommerce | 70% | 상품 이미지 캐시 가능 |
| forum | 60% | 정적/동적 혼합 |
| api/saas | 30% | 동적 응답 위주 |
| gaming | 20% | 실시간 통신 |
| chat | 10% | 실시간 메시지 |
**응답 필드** (`bandwidth_estimate`):
- `cdn_enabled`: CDN 사용 여부
- `cdn_cache_hit_rate`: 적용된 캐시 히트율
- `gross_monthly_tb`: CDN 적용 전 총 트래픽
- `origin_monthly_tb`: CDN 적용 후 원본 서버 트래픽
**응답 필드** (`bandwidth_info`):
- `cdn_savings_tb`: CDN으로 절감된 트래픽 (TB)
- `cdn_savings_cost`: CDN으로 절감된 비용 ($)
**비용 절감 예시** (10,000 동시접속 video streaming):
```
CDN 없음: 총 2,966TB → 원본 2,966TB → 초과비용 $18,370
CDN 92%: 총 2,966TB → 원본 237TB → 초과비용 $1,464
절감: 2,729TB 절감, $16,906 절감 (92%)
```
### Flexible Region Matching
Both `/api/recommend` and `/api/servers` use `buildFlexibleRegionConditionsAnvil()` for anvil_regions:
@@ -266,6 +304,15 @@ curl -s -X POST https://cloud-orchestrator.kappa-d8e.workers.dev/api/recommend -
# Server list with filters (supports flexible region: korea, seoul, tokyo, etc.)
curl -s "https://cloud-orchestrator.kappa-d8e.workers.dev/api/servers?region=korea&minCpu=4" | jq .
# CDN cache hit rate - default (use_case별 자동 적용)
curl -s -X POST https://cloud-orchestrator.kappa-d8e.workers.dev/api/recommend -H "Content-Type: application/json" -d '{"tech_stack":["nginx"],"expected_users":10000,"use_case":"video streaming","region_preference":["japan"]}' | jq '.bandwidth_estimate | {cdn_enabled, cdn_cache_hit_rate, gross_monthly_tb, origin_monthly_tb}'
# CDN disabled (원본 서버 전체 트래픽)
curl -s -X POST https://cloud-orchestrator.kappa-d8e.workers.dev/api/recommend -H "Content-Type: application/json" -d '{"tech_stack":["nginx"],"expected_users":10000,"use_case":"video streaming","region_preference":["japan"],"cdn_enabled":false}' | jq '.bandwidth_estimate'
# Custom CDN cache hit rate (80%)
curl -s -X POST https://cloud-orchestrator.kappa-d8e.workers.dev/api/recommend -H "Content-Type: application/json" -d '{"tech_stack":["nginx"],"expected_users":10000,"use_case":"video streaming","region_preference":["japan"],"cdn_cache_hit_rate":0.80}' | jq '.recommendations[0].bandwidth_info | {cdn_cache_hit_rate, cdn_savings_tb, cdn_savings_cost}'
# HTML Report (encode recommendation result as base64)
# 1. Get recommendation and save to variable
RESULT=$(curl -s -X POST https://cloud-orchestrator.kappa-d8e.workers.dev/api/recommend -H "Content-Type: application/json" -d '{"tech_stack":["nodejs"],"expected_users":500,"use_case":"simple api","lang":"ko"}')
@@ -277,7 +324,29 @@ echo $REPORT_URL
## Recent Changes
### Major Architecture Refactoring (Latest)
### CDN Cache Hit Rate (Latest)
**New Feature**: CDN 캐시 히트율 기반 원본 서버 트래픽 및 비용 계산
**API Parameters Added**:
- `cdn_enabled`: CDN 사용 여부 (기본: true)
- `cdn_cache_hit_rate`: 캐시 히트율 override (0.0-1.0)
**Use Case별 기본 캐시 히트율**:
- video/streaming: 92%, blog/static: 90%, file: 85%
- ecommerce: 70%, forum: 60%, api: 30%, gaming: 20%, chat: 10%
**Response Fields Added**:
- `bandwidth_estimate`: cdn_enabled, cdn_cache_hit_rate, gross_monthly_tb, origin_monthly_tb
- `bandwidth_info`: cdn_savings_tb, cdn_savings_cost
**Cost Impact Example** (10K concurrent video streaming):
- Without CDN: $18,370 overage
- With CDN 92%: $1,464 overage (92% savings)
**Tests Added**: 4 new CDN-specific tests (total 59 tests)
### Major Architecture Refactoring
**Security Hardening**:
- Fixed XSS vulnerability in HTML reports with `escapeHtml()`