Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve user credit calculation in get_user_credits #3367

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions backend/api/quivr_api/modules/user/repository/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,27 @@ def delete_user_data(self, user_id):
self.db.table("users").delete().filter("id", "eq", str(user_id)).execute()

def get_user_credits(self, user_id):
user_usage_instance = user_usage.UserUsage(id=user_id)
try:
user_usage_instance = user_usage.UserUsage(id=user_id)

user_monthly_usage = user_usage_instance.get_user_monthly_usage(
time.strftime("%Y%m%d")
)
monthly_chat_credit = (
self.db.from_("user_settings")
.select("monthly_chat_credit")
.filter("user_id", "eq", str(user_id))
.execute()
.data[0]["monthly_chat_credit"]
)
user_monthly_usage = user_usage_instance.get_user_monthly_usage(
time.strftime("%Y%m%d")
)

response = self.db.from_("user_settings").select("monthly_chat_credit").filter(
"user_id", "eq", str(user_id)
).execute()

if not response.data:
raise ValueError("No data found for user settings")

monthly_chat_credit = response.data[0].get("monthly_chat_credit")
if monthly_chat_credit is None:
raise ValueError("Monthly chat credit not found")

return monthly_chat_credit - user_monthly_usage

return monthly_chat_credit - user_monthly_usage
except Exception as e:
# Log the exception or handle it as needed
print(f"An error occurred while getting user credits: {e}")
return 25 # or a default value, depending on your needs
4 changes: 0 additions & 4 deletions backend/api/quivr_api/modules/user/service/user_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ def get_user_monthly_usage(self, date):
"""
Fetch the user monthly usage from the database
"""
posthog = PostHogSettings()
request = self.supabase_db.get_user_requests_count_for_month(self.id, date)
posthog.set_user_properties(
self.id, "MONTHLY_USAGE", {"monthly_chat_usage": request}
)

return request

Expand Down
Loading