mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-14 17:03:29 +03:00
feat: 🎸 usage (#2057)
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):
This commit is contained in:
parent
f17e22ca49
commit
3a24990af5
@ -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
|
||||
|
@ -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(
|
||||
|
@ -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
|
||||
)
|
||||
|
||||
|
@ -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(
|
||||
|
@ -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,
|
||||
|
@ -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[];
|
||||
|
9
supabase/migrations/20240122194117_monthly-credit.sql
Normal file
9
supabase/migrations/20240122194117_monthly-credit.sql
Normal file
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user