2023-08-21 15:05:13 +03:00
|
|
|
from logger import get_logger
|
|
|
|
from models.databases.supabase.supabase import SupabaseDB
|
2024-01-04 15:57:13 +03:00
|
|
|
from models.settings import PostHogSettings, get_supabase_db
|
2023-11-15 15:17:51 +03:00
|
|
|
from modules.user.entity.user_identity import UserIdentity
|
2023-08-21 15:05:13 +03:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class UserUsage(UserIdentity):
|
|
|
|
daily_requests_count: int = 0
|
|
|
|
|
|
|
|
def __init__(self, **data):
|
|
|
|
super().__init__(**data)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supabase_db(self) -> SupabaseDB:
|
|
|
|
return get_supabase_db()
|
|
|
|
|
|
|
|
def get_user_usage(self):
|
|
|
|
"""
|
|
|
|
Fetch the user request stats from the database
|
|
|
|
"""
|
|
|
|
request = self.supabase_db.get_user_usage(self.id)
|
|
|
|
|
|
|
|
return request
|
|
|
|
|
2024-01-04 03:14:03 +03:00
|
|
|
def get_model_settings(self):
|
|
|
|
"""
|
|
|
|
Fetch the user request stats from the database
|
|
|
|
"""
|
|
|
|
request = self.supabase_db.get_model_settings()
|
|
|
|
|
|
|
|
return request
|
|
|
|
|
2023-09-13 14:47:12 +03:00
|
|
|
def get_user_settings(self):
|
|
|
|
"""
|
|
|
|
Fetch the user settings from the database
|
|
|
|
"""
|
2024-01-04 15:57:13 +03:00
|
|
|
posthog = PostHogSettings()
|
2023-09-13 14:47:12 +03:00
|
|
|
request = self.supabase_db.get_user_settings(self.id)
|
2024-01-04 15:57:13 +03:00
|
|
|
if request is not None and request.get("is_premium", False):
|
|
|
|
posthog.set_once_user_properties(
|
2024-01-04 16:17:10 +03:00
|
|
|
self.id, "HAS_OR_HAD_PREMIUM", {"is_was_premium": "true"}
|
2024-01-04 15:57:13 +03:00
|
|
|
)
|
|
|
|
posthog.set_user_properties(
|
|
|
|
self.id, "CURRENT_PREMIUM", {"is_premium": "true"}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
posthog.set_user_properties(
|
|
|
|
self.id, "CURRENT_PREMIUM", {"is_premium": "false"}
|
|
|
|
)
|
2023-09-13 14:47:12 +03:00
|
|
|
|
|
|
|
return request
|
|
|
|
|
2024-01-22 22:46:26 +03:00
|
|
|
def get_user_monthly_usage(self, date):
|
2024-01-04 03:40:23 +03:00
|
|
|
"""
|
|
|
|
Fetch the user daily usage from the database
|
|
|
|
"""
|
2024-01-04 15:57:13 +03:00
|
|
|
posthog = PostHogSettings()
|
2024-01-22 22:46:26 +03:00
|
|
|
request = self.supabase_db.get_user_requests_count_for_month(self.id, date)
|
2024-01-04 15:57:13 +03:00
|
|
|
posthog.set_user_properties(
|
2024-01-22 22:46:26 +03:00
|
|
|
self.id, "MONTHLY_USAGE", {"monthly_chat_usage": request}
|
2024-01-04 15:57:13 +03:00
|
|
|
)
|
2024-01-04 03:40:23 +03:00
|
|
|
|
|
|
|
return request
|
|
|
|
|
2024-01-04 03:14:03 +03:00
|
|
|
def handle_increment_user_request_count(self, date, number=1):
|
2023-08-21 15:05:13 +03:00
|
|
|
"""
|
|
|
|
Increment the user request count in the database
|
|
|
|
"""
|
2024-01-22 22:46:26 +03:00
|
|
|
current_requests_count = self.supabase_db.get_user_requests_count_for_month(
|
2023-08-21 15:05:13 +03:00
|
|
|
self.id, date
|
|
|
|
)
|
2024-02-07 10:23:37 +03:00
|
|
|
|
|
|
|
daily_requests_count = self.supabase_db.get_user_requests_count_for_day(
|
|
|
|
self.id, date
|
|
|
|
)
|
|
|
|
|
|
|
|
if daily_requests_count == 0:
|
2024-02-08 01:15:24 +03:00
|
|
|
logger.info("Request count is 0, creating new record")
|
2023-08-21 15:05:13 +03:00
|
|
|
if self.email is None:
|
|
|
|
raise ValueError("User Email should be defined for daily usage table")
|
|
|
|
self.supabase_db.create_user_daily_usage(
|
2024-01-04 03:14:03 +03:00
|
|
|
user_id=self.id, date=date, user_email=self.email, number=number
|
2023-08-21 15:05:13 +03:00
|
|
|
)
|
2024-02-07 10:23:37 +03:00
|
|
|
self.daily_requests_count = number
|
2023-08-21 15:05:13 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
self.supabase_db.increment_user_request_count(
|
|
|
|
user_id=self.id,
|
|
|
|
date=date,
|
2024-02-09 22:42:59 +03:00
|
|
|
number=daily_requests_count + number,
|
2023-08-21 15:05:13 +03:00
|
|
|
)
|
|
|
|
|
2024-02-07 10:23:37 +03:00
|
|
|
self.daily_requests_count = current_requests_count + number
|
2023-08-21 15:05:13 +03:00
|
|
|
|
|
|
|
logger.info(
|
2024-02-07 10:23:37 +03:00
|
|
|
f"User {self.email} request count updated to {self.daily_requests_count}"
|
2023-08-21 15:05:13 +03:00
|
|
|
)
|