File size: 2,370 Bytes
c16e1c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa63765
c16e1c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa63765
c16e1c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa63765
c16e1c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa63765
c16e1c9
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
from fastapi import APIRouter, Header, HTTPException

router = APIRouter()

# Mock in-memory analytics (replace with Supabase later)
ANALYTICS_DATA = {
    "tool_usage": {
        "rag": 12,
        "web": 8,
        "admin": 3
    },
    "redflags": [
        {
            "tenant": "tenant123",
            "match": "salary",
            "message": "get salary data now",
            "timestamp": "2025-01-14T10:22:00Z"
        }
    ],
    "activity": {
        "total_queries": 23,
        "active_users": 4,
        "last_query": "2025-01-14T10:24:31Z"
    }
}


@router.get("/overview")
async def analytics_overview(
    x_tenant_id: str = Header(None)
):
    """
    Returns an overview of analytics for the dashboard.
    """

    if not x_tenant_id:
        raise HTTPException(status_code=400, detail="Missing tenant ID")

    return {
        "tenant_id": x_tenant_id,
        "overview": {
            "total_queries": ANALYTICS_DATA["activity"]["total_queries"],
            "tool_usage": ANALYTICS_DATA["tool_usage"],
            "redflag_count": len(ANALYTICS_DATA["redflags"]),
            "active_users": ANALYTICS_DATA["activity"]["active_users"]
        }
    }


@router.get("/tool-usage")
async def analytics_tool_usage(
    x_tenant_id: str = Header(None)
):
    """
    Returns how often each tool (RAG, Web, Admin) was used.
    """

    if not x_tenant_id:
        raise HTTPException(status_code=400, detail="Missing tenant ID")

    return {
        "tenant_id": x_tenant_id,
        "tool_usage": ANALYTICS_DATA["tool_usage"]
    }


@router.get("/redflags")
async def analytics_redflags(
    x_tenant_id: str = Header(None)
):
    """
    Returns red-flag violations for this tenant.
    """

    if not x_tenant_id:
        raise HTTPException(status_code=400, detail="Missing tenant ID")

    redflags = [
        r for r in ANALYTICS_DATA["redflags"]
        if r["tenant"] == x_tenant_id
    ]

    return {
        "tenant_id": x_tenant_id,
        "redflags": redflags
    }


@router.get("/activity")
async def analytics_activity(
    x_tenant_id: str = Header(None)
):
    """
    Returns general tenant activity statistics.
    """

    if not x_tenant_id:
        raise HTTPException(status_code=400, detail="Missing tenant ID")

    return {
        "tenant_id": x_tenant_id,
        "activity": ANALYTICS_DATA["activity"]
    }