From 3a24990af527095e4a3e52e010e9443b887122e9 Mon Sep 17 00:00:00 2001 From: Stan Girard Date: Mon, 22 Jan 2024 11:46:26 -0800 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20usage=20(#2057)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit moving from daily to monthly # Description Please include a summary of the changes and the related issue. Please also include relevant motivation and context. ## Checklist before requesting a review Please delete options that are not relevant. - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented hard-to-understand areas - [ ] I have ideally added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged ## Screenshots (if appropriate): --- backend/models/databases/repository.py | 2 +- backend/models/databases/supabase/user_usage.py | 14 +++++++++----- backend/models/user_usage.py | 8 ++++---- backend/modules/chat/controller/chat/utils.py | 8 ++++---- backend/modules/user/controller/user_controller.py | 4 ++-- frontend/lib/types/User.ts | 2 +- .../migrations/20240122194117_monthly-credit.sql | 9 +++++++++ 7 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 supabase/migrations/20240122194117_monthly-credit.sql diff --git a/backend/models/databases/repository.py b/backend/models/databases/repository.py index 7cc3edb91..dc62ff06b 100644 --- a/backend/models/databases/repository.py +++ b/backend/models/databases/repository.py @@ -13,7 +13,7 @@ class Repository(ABC): pass @abstractmethod - def get_user_requests_count_for_day(self, user_id: UUID, date: datetime): + def get_user_requests_count_for_month(self, user_id: UUID, date: datetime): pass @abstractmethod diff --git a/backend/models/databases/supabase/user_usage.py b/backend/models/databases/supabase/user_usage.py index d0b8a6a82..f88800ea5 100644 --- a/backend/models/databases/supabase/user_usage.py +++ b/backend/models/databases/supabase/user_usage.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timedelta from uuid import UUID from logger import get_logger @@ -122,7 +122,7 @@ class UserUsage(Repository): { "max_brains": product_settings["max_brains"], "max_brain_size": product_settings["max_brain_size"], - "daily_chat_credit": product_settings["daily_chat_credit"], + "monthly_chat_credit": product_settings["monthly_chat_credit"], "api_access": product_settings["api_access"], "models": product_settings["models"], } @@ -229,20 +229,24 @@ class UserUsage(Repository): ) return requests_stats.data - def get_user_requests_count_for_day(self, user_id, date): + def get_user_requests_count_for_month(self, user_id, date): """ Fetch the user request count from the database """ + date_30_days_ago = datetime.now() - timedelta(days=30) + response = ( self.db.from_("user_daily_usage") .select("daily_requests_count") .filter("user_id", "eq", user_id) - .filter("date", "eq", date) + .filter("date", "gte", date_30_days_ago) .execute() ).data if response and len(response) > 0: - return response[0]["daily_requests_count"] + logger.info("🔥🔥🔥🔥🔥") + logger.info(response) + return sum(row["daily_requests_count"] for row in response) return 0 def increment_user_request_count( diff --git a/backend/models/user_usage.py b/backend/models/user_usage.py index a04a77a23..3e71acfbc 100644 --- a/backend/models/user_usage.py +++ b/backend/models/user_usage.py @@ -52,14 +52,14 @@ class UserUsage(UserIdentity): return request - def get_user_daily_usage(self, date): + def get_user_monthly_usage(self, date): """ Fetch the user daily usage from the database """ posthog = PostHogSettings() - request = self.supabase_db.get_user_requests_count_for_day(self.id, date) + request = self.supabase_db.get_user_requests_count_for_month(self.id, date) posthog.set_user_properties( - self.id, "DAILY_USAGE", {"daily_chat_usage": request} + self.id, "MONTHLY_USAGE", {"monthly_chat_usage": request} ) return request @@ -68,7 +68,7 @@ class UserUsage(UserIdentity): """ Increment the user request count in the database """ - current_requests_count = self.supabase_db.get_user_requests_count_for_day( + current_requests_count = self.supabase_db.get_user_requests_count_for_month( self.id, date ) diff --git a/backend/modules/chat/controller/chat/utils.py b/backend/modules/chat/controller/chat/utils.py index fec5a0671..ebafe2e98 100644 --- a/backend/modules/chat/controller/chat/utils.py +++ b/backend/modules/chat/controller/chat/utils.py @@ -28,8 +28,8 @@ def check_user_requests_limit(user: UserIdentity, model: str): date = time.strftime("%Y%m%d") - daily_chat_credit = userSettings.get("daily_chat_credit", 0) - daily_user_count = userDailyUsage.get_user_daily_usage(date) + monthly_chat_credit = userSettings.get("monthly_chat_credit", 0) + daily_user_count = userDailyUsage.get_user_monthly_usage(date) models_price = userDailyUsage.get_model_settings() user_choosen_model_price = 1000 @@ -37,10 +37,10 @@ def check_user_requests_limit(user: UserIdentity, model: str): if model_setting["name"] == model: user_choosen_model_price = model_setting["price"] - if int(daily_user_count + user_choosen_model_price) > int(daily_chat_credit): + if int(daily_user_count + user_choosen_model_price) > int(monthly_chat_credit): raise HTTPException( status_code=429, # pyright: ignore reportPrivateUsage=none - detail=f"You have reached your daily chat limit of {daily_chat_credit} requests per day. Please upgrade your plan to increase your daily chat limit.", + detail=f"You have reached your monthly chat limit of {monthly_chat_credit} requests per months. Please upgrade your plan to increase your daily chat limit.", ) else: userDailyUsage.handle_increment_user_request_count( diff --git a/backend/modules/user/controller/user_controller.py b/backend/modules/user/controller/user_controller.py index 1339d2486..49758d6df 100644 --- a/backend/modules/user/controller/user_controller.py +++ b/backend/modules/user/controller/user_controller.py @@ -38,7 +38,7 @@ async def get_user_endpoint( max_brain_size = user_settings.get("max_brain_size", 1000000000) date = time.strftime("%Y%m%d") - daily_chat_credit = user_settings.get("daily_chat_credit", 10) + monthly_chat_credit = user_settings.get("monthly_chat_credit", 10) user_daily_usage = UserUsage(id=current_user.id) requests_stats = user_daily_usage.get_user_usage() @@ -53,7 +53,7 @@ async def get_user_endpoint( "email": current_user.email, "max_brain_size": max_brain_size, "current_brain_size": defaul_brain_size, - "daily_chat_credit": daily_chat_credit, + "monthly_chat_credit": monthly_chat_credit, "requests_stats": requests_stats, "models": user_settings.get("models", []), "date": date, diff --git a/frontend/lib/types/User.ts b/frontend/lib/types/User.ts index ba93598b9..34badaf22 100644 --- a/frontend/lib/types/User.ts +++ b/frontend/lib/types/User.ts @@ -8,7 +8,7 @@ export interface UserStats { email: string; max_brain_size: number; current_brain_size: number; - daily_chat_credit: number; + monthly_chat_credit: number; requests_stats: RequestStat[]; date: string; models: string[]; diff --git a/supabase/migrations/20240122194117_monthly-credit.sql b/supabase/migrations/20240122194117_monthly-credit.sql new file mode 100644 index 000000000..fb0de7898 --- /dev/null +++ b/supabase/migrations/20240122194117_monthly-credit.sql @@ -0,0 +1,9 @@ +alter table "public"."product_to_features" drop column "daily_chat_credit"; + +alter table "public"."product_to_features" add column "monthly_chat_credit" integer not null default 20; + +alter table "public"."user_settings" drop column "daily_chat_credit"; + +alter table "public"."user_settings" add column "monthly_chat_credit" integer default 100; + +