fix: 🐛 usage (#2062)

fixed where monthly was used in daily

# 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:
Stan Girard 2024-01-22 14:42:41 -08:00 committed by GitHub
parent e5d8d0f621
commit 54eb76da25
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 48 additions and 93 deletions

View File

@ -229,6 +229,22 @@ class UserUsage(Repository):
)
return requests_stats.data
def get_user_requests_count_for_day(self, user_id, date):
"""
Fetch the user request count from the database
"""
response = (
self.db.from_("user_daily_usage")
.select("daily_requests_count")
.filter("user_id", "eq", user_id)
.filter("date", "eq", date)
.execute()
).data
if response and len(response) > 0:
return response[0]["daily_requests_count"]
return 0
def get_user_requests_count_for_month(self, user_id, date):
"""
Fetch the user request count from the database
@ -244,20 +260,17 @@ class UserUsage(Repository):
).data
if response and len(response) > 0:
logger.info("🔥🔥🔥🔥🔥")
logger.info(response)
return sum(row["daily_requests_count"] for row in response)
return 0
def increment_user_request_count(
self, user_id, date, current_requests_count: int, number: int = 1
):
def increment_user_request_count(self, user_id, date, number: int = 1):
"""
Increment the user's requests count for a specific day
"""
current = self.get_user_requests_count_for_day(user_id, date)
self.update_user_request_count(
user_id, daily_requests_count=current_requests_count + number, date=date
user_id, daily_requests_count=current + number, date=date
)
def update_user_request_count(self, user_id, daily_requests_count, date):

View File

@ -71,8 +71,10 @@ class UserUsage(UserIdentity):
current_requests_count = self.supabase_db.get_user_requests_count_for_month(
self.id, date
)
logger.info("🔥🔥🔥🔥🔥🔥🔥🔥")
logger.info(current_requests_count)
if current_requests_count == 0:
logger.info("Request count is 0, creating new record")
if self.email is None:
raise ValueError("User Email should be defined for daily usage table")
self.supabase_db.create_user_daily_usage(
@ -84,7 +86,6 @@ class UserUsage(UserIdentity):
self.supabase_db.increment_user_request_count(
user_id=self.id,
date=date,
current_requests_count=current_requests_count,
number=number,
)

View File

@ -2,9 +2,12 @@ import time
from uuid import UUID
from fastapi import HTTPException
from logger import get_logger
from models import UserUsage
from modules.user.entity.user_identity import UserIdentity
logger = get_logger(__name__)
class NullableUUID(UUID):
@classmethod
@ -33,6 +36,11 @@ def check_user_requests_limit(user: UserIdentity, model: str):
models_price = userDailyUsage.get_model_settings()
user_choosen_model_price = 1000
# Log all info
logger.info(
f"User {user.id} with email {user.email} has {monthly_chat_credit} monthly chat credit and has used {daily_user_count} requests today."
)
logger.info("🔥🔥")
for model_setting in models_price:
if model_setting["name"] == model:
user_choosen_model_price = model_setting["price"]

File diff suppressed because one or more lines are too long