"""Career domain analysis tools: pipeline stats and job fit analysis."""
import logging
from datetime import datetime
from typing import Optional
from ...models import PipelineStats
from ...utils.storage import get_db_connection
logger = logging.getLogger(__name__)
async def analyze_application_pipeline() -> PipelineStats:
"""
Analyze the job application pipeline and return statistics.
Returns:
Pipeline statistics including counts by status
"""
logger.info("Analyzing application pipeline")
try:
async with get_db_connection() as db:
# Get total count
cursor = await db.execute("SELECT COUNT(*) FROM applications")
row = await cursor.fetchone()
total = row[0] if row else 0
# Get counts by status
cursor = await db.execute(
"""
SELECT status, COUNT(*)
FROM applications
GROUP BY status
"""
)
rows = await cursor.fetchall()
by_status = {row[0]: row[1] for row in rows}
# Calculate success rate (offered / applied)
applied_count = by_status.get("applied", 0)
offered_count = by_status.get("offered", 0)
success_rate = (
(offered_count / applied_count * 100)
if applied_count > 0
else None
)
stats = PipelineStats(
total_applications=total,
by_status=by_status,
avg_response_time_days=None, # TODO: Calculate when we track response dates
success_rate=success_rate
)
logger.info(f"Pipeline analysis complete: {total} total applications")
return stats
except Exception as e:
logger.error(f"Error analyzing pipeline: {e}", exc_info=True)
raise