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

fix current time issue #175

Merged
merged 1 commit into from
Sep 19, 2023
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
1 change: 1 addition & 0 deletions src/sherpa_ai/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
environ.get("DAILY_LIMIT_REACHED_MESSAGE")
or "Sorry for the inconvenience, but it seems that you have exceeded your daily token limit. As a result, you will need to try again after 24 hours. Thank you for your understanding."
)
LIMIT_TIME_SIZE_IN_HOURS = environ.get("LIMIT_TIME_SIZE_IN_HOURS") or "24"


# Configure logger. To get JSON serialization, set serialize=True.
Expand Down
12 changes: 5 additions & 7 deletions src/sherpa_ai/database/user_usage_tracker.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import sqlite3
import time

import sherpa_ai.config as cfg

class UserUsageTracker:
def __init__(
self,
db_name="token_countersaa.db",
current_time=int(time.time()),
max_daily_token=20000,
):
self.conn = sqlite3.connect(db_name)
self.create_table()
self.max_daily_token = int(max_daily_token)
self.current_time = current_time

def create_table(self):
create_token_reset_table_query = """
Expand Down Expand Up @@ -76,7 +74,7 @@ def add_data(self, combined_id, token, reset_timestamp=False):
VALUES (?, ?, ?, ?)
"""
self.conn.execute(
insert_query, (combined_id, token, self.current_time, reset_timestamp)
insert_query, (combined_id, token, int(time.time()), reset_timestamp)
)
self.conn.commit()

Expand Down Expand Up @@ -133,7 +131,7 @@ def get_last_reset_info(self, combined_id):
else:
return None
def seconds_to_hms(self,seconds):
remaining_seconds = 24 * 3600 - seconds # Calculate the remaining seconds to reach 24 hours
remaining_seconds = int(float(cfg.LIMIT_TIME_SIZE_IN_HOURS) * 3600 - seconds) # Calculate the remaining seconds to reach 24 hours
hours = remaining_seconds // 3600
minutes = (remaining_seconds % 3600) // 60
seconds = remaining_seconds % 60
Expand All @@ -156,9 +154,9 @@ def check_usage(self, user_id, combined_id, token_ammount):
time_since_last_reset = 99999

if last_reset_info is not None and last_reset_info["timestamp"] is not None:
time_since_last_reset = self.current_time - last_reset_info["timestamp"]
time_since_last_reset = int(time.time()) - last_reset_info["timestamp"]
# If more than 24 hours have passed since last reset
if time_since_last_reset != 0 and time_since_last_reset > 86400:
if time_since_last_reset != 0 and time_since_last_reset > 3600*float(cfg.LIMIT_TIME_SIZE_IN_HOURS):
print(f"TIMESTAMP DIFFERENT: {time_since_last_reset}")
self.reset_usage(combined_id=combined_id, token_ammount=token_ammount)
return {
Expand Down