Skip to content

Commit 7e04198

Browse files
committed
add some telemetry for usage limits, esp. if approaching limit / passed limit
1 parent 2fcf432 commit 7e04198

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

server/app/helpers/subscription_limits.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from app.database.crud.paper_crud import paper_crud
1616
from app.database.crud.subscription_crud import subscription_crud
1717
from app.database.models import SubscriptionPlan, SubscriptionStatus
18+
from app.database.telemetry import track_event
1819
from app.schemas.user import CurrentUser
1920
from sqlalchemy.orm import Session
2021

@@ -124,6 +125,16 @@ def can_user_upload_paper(db: Session, user: CurrentUser) -> tuple[bool, Optiona
124125

125126
# If the user has reached their paper upload limit
126127
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+
)
127138
plan_name = {
128139
SubscriptionPlan.BASIC: "Basic",
129140
SubscriptionPlan.RESEARCHER: "Researcher",
@@ -157,6 +168,16 @@ def can_user_access_knowledge_base(
157168

158169
# If the user has exceeded their knowledge base size limit
159170
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+
)
160181
plan_name = {
161182
SubscriptionPlan.BASIC: "Basic",
162183
SubscriptionPlan.RESEARCHER: "Researcher",
@@ -204,6 +225,72 @@ def get_user_usage_info(db: Session, user: CurrentUser) -> Dict:
204225
audio_overviews_allowed = limits[AUDIO_OVERVIEWS_KEY]
205226
audio_overviews_used_this_month = get_user_audio_overviews_used_this_month(db, user)
206227

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+
207294
chat_credits_remaining = (
208295
None
209296
if chat_credits_allowed == float("inf")

0 commit comments

Comments
 (0)