feat: YouTube quota GCP monitoring + legacy beta cleanup (#460)

## Summary
- Remove the deprecated beta enrollment system (endpoints, models, email
templates, admin UI, rate limits) — ~20 files cleaned
- Remove legacy count-based YouTube upload tracking from
rate_limit_service and dead blocklist methods
- Add GCP Cloud Monitoring integration to cross-reference YouTube API
quota usage with Firestore tracking
- Add drift detection (alerts when GCP reported vs Firestore tracked
differs >10%)
- Display GCP quota data in admin rate-limits dashboard

## Changes

### Backend (15 files)
- **Removed:** 5 beta endpoints, `BetaTesterStatus` enum, beta User
fields, beta email templates, `validate_email_for_beta()`, deprecated
YouTube/beta rate limit methods, dead blocklist CRUD methods
- **Added:** `get_gcp_quota_usage()` to `YouTubeQuotaService` with Cloud
Monitoring API query, 5-min cache, and drift detection
- **Updated:** Rate limits API response model with GCP quota fields
(`gcp_quota_available`, `gcp_quota_units_consumed`, `quota_drift`,
`quota_drift_alert`)

### Frontend (11 files)
- **Deleted:** `frontend/app/admin/beta/page.tsx` (entire beta admin
page)
- **Removed:** Beta sidebar nav, layout breadcrumb, admin stats card,
beta types/API methods, legacy YouTube stats cards
- **Added:** GCP Cloud Monitoring section in rate-limits page showing
reported usage, delay, and drift alerts

### Tests (4 files)
- Removed 8 obsolete tests for deprecated methods
- Created `test_gcp_quota_monitor.py` with 10 tests covering GCP quota
querying, caching, fallbacks, and drift detection
- Updated mock data and assertions across test files

### Docs (3 files)
- Removed beta endpoint docs from API.md, added GCP quota fields
- Updated ARCHITECTURE.md with Cloud Monitoring integration
- Removed beta program section from PRODUCT-VISION.md

## Test plan
- [x] All 2265 backend tests pass (1 pre-existing failure unrelated)
- [x] All 516 frontend Jest tests pass
- [x] New GCP quota monitor tests cover success, caching, fallbacks,
drift alerts
- [ ] Verify admin rate-limits page no longer shows beta/legacy cards
- [ ] Verify GCP quota section shows "unavailable" gracefully when
monitoring data not yet populated

@coderabbitai ignore

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Andrew Beveridge ☄️andClaude Opus 4.6 authored and GitHub committed 2026-03-03 20:48:07 -05:00
1 parent d91c2c2b4e
commit 937f8f7ec1
32 files changed
+670 -1864

No files matched your search

-8
View File
@@ -55,7 +55,6 @@ class AdminStatsOverview(BaseModel):
jobs_last_30d: int
jobs_by_status: JobsByStatusResponse
total_credits_issued_30d: int
total_beta_testers: int
class FileInfo(BaseModel):
@@ -203,8 +202,6 @@ async def get_admin_stats_overview(
1 for u in all_users
if u.get("last_login_at") and _normalize_datetime(u["last_login_at"]) >= thirty_days_ago
)
total_beta_testers = sum(1 for u in all_users if u.get("is_beta_tester"))
# Calculate credits from filtered users
total_credits_issued_30d = 0
for user_data in all_users:
@@ -270,10 +267,6 @@ async def get_admin_stats_overview(
active_users_30d = get_count(
users_collection.where(filter=FieldFilter("last_login_at", ">=", thirty_days_ago))
)
total_beta_testers = get_count(
users_collection.where(filter=FieldFilter("is_beta_tester", "==", True))
)
# Job statistics
total_jobs = get_count(jobs_collection)
jobs_last_7d = get_count(
@@ -341,7 +334,6 @@ async def get_admin_stats_overview(
jobs_last_30d=jobs_last_30d,
jobs_by_status=jobs_by_status,
total_credits_issued_30d=total_credits_issued_30d,
total_beta_testers=total_beta_testers,
)
+28 -14
View File
@@ -32,13 +32,10 @@ class RateLimitStatsResponse(BaseModel):
"""Current rate limit statistics."""
# Configuration
jobs_per_day_limit: int
youtube_uploads_per_day_limit: int
beta_ip_per_day_limit: int
rate_limiting_enabled: bool
# Current usage (legacy count-based)
# YouTube uploads today (from quota service, PT-based)
youtube_uploads_today: int
youtube_uploads_remaining: int
# YouTube quota (unit-based tracking)
youtube_quota_units_consumed: int
@@ -61,6 +58,14 @@ class RateLimitStatsResponse(BaseModel):
# User override stats
total_overrides: int
# GCP quota monitoring (Phase 3)
gcp_quota_available: bool = False
gcp_quota_units_consumed: Optional[int] = None
gcp_quota_last_datapoint: Optional[str] = None
gcp_quota_data_delay_minutes: Optional[int] = None
quota_drift: Optional[int] = None
quota_drift_alert: bool = False
class YouTubeQueueEntry(BaseModel):
"""A queued YouTube upload entry."""
@@ -166,12 +171,7 @@ async def get_rate_limit_stats(
rate_limit_service = get_rate_limit_service()
email_validation = get_email_validation_service()
# Get YouTube uploads today (legacy count)
youtube_today = rate_limit_service.get_youtube_uploads_today()
youtube_limit = settings.rate_limit_youtube_uploads_per_day
youtube_remaining = max(0, youtube_limit - youtube_today) if youtube_limit > 0 else -1
# Get YouTube quota stats (unit-based)
# Get YouTube quota stats (unit-based, PT timezone)
from backend.services.youtube_quota_service import get_youtube_quota_service
quota_service = get_youtube_quota_service()
quota_stats = quota_service.get_quota_stats()
@@ -187,13 +187,26 @@ async def get_rate_limit_stats(
# Get override count
overrides = rate_limit_service.get_all_overrides()
# Get GCP quota data if available
gcp_data = {}
try:
gcp_result = quota_service.get_gcp_quota_usage()
if gcp_result.get("available"):
gcp_data = {
"gcp_quota_available": True,
"gcp_quota_units_consumed": gcp_result.get("gcp_units_consumed"),
"gcp_quota_last_datapoint": gcp_result.get("gcp_last_datapoint_time"),
"gcp_quota_data_delay_minutes": gcp_result.get("gcp_data_delay_minutes"),
"quota_drift": gcp_result.get("drift"),
"quota_drift_alert": gcp_result.get("drift_alert", False),
}
except Exception:
pass # GCP quota is optional, graceful fallback
return RateLimitStatsResponse(
jobs_per_day_limit=settings.rate_limit_jobs_per_day,
youtube_uploads_per_day_limit=settings.rate_limit_youtube_uploads_per_day,
beta_ip_per_day_limit=settings.rate_limit_beta_ip_per_day,
rate_limiting_enabled=settings.enable_rate_limiting,
youtube_uploads_today=youtube_today,
youtube_uploads_remaining=youtube_remaining,
youtube_uploads_today=quota_stats["upload_count"],
youtube_quota_units_consumed=quota_stats["units_consumed"],
youtube_quota_units_remaining=quota_stats["units_remaining"],
youtube_quota_daily_limit=quota_stats["units_limit"],
@@ -207,6 +220,7 @@ async def get_rate_limit_stats(
blocked_emails_count=blocklist_stats["blocked_emails_count"],
blocked_ips_count=blocklist_stats["blocked_ips_count"],
total_overrides=len(overrides),
**gcp_data,
)
-413
View File
@@ -43,12 +43,6 @@ from backend.models.user import (
AddCreditsRequest,
AddCreditsResponse,
UserListResponse,
BetaTesterStatus,
BetaTesterEnrollRequest,
BetaTesterEnrollResponse,
BetaFeedbackRequest,
BetaFeedbackResponse,
BetaTesterFeedback,
UserFeedback,
UserFeedbackRequest,
UserFeedbackResponse,
@@ -1014,294 +1008,6 @@ async def stripe_webhook(
return {"status": "received", "type": event_type}
# =============================================================================
# Beta Tester Program
# =============================================================================
BETA_TESTER_FREE_CREDITS = 1 # Number of free credits for beta testers
@router.post("/beta/enroll", response_model=BetaTesterEnrollResponse)
async def enroll_beta_tester(
request: BetaTesterEnrollRequest,
http_request: Request,
user_service: UserService = Depends(get_user_service),
email_service: EmailService = Depends(get_email_service),
):
"""
Enroll as a beta tester to receive free karaoke credits.
Requirements:
- Accept that there may be work to review/correct lyrics
- Promise to provide feedback after using the tool
Returns free credits and optionally a session token for new users.
"""
from backend.services.email_validation_service import get_email_validation_service
from backend.services.rate_limit_service import get_rate_limit_service
# Check if email service is configured
if not email_service.is_configured():
logger.error("Email service not configured - cannot send beta welcome emails")
raise HTTPException(
status_code=503,
detail="Email service is not available. Please contact support."
)
email = request.email.lower()
email_validation = get_email_validation_service()
rate_limit_service = get_rate_limit_service()
# ----- ABUSE PREVENTION CHECKS -----
# 1. Validate email (disposable domain, blocked email checks)
is_valid, error_message = email_validation.validate_email_for_beta(email)
if not is_valid:
logger.warning(f"Beta enrollment rejected - email validation failed: {email} - {error_message}")
raise HTTPException(
status_code=400,
detail=error_message
)
# 2. Check IP blocking
ip_address = http_request.client.host if http_request.client else None
if ip_address and email_validation.is_ip_blocked(ip_address):
logger.warning(f"Beta enrollment rejected - IP blocked: {ip_address}")
raise HTTPException(
status_code=403,
detail="Access denied from this location"
)
# 3. Check for E2E test bypass (allows automated testing to skip IP rate limit)
from backend.config import settings
e2e_bypass_key = http_request.headers.get("X-E2E-Bypass-Key")
skip_ip_rate_limit = False
if e2e_bypass_key and settings.e2e_bypass_key:
if e2e_bypass_key == settings.e2e_bypass_key:
logger.info(f"Beta enrollment: E2E bypass key validated for {_mask_email(email)}")
skip_ip_rate_limit = True
else:
logger.warning(f"Beta enrollment: Invalid E2E bypass key attempted for {_mask_email(email)}")
# 4. Check IP-based enrollment rate limit (1 per 24h per IP)
if ip_address and not skip_ip_rate_limit:
allowed, remaining, message = rate_limit_service.check_beta_ip_limit(ip_address)
if not allowed:
logger.warning(f"Beta enrollment rejected - IP rate limit: {ip_address} - {message}")
raise HTTPException(
status_code=429,
detail="Too many beta enrollments from your location. Please try again tomorrow."
)
# 5. Check for duplicate enrollment via normalized email
normalized_email = email_validation.normalize_email(email)
if normalized_email != email:
# Check if normalized version is already enrolled
normalized_user = user_service.get_user(normalized_email)
if normalized_user and normalized_user.is_beta_tester:
logger.warning(f"Beta enrollment rejected - normalized email already enrolled: {email} -> {normalized_email}")
raise HTTPException(
status_code=400,
detail="An account with this email address is already enrolled in the beta program"
)
# ----- END ABUSE PREVENTION CHECKS -----
# Validate acceptance
if not request.accept_corrections_work:
raise HTTPException(
status_code=400,
detail="You must accept that you may need to review/correct lyrics"
)
if len(request.promise_text.strip()) < 10:
raise HTTPException(
status_code=400,
detail="Please write a sentence confirming your promise to provide feedback"
)
# Get or create user
user = user_service.get_or_create_user(email)
# Check if already enrolled as beta tester
if user.is_beta_tester:
raise HTTPException(
status_code=400,
detail="You are already enrolled in the beta program"
)
# Get client info (ip_address already set above in abuse prevention)
user_agent = http_request.headers.get("user-agent")
# Enroll as beta tester
from datetime import datetime
user_service.update_user(
email,
is_beta_tester=True,
beta_tester_status=BetaTesterStatus.ACTIVE.value,
beta_enrolled_at=datetime.utcnow(),
beta_promise_text=request.promise_text.strip(),
)
# Add free credits
_, new_balance, _ = user_service.add_credits(
email=email,
amount=BETA_TESTER_FREE_CREDITS,
reason="beta_tester_enrollment",
)
# Record IP enrollment for rate limiting
if ip_address:
try:
rate_limit_service.record_beta_enrollment(ip_address, email)
except Exception as e:
logger.warning(f"Failed to record beta IP enrollment: {e}")
# Create session for the user (so they can start using the service immediately)
session = user_service.create_session(email, ip_address=ip_address, user_agent=user_agent)
# Send welcome email
email_service.send_beta_welcome_email(email, BETA_TESTER_FREE_CREDITS)
logger.info(f"Beta tester enrolled: {email}, granted {BETA_TESTER_FREE_CREDITS} credits")
return BetaTesterEnrollResponse(
status="success",
message=f"Welcome to the beta program! You have {new_balance} free credits.",
credits_granted=BETA_TESTER_FREE_CREDITS,
session_token=session.token,
)
@router.post("/beta/feedback", response_model=BetaFeedbackResponse)
async def submit_beta_feedback(
request: BetaFeedbackRequest,
authorization: Optional[str] = Header(None),
user_service: UserService = Depends(get_user_service),
):
"""
Submit feedback as a beta tester.
Requires authentication. Updates beta tester status to completed.
May grant bonus credits for detailed feedback.
"""
if not authorization:
raise HTTPException(status_code=401, detail="Authentication required")
# Extract token
token = authorization.replace("Bearer ", "") if authorization.startswith("Bearer ") else authorization
# Validate session
valid, user, message = user_service.validate_session(token)
if not valid or not user:
raise HTTPException(status_code=401, detail=message)
# Check if user is a beta tester
if not user.is_beta_tester:
raise HTTPException(
status_code=400,
detail="Only beta testers can submit feedback through this endpoint"
)
# Check if already completed feedback
if user.beta_tester_status == BetaTesterStatus.COMPLETED:
raise HTTPException(
status_code=400,
detail="You have already submitted feedback. Thank you!"
)
# Save feedback to Firestore
import uuid
feedback = BetaTesterFeedback(
id=str(uuid.uuid4()),
user_email=user.email,
job_id=request.job_id,
overall_rating=request.overall_rating,
ease_of_use_rating=request.ease_of_use_rating,
lyrics_accuracy_rating=request.lyrics_accuracy_rating,
correction_experience_rating=request.correction_experience_rating,
what_went_well=request.what_went_well,
what_could_improve=request.what_could_improve,
additional_comments=request.additional_comments,
would_recommend=request.would_recommend,
would_use_again=request.would_use_again,
submitted_via="web",
)
# Save to Firestore
user_service.db.collection("beta_feedback").document(feedback.id).set(
feedback.model_dump(mode='json')
)
# Update user status
user_service.update_user(
user.email,
beta_tester_status=BetaTesterStatus.COMPLETED.value,
)
# Calculate bonus credits for detailed feedback
bonus_credits = 0
has_detailed_feedback = (
(request.what_went_well and len(request.what_went_well) > 50) or
(request.what_could_improve and len(request.what_could_improve) > 50) or
(request.additional_comments and len(request.additional_comments) > 50)
)
if has_detailed_feedback:
# Grant bonus credit for detailed feedback
bonus_credits = 1
user_service.add_credits(
email=user.email,
amount=bonus_credits,
reason="beta_feedback_bonus",
)
logger.info(f"Beta feedback received from {user.email}, bonus: {bonus_credits}")
return BetaFeedbackResponse(
status="success",
message="Thank you for your feedback!" + (
f" You earned {bonus_credits} bonus credit for your detailed response!"
if bonus_credits > 0 else ""
),
bonus_credits=bonus_credits,
)
@router.get("/beta/feedback-form")
async def get_feedback_form_data(
authorization: Optional[str] = Header(None),
user_service: UserService = Depends(get_user_service),
):
"""
Get data needed to show the feedback form.
Returns whether the user needs to submit feedback and any job context.
"""
if not authorization:
raise HTTPException(status_code=401, detail="Authentication required")
token = authorization.replace("Bearer ", "") if authorization.startswith("Bearer ") else authorization
valid, user, message = user_service.validate_session(token)
if not valid or not user:
raise HTTPException(status_code=401, detail=message)
return {
"is_beta_tester": user.is_beta_tester,
"beta_status": user.beta_tester_status,
"needs_feedback": (
user.is_beta_tester and
user.beta_tester_status == BetaTesterStatus.PENDING_FEEDBACK.value
),
"can_submit_feedback": (
user.is_beta_tester and
user.beta_tester_status != BetaTesterStatus.COMPLETED.value
),
}
# =============================================================================
# User Feedback-for-Credits Endpoints
# =============================================================================
@@ -1475,8 +1181,6 @@ class UserDetailResponse(BaseModel):
last_login_at: Optional[str] = None
total_jobs_created: int = 0
total_jobs_completed: int = 0
is_beta_tester: bool = False
beta_tester_status: Optional[str] = None
credit_transactions: list[dict] = []
recent_jobs: list[dict] = []
active_sessions_count: int = 0
@@ -1639,8 +1343,6 @@ async def get_user_detail(
last_login_at=user.last_login_at.isoformat() if user.last_login_at else None,
total_jobs_created=user.total_jobs_created,
total_jobs_completed=user.total_jobs_completed,
is_beta_tester=user.is_beta_tester,
beta_tester_status=user.beta_tester_status,
credit_transactions=credit_transactions,
recent_jobs=recent_jobs,
active_sessions_count=active_sessions_count,
@@ -1775,118 +1477,3 @@ async def set_user_role(
return {"status": "success", "message": f"User {email} role set to {role.value}"}
@router.get("/admin/beta/feedback")
async def list_beta_feedback(
limit: int = 50,
auth_data: Tuple[str, UserType, int] = Depends(require_admin),
user_service: UserService = Depends(get_user_service),
):
"""
List all beta tester feedback (admin only).
"""
from google.cloud import firestore
query = user_service.db.collection("beta_feedback")
query = query.order_by("created_at", direction=firestore.Query.DESCENDING)
query = query.limit(limit)
docs = query.stream()
feedback_list = [doc.to_dict() for doc in docs]
return {
"feedback": feedback_list,
"total": len(feedback_list),
}
@router.get("/admin/beta/stats")
async def get_beta_stats(
exclude_test: bool = True,
auth_data: Tuple[str, UserType, int] = Depends(require_admin),
user_service: UserService = Depends(get_user_service),
):
"""
Get beta tester program statistics (admin only).
Args:
exclude_test: If True (default), exclude test users from beta stats
"""
from google.cloud.firestore_v1 import FieldFilter
from google.cloud.firestore_v1 import aggregation
users_collection = user_service.db.collection(USERS_COLLECTION)
if exclude_test:
# Stream and filter in Python since Firestore doesn't support "not ends with"
all_beta_users = []
for doc in users_collection.where(filter=FieldFilter("is_beta_tester", "==", True)).stream():
data = doc.to_dict()
if not is_test_email(data.get("email", "")):
all_beta_users.append(data)
total_beta_testers = len(all_beta_users)
active_testers = sum(1 for u in all_beta_users if u.get("beta_tester_status") == "active")
pending_feedback = sum(1 for u in all_beta_users if u.get("beta_tester_status") == "pending_feedback")
completed_feedback = sum(1 for u in all_beta_users if u.get("beta_tester_status") == "completed")
# Filter feedback by non-test users
all_feedback = []
for doc in user_service.db.collection("beta_feedback").stream():
data = doc.to_dict()
if not is_test_email(data.get("user_email", "")):
all_feedback.append(data)
feedback_docs = all_feedback
else:
# Use efficient aggregation queries when including test data
def get_count(query) -> int:
agg_query = aggregation.AggregationQuery(query)
agg_query.count(alias="count")
results = agg_query.get()
return results[0][0].value if results else 0
total_beta_testers = get_count(
users_collection.where(filter=FieldFilter("is_beta_tester", "==", True))
)
active_testers = get_count(
users_collection.where(filter=FieldFilter("beta_tester_status", "==", "active"))
)
pending_feedback = get_count(
users_collection.where(filter=FieldFilter("beta_tester_status", "==", "pending_feedback"))
)
completed_feedback = get_count(
users_collection.where(filter=FieldFilter("beta_tester_status", "==", "completed"))
)
feedback_docs = [doc.to_dict() for doc in user_service.db.collection("beta_feedback").stream()]
# Calculate average ratings from feedback
avg_overall = 0
avg_ease = 0
avg_accuracy = 0
avg_correction = 0
if feedback_docs:
total = len(feedback_docs)
for data in feedback_docs:
avg_overall += data.get("overall_rating", 0)
avg_ease += data.get("ease_of_use_rating", 0)
avg_accuracy += data.get("lyrics_accuracy_rating", 0)
avg_correction += data.get("correction_experience_rating", 0)
avg_overall = round(avg_overall / total, 2)
avg_ease = round(avg_ease / total, 2)
avg_accuracy = round(avg_accuracy / total, 2)
avg_correction = round(avg_correction / total, 2)
return {
"total_beta_testers": total_beta_testers,
"active_testers": active_testers,
"pending_feedback": pending_feedback,
"completed_feedback": completed_feedback,
"total_feedback_submissions": len(feedback_docs),
"average_ratings": {
"overall": avg_overall,
"ease_of_use": avg_ease,
"lyrics_accuracy": avg_accuracy,
"correction_experience": avg_correction,
},
}
-7
View File
@@ -128,10 +128,6 @@ class Settings(BaseSettings):
enable_rate_limiting: bool = os.getenv("ENABLE_RATE_LIMITING", "true").lower() in ("true", "1", "yes")
# Maximum jobs a user can create per day (0 = unlimited)
rate_limit_jobs_per_day: int = int(os.getenv("RATE_LIMIT_JOBS_PER_DAY", "5"))
# Maximum YouTube uploads system-wide per day (0 = unlimited)
# DEPRECATED: Use youtube_quota_daily_limit instead. Kept for backward compatibility.
rate_limit_youtube_uploads_per_day: int = int(os.getenv("RATE_LIMIT_YOUTUBE_UPLOADS_PER_DAY", "10"))
# YouTube Data API v3 Quota Configuration
# Daily quota limit (YouTube grants 10,000 units/day by default)
youtube_quota_daily_limit: int = int(os.getenv("YOUTUBE_QUOTA_DAILY_LIMIT", "10000"))
@@ -139,9 +135,6 @@ class Settings(BaseSettings):
youtube_quota_upload_cost: int = int(os.getenv("YOUTUBE_QUOTA_UPLOAD_COST", "300"))
# Safety margin to reserve (avoid hitting exact limit)
youtube_quota_safety_margin: int = int(os.getenv("YOUTUBE_QUOTA_SAFETY_MARGIN", "500"))
# Maximum beta enrollments from same IP per day (0 = unlimited)
rate_limit_beta_ip_per_day: int = int(os.getenv("RATE_LIMIT_BETA_IP_PER_DAY", "1"))
# E2E test bypass key for rate limiting (set via secret in production)
e2e_bypass_key: str = os.getenv("E2E_BYPASS_KEY", "")
default_youtube_description: str = os.getenv(
-98
View File
@@ -6,7 +6,6 @@ Supports:
- Credit system for karaoke generation
- Role-based access control (user/admin)
- Stripe integration for payments
- Beta tester program with feedback collection
"""
from datetime import datetime, timezone
from enum import Enum
@@ -20,14 +19,6 @@ class UserRole(str, Enum):
ADMIN = "admin"
class BetaTesterStatus(str, Enum):
"""Status of beta tester participation."""
ACTIVE = "active" # Enrolled, free credits available
PENDING_FEEDBACK = "pending_feedback" # Job completed, awaiting feedback
COMPLETED = "completed" # Feedback submitted
EXPIRED = "expired" # 24hr deadline passed without feedback
class CreditTransaction(BaseModel):
"""Record of a credit transaction."""
id: str
@@ -92,14 +83,6 @@ class User(BaseModel):
# Optional profile fields for future use
display_name: Optional[str] = None
# Beta tester program
is_beta_tester: bool = False
beta_tester_status: Optional[BetaTesterStatus] = None
beta_enrolled_at: Optional[datetime] = None
beta_promise_text: Optional[str] = None # User's promise statement
beta_feedback_due_at: Optional[datetime] = None # 24hr after job completion
beta_feedback_email_sent: bool = False
# Push notification subscriptions (Web Push API)
# Users can subscribe from multiple devices/browsers
push_subscriptions: List[PushSubscription] = Field(default_factory=list)
@@ -214,87 +197,6 @@ class UserListResponse(BaseModel):
total: int
# ============================================================================
# Beta Tester Program Models
# ============================================================================
class BetaTesterFeedback(BaseModel):
"""
Feedback from a beta tester after using the service.
Stored in Firestore 'beta_feedback' collection.
"""
id: str # Unique feedback ID
user_email: str
job_id: Optional[str] = None # The job they're providing feedback on
# Ratings (1-5 scale)
overall_rating: int = Field(ge=1, le=5)
ease_of_use_rating: int = Field(ge=1, le=5)
lyrics_accuracy_rating: int = Field(ge=1, le=5)
correction_experience_rating: int = Field(ge=1, le=5)
# Open-ended feedback
what_went_well: Optional[str] = None
what_could_improve: Optional[str] = None
additional_comments: Optional[str] = None
# Would they recommend / use again?
would_recommend: bool = True
would_use_again: bool = True
# Metadata
created_at: datetime = Field(default_factory=datetime.utcnow)
submitted_via: str = "web" # 'web' or 'email'
class BetaTesterEnrollRequest(BaseModel):
"""Request to enroll as a beta tester."""
email: str
promise_text: str = Field(
min_length=10,
description="User's promise to provide feedback (min 10 chars)"
)
accept_corrections_work: bool = Field(
description="User accepts they may need to review/correct lyrics"
)
class BetaTesterEnrollResponse(BaseModel):
"""Response after enrolling as beta tester."""
status: str
message: str
credits_granted: int
session_token: Optional[str] = None # If new user, provide session
class BetaFeedbackRequest(BaseModel):
"""Request to submit beta tester feedback."""
job_id: Optional[str] = None
# Ratings (1-5 scale)
overall_rating: int = Field(ge=1, le=5)
ease_of_use_rating: int = Field(ge=1, le=5)
lyrics_accuracy_rating: int = Field(ge=1, le=5)
correction_experience_rating: int = Field(ge=1, le=5)
# Open-ended feedback
what_went_well: Optional[str] = None
what_could_improve: Optional[str] = None
additional_comments: Optional[str] = None
# Would they recommend / use again?
would_recommend: bool = True
would_use_again: bool = True
class BetaFeedbackResponse(BaseModel):
"""Response after submitting feedback."""
status: str
message: str
bonus_credits: int = 0 # Bonus credits for great feedback
# ============================================================================
# User Feedback-for-Credits Models
# ============================================================================
-198
View File
@@ -529,204 +529,6 @@ Complete 2 videos and earn 2 more free credits by sharing your feedback!
Get started: {self.frontend_url}
---
© {self._get_year()} Nomad Karaoke
"""
return self.provider.send_email(email, subject, html_content, text_content)
def send_beta_welcome_email(self, email: str, credits: int = 1) -> bool:
"""
Send welcome email to new beta testers.
Args:
email: User's email address
credits: Initial free credits granted
Returns:
True if email was sent successfully
"""
subject = "Welcome Beta Tester! Free Karaoke Credits Inside 🎤"
extra_styles = f"""
.beta-badge {{
display: inline-block;
background: linear-gradient(135deg, {self.BRAND_PRIMARY}, {self.BRAND_SECONDARY});
color: #333;
padding: 4px 12px;
border-radius: 20px;
font-size: 12px;
font-weight: bold;
margin-top: 10px;
}}
.credits-box {{
background-color: #ecfdf5;
border: 2px solid {self.BRAND_SUCCESS};
border-radius: 12px;
padding: 24px;
text-align: center;
margin: 20px 0;
}}
.credits-number {{
font-size: 48px;
font-weight: bold;
color: {self.BRAND_SUCCESS};
}}
.reminder {{
background-color: #fef3c7;
border: 1px solid #fcd34d;
border-radius: 8px;
padding: 16px;
margin: 20px 0;
}}
"""
content = f"""
<p>Thank you for joining our beta program!</p>
<p>As promised, here's your free credit to create a karaoke video:</p>
<div class="credits-box">
<div>Your balance:</div>
<div class="credits-number">{credits}</div>
<div>free credit{'' if credits == 1 else 's'}</div>
</div>
<div class="reminder">
<strong>📝 Your Promise:</strong><br>
Remember, as a beta tester you agreed to:
<ul style="margin: 10px 0; padding-left: 20px;">
<li>Review and correct any lyrics transcription errors</li>
<li>Share your honest feedback after trying the tool</li>
</ul>
We'll send you a quick feedback form after your job completes!
</div>
<p style="text-align: center;">
<a href="{self.frontend_url}" class="button">Create Your Karaoke Video</a>
</p>
<p>Thanks for helping us make Nomad Karaoke better!</p>
"""
extra_header = '<div><span class="beta-badge">BETA TESTER</span></div>'
html_content = self._build_email_html(content, extra_styles, extra_header)
text_content = f"""
Welcome Beta Tester!
Thank you for joining our beta program!
You've been granted {credits} free credit{'' if credits == 1 else 's'} to create karaoke videos.
YOUR PROMISE:
As a beta tester, you agreed to:
- Review and correct any lyrics transcription errors
- Share your honest feedback after trying the tool
We'll send you a quick feedback form after your job completes!
Create your karaoke video: {self.frontend_url}
Thanks for helping us make Nomad Karaoke better!
---
© {self._get_year()} Nomad Karaoke
"""
return self.provider.send_email(email, subject, html_content, text_content)
def send_feedback_request_email(self, email: str, feedback_url: str, job_title: Optional[str] = None) -> bool:
"""
Send feedback request email to beta testers.
Args:
email: User's email address
feedback_url: URL to the feedback form
job_title: Optional title of the completed job
Returns:
True if email was sent successfully
"""
subject = "Quick feedback on your karaoke experience? 🎤"
# Escape job_title to prevent XSS in email clients that render HTML
safe_job_title = html.escape(job_title) if job_title else ""
job_context = f" for <strong>{safe_job_title}</strong>" if job_title else ""
extra_styles = f"""
.feedback-box {{
background: linear-gradient(135deg, #fff0f9, #ffe0f2);
border: 2px solid {self.BRAND_PRIMARY};
border-radius: 12px;
padding: 24px;
text-align: center;
margin: 20px 0;
}}
.stars {{
font-size: 36px;
margin: 10px 0;
}}
.time-note {{
background-color: #fef3c7;
border-radius: 8px;
padding: 12px;
margin: 20px 0;
font-size: 14px;
}}
"""
content = f"""
<p>Hi there!</p>
<p>Hope you enjoyed creating your karaoke video{job_context}! As a beta tester, your feedback is super valuable to us.</p>
<div class="feedback-box">
<div class="stars">⭐⭐⭐⭐⭐</div>
<p><strong>How was your experience?</strong></p>
<p>Quick 2-minute survey - we'd love to hear your thoughts!</p>
</div>
<p style="text-align: center;">
<a href="{feedback_url}" class="button">Share Your Feedback</a>
</p>
<div class="time-note">
⏱️ This takes less than 2 minutes and helps us improve the tool for everyone!
</div>
<p>Specifically, we'd love to know:</p>
<ul>
<li>How easy was it to use?</li>
<li>Were the lyrics accurate?</li>
<li>How was the correction experience?</li>
<li>What could we improve?</li>
</ul>
<p>Thanks for being part of making Nomad Karaoke better!</p>
"""
html_content = self._build_email_html(content, extra_styles)
text_content = f"""
Quick feedback on your karaoke experience?
Hi there!
Hope you enjoyed creating your karaoke video{' for ' + safe_job_title if safe_job_title else ''}!
As a beta tester, your feedback is super valuable to us.
Share your feedback (2-minute survey): {feedback_url}
We'd love to know:
- How easy was it to use?
- Were the lyrics accurate?
- How was the correction experience?
- What could we improve?
Thanks for being part of making Nomad Karaoke better!
---
© {self._get_year()} Nomad Karaoke
"""
+1 -31
View File
@@ -2,7 +2,7 @@
Email Validation Service.
Provides email validation, normalization, and blocklist checking
to prevent abuse during beta enrollment and other flows.
to prevent abuse during enrollment and other flows.
"""
import hashlib
@@ -369,36 +369,6 @@ class EmailValidationService:
config = self.get_blocklist_config()
return ip_address in config["blocked_ips"]
def validate_email_for_beta(self, email: str) -> Tuple[bool, str]:
"""
Validate an email for beta enrollment.
Performs all validation checks:
1. Basic format validation
2. Disposable domain check
3. Blocked email check
Args:
email: Email address to validate
Returns:
Tuple of (is_valid, error_message)
"""
if not email or "@" not in email:
return False, "Invalid email format"
# Check disposable domain
if self.is_disposable_domain(email):
logger.warning(f"Beta enrollment blocked - disposable domain: {_mask_email(email)}")
return False, "Disposable email addresses are not allowed"
# Check blocked email
if self.is_email_blocked(email):
logger.warning(f"Beta enrollment blocked - email blocked: {_mask_email(email)}")
return False, "This email address is not allowed"
return True, ""
def hash_ip(self, ip_address: str) -> str:
"""
Hash an IP address for privacy-preserving storage.
+1 -375
View File
@@ -1,10 +1,9 @@
"""
Rate limiting service for job creation, YouTube uploads, and beta enrollment.
Rate limiting service for job creation.
Uses Firestore for distributed rate limit tracking with date-based document IDs
that automatically reset at UTC midnight.
"""
import hashlib
import logging
from datetime import datetime, timezone
from typing import Tuple, Optional, List, Dict, Any
@@ -18,7 +17,6 @@ logger = logging.getLogger(__name__)
# Firestore collection names
RATE_LIMITS_COLLECTION = "rate_limits"
BLOCKLISTS_COLLECTION = "blocklists"
OVERRIDES_COLLECTION = "overrides"
@@ -37,11 +35,6 @@ def _seconds_until_midnight_utc() -> int:
return int((next_midnight - now).total_seconds())
def _hash_ip(ip_address: str) -> str:
"""Hash an IP address for storage (privacy-preserving)."""
return hashlib.sha256(ip_address.encode()).hexdigest()[:16]
class RateLimitService:
"""
Service for managing rate limits across the platform.
@@ -56,9 +49,6 @@ class RateLimitService:
self.db = firestore.Client(project=settings.google_cloud_project)
else:
self.db = db
self._blocklist_cache: Optional[Dict[str, Any]] = None
self._blocklist_cache_time: Optional[datetime] = None
self._cache_ttl_seconds = 300 # 5 minute cache
# -------------------------------------------------------------------------
# Job Rate Limiting
@@ -163,195 +153,6 @@ class RateLimitService:
update_in_transaction(transaction, doc_ref)
logger.info(f"Recorded job {job_id} for user {user_email} rate limiting")
# -------------------------------------------------------------------------
# YouTube Upload Rate Limiting (DEPRECATED - use YouTubeQuotaService)
# -------------------------------------------------------------------------
def check_youtube_upload_limit(self) -> Tuple[bool, int, str]:
"""
Check if the system can perform a YouTube upload.
DEPRECATED: Use YouTubeQuotaService.check_quota_available() instead.
This method now delegates to the quota service for backward compatibility.
Returns:
Tuple of (allowed, remaining, message)
"""
try:
from backend.services.youtube_quota_service import get_youtube_quota_service
quota_service = get_youtube_quota_service()
except (ImportError, Exception) as e:
logger.warning(f"Quota service unavailable, falling back to legacy check: {e}")
# Fallback to legacy behavior below
quota_service = None
if quota_service is not None:
try:
return quota_service.check_quota_available()
except Exception as e:
logger.error(f"Quota service check failed: {e}")
return False, 0, f"Quota service error: {e}"
# Fallback to legacy behavior
if not settings.enable_rate_limiting:
return True, -1, "Rate limiting disabled"
limit = settings.rate_limit_youtube_uploads_per_day
if limit == 0:
return True, -1, "No YouTube upload limit configured"
current_count = self.get_youtube_uploads_today()
remaining = max(0, limit - current_count)
if current_count >= limit:
return False, 0, f"Daily YouTube upload limit reached ({limit} per day)"
return True, remaining, f"{remaining} YouTube uploads remaining today"
def get_youtube_uploads_today(self) -> int:
"""Get the number of YouTube uploads performed today (system-wide)."""
date_str = _get_today_date_str()
doc_ref = self.db.collection(RATE_LIMITS_COLLECTION).document(
f"youtube_uploads_{date_str}"
)
doc = doc_ref.get()
if not doc.exists:
return 0
return doc.to_dict().get("count", 0)
def record_youtube_upload(self, job_id: str, user_email: str) -> None:
"""
Record a YouTube upload for rate limiting.
DEPRECATED: Use YouTubeQuotaService.record_operation() instead.
This method still records to the legacy collection for backward compatibility.
"""
if not settings.enable_rate_limiting:
return
date_str = _get_today_date_str()
doc_ref = self.db.collection(RATE_LIMITS_COLLECTION).document(
f"youtube_uploads_{date_str}"
)
@firestore.transactional
def update_in_transaction(transaction, doc_ref):
doc = doc_ref.get(transaction=transaction)
if doc.exists:
data = doc.to_dict()
count = data.get("count", 0) + 1
uploads = data.get("uploads", [])
else:
count = 1
uploads = []
uploads.append({
"job_id": job_id,
"user_email": user_email,
"timestamp": datetime.now(timezone.utc),
})
transaction.set(doc_ref, {
"date": date_str,
"count": count,
"uploads": uploads,
"updated_at": datetime.now(timezone.utc),
})
transaction = self.db.transaction()
update_in_transaction(transaction, doc_ref)
logger.info(f"Recorded YouTube upload for job {job_id}")
# -------------------------------------------------------------------------
# Beta Enrollment IP Rate Limiting
# -------------------------------------------------------------------------
def check_beta_ip_limit(self, ip_address: str) -> Tuple[bool, int, str]:
"""
Check if an IP address can enroll in the beta program.
Args:
ip_address: Client IP address
Returns:
Tuple of (allowed, remaining, message)
"""
if not settings.enable_rate_limiting:
return True, -1, "Rate limiting disabled"
limit = settings.rate_limit_beta_ip_per_day
if limit == 0:
return True, -1, "No beta IP limit configured"
# Check today's enrollment count for this IP
ip_hash = _hash_ip(ip_address)
date_str = _get_today_date_str()
doc_ref = self.db.collection(RATE_LIMITS_COLLECTION).document(
f"beta_ip_{ip_hash}_{date_str}"
)
doc = doc_ref.get()
current_count = 0
if doc.exists:
current_count = doc.to_dict().get("count", 0)
remaining = max(0, limit - current_count)
if current_count >= limit:
seconds = _seconds_until_midnight_utc()
logger.warning(
f"Beta enrollment IP limit exceeded for {ip_hash}: {current_count}/{limit} enrollments today"
)
return (
False,
0,
f"Too many beta enrollments from this network today. Please try again tomorrow."
)
return True, remaining, f"{remaining} beta enrollments remaining from this IP today"
def record_beta_enrollment(self, ip_address: str, email: str) -> None:
"""
Record a beta enrollment for IP rate limiting.
Uses Firestore transactions for atomic increment.
"""
if not settings.enable_rate_limiting:
return
ip_hash = _hash_ip(ip_address)
date_str = _get_today_date_str()
doc_ref = self.db.collection(RATE_LIMITS_COLLECTION).document(
f"beta_ip_{ip_hash}_{date_str}"
)
@firestore.transactional
def update_in_transaction(transaction, doc_ref):
doc = doc_ref.get(transaction=transaction)
if doc.exists:
data = doc.to_dict()
count = data.get("count", 0) + 1
enrollments = data.get("enrollments", [])
else:
count = 1
enrollments = []
enrollments.append({
"email": email,
"timestamp": datetime.now(timezone.utc),
})
transaction.set(doc_ref, {
"ip_hash": ip_hash,
"date": date_str,
"count": count,
"enrollments": enrollments,
"updated_at": datetime.now(timezone.utc),
})
transaction = self.db.transaction()
update_in_transaction(transaction, doc_ref)
logger.info(f"Recorded beta enrollment from IP {ip_hash} for {email}")
# -------------------------------------------------------------------------
# User Overrides (Whitelist)
# -------------------------------------------------------------------------
@@ -422,175 +223,6 @@ class RateLimitService:
docs = self.db.collection(OVERRIDES_COLLECTION).stream()
return {doc.id: doc.to_dict() for doc in docs}
# -------------------------------------------------------------------------
# Blocklist Management
# -------------------------------------------------------------------------
def _load_blocklist(self, force_refresh: bool = False) -> Dict[str, Any]:
"""
Load blocklist from Firestore with caching.
Returns:
Dict with keys: disposable_domains, blocked_emails, blocked_ips
"""
now = datetime.now(timezone.utc)
# Check cache
if not force_refresh and self._blocklist_cache and self._blocklist_cache_time:
cache_age = (now - self._blocklist_cache_time).total_seconds()
if cache_age < self._cache_ttl_seconds:
return self._blocklist_cache
# Load from Firestore
doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config")
doc = doc_ref.get()
if not doc.exists:
# Initialize with empty blocklist
self._blocklist_cache = {
"disposable_domains": [],
"blocked_emails": [],
"blocked_ips": [],
}
else:
self._blocklist_cache = doc.to_dict()
self._blocklist_cache_time = now
return self._blocklist_cache
def is_disposable_domain(self, domain: str) -> bool:
"""Check if a domain is in the disposable domains blocklist."""
blocklist = self._load_blocklist()
return domain.lower() in [d.lower() for d in blocklist.get("disposable_domains", [])]
def is_blocked_email(self, email: str) -> bool:
"""Check if an email is explicitly blocked."""
blocklist = self._load_blocklist()
return email.lower() in [e.lower() for e in blocklist.get("blocked_emails", [])]
def is_blocked_ip(self, ip_address: str) -> bool:
"""Check if an IP address is blocked."""
blocklist = self._load_blocklist()
return ip_address in blocklist.get("blocked_ips", [])
def get_blocklist(self) -> Dict[str, Any]:
"""Get all blocklist data."""
return self._load_blocklist(force_refresh=True)
def add_disposable_domain(self, domain: str, admin_email: str) -> None:
"""Add a domain to the disposable domains blocklist."""
blocklist = self._load_blocklist(force_refresh=True)
domains = set(blocklist.get("disposable_domains", []))
domains.add(domain.lower())
doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config")
doc_ref.set({
**blocklist,
"disposable_domains": list(domains),
"updated_at": datetime.now(timezone.utc),
"updated_by": admin_email,
}, merge=True)
self._blocklist_cache = None # Invalidate cache
logger.info(f"Added disposable domain {domain} by {admin_email}")
def remove_disposable_domain(self, domain: str) -> bool:
"""Remove a domain from the disposable domains blocklist."""
blocklist = self._load_blocklist(force_refresh=True)
domains = set(blocklist.get("disposable_domains", []))
if domain.lower() not in [d.lower() for d in domains]:
return False
domains = {d for d in domains if d.lower() != domain.lower()}
doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config")
doc_ref.set({
**blocklist,
"disposable_domains": list(domains),
"updated_at": datetime.now(timezone.utc),
}, merge=True)
self._blocklist_cache = None
logger.info(f"Removed disposable domain {domain}")
return True
def add_blocked_email(self, email: str, admin_email: str) -> None:
"""Add an email to the blocked emails list."""
blocklist = self._load_blocklist(force_refresh=True)
emails = set(blocklist.get("blocked_emails", []))
emails.add(email.lower())
doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config")
doc_ref.set({
**blocklist,
"blocked_emails": list(emails),
"updated_at": datetime.now(timezone.utc),
"updated_by": admin_email,
}, merge=True)
self._blocklist_cache = None
logger.info(f"Added blocked email {email} by {admin_email}")
def remove_blocked_email(self, email: str) -> bool:
"""Remove an email from the blocked emails list."""
blocklist = self._load_blocklist(force_refresh=True)
emails = set(blocklist.get("blocked_emails", []))
if email.lower() not in [e.lower() for e in emails]:
return False
emails = {e for e in emails if e.lower() != email.lower()}
doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config")
doc_ref.set({
**blocklist,
"blocked_emails": list(emails),
"updated_at": datetime.now(timezone.utc),
}, merge=True)
self._blocklist_cache = None
logger.info(f"Removed blocked email {email}")
return True
def add_blocked_ip(self, ip_address: str, admin_email: str) -> None:
"""Add an IP address to the blocked IPs list."""
blocklist = self._load_blocklist(force_refresh=True)
ips = set(blocklist.get("blocked_ips", []))
ips.add(ip_address)
doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config")
doc_ref.set({
**blocklist,
"blocked_ips": list(ips),
"updated_at": datetime.now(timezone.utc),
"updated_by": admin_email,
}, merge=True)
self._blocklist_cache = None
logger.info(f"Added blocked IP {ip_address} by {admin_email}")
def remove_blocked_ip(self, ip_address: str) -> bool:
"""Remove an IP address from the blocked IPs list."""
blocklist = self._load_blocklist(force_refresh=True)
ips = set(blocklist.get("blocked_ips", []))
if ip_address not in ips:
return False
ips.discard(ip_address)
doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config")
doc_ref.set({
**blocklist,
"blocked_ips": list(ips),
"updated_at": datetime.now(timezone.utc),
}, merge=True)
self._blocklist_cache = None
logger.info(f"Removed blocked IP {ip_address}")
return True
# -------------------------------------------------------------------------
# Stats
# -------------------------------------------------------------------------
@@ -604,9 +236,6 @@ class RateLimitService:
"""
date_str = _get_today_date_str()
# Get YouTube upload count
youtube_count = self.get_youtube_uploads_today()
# Count unique users with jobs today
users_with_jobs = set()
total_jobs_today = 0
@@ -624,12 +253,9 @@ class RateLimitService:
return {
"date": date_str,
"youtube_uploads_today": youtube_count,
"youtube_uploads_limit": settings.rate_limit_youtube_uploads_per_day,
"total_jobs_today": total_jobs_today,
"users_with_jobs_today": len(users_with_jobs),
"job_limit_per_user": settings.rate_limit_jobs_per_day,
"beta_ip_limit_per_day": settings.rate_limit_beta_ip_per_day,
"rate_limiting_enabled": settings.enable_rate_limiting,
"seconds_until_reset": _seconds_until_midnight_utc(),
}
+114 -2
View File
@@ -12,7 +12,8 @@ that reflects actual API quota costs:
- videos.delete: 50 units
"""
import logging
from datetime import datetime
import time
from datetime import datetime, timedelta
from typing import Tuple, Optional, Dict, Any
from zoneinfo import ZoneInfo
@@ -23,6 +24,11 @@ from backend.config import settings
logger = logging.getLogger(__name__)
# GCP quota cache (module-level for singleton behavior)
_gcp_quota_cache: Optional[Dict[str, Any]] = None
_gcp_quota_cache_time: float = 0
_GCP_CACHE_TTL_SECONDS = 300 # 5 minutes
YOUTUBE_QUOTA_COLLECTION = "youtube_quota"
PACIFIC_TZ = ZoneInfo("America/Los_Angeles")
@@ -190,12 +196,17 @@ class YouTubeQuotaService:
"estimated_uploads_remaining": effective_limit // settings.youtube_quota_upload_cost if settings.youtube_quota_upload_cost > 0 else -1,
"seconds_until_reset": _seconds_until_midnight_pt(),
"operations_count": 0,
"upload_count": 0,
}
data = doc.to_dict()
consumed = data.get("units_consumed", 0)
remaining = max(0, effective_limit - consumed)
# Count videos.insert operations specifically (actual uploads)
operations = data.get("operations", [])
upload_count = sum(1 for op in operations if op.get("operation") == "videos.insert")
return {
"date_pt": date_str,
"units_consumed": consumed,
@@ -205,9 +216,110 @@ class YouTubeQuotaService:
"upload_cost": settings.youtube_quota_upload_cost,
"estimated_uploads_remaining": remaining // settings.youtube_quota_upload_cost if settings.youtube_quota_upload_cost > 0 else -1,
"seconds_until_reset": _seconds_until_midnight_pt(),
"operations_count": len(data.get("operations", [])),
"operations_count": len(operations),
"upload_count": upload_count,
}
def get_gcp_quota_usage(self) -> Dict[str, Any]:
"""
Query GCP Cloud Monitoring for actual YouTube API quota usage.
Uses the metric serviceruntime.googleapis.com/quota/rate/net_usage
for youtube.googleapis.com. Results are cached for 5 minutes.
Important: GCP metrics have ~1-2 hour delay, so they cannot replace
Firestore tracking for real-time gating. They serve as source-of-truth
for dashboard display and drift detection.
Returns:
Dict with gcp_units_consumed, gcp_last_datapoint_time,
gcp_data_delay_minutes, available flag, and drift info.
"""
global _gcp_quota_cache, _gcp_quota_cache_time
# Check cache
now = time.time()
if _gcp_quota_cache is not None and (now - _gcp_quota_cache_time) < _GCP_CACHE_TTL_SECONDS:
return _gcp_quota_cache
try:
from google.cloud import monitoring_v3
client = monitoring_v3.MetricServiceClient()
project_name = f"projects/{settings.google_cloud_project}"
# Query for today's YouTube quota usage (PT date range)
now_dt = datetime.now(PACIFIC_TZ)
start_of_day = now_dt.replace(hour=0, minute=0, second=0, microsecond=0)
interval = monitoring_v3.TimeInterval(
start_time=start_of_day,
end_time=now_dt,
)
# Query the quota usage metric for YouTube Data API
results = client.list_time_series(
request={
"name": project_name,
"filter": (
'metric.type = "serviceruntime.googleapis.com/quota/rate/net_usage" '
'AND resource.labels.service = "youtube.googleapis.com"'
),
"interval": interval,
"view": monitoring_v3.ListTimeSeriesRequest.TimeSeriesView.FULL,
"aggregation": monitoring_v3.Aggregation(
alignment_period={"seconds": 86400}, # Aggregate full day
per_series_aligner=monitoring_v3.Aggregation.Aligner.ALIGN_SUM,
),
}
)
total_units = 0
last_datapoint_time = None
for ts in results:
for point in ts.points:
total_units += int(point.value.int64_value)
point_time = point.interval.end_time
if last_datapoint_time is None or point_time > last_datapoint_time:
last_datapoint_time = point_time
# Calculate data delay
delay_minutes = None
last_datapoint_str = None
if last_datapoint_time:
delay = now_dt - last_datapoint_time.astimezone(PACIFIC_TZ)
delay_minutes = int(delay.total_seconds() / 60)
last_datapoint_str = last_datapoint_time.isoformat()
# Calculate drift against Firestore tracking
firestore_consumed = self._get_units_consumed_today()
drift = abs(total_units - firestore_consumed) if total_units > 0 else None
drift_alert = False
if drift is not None and firestore_consumed > 0:
drift_pct = drift / firestore_consumed
drift_alert = drift_pct > 0.10 # Alert if >10% drift
result = {
"available": True,
"gcp_units_consumed": total_units,
"gcp_last_datapoint_time": last_datapoint_str,
"gcp_data_delay_minutes": delay_minutes,
"drift": drift,
"drift_alert": drift_alert,
}
_gcp_quota_cache = result
_gcp_quota_cache_time = now
return result
except ImportError:
logger.debug("google-cloud-monitoring not installed, GCP quota unavailable")
return {"available": False}
except Exception as e:
logger.warning(f"GCP quota query failed: {e}")
return {"available": False}
def _get_units_consumed_today(self) -> int:
"""Get total quota units consumed today (Pacific Time)."""
date_str = _get_today_date_pt()
@@ -209,62 +209,6 @@ class TestBlocklistManagement:
assert mock_db.collection.return_value.document.return_value.get.call_count == 1
class TestBetaEnrollmentValidation:
"""Test beta enrollment validation."""
@pytest.fixture
def mock_db(self):
"""Create a mock Firestore client."""
mock = MagicMock()
mock_doc = Mock()
mock_doc.exists = True
mock_doc.to_dict.return_value = {
"disposable_domains": [],
"blocked_emails": ["blocked@example.com"],
"blocked_ips": [],
}
mock.collection.return_value.document.return_value.get.return_value = mock_doc
return mock
@pytest.fixture
def email_service(self, mock_db):
"""Create EmailValidationService instance with mocks."""
from backend.services.email_validation_service import EmailValidationService
EmailValidationService._blocklist_cache = None
EmailValidationService._blocklist_cache_time = None
service = EmailValidationService(db=mock_db)
return service
def test_validate_legitimate_email(self, email_service):
"""Test legitimate email passes validation."""
is_valid, error = email_service.validate_email_for_beta("user@gmail.com")
assert is_valid is True
assert error == ""
def test_validate_disposable_email_rejected(self, email_service):
"""Test disposable email is rejected."""
is_valid, error = email_service.validate_email_for_beta("user@tempmail.com")
assert is_valid is False
assert "Disposable email" in error
def test_validate_blocked_email_rejected(self, email_service):
"""Test blocked email is rejected."""
is_valid, error = email_service.validate_email_for_beta("blocked@example.com")
assert is_valid is False
assert "not allowed" in error
def test_validate_invalid_format_rejected(self, email_service):
"""Test invalid email format is rejected."""
is_valid, error = email_service.validate_email_for_beta("notanemail")
assert is_valid is False
assert "Invalid email format" in error
def test_validate_empty_email_rejected(self, email_service):
"""Test empty email is rejected."""
is_valid, error = email_service.validate_email_for_beta("")
assert is_valid is False
class TestIPHashing:
"""Test IP address hashing."""
+365
View File
@@ -0,0 +1,365 @@
"""
Unit tests for YouTubeQuotaService.get_gcp_quota_usage().
Tests GCP Cloud Monitoring integration for quota drift detection,
caching behavior, and graceful fallbacks.
"""
import pytest
import time
from unittest.mock import Mock, MagicMock, patch
from datetime import datetime
from zoneinfo import ZoneInfo
# Mock Google Cloud before imports
import sys
sys.modules.setdefault('google.cloud.firestore', MagicMock())
sys.modules.setdefault('google.cloud.storage', MagicMock())
PACIFIC_TZ = ZoneInfo("America/Los_Angeles")
class TestGCPQuotaMonitor:
"""Test get_gcp_quota_usage() on YouTubeQuotaService."""
@pytest.fixture
def mock_db(self):
"""Create a mock Firestore client."""
mock = MagicMock()
return mock
@pytest.fixture
def mock_settings(self):
"""Create mock settings."""
settings = Mock()
settings.google_cloud_project = "test-project"
settings.youtube_quota_daily_limit = 10000
settings.youtube_quota_upload_cost = 300
settings.youtube_quota_safety_margin = 500
return settings
@pytest.fixture(autouse=True)
def clear_gcp_cache(self):
"""Clear the module-level GCP quota cache before each test."""
import backend.services.youtube_quota_service as mod
mod._gcp_quota_cache = None
mod._gcp_quota_cache_time = 0
yield
# Also clear after test to avoid leaking state
mod._gcp_quota_cache = None
mod._gcp_quota_cache_time = 0
@pytest.fixture
def quota_service(self, mock_db, mock_settings):
"""Create YouTubeQuotaService instance with mocks."""
with patch('backend.services.youtube_quota_service.settings', mock_settings):
from backend.services.youtube_quota_service import YouTubeQuotaService
service = YouTubeQuotaService(db=mock_db)
return service
def _make_mock_point(self, value, end_time):
"""Create a mock Cloud Monitoring data point."""
point = Mock()
point.value.int64_value = value
point.interval.end_time = end_time
return point
def _make_mock_time_series(self, points):
"""Create a mock time series with given points."""
ts = Mock()
ts.points = points
return ts
# =========================================================================
# Successful Query Tests
# =========================================================================
def test_successful_query_returns_units_consumed(self, quota_service, mock_db, mock_settings):
"""Test successful GCP monitoring query returns units and drift info."""
# Mock Firestore for _get_units_consumed_today (for drift calc)
mock_doc = Mock()
mock_doc.exists = True
mock_doc.to_dict.return_value = {"units_consumed": 800}
mock_db.collection.return_value.document.return_value.get.return_value = mock_doc
# Mock the Cloud Monitoring client
now_pt = datetime.now(PACIFIC_TZ)
mock_point = self._make_mock_point(850, now_pt)
mock_ts = self._make_mock_time_series([mock_point])
mock_monitoring_client = Mock()
mock_monitoring_client.list_time_series.return_value = [mock_ts]
mock_monitoring_v3 = MagicMock()
mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client
# The method does `from google.cloud import monitoring_v3` locally,
# so we inject the mock into sys.modules for that import to resolve.
with patch('backend.services.youtube_quota_service.settings', mock_settings):
sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3
try:
result = quota_service.get_gcp_quota_usage()
finally:
sys.modules.pop('google.cloud.monitoring_v3', None)
assert result["available"] is True
assert result["gcp_units_consumed"] == 850
assert result["gcp_last_datapoint_time"] is not None
# Drift = abs(850 - 800) = 50
assert result["drift"] == 50
assert result["drift_alert"] is False # 50/800 = 6.25% < 10%
def test_successful_query_multiple_time_series(self, quota_service, mock_db, mock_settings):
"""Test query with multiple time series sums all values."""
mock_doc = Mock()
mock_doc.exists = True
mock_doc.to_dict.return_value = {"units_consumed": 500}
mock_db.collection.return_value.document.return_value.get.return_value = mock_doc
now_pt = datetime.now(PACIFIC_TZ)
mock_point1 = self._make_mock_point(200, now_pt)
mock_point2 = self._make_mock_point(300, now_pt)
mock_ts1 = self._make_mock_time_series([mock_point1])
mock_ts2 = self._make_mock_time_series([mock_point2])
mock_monitoring_client = Mock()
mock_monitoring_client.list_time_series.return_value = [mock_ts1, mock_ts2]
mock_monitoring_v3 = MagicMock()
mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client
with patch('backend.services.youtube_quota_service.settings', mock_settings):
sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3
try:
result = quota_service.get_gcp_quota_usage()
finally:
sys.modules.pop('google.cloud.monitoring_v3', None)
assert result["available"] is True
assert result["gcp_units_consumed"] == 500 # 200 + 300
def test_successful_query_no_data_points(self, quota_service, mock_db, mock_settings):
"""Test query that returns no data points (e.g., start of day)."""
mock_doc = Mock()
mock_doc.exists = False
mock_db.collection.return_value.document.return_value.get.return_value = mock_doc
mock_monitoring_client = Mock()
mock_monitoring_client.list_time_series.return_value = []
mock_monitoring_v3 = MagicMock()
mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client
with patch('backend.services.youtube_quota_service.settings', mock_settings):
sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3
try:
result = quota_service.get_gcp_quota_usage()
finally:
sys.modules.pop('google.cloud.monitoring_v3', None)
assert result["available"] is True
assert result["gcp_units_consumed"] == 0
assert result["gcp_last_datapoint_time"] is None
assert result["drift"] is None # No GCP data to compare
# =========================================================================
# Caching Tests
# =========================================================================
def test_caching_returns_cached_result(self, quota_service, mock_db, mock_settings):
"""Test that second call within 5 min returns cached result."""
mock_doc = Mock()
mock_doc.exists = False
mock_db.collection.return_value.document.return_value.get.return_value = mock_doc
mock_monitoring_client = Mock()
mock_monitoring_client.list_time_series.return_value = []
mock_monitoring_v3 = MagicMock()
mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client
with patch('backend.services.youtube_quota_service.settings', mock_settings):
sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3
try:
# First call - should hit the monitoring API
result1 = quota_service.get_gcp_quota_usage()
assert mock_monitoring_client.list_time_series.call_count == 1
# Second call - should use cache
result2 = quota_service.get_gcp_quota_usage()
assert mock_monitoring_client.list_time_series.call_count == 1 # Not called again
finally:
sys.modules.pop('google.cloud.monitoring_v3', None)
assert result1 == result2
def test_cache_expires_after_ttl(self, quota_service, mock_db, mock_settings):
"""Test that cache expires and re-queries after TTL."""
import backend.services.youtube_quota_service as mod
mock_doc = Mock()
mock_doc.exists = False
mock_db.collection.return_value.document.return_value.get.return_value = mock_doc
mock_monitoring_client = Mock()
mock_monitoring_client.list_time_series.return_value = []
mock_monitoring_v3 = MagicMock()
mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client
with patch('backend.services.youtube_quota_service.settings', mock_settings):
sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3
try:
# First call
quota_service.get_gcp_quota_usage()
assert mock_monitoring_client.list_time_series.call_count == 1
# Simulate cache expiry by backdating the cache time
mod._gcp_quota_cache_time = time.time() - 301 # 301 seconds ago (TTL is 300)
# Second call - should re-query
quota_service.get_gcp_quota_usage()
assert mock_monitoring_client.list_time_series.call_count == 2
finally:
sys.modules.pop('google.cloud.monitoring_v3', None)
# =========================================================================
# ImportError Fallback Tests
# =========================================================================
def test_import_error_fallback(self, quota_service, mock_settings):
"""Test graceful fallback when google-cloud-monitoring is not installed."""
# Remove the module from sys.modules so the import fails
saved = sys.modules.pop('google.cloud.monitoring_v3', None)
with patch('backend.services.youtube_quota_service.settings', mock_settings):
# Patch the import to raise ImportError
original_import = __builtins__.__import__ if hasattr(__builtins__, '__import__') else __import__
def mock_import(name, *args, **kwargs):
if 'monitoring_v3' in name:
raise ImportError("No module named 'google.cloud.monitoring_v3'")
return original_import(name, *args, **kwargs)
with patch('builtins.__import__', side_effect=mock_import):
result = quota_service.get_gcp_quota_usage()
assert result == {"available": False}
# Restore if it was there
if saved is not None:
sys.modules['google.cloud.monitoring_v3'] = saved
# =========================================================================
# General Exception Fallback Tests
# =========================================================================
def test_general_exception_fallback(self, quota_service, mock_settings):
"""Test graceful fallback when Cloud Monitoring query raises an error."""
mock_monitoring_client = Mock()
mock_monitoring_client.list_time_series.side_effect = Exception("API quota exceeded")
mock_monitoring_v3 = MagicMock()
mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client
with patch('backend.services.youtube_quota_service.settings', mock_settings):
sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3
try:
result = quota_service.get_gcp_quota_usage()
finally:
sys.modules.pop('google.cloud.monitoring_v3', None)
assert result == {"available": False}
# =========================================================================
# Drift Alert Tests
# =========================================================================
def test_drift_alert_when_drift_exceeds_threshold(self, quota_service, mock_db, mock_settings):
"""Test drift alert is triggered when drift > 10% of Firestore value."""
# Firestore says 1000 units consumed
mock_doc = Mock()
mock_doc.exists = True
mock_doc.to_dict.return_value = {"units_consumed": 1000}
mock_db.collection.return_value.document.return_value.get.return_value = mock_doc
# GCP says 1200 units consumed (200 drift = 20% > 10%)
now_pt = datetime.now(PACIFIC_TZ)
mock_point = self._make_mock_point(1200, now_pt)
mock_ts = self._make_mock_time_series([mock_point])
mock_monitoring_client = Mock()
mock_monitoring_client.list_time_series.return_value = [mock_ts]
mock_monitoring_v3 = MagicMock()
mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client
with patch('backend.services.youtube_quota_service.settings', mock_settings):
sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3
try:
result = quota_service.get_gcp_quota_usage()
finally:
sys.modules.pop('google.cloud.monitoring_v3', None)
assert result["available"] is True
assert result["drift"] == 200 # abs(1200 - 1000)
assert result["drift_alert"] is True # 200/1000 = 20% > 10%
def test_no_drift_alert_within_threshold(self, quota_service, mock_db, mock_settings):
"""Test no drift alert when drift <= 10% of Firestore value."""
# Firestore says 1000 units consumed
mock_doc = Mock()
mock_doc.exists = True
mock_doc.to_dict.return_value = {"units_consumed": 1000}
mock_db.collection.return_value.document.return_value.get.return_value = mock_doc
# GCP says 1050 units consumed (50 drift = 5% <= 10%)
now_pt = datetime.now(PACIFIC_TZ)
mock_point = self._make_mock_point(1050, now_pt)
mock_ts = self._make_mock_time_series([mock_point])
mock_monitoring_client = Mock()
mock_monitoring_client.list_time_series.return_value = [mock_ts]
mock_monitoring_v3 = MagicMock()
mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client
with patch('backend.services.youtube_quota_service.settings', mock_settings):
sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3
try:
result = quota_service.get_gcp_quota_usage()
finally:
sys.modules.pop('google.cloud.monitoring_v3', None)
assert result["available"] is True
assert result["drift"] == 50
assert result["drift_alert"] is False
def test_no_drift_when_firestore_zero(self, quota_service, mock_db, mock_settings):
"""Test drift_alert is False when Firestore has zero usage (avoid division by zero)."""
mock_doc = Mock()
mock_doc.exists = False
mock_db.collection.return_value.document.return_value.get.return_value = mock_doc
now_pt = datetime.now(PACIFIC_TZ)
mock_point = self._make_mock_point(100, now_pt)
mock_ts = self._make_mock_time_series([mock_point])
mock_monitoring_client = Mock()
mock_monitoring_client.list_time_series.return_value = [mock_ts]
mock_monitoring_v3 = MagicMock()
mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client
with patch('backend.services.youtube_quota_service.settings', mock_settings):
sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3
try:
result = quota_service.get_gcp_quota_usage()
finally:
sys.modules.pop('google.cloud.monitoring_v3', None)
assert result["available"] is True
assert result["gcp_units_consumed"] == 100
# Firestore consumed is 0, so drift_alert should be False (no division by zero)
assert result["drift_alert"] is False
+24 -122
View File
@@ -1,7 +1,7 @@
"""
Unit tests for RateLimitService.
Tests rate limiting logic for jobs, YouTube uploads, and beta enrollments.
Tests rate limiting logic for job creation, user overrides, and stats.
"""
import pytest
@@ -29,8 +29,6 @@ class TestRateLimitService:
settings = Mock()
settings.enable_rate_limiting = True
settings.rate_limit_jobs_per_day = 5
settings.rate_limit_youtube_uploads_per_day = 10
settings.rate_limit_beta_ip_per_day = 1
return settings
@pytest.fixture
@@ -130,10 +128,6 @@ class TestRateLimitService:
mock_override_doc.exists = True
mock_override_doc.to_dict.return_value = {"bypass_job_limit": True}
def mock_get(*args, **kwargs):
# Return different docs based on collection
return mock_override_doc if "overrides" in str(mock_db.collection.call_args) else mock_job_doc
mock_db.collection.return_value.document.return_value.get.side_effect = [mock_override_doc, mock_job_doc]
with patch('backend.services.rate_limit_service.settings', mock_settings):
@@ -172,105 +166,6 @@ class TestRateLimitService:
assert remaining == -1
assert "Rate limiting disabled" in message
# =========================================================================
# YouTube Upload Rate Limiting Tests
# =========================================================================
def test_check_youtube_limit_delegates_to_quota_service(self, rate_limit_service, mock_db, mock_settings):
"""Test YouTube upload check delegates to YouTubeQuotaService."""
mock_quota_service = Mock()
mock_quota_service.check_quota_available.return_value = (True, 9500, "9500 quota units remaining")
with patch('backend.services.rate_limit_service.settings', mock_settings), \
patch('backend.services.youtube_quota_service.get_youtube_quota_service', return_value=mock_quota_service):
allowed, remaining, message = rate_limit_service.check_youtube_upload_limit()
assert allowed is True
assert remaining == 9500
mock_quota_service.check_quota_available.assert_called_once()
def test_check_youtube_limit_quota_exceeded(self, rate_limit_service, mock_db, mock_settings):
"""Test YouTube upload check when quota is exceeded."""
mock_quota_service = Mock()
mock_quota_service.check_quota_available.return_value = (False, 0, "YouTube API quota exhausted")
with patch('backend.services.rate_limit_service.settings', mock_settings), \
patch('backend.services.youtube_quota_service.get_youtube_quota_service', return_value=mock_quota_service):
allowed, remaining, message = rate_limit_service.check_youtube_upload_limit()
assert allowed is False
assert remaining == 0
assert "quota" in message.lower()
def test_check_youtube_limit_fallback_on_quota_service_error(self, rate_limit_service, mock_db, mock_settings):
"""Test YouTube upload falls back to legacy when quota service unavailable."""
mock_doc = Mock()
mock_doc.exists = True
mock_doc.to_dict.return_value = {"count": 3}
mock_db.collection.return_value.document.return_value.get.return_value = mock_doc
with patch('backend.services.rate_limit_service.settings', mock_settings), \
patch('backend.services.youtube_quota_service.get_youtube_quota_service', side_effect=Exception("Service unavailable")):
allowed, remaining, message = rate_limit_service.check_youtube_upload_limit()
assert allowed is True
assert remaining == 7 # 10 - 3
def test_check_youtube_limit_fallback_zero_configured(self, rate_limit_service, mock_db, mock_settings):
"""Test that zero limit means unlimited when falling back to legacy."""
mock_settings.rate_limit_youtube_uploads_per_day = 0
with patch('backend.services.rate_limit_service.settings', mock_settings), \
patch('backend.services.youtube_quota_service.get_youtube_quota_service', side_effect=Exception("Service unavailable")):
allowed, remaining, message = rate_limit_service.check_youtube_upload_limit()
assert allowed is True
assert remaining == -1
assert "No YouTube upload limit" in message
def test_check_youtube_limit_denies_on_runtime_error(self, rate_limit_service, mock_db, mock_settings):
"""Test YouTube upload denied when quota service check_quota_available raises."""
mock_quota_service = Mock()
mock_quota_service.check_quota_available.side_effect = Exception("Firestore timeout")
with patch('backend.services.rate_limit_service.settings', mock_settings), \
patch('backend.services.youtube_quota_service.get_youtube_quota_service', return_value=mock_quota_service):
allowed, remaining, message = rate_limit_service.check_youtube_upload_limit()
assert allowed is False
assert remaining == 0
assert "Quota service error" in message
# =========================================================================
# Beta Enrollment IP Rate Limiting Tests
# =========================================================================
def test_check_beta_ip_limit_first_enrollment(self, rate_limit_service, mock_db, mock_settings):
"""Test first enrollment from IP is allowed."""
mock_doc = Mock()
mock_doc.exists = False
mock_db.collection.return_value.document.return_value.get.return_value = mock_doc
with patch('backend.services.rate_limit_service.settings', mock_settings):
allowed, remaining, message = rate_limit_service.check_beta_ip_limit("192.168.1.1")
assert allowed is True
assert remaining == 1
def test_check_beta_ip_limit_already_enrolled(self, rate_limit_service, mock_db, mock_settings):
"""Test second enrollment from same IP is blocked."""
mock_doc = Mock()
mock_doc.exists = True
mock_doc.to_dict.return_value = {"count": 1}
mock_db.collection.return_value.document.return_value.get.return_value = mock_doc
with patch('backend.services.rate_limit_service.settings', mock_settings):
allowed, remaining, message = rate_limit_service.check_beta_ip_limit("192.168.1.1")
assert allowed is False
assert remaining == 0
assert "Too many beta enrollments" in message
# =========================================================================
# Recording Tests
# =========================================================================
@@ -293,22 +188,6 @@ class TestRateLimitService:
# Verify transaction was used
mock_db.transaction.assert_called()
def test_record_youtube_upload(self, rate_limit_service, mock_db, mock_settings):
"""Test recording a YouTube upload."""
mock_transaction = Mock()
mock_db.transaction.return_value = mock_transaction
mock_doc = Mock()
mock_doc.exists = False
mock_db.collection.return_value.document.return_value.get.return_value = mock_doc
with patch('backend.services.rate_limit_service.settings', mock_settings):
with patch('backend.services.rate_limit_service.firestore') as mock_firestore:
mock_firestore.transactional = lambda f: f
rate_limit_service.record_youtube_upload("job123", "user@example.com")
mock_db.transaction.assert_called()
def test_record_skipped_when_disabled(self, rate_limit_service, mock_db, mock_settings):
"""Test that recording is skipped when rate limiting is disabled."""
mock_settings.enable_rate_limiting = False
@@ -381,6 +260,29 @@ class TestRateLimitService:
assert result is False
# =========================================================================
# Stats Tests
# =========================================================================
def test_get_rate_limit_stats(self, rate_limit_service, mock_db, mock_settings):
"""Test getting rate limit statistics."""
# Mock Firestore stream returning one user's job doc
mock_doc = Mock()
mock_doc.to_dict.return_value = {
"user_email": "user@example.com",
"count": 3,
}
mock_db.collection.return_value.where.return_value.stream.return_value = [mock_doc]
with patch('backend.services.rate_limit_service.settings', mock_settings):
stats = rate_limit_service.get_rate_limit_stats()
assert stats["rate_limiting_enabled"] is True
assert stats["job_limit_per_user"] == 5
assert stats["total_jobs_today"] == 3
assert stats["users_with_jobs_today"] == 1
assert "seconds_until_reset" in stats
class TestRateLimitExceededError:
"""Test RateLimitExceededError exception."""
+16 -6
View File
@@ -47,8 +47,6 @@ def mock_settings():
settings = Mock()
settings.enable_rate_limiting = True
settings.rate_limit_jobs_per_day = 5
settings.rate_limit_youtube_uploads_per_day = 10
settings.rate_limit_beta_ip_per_day = 1
return settings
@@ -65,7 +63,6 @@ class TestGetRateLimitStats:
# Setup rate limit service mock
mock_rls = Mock()
mock_rls.get_youtube_uploads_today.return_value = 3
mock_rls.get_all_overrides.return_value = {"user1@example.com": {}}
mock_get_rls.return_value = mock_rls
@@ -87,9 +84,11 @@ class TestGetRateLimitStats:
"units_limit": 10000,
"effective_limit": 9500,
"upload_cost": 300,
"upload_count": 3,
"estimated_uploads_remaining": 28,
"seconds_until_reset": 43200,
}
mock_quota.get_gcp_quota_usage.return_value = {"available": False}
mock_get_qs.return_value = mock_quota
# Setup queue service mock
@@ -111,15 +110,26 @@ class TestGetRateLimitStats:
assert response.status_code == 200
data = response.json()
assert data["jobs_per_day_limit"] == 5
assert data["youtube_uploads_per_day_limit"] == 10
assert data["rate_limiting_enabled"] is True
assert data["youtube_uploads_today"] == 3
assert data["youtube_uploads_remaining"] == 7
assert data["disposable_domains_count"] == 100
assert data["total_overrides"] == 1
# New quota fields
# Quota unit fields
assert data["youtube_quota_units_consumed"] == 900
assert data["youtube_quota_units_remaining"] == 8600
assert data["youtube_quota_daily_limit"] == 10000
assert data["youtube_quota_effective_limit"] == 9500
assert data["youtube_quota_upload_cost"] == 300
assert data["youtube_quota_estimated_uploads_remaining"] == 28
# Queue fields
assert data["youtube_uploads_queued"] == 0
assert data["youtube_uploads_failed"] == 0
# GCP quota (unavailable in this test)
assert data["gcp_quota_available"] is False
# Removed fields should not be present
assert "youtube_uploads_per_day_limit" not in data
assert "beta_ip_per_day_limit" not in data
assert "youtube_uploads_remaining" not in data
class TestGetUserRateLimitStatus:
@@ -612,16 +612,6 @@ class VideoWorkerOrchestrator:
except Exception as e:
self.job_log.warning(f"Failed to record YouTube quota usage: {e}")
# Also record in legacy rate limit service for backward compatibility
try:
from backend.services.rate_limit_service import get_rate_limit_service
rate_limit_service = get_rate_limit_service()
rate_limit_service.record_youtube_upload(
job_id=self.config.job_id,
user_email=user_email
)
except Exception as e:
self.job_log.warning(f"Failed to record YouTube upload for legacy rate limiting: {e}")
else:
self.job_log.warning("YouTube upload did not return a URL")
+18 -62
View File
@@ -774,47 +774,9 @@ POST /api/users/webhooks/stripe
Handles `checkout.session.completed` events.
## Beta Tester Program (Deprecated)
> **Note**: The beta program was removed from the homepage in PR #430. These endpoints are preserved for backward compatibility but are no longer actively promoted. New users receive 2 welcome credits instead. See [User Feedback for Credits](#user-feedback-for-credits) for the current feedback-for-credits mechanism.
### Enroll as Beta Tester
```http
POST /api/users/beta/enroll
Content-Type: application/json
{
"email": "user@example.com",
"promise_text": "I promise to provide feedback...",
"accept_corrections_work": true
}
```
Returns 1 free credit and session token.
### Submit Beta Feedback
```http
POST /api/users/beta/feedback
Authorization: Bearer SESSION_TOKEN
Content-Type: application/json
{
"overall_rating": 4,
"ease_of_use_rating": 5,
"lyrics_accuracy_rating": 4,
"correction_experience_rating": 3,
"what_went_well": "...",
"what_could_improve": "..."
}
```
Bonus credit for detailed feedback (50+ chars).
### User Feedback for Credits
Available to all users (not just beta testers). Users who have completed 2+ jobs can submit feedback to earn 2 free credits.
Users who have completed 2+ jobs can submit feedback to earn 2 free credits.
#### Check Eligibility
@@ -903,8 +865,7 @@ Returns platform statistics:
"complete": 400,
"failed": 20
},
"total_credits_issued_30d": 200,
"total_beta_testers": 30
"total_credits_issued_30d": 200
}
```
@@ -997,24 +958,6 @@ POST /api/users/admin/users/{email}/disable
Authorization: Bearer ADMIN_TOKEN
```
### Beta Stats (Admin)
```http
GET /api/users/admin/beta/stats
GET /api/users/admin/beta/stats?exclude_test=false
Authorization: Bearer ADMIN_TOKEN
```
Query parameters:
- `exclude_test` (bool, default: true) - Filter out test users from beta statistics
### Beta Feedback List (Admin)
```http
GET /api/users/admin/beta/feedback?limit=50
Authorization: Bearer ADMIN_TOKEN
```
### Delete Job (Admin)
```http
@@ -1568,8 +1511,7 @@ Called by Cloud Tasks 5 minutes after a job enters a blocking state (primarily `
### User Limits
- **Jobs per day**: 5 (configurable via `RATE_LIMIT_JOBS_PER_DAY`)
- **YouTube uploads**: Quota-aware, ~33 uploads/day within 10,000 units/day API quota (configurable via `YOUTUBE_QUOTA_DAILY_LIMIT`, `YOUTUBE_QUOTA_UPLOAD_COST`, `YOUTUBE_QUOTA_SAFETY_MARGIN`). Uploads exceeding quota are queued and processed hourly.
- **Beta enrollment per IP**: 1 per 24 hours (configurable via `RATE_LIMIT_BETA_IP_PER_DAY`)
- **YouTube uploads**: Quota-aware, ~33 uploads/day within 10,000 units/day API quota (configurable via `YOUTUBE_QUOTA_DAILY_LIMIT`, `YOUTUBE_QUOTA_UPLOAD_COST`, `YOUTUBE_QUOTA_SAFETY_MARGIN`). Uploads exceeding quota are queued and processed hourly. GCP Cloud Monitoring integration provides cross-referencing of actual API quota consumption (see Admin Rate Limits API).
Rate limiting can be disabled via `ENABLE_RATE_LIMITING=false`.
@@ -1598,7 +1540,21 @@ Admins can grant users bypass permissions or custom limits via the admin UI at `
```http
GET /api/admin/rate-limits/stats
```
Returns current rate limit statistics (usage, blocklist counts, override counts).
Returns current rate limit statistics including YouTube quota usage, blocklist counts, override counts, and GCP Cloud Monitoring cross-reference data.
Response fields include:
- `jobs_per_day_limit`, `rate_limiting_enabled` - Configuration
- `youtube_uploads_today` - Upload count from Firestore quota tracking (PT-based)
- `youtube_quota_units_consumed`, `youtube_quota_units_remaining`, `youtube_quota_daily_limit`, `youtube_quota_effective_limit`, `youtube_quota_upload_cost`, `youtube_quota_estimated_uploads_remaining`, `youtube_quota_seconds_until_reset` - YouTube quota unit tracking
- `youtube_uploads_queued`, `youtube_uploads_failed` - Upload queue status
- `disposable_domains_count`, `blocked_emails_count`, `blocked_ips_count` - Blocklist stats
- `total_overrides` - User override count
- `gcp_quota_available` - Whether GCP Cloud Monitoring data is available
- `gcp_quota_units_consumed` - Actual API quota units consumed per GCP (may differ from local tracking)
- `gcp_quota_last_datapoint` - Timestamp of the most recent GCP monitoring data point
- `gcp_quota_data_delay_minutes` - How stale the GCP data is (Cloud Monitoring has ~5 min delay)
- `quota_drift` - Difference between local tracking and GCP-reported consumption
- `quota_drift_alert` - Whether drift exceeds alert threshold
```http
GET /api/admin/rate-limits/users/{email}
+4 -3
View File
@@ -169,6 +169,7 @@ LyricsTranscriber LyricsTranscriber
| SendGrid | Email notifications | Optional |
| Cloud Tasks | Delayed task scheduling (idle reminders, YouTube queue) | Optional |
| Cloud Scheduler | Hourly YouTube upload queue processing | Optional |
| Cloud Monitoring | Cross-reference YouTube API quota consumption from GCP metrics | Optional |
| karaoke-decide | Song catalog (MusicBrainz + Spotify) for autocomplete | Optional |
| KaraokeNerds | Community karaoke version detection | Optional |
@@ -186,7 +187,6 @@ karaoke-gen shares a GCP project (`nomadkaraoke`) with karaoke-decide, but uses
| `sessions` | Magic link auth sessions | user_email, token, expires_at |
| `magic_links` | Passwordless auth tokens | email, token, expires_at, used |
| `user_feedback` | User feedback for credits (all users) | user_email, ratings, comments, created_at |
| `beta_feedback` | Beta program feedback (deprecated) | user_email, ratings, comments |
| `youtube_quota` | Daily YouTube API quota tracking | date_pt, units_consumed, units_limit, operations[] |
| `youtube_upload_queue` | Deferred YouTube uploads | job_id, status, user_email, queued_at, youtube_url |
@@ -238,7 +238,8 @@ The Video Worker uses an orchestrator pattern to ensure all features work regard
| `template_service.py` | GCS-backed email templates |
| `job_notification_service.py` | Email orchestration (completion, reminders) |
| `audio_transcoding_service.py` | Transcode FLAC → OGG Opus for review UI playback |
| `youtube_quota_service.py` | Track YouTube API quota units (Firestore, PT midnight reset) |
| `youtube_quota_service.py` | Track YouTube API quota units (Firestore, PT midnight reset) + GCP Cloud Monitoring cross-reference |
| `email_validation_service.py` | Email validation, disposable domain detection, blocklist management |
| `youtube_upload_queue_service.py` | Deferred YouTube upload queue management |
| `catalog_proxy_service.py` | Proxy to karaoke-decide catalog API (artist/track search) |
| `karaokenerds_service.py` | Scrape karaokenerds.com for community karaoke versions |
@@ -466,7 +467,7 @@ if tenant_config and not tenant_config.features.audio_search:
### Tenant Portal Frontend (B2B Deployment)
B2B tenants are served from a **separate Cloudflare Pages project** (`karaoke-gen-tenant`) built with `NEXT_PUBLIC_TENANT_PORTAL=true`. This build flag enables tree-shaking of consumer-only content (landing page, pricing, beta enrollment) at build time, keeping the bundle lean for white-label deployments.
B2B tenants are served from a **separate Cloudflare Pages project** (`karaoke-gen-tenant`) built with `NEXT_PUBLIC_TENANT_PORTAL=true`. This build flag enables tree-shaking of consumer-only content (landing page, pricing) at build time, keeping the bundle lean for white-label deployments.
**Runtime tenant injection**: A Cloudflare Pages edge function intercepts HTML responses and injects tenant-specific configuration before the page reaches the browser:
-5
View File
@@ -123,10 +123,6 @@ This is significantly cheaper than:
- Hiring a studio: $100+ per track
- DIY tools + your time: Hours of frustration
### Beta Tester Program
New users can join the beta program to get free credits in exchange for feedback, helping improve the system while building initial user base.
### B2B / White-Label
Commercial producers pay for API access or custom integration to generate tracks at scale with their own branding, instrumentals, and distribution.
@@ -144,7 +140,6 @@ Commercial producers pay for API access or custom integration to generate tracks
- Token-based authentication
- Magic link authentication
- Payment flow (Stripe)
- Beta tester program
- YouTube upload integration
- Dropbox/Google Drive distribution
-288
View File
@@ -1,288 +0,0 @@
"use client"
import { useEffect, useState } from "react"
import { adminApi, AdminBetaStats, AdminBetaFeedback } from "@/lib/api"
import { useAdminSettings } from "@/lib/admin-settings"
import { StatsCard, StatsGrid } from "@/components/admin/stats-card"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Badge } from "@/components/ui/badge"
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"
import {
Users,
MessageSquare,
Star,
RefreshCw,
Loader2,
ThumbsUp,
ThumbsDown,
} from "lucide-react"
import { Button } from "@/components/ui/button"
import { useToast } from "@/hooks/use-toast"
export default function AdminBetaPage() {
const { toast } = useToast()
const { showTestData } = useAdminSettings()
const [stats, setStats] = useState<AdminBetaStats | null>(null)
const [feedback, setFeedback] = useState<AdminBetaFeedback[]>([])
const [loading, setLoading] = useState(true)
const loadData = async () => {
try {
setLoading(true)
const [statsData, feedbackData] = await Promise.all([
adminApi.getBetaStats({ exclude_test: !showTestData }),
adminApi.getBetaFeedback(50),
])
setStats(statsData)
setFeedback(feedbackData.feedback || [])
} catch (err: any) {
console.error("Failed to load beta data:", err)
toast({
title: "Error",
description: err.message || "Failed to load beta program data",
variant: "destructive",
})
} finally {
setLoading(false)
}
}
useEffect(() => {
loadData()
}, [showTestData])
const renderStars = (rating?: number) => {
if (!rating) return "—"
return (
<div className="flex gap-0.5">
{[1, 2, 3, 4, 5].map((star) => (
<Star
key={star}
className={`w-4 h-4 ${
star <= rating ? "fill-yellow-400 text-yellow-400" : "text-muted"
}`}
/>
))}
</div>
)
}
const formatDate = (dateStr?: string) => {
if (!dateStr) return "—"
const date = new Date(dateStr)
if (isNaN(date.getTime())) return "—"
return date.toLocaleDateString()
}
return (
<div className="space-y-6">
{/* Header */}
<div className="flex items-center justify-between">
<div>
<h1 className="text-2xl font-bold tracking-tight">Beta Program</h1>
<p className="text-muted-foreground">
Manage beta testers and view feedback
</p>
</div>
<Button variant="outline" size="sm" onClick={loadData} disabled={loading}>
<RefreshCw className={`w-4 h-4 mr-2 ${loading ? "animate-spin" : ""}`} />
Refresh
</Button>
</div>
{/* Stats Grid */}
<StatsGrid>
<StatsCard
title="Total Beta Testers"
value={stats?.total_beta_testers ?? 0}
icon={Users}
loading={loading}
/>
<StatsCard
title="Active Testers"
value={stats?.active_testers ?? 0}
icon={Users}
loading={loading}
/>
<StatsCard
title="Pending Feedback"
value={stats?.pending_feedback ?? 0}
icon={MessageSquare}
loading={loading}
/>
<StatsCard
title="Total Feedback"
value={stats?.total_feedback_submissions ?? 0}
icon={MessageSquare}
loading={loading}
/>
</StatsGrid>
{/* Average Ratings */}
<Card>
<CardHeader>
<CardTitle className="text-base">Average Ratings</CardTitle>
<CardDescription>Overall feedback scores from beta testers</CardDescription>
</CardHeader>
<CardContent>
{loading ? (
<div className="flex items-center justify-center py-8">
<Loader2 className="w-6 h-6 animate-spin text-muted-foreground" />
</div>
) : stats?.average_ratings ? (
<div className="grid gap-4 md:grid-cols-4">
<div className="rounded-lg border p-4">
<p className="text-sm text-muted-foreground">Overall</p>
<div className="flex items-center gap-2 mt-1">
<span className="text-2xl font-bold">
{stats.average_ratings.overall?.toFixed(1) || "—"}
</span>
<span className="text-muted-foreground">/ 5</span>
</div>
</div>
<div className="rounded-lg border p-4">
<p className="text-sm text-muted-foreground">Ease of Use</p>
<div className="flex items-center gap-2 mt-1">
<span className="text-2xl font-bold">
{stats.average_ratings.ease_of_use?.toFixed(1) || "—"}
</span>
<span className="text-muted-foreground">/ 5</span>
</div>
</div>
<div className="rounded-lg border p-4">
<p className="text-sm text-muted-foreground">Lyrics Accuracy</p>
<div className="flex items-center gap-2 mt-1">
<span className="text-2xl font-bold">
{stats.average_ratings.lyrics_accuracy?.toFixed(1) || "—"}
</span>
<span className="text-muted-foreground">/ 5</span>
</div>
</div>
<div className="rounded-lg border p-4">
<p className="text-sm text-muted-foreground">Correction Experience</p>
<div className="flex items-center gap-2 mt-1">
<span className="text-2xl font-bold">
{stats.average_ratings.correction_experience?.toFixed(1) || "—"}
</span>
<span className="text-muted-foreground">/ 5</span>
</div>
</div>
</div>
) : (
<p className="text-muted-foreground">No ratings available yet</p>
)}
</CardContent>
</Card>
{/* Feedback List */}
<Card>
<CardHeader>
<CardTitle className="text-base">Recent Feedback</CardTitle>
<CardDescription>Feedback submissions from beta testers</CardDescription>
</CardHeader>
<CardContent>
{loading ? (
<div className="flex items-center justify-center py-8">
<Loader2 className="w-6 h-6 animate-spin text-muted-foreground" />
</div>
) : feedback.length === 0 ? (
<p className="text-muted-foreground text-center py-8">No feedback submissions yet</p>
) : (
<div className="space-y-4">
{feedback.map((item) => (
<div key={item.id} className="border rounded-lg p-4">
<div className="flex items-start justify-between gap-4">
<div className="flex-1">
<div className="flex items-center gap-2 mb-2">
<span className="font-medium text-sm">{item.user_email}</span>
{item.job_id && (
<Badge variant="outline" className="text-xs">
Job: {item.job_id}
</Badge>
)}
<span className="text-xs text-muted-foreground">
{formatDate(item.created_at)}
</span>
</div>
{/* Ratings */}
<div className="flex flex-wrap gap-4 mb-3 text-sm">
<div className="flex items-center gap-1">
<span className="text-muted-foreground">Overall:</span>
{renderStars(item.overall_rating)}
</div>
{item.ease_of_use_rating && (
<div className="flex items-center gap-1">
<span className="text-muted-foreground">Ease:</span>
{renderStars(item.ease_of_use_rating)}
</div>
)}
{item.lyrics_accuracy_rating && (
<div className="flex items-center gap-1">
<span className="text-muted-foreground">Accuracy:</span>
{renderStars(item.lyrics_accuracy_rating)}
</div>
)}
</div>
{/* Comments */}
{item.what_went_well && (
<div className="mb-2">
<p className="text-xs text-muted-foreground">What went well:</p>
<p className="text-sm">{item.what_went_well}</p>
</div>
)}
{item.what_could_improve && (
<div className="mb-2">
<p className="text-xs text-muted-foreground">What could improve:</p>
<p className="text-sm">{item.what_could_improve}</p>
</div>
)}
{item.additional_comments && (
<div>
<p className="text-xs text-muted-foreground">Additional comments:</p>
<p className="text-sm">{item.additional_comments}</p>
</div>
)}
</div>
{/* Recommend/Use Again */}
<div className="flex flex-col gap-1 text-sm">
{item.would_recommend !== undefined && (
<div className="flex items-center gap-1">
{item.would_recommend ? (
<ThumbsUp className="w-4 h-4 text-green-500" />
) : (
<ThumbsDown className="w-4 h-4 text-red-500" />
)}
<span className="text-xs text-muted-foreground">Recommend</span>
</div>
)}
{item.would_use_again !== undefined && (
<div className="flex items-center gap-1">
{item.would_use_again ? (
<ThumbsUp className="w-4 h-4 text-green-500" />
) : (
<ThumbsDown className="w-4 h-4 text-red-500" />
)}
<span className="text-xs text-muted-foreground">Use Again</span>
</div>
)}
</div>
</div>
</div>
))}
</div>
)}
</CardContent>
</Card>
</div>
)
}
+2 -2
View File
@@ -53,8 +53,8 @@ function AdminBreadcrumb() {
breadcrumbs.push({ label: "Payments" })
} else if (segments[1] === "searches") {
breadcrumbs.push({ label: "Audio Searches" })
} else if (segments[1] === "beta") {
breadcrumbs.push({ label: "Beta Program" })
} else if (segments[1] === "payments") {
breadcrumbs.push({ label: "Payments" })
} else if (segments[1] === "rate-limits") {
breadcrumbs.push({ label: "Rate Limits" })
}
-8
View File
@@ -11,7 +11,6 @@ import {
Briefcase,
CreditCard,
DollarSign,
TestTube2,
Clock,
CheckCircle,
XCircle,
@@ -112,13 +111,6 @@ export default function AdminDashboardPage() {
icon={CreditCard}
loading={loading}
/>
<StatsCard
title="Beta Testers"
value={stats?.total_beta_testers ?? 0}
description="Active beta participants"
icon={TestTube2}
loading={loading}
/>
</StatsGrid>
{/* Secondary Stats */}
@@ -48,11 +48,8 @@ const mockAdminApi = adminApi as jest.Mocked<typeof adminApi>
// Mock data
const mockStats = {
jobs_per_day_limit: 5,
youtube_uploads_per_day_limit: 10,
beta_ip_per_day_limit: 1,
rate_limiting_enabled: true,
youtube_uploads_today: 3,
youtube_uploads_remaining: 7,
youtube_quota_units_consumed: 900,
youtube_quota_units_remaining: 8600,
youtube_quota_daily_limit: 10000,
@@ -62,6 +59,8 @@ const mockStats = {
youtube_quota_seconds_until_reset: 43200,
youtube_uploads_queued: 0,
youtube_uploads_failed: 0,
gcp_quota_available: false,
quota_drift_alert: false,
disposable_domains_count: 130,
blocked_emails_count: 2,
blocked_ips_count: 1,
@@ -166,8 +165,6 @@ describe("AdminRateLimitsPage", () => {
// Check that stats are displayed
expect(screen.getByText("Jobs Per Day")).toBeInTheDocument()
expect(screen.getByText("5")).toBeInTheDocument()
expect(screen.getByText("YouTube Uploads")).toBeInTheDocument()
expect(screen.getByText("10")).toBeInTheDocument()
})
it("displays YouTube API quota section", async () => {
+41 -14
View File
@@ -324,25 +324,13 @@ export default function AdminRateLimitsPage() {
<CardDescription>Current rate limiting settings</CardDescription>
</CardHeader>
<CardContent>
<StatsGrid columns={4}>
<StatsGrid columns={2}>
<StatsCard
title="Jobs Per Day"
value={stats?.jobs_per_day_limit ?? 0}
description="Per user limit"
icon={Briefcase}
/>
<StatsCard
title="YouTube Uploads"
value={stats?.youtube_uploads_per_day_limit ?? 0}
description="System-wide limit"
icon={Youtube}
/>
<StatsCard
title="Beta IP Limit"
value={stats?.beta_ip_per_day_limit ?? 0}
description="Enrollments per IP/day"
icon={Globe}
/>
<StatsCard
title="Status"
value={stats?.rate_limiting_enabled ? "Enabled" : "Disabled"}
@@ -411,6 +399,45 @@ export default function AdminRateLimitsPage() {
icon={UserCheck}
/>
</StatsGrid>
{/* GCP Quota Cross-Reference */}
<div className="border rounded-lg p-4 space-y-2">
<div className="flex items-center gap-2 text-sm font-medium">
<Server className="w-4 h-4" />
GCP Cloud Monitoring
</div>
{stats?.gcp_quota_available ? (
<div className="space-y-1 text-sm">
<div className="flex items-center gap-2">
<span className="text-muted-foreground">GCP Reported:</span>
<span className="font-mono">{stats.gcp_quota_units_consumed?.toLocaleString() ?? 0} units</span>
{stats.gcp_quota_data_delay_minutes != null && (
<span className="text-xs text-muted-foreground">
({stats.gcp_quota_data_delay_minutes}m delay)
</span>
)}
</div>
{stats.quota_drift != null && (
<div className="flex items-center gap-2">
<span className="text-muted-foreground">Drift:</span>
<span className={`font-mono ${stats.quota_drift_alert ? "text-red-600 font-semibold" : ""}`}>
{stats.quota_drift} units
</span>
{stats.quota_drift_alert && (
<Badge variant="destructive" className="text-xs">
<AlertTriangle className="w-3 h-3 mr-1" />
&gt;10% drift
</Badge>
)}
</div>
)}
</div>
) : (
<p className="text-xs text-muted-foreground">
GCP quota data unavailable (metrics have ~1-2h delay)
</p>
)}
</div>
</CardContent>
</Card>
@@ -572,7 +599,7 @@ export default function AdminRateLimitsPage() {
Disposable Email Domains
</CardTitle>
<CardDescription>
Domains blocked from beta enrollment ({filteredDomains.length} of {blocklists?.disposable_domains.length || 0})
Blocked disposable email domains ({filteredDomains.length} of {blocklists?.disposable_domains.length || 0})
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
-6
View File
@@ -355,12 +355,6 @@ export default function AdminUserDetailPage() {
{user.email_verified ? "Yes" : "No"}
</Badge>
</div>
<div className="flex justify-between">
<span className="text-muted-foreground">Beta Tester</span>
<Badge variant={user.is_beta_tester ? "default" : "secondary"}>
{user.is_beta_tester ? user.beta_tester_status || "Yes" : "No"}
</Badge>
</div>
<div className="flex justify-between">
<span className="text-muted-foreground">Created</span>
<span className="text-sm">{formatDate(user.created_at)}</span>
@@ -7,7 +7,6 @@ import {
Users,
Briefcase,
DollarSign,
TestTube2,
ArrowLeft,
Settings,
Search,
@@ -54,11 +53,6 @@ const navItems = [
href: "/admin/searches",
icon: Search,
},
{
title: "Beta Program",
href: "/admin/beta",
icon: TestTube2,
},
{
title: "Rate Limits",
href: "/admin/rate-limits",
-2
View File
@@ -87,7 +87,6 @@ export async function getUserInfo(
): Promise<{
email: string;
credits: number;
isBeta: boolean;
} | null> {
const response = await request.get(`${apiUrl}/api/users/me`, {
headers: { Authorization: `Bearer ${token}` },
@@ -99,7 +98,6 @@ export async function getUserInfo(
return {
email: data.email,
credits: data.credits,
isBeta: data.is_beta || false,
};
}
@@ -120,11 +120,6 @@ test.describe("Admin Dashboard - Production", () => {
await expect(page).toHaveURL(/\/admin\/jobs/);
await expect(page.locator("h1")).toContainText("Jobs");
// Click Beta Program in sidebar
await page.getByRole("link", { name: /Beta/i }).first().click();
await expect(page).toHaveURL(/\/admin\/beta/);
await expect(page.locator("h1")).toContainText("Beta Program");
// Navigate back to dashboard
await page.getByRole("link", { name: /Dashboard/i }).first().click();
await expect(page).toHaveURL(/\/admin$/);
@@ -162,18 +157,6 @@ test.describe("Admin Dashboard - Production", () => {
await expect(page.getByPlaceholder(/Filter by user/i)).toBeVisible();
});
test("beta page loads with stats", async ({ page }) => {
await page.goto(`${PROD_URL}/admin/beta`);
// Wait for page to load
await expect(page.locator("h1")).toContainText("Beta Program", {
timeout: 30000,
});
// Check stats cards
await expect(page.getByText("Total Beta Testers")).toBeVisible();
await expect(page.getByText("Feedback Submissions")).toBeVisible();
});
});
test.describe("Admin API - Production", () => {
@@ -13,11 +13,19 @@ import { setAuthToken } from "../fixtures/test-helper"
// Mock data for API responses
const mockStats = {
jobs_per_day_limit: 5,
youtube_uploads_per_day_limit: 10,
beta_ip_per_day_limit: 1,
rate_limiting_enabled: true,
youtube_uploads_today: 3,
youtube_uploads_remaining: 7,
youtube_quota_units_consumed: 900,
youtube_quota_units_remaining: 8600,
youtube_quota_daily_limit: 10000,
youtube_quota_effective_limit: 9500,
youtube_quota_upload_cost: 300,
youtube_quota_estimated_uploads_remaining: 28,
youtube_quota_seconds_until_reset: 43200,
youtube_uploads_queued: 0,
youtube_uploads_failed: 0,
gcp_quota_available: false,
quota_drift_alert: false,
disposable_domains_count: 130,
blocked_emails_count: 2,
blocked_ips_count: 1,
@@ -92,6 +100,14 @@ test.describe("Admin Rate Limits Page", () => {
})
})
await page.route("**/api/admin/youtube-queue**", async (route) => {
await route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify({ entries: [], total: 0 }),
})
})
await page.route("**/api/admin/stats/overview", async (route) => {
await route.fulfill({
status: 200,
+7 -94
View File
@@ -1101,32 +1101,6 @@ export const api = {
return result.checkout_url;
},
// ==========================================================================
// Beta Tester API endpoints
// ==========================================================================
/**
* Enroll as a beta tester to receive free credits
*/
async enrollBetaTester(
email: string,
promiseText: string,
acceptCorrectionsWork: boolean
): Promise<BetaEnrollResponse> {
const response = await fetch(`${API_BASE_URL}/api/users/beta/enroll`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: email.toLowerCase(),
promise_text: promiseText,
accept_corrections_work: acceptCorrectionsWork,
}),
});
return handleResponse(response);
},
// ==========================================================================
// Push Notifications API endpoints
// ==========================================================================
@@ -1229,13 +1203,6 @@ export interface CreditPackage {
description: string;
}
export interface BetaEnrollResponse {
status: string;
message: string;
credits_granted: number;
session_token: string | null;
}
// Types for push notifications
export interface PushSubscriptionInfo {
endpoint: string;
@@ -1270,7 +1237,6 @@ export interface AdminStatsOverview {
cancelled: number;
};
total_credits_issued_30d: number;
total_beta_testers: number;
}
export interface AdminUser {
@@ -1302,8 +1268,6 @@ export interface AdminUserDetail {
last_login_at?: string;
total_jobs_created: number;
total_jobs_completed: number;
is_beta_tester: boolean;
beta_tester_status?: string;
credit_transactions: Array<{
id: string;
amount: number;
@@ -1322,36 +1286,6 @@ export interface AdminUserDetail {
active_sessions_count: number;
}
export interface AdminBetaStats {
total_beta_testers: number;
active_testers: number;
pending_feedback: number;
completed_feedback: number;
total_feedback_submissions: number;
average_ratings: {
overall: number;
ease_of_use: number;
lyrics_accuracy: number;
correction_experience: number;
};
}
export interface AdminBetaFeedback {
id: string;
user_email: string;
job_id?: string;
overall_rating: number;
ease_of_use_rating?: number;
lyrics_accuracy_rating?: number;
correction_experience_rating?: number;
what_went_well?: string;
what_could_improve?: string;
additional_comments?: string;
would_recommend?: boolean;
would_use_again?: boolean;
created_at: string;
}
export interface AdminJobListParams {
status?: string;
user_email?: string;
@@ -1560,31 +1494,6 @@ export const adminApi = {
return handleResponse<Job[]>(response);
},
/**
* Get beta program statistics
*/
async getBetaStats(params?: { exclude_test?: boolean }): Promise<AdminBetaStats> {
const searchParams = new URLSearchParams();
if (params?.exclude_test !== undefined) {
searchParams.set('exclude_test', String(params.exclude_test));
}
const url = `${API_BASE_URL}/api/users/admin/beta/stats${searchParams.toString() ? '?' + searchParams.toString() : ''}`;
const response = await fetch(url, {
headers: getAuthHeaders()
});
return handleResponse(response);
},
/**
* Get beta program feedback list
*/
async getBetaFeedback(limit: number = 50): Promise<{ feedback: AdminBetaFeedback[]; total: number }> {
const response = await fetch(`${API_BASE_URL}/api/users/admin/beta/feedback?limit=${limit}`, {
headers: getAuthHeaders()
});
return handleResponse(response);
},
/**
* Delete a job (admin)
*/
@@ -2499,11 +2408,8 @@ export interface DeleteOutputsResponse {
// Rate Limits API Types
export interface RateLimitStatsResponse {
jobs_per_day_limit: number;
youtube_uploads_per_day_limit: number;
beta_ip_per_day_limit: number;
rate_limiting_enabled: boolean;
youtube_uploads_today: number;
youtube_uploads_remaining: number;
// YouTube quota (unit-based tracking)
youtube_quota_units_consumed: number;
youtube_quota_units_remaining: number;
@@ -2515,6 +2421,13 @@ export interface RateLimitStatsResponse {
// YouTube upload queue
youtube_uploads_queued: number;
youtube_uploads_failed: number;
// GCP quota monitoring (Cloud Monitoring cross-reference)
gcp_quota_available: boolean;
gcp_quota_units_consumed?: number;
gcp_quota_last_datapoint?: string;
gcp_quota_data_delay_minutes?: number;
quota_drift?: number;
quota_drift_alert: boolean;
disposable_domains_count: number;
blocked_emails_count: number;
blocked_ips_count: number;
Generated
+26 -1
View File
@@ -2602,6 +2602,31 @@ proto-plus = [
]
protobuf = ">=3.20.2,<4.21.0 || >4.21.0,<4.21.1 || >4.21.1,<4.21.2 || >4.21.2,<4.21.3 || >4.21.3,<4.21.4 || >4.21.4,<4.21.5 || >4.21.5,<7.0.0.dev0"
[[package]]
name = "google-cloud-monitoring"
version = "2.29.1"
description = "Google Cloud Monitoring API client library"
optional = false
python-versions = ">=3.7"
groups = ["main"]
files = [
{file = "google_cloud_monitoring-2.29.1-py3-none-any.whl", hash = "sha256:944a57031f20da38617d184d5658c1f938e019e8061f27fd944584831a1b9d5a"},
{file = "google_cloud_monitoring-2.29.1.tar.gz", hash = "sha256:86cac55cdd2608561819d19544fb3c129bbb7dcecc445d8de426e34cd6fa8e49"},
]
[package.dependencies]
google-api-core = {version = ">=1.34.1,<2.0.dev0 || >=2.11.dev0,<3.0.0", extras = ["grpc"]}
google-auth = ">=2.14.1,<2.24.0 || >2.24.0,<2.25.0 || >2.25.0,<3.0.0"
grpcio = ">=1.33.2,<2.0.0"
proto-plus = [
{version = ">=1.25.0,<2.0.0", markers = "python_version >= \"3.13\""},
{version = ">=1.22.3,<2.0.0"},
]
protobuf = ">=3.20.2,<4.21.0 || >4.21.0,<4.21.1 || >4.21.1,<4.21.2 || >4.21.2,<4.21.3 || >4.21.3,<4.21.4 || >4.21.4,<4.21.5 || >4.21.5,<7.0.0"
[package.extras]
pandas = ["pandas (>=0.23.2)"]
[[package]]
name = "google-cloud-run"
version = "0.13.0"
@@ -10687,4 +10712,4 @@ local-whisper = ["whisper-timestamped"]
[metadata]
lock-version = "2.1"
python-versions = ">=3.10,<3.14"
content-hash = "539510a3645bab46545d8e7594dba666f6f7ce47b4253dd94cd1310d0a7f0806"
content-hash = "7acf9e306a9ac7b7a4547eb130be6747cb522cb80f16b0c914467e4a252d3dcd"
+2 -1
View File
@@ -1,6 +1,6 @@
[tool.poetry]
name = "karaoke-gen"
version = "0.124.0"
version = "0.125.0"
description = "Generate karaoke videos with synchronized lyrics. Handles the entire process from downloading audio and lyrics to creating the final video with title screens."
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
license = "MIT"
@@ -92,6 +92,7 @@ google-cloud-storage = ">=2.14.0"
google-cloud-secret-manager = ">=2.18.0"
google-cloud-tasks = ">=2.16.0" # Cloud Tasks for worker coordination (Phase 1 scalability)
google-cloud-run = ">=0.10.0" # Cloud Run Jobs for long-running video encoding (Phase 2 scalability)
google-cloud-monitoring = ">=2.19.0" # Cloud Monitoring for YouTube API quota metrics
python-dotenv = ">=1.0.0"
pydub = ">=0.25.1"
# Payment and email services
-3
View File
@@ -73,7 +73,6 @@ def investigate_user(
"total_jobs_completed": user_data.get("total_jobs_completed", 0),
"is_active": user_data.get("is_active", True),
"email_verified": user_data.get("email_verified", False),
"is_beta_tester": user_data.get("is_beta_tester", False),
"display_name": user_data.get("display_name"),
}
# Credit transactions (last 20)
@@ -199,8 +198,6 @@ def _print_human_readable(results: Dict[str, Any]):
print(f" Jobs Completed: {user['total_jobs_completed']}")
print(f" Active: {user['is_active']}")
print(f" Email Verified: {user['email_verified']}")
if user.get('is_beta_tester'):
print(" Beta Tester: Yes")
if user.get('display_name'):
print(f" Display Name: {user['display_name']}")
else:
-4
View File
@@ -47,10 +47,6 @@ def main():
email_service.send_welcome_email("user@example.com", credits=3)
save_preview(output_dir, "welcome", preview_provider)
# Beta welcome email
email_service.send_beta_welcome_email("user@example.com", credits=1)
save_preview(output_dir, "beta_welcome", preview_provider)
# Feedback request email
email_service.send_feedback_request_email(
"user@example.com",