|
15 | 15 | from app.database.crud.paper_crud import paper_crud
|
16 | 16 | from app.database.crud.subscription_crud import subscription_crud
|
17 | 17 | from app.database.models import SubscriptionPlan, SubscriptionStatus
|
| 18 | +from app.database.telemetry import track_event |
18 | 19 | from app.schemas.user import CurrentUser
|
19 | 20 | from sqlalchemy.orm import Session
|
20 | 21 |
|
@@ -124,6 +125,16 @@ def can_user_upload_paper(db: Session, user: CurrentUser) -> tuple[bool, Optiona
|
124 | 125 |
|
125 | 126 | # If the user has reached their paper upload limit
|
126 | 127 | if current_paper_count >= paper_limit:
|
| 128 | + track_event( |
| 129 | + "action_blocked_limit_reached", |
| 130 | + user_id=str(user.id), |
| 131 | + properties={ |
| 132 | + "current_paper_count": current_paper_count, |
| 133 | + "paper_limit": paper_limit, |
| 134 | + "type": "paper_uploads", |
| 135 | + "plan": plan.value, |
| 136 | + }, |
| 137 | + ) |
127 | 138 | plan_name = {
|
128 | 139 | SubscriptionPlan.BASIC: "Basic",
|
129 | 140 | SubscriptionPlan.RESEARCHER: "Researcher",
|
@@ -157,6 +168,16 @@ def can_user_access_knowledge_base(
|
157 | 168 |
|
158 | 169 | # If the user has exceeded their knowledge base size limit
|
159 | 170 | if current_size_mb >= kb_limit:
|
| 171 | + track_event( |
| 172 | + "action_blocked_limit_reached", |
| 173 | + user_id=str(user.id), |
| 174 | + properties={ |
| 175 | + "current_size_mb": current_size_mb, |
| 176 | + "kb_limit": kb_limit, |
| 177 | + "type": "knowledge_base_size", |
| 178 | + "plan": plan.value, |
| 179 | + }, |
| 180 | + ) |
160 | 181 | plan_name = {
|
161 | 182 | SubscriptionPlan.BASIC: "Basic",
|
162 | 183 | SubscriptionPlan.RESEARCHER: "Researcher",
|
@@ -204,6 +225,72 @@ def get_user_usage_info(db: Session, user: CurrentUser) -> Dict:
|
204 | 225 | audio_overviews_allowed = limits[AUDIO_OVERVIEWS_KEY]
|
205 | 226 | audio_overviews_used_this_month = get_user_audio_overviews_used_this_month(db, user)
|
206 | 227 |
|
| 228 | + # Calculate usage percentages |
| 229 | + paper_usage_percentage = ( |
| 230 | + (current_paper_count / paper_limit) * 100 if paper_limit != float("inf") else 0 |
| 231 | + ) |
| 232 | + kb_usage_percentage = ( |
| 233 | + (total_size / total_size_allowed) * 100 |
| 234 | + if total_size_allowed != float("inf") |
| 235 | + else 0 |
| 236 | + ) |
| 237 | + chat_credits_usage_percentage = ( |
| 238 | + (chat_credits_used / chat_credits_allowed) * 100 |
| 239 | + if chat_credits_allowed != float("inf") |
| 240 | + else 0 |
| 241 | + ) |
| 242 | + audio_overviews_usage_percentage = ( |
| 243 | + (audio_overviews_used_this_month / audio_overviews_allowed) * 100 |
| 244 | + if audio_overviews_allowed != float("inf") |
| 245 | + else 0 |
| 246 | + ) |
| 247 | + |
| 248 | + # Track event if usage is > 75% |
| 249 | + if paper_usage_percentage > 75: |
| 250 | + track_event( |
| 251 | + "high_usage_limit", |
| 252 | + user_id=str(user.id), |
| 253 | + properties={ |
| 254 | + "metric": "paper_uploads", |
| 255 | + "usage": current_paper_count, |
| 256 | + "limit": paper_limit, |
| 257 | + "plan": plan.value, |
| 258 | + }, |
| 259 | + ) |
| 260 | + if kb_usage_percentage > 75: |
| 261 | + track_event( |
| 262 | + "high_usage_limit", |
| 263 | + user_id=str(user.id), |
| 264 | + properties={ |
| 265 | + "metric": "knowledge_base_size", |
| 266 | + "usage": total_size, |
| 267 | + "limit": total_size_allowed, |
| 268 | + "plan": plan.value, |
| 269 | + }, |
| 270 | + ) |
| 271 | + if chat_credits_usage_percentage > 75: |
| 272 | + track_event( |
| 273 | + "high_usage_limit", |
| 274 | + user_id=str(user.id), |
| 275 | + properties={ |
| 276 | + "metric": "chat_credits", |
| 277 | + "usage": chat_credits_used, |
| 278 | + "limit": chat_credits_allowed, |
| 279 | + "plan": plan.value, |
| 280 | + }, |
| 281 | + ) |
| 282 | + if audio_overviews_usage_percentage > 75: |
| 283 | + track_event( |
| 284 | + "high_usage_limit", |
| 285 | + user_id=str(user.id), |
| 286 | + properties={ |
| 287 | + "metric": "audio_overviews", |
| 288 | + "usage": audio_overviews_used_this_month, |
| 289 | + "limit": audio_overviews_allowed, |
| 290 | + "plan": plan.value, |
| 291 | + }, |
| 292 | + ) |
| 293 | + |
207 | 294 | chat_credits_remaining = (
|
208 | 295 | None
|
209 | 296 | if chat_credits_allowed == float("inf")
|
|
0 commit comments