File size: 2,909 Bytes
27d04ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Analytics API routes
Endpoints for analyzing preprocessed session data
"""

from fastapi import APIRouter, HTTPException
from services.analytics_service import (
    get_session_summary,
    get_channel_analytics,
    get_watch_patterns,
    get_search_analytics,
    get_subscription_overlap,
    get_full_analytics,
    get_habit_formation,
    get_temporal_trends
)

router = APIRouter()


@router.get("/{token}/summary")
async def analytics_summary(token: str):
    """Get session summary stats."""
    result = get_session_summary(token)
    if "error" in result:
        raise HTTPException(status_code=404, detail=result["error"])
    return result


@router.get("/{token}/channels")
async def analytics_channels(token: str, top_n: int = 20, engagement: str = "all"):
    """Get channel analytics with top N channels. engagement: 'all' | 'watch' (active) | 'view' (passive)"""
    result = get_channel_analytics(token, top_n, engagement)
    if "error" in result:
        raise HTTPException(status_code=404, detail=result["error"])
    return result


@router.get("/{token}/watch-patterns")
async def analytics_watch_patterns(token: str):
    """Get watch time patterns (hourly/daily)."""
    result = get_watch_patterns(token)
    if "error" in result:
        raise HTTPException(status_code=404, detail=result["error"])
    return result


@router.get("/{token}/searches")
async def analytics_searches(token: str, top_n: int = 20):
    """Get search analytics."""
    result = get_search_analytics(token, top_n)
    if "error" in result:
        raise HTTPException(status_code=404, detail=result["error"])
    return result


@router.get("/{token}/subscription-overlap")
async def analytics_subscription_overlap(token: str):
    """Get subscription vs watch overlap analysis."""
    result = get_subscription_overlap(token)
    if "error" in result:
        raise HTTPException(status_code=404, detail=result["error"])
    return result


@router.get("/{token}/habits")
async def analytics_habits(token: str, min_streak_days: int = 3):
    """Get habit formation analytics - channels and content watched daily."""
    result = get_habit_formation(token, min_streak_days)
    if "error" in result:
        raise HTTPException(status_code=404, detail=result["error"])
    return result


@router.get("/{token}/temporal-trends")
async def analytics_temporal_trends(token: str):
    """Get month-by-month analysis of how peak watching times change."""
    result = get_temporal_trends(token)
    if "error" in result:
        raise HTTPException(status_code=404, detail=result["error"])
    return result


@router.get("/{token}/full")
async def analytics_full(token: str):
    """Get all analytics in one call."""
    result = get_full_analytics(token)
    if "error" in result.get("summary", {}):
        raise HTTPException(status_code=404, detail=result["summary"]["error"])
    return result