|
| 1 | +from fastapi import APIRouter, Depends, HTTPException |
| 2 | +from sqlmodel import Session, select |
| 3 | +import polars as pl |
| 4 | +from typing import List, Dict, Any # Dict and Any might be needed for Polars conversion |
| 5 | +from datetime import date |
| 6 | +import pydantic # Ensure pydantic is imported |
| 7 | + |
| 8 | +from app.models import User, Item # Assuming User model is in app.models, Import Item |
| 9 | +from app.api.deps import SessionDep, get_current_active_superuser # SessionDep for dependency injection |
| 10 | + |
| 11 | +from opentelemetry import trace |
| 12 | + |
| 13 | +# Pydantic models for response |
| 14 | +class UserSignupTrend(pydantic.BaseModel): # Corrected: pydantic.BaseModel |
| 15 | + signup_date: date |
| 16 | + count: int |
| 17 | + |
| 18 | +class UserActivity(pydantic.BaseModel): # Corrected: pydantic.BaseModel |
| 19 | + active_users: int |
| 20 | + inactive_users: int |
| 21 | + |
| 22 | +class UserAnalyticsSummary(pydantic.BaseModel): # Corrected: pydantic.BaseModel |
| 23 | + total_users: int |
| 24 | + signup_trends: List[UserSignupTrend] |
| 25 | + activity_summary: UserActivity |
| 26 | + # Add more fields as desired, e.g., average_items_per_user: float |
| 27 | + |
| 28 | +# Pydantic models for Item analytics |
| 29 | +class ItemCreationTrend(pydantic.BaseModel): |
| 30 | + creation_date: date |
| 31 | + count: int |
| 32 | + |
| 33 | +# class ItemOwnerDistribution(pydantic.BaseModel): # Optional for now |
| 34 | +# owner_id: str |
| 35 | +# item_count: int |
| 36 | + |
| 37 | +class ItemAnalyticsTrends(pydantic.BaseModel): |
| 38 | + total_items: int |
| 39 | + creation_trends: List[ItemCreationTrend] |
| 40 | + # owner_distribution: List[ItemOwnerDistribution] # Optional |
| 41 | + |
| 42 | + |
| 43 | +router = APIRouter(prefix="/analytics", tags=["analytics"]) |
| 44 | +tracer = trace.get_tracer(__name__) |
| 45 | + |
| 46 | +@router.get("/user-summary", response_model=UserAnalyticsSummary) |
| 47 | +def get_user_summary(session: SessionDep): # get_current_active_superuser is imported but not used here yet |
| 48 | + with tracer.start_as_current_span("user_summary_endpoint"): |
| 49 | + |
| 50 | + users_list: List[User] |
| 51 | + with tracer.start_as_current_span("fetch_all_users_sql"): |
| 52 | + statement = select(User) |
| 53 | + users_list = session.exec(statement).all() |
| 54 | + |
| 55 | + if not users_list: |
| 56 | + return UserAnalyticsSummary( |
| 57 | + total_users=0, |
| 58 | + signup_trends=[], |
| 59 | + activity_summary=UserActivity(active_users=0, inactive_users=0) |
| 60 | + ) |
| 61 | + |
| 62 | + with tracer.start_as_current_span("convert_users_to_polars"): |
| 63 | + users_data = [] |
| 64 | + for user in users_list: |
| 65 | + user_dict = { |
| 66 | + "id": user.id, # No explicit str() casting for now, per instructions |
| 67 | + "email": user.email, |
| 68 | + "is_active": user.is_active, |
| 69 | + "is_superuser": user.is_superuser, |
| 70 | + "full_name": user.full_name, |
| 71 | + } |
| 72 | + # Attempt to get 'created_at' if it exists (it doesn't in the standard model) |
| 73 | + if hasattr(user, 'created_at') and user.created_at: |
| 74 | + user_dict['created_at'] = user.created_at |
| 75 | + users_data.append(user_dict) |
| 76 | + |
| 77 | + if not users_data: # Should not happen if users_list is not empty, but as a safe guard |
| 78 | + return UserAnalyticsSummary( |
| 79 | + total_users=0, |
| 80 | + signup_trends=[], |
| 81 | + activity_summary=UserActivity(active_users=0, inactive_users=0) |
| 82 | + ) |
| 83 | + |
| 84 | + # Create DataFrame without explicit casting of 'id' first. |
| 85 | + # If Polars errors on UUID, the instruction is to add: |
| 86 | + # df_users = df_users.with_columns(pl.col('id').cast(pl.Utf8)) |
| 87 | + df_users = pl.DataFrame(users_data) |
| 88 | + |
| 89 | + |
| 90 | + total_users = df_users.height # More idiomatic for Polars than len(df_users) |
| 91 | + |
| 92 | + with tracer.start_as_current_span("calculate_user_activity_polars"): |
| 93 | + active_users = df_users.filter(pl.col("is_active") == True).height |
| 94 | + inactive_users = total_users - active_users |
| 95 | + |
| 96 | + signup_trends_data = [] |
| 97 | + if 'created_at' in df_users.columns: # This will be false as User model has no created_at |
| 98 | + with tracer.start_as_current_span("calculate_signup_trends_polars"): |
| 99 | + # Ensure 'created_at' is a datetime type. If string, parse it. |
| 100 | + # Assuming it's already a datetime.date or datetime.datetime from SQLModel |
| 101 | + # If it's datetime, cast to date for daily trends |
| 102 | + df_users_with_date = df_users.with_columns(pl.col("created_at").cast(pl.Date).alias("signup_day")) |
| 103 | + |
| 104 | + signup_counts_df = df_users_with_date.group_by("signup_day").agg( |
| 105 | + pl.count().alias("count") |
| 106 | + ).sort("signup_day") |
| 107 | + |
| 108 | + signup_trends_data = [ |
| 109 | + UserSignupTrend(signup_date=row["signup_day"], count=row["count"]) |
| 110 | + for row in signup_counts_df.to_dicts() |
| 111 | + ] |
| 112 | + |
| 113 | + return UserAnalyticsSummary( |
| 114 | + total_users=total_users, |
| 115 | + signup_trends=signup_trends_data, # Will be empty as 'created_at' is not in User model |
| 116 | + activity_summary=UserActivity(active_users=active_users, inactive_users=inactive_users) |
| 117 | + ) |
| 118 | + |
| 119 | +@router.get("/item-trends", response_model=ItemAnalyticsTrends) |
| 120 | +def get_item_trends(session: SessionDep): |
| 121 | + with tracer.start_as_current_span("item_trends_endpoint"): |
| 122 | + |
| 123 | + items_list: List[Item] |
| 124 | + with tracer.start_as_current_span("fetch_all_items_sql"): |
| 125 | + statement = select(Item) |
| 126 | + items_list = session.exec(statement).all() |
| 127 | + |
| 128 | + if not items_list: |
| 129 | + return ItemAnalyticsTrends( |
| 130 | + total_items=0, |
| 131 | + creation_trends=[] |
| 132 | + # owner_distribution=[] # Optional |
| 133 | + ) |
| 134 | + |
| 135 | + with tracer.start_as_current_span("convert_items_to_polars"): |
| 136 | + items_data = [] |
| 137 | + for item in items_list: |
| 138 | + item_dict = { |
| 139 | + "id": str(item.id), # Cast UUID to string |
| 140 | + "title": item.title, |
| 141 | + "description": item.description, |
| 142 | + "owner_id": str(item.owner_id) # Cast UUID to string |
| 143 | + } |
| 144 | + # IMPORTANT: Item model does not have 'created_at'. |
| 145 | + # This will result in empty creation_trends. |
| 146 | + if hasattr(item, 'created_at') and item.created_at: |
| 147 | + item_dict['created_at'] = item.created_at |
| 148 | + items_data.append(item_dict) |
| 149 | + |
| 150 | + if not items_data: # Safety check |
| 151 | + return ItemAnalyticsTrends(total_items=0, creation_trends=[]) |
| 152 | + |
| 153 | + df_items = pl.DataFrame(items_data) |
| 154 | + |
| 155 | + total_items = df_items.height |
| 156 | + |
| 157 | + creation_trends_data = [] |
| 158 | + if 'created_at' in df_items.columns: |
| 159 | + with tracer.start_as_current_span("calculate_item_creation_trends_polars"): |
| 160 | + # Ensure 'created_at' is datetime, then cast to date for daily trends |
| 161 | + df_items_with_date = df_items.with_columns( |
| 162 | + pl.col("created_at").cast(pl.Date).alias("creation_day") |
| 163 | + ) |
| 164 | + |
| 165 | + creation_counts_df = df_items_with_date.group_by("creation_day").agg( |
| 166 | + pl.count().alias("count") |
| 167 | + ).sort("creation_day") |
| 168 | + |
| 169 | + creation_trends_data = [ |
| 170 | + ItemCreationTrend(creation_date=row["creation_day"], count=row["count"]) |
| 171 | + for row in creation_counts_df.to_dicts() |
| 172 | + ] |
| 173 | + |
| 174 | + # Placeholder for owner distribution if implemented later |
| 175 | + # owner_distribution_data = [] |
| 176 | + |
| 177 | + return ItemAnalyticsTrends( |
| 178 | + total_items=total_items, |
| 179 | + creation_trends=creation_trends_data |
| 180 | + # owner_distribution=owner_distribution_data # Optional |
| 181 | + ) |
0 commit comments