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

add a count down function #173

Merged
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
3 changes: 2 additions & 1 deletion src/apps/slackapp/slackapp/bolt_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Importing necessary modules
##############################################

import time
from typing import Dict, List

from flask import Flask, request
Expand Down Expand Up @@ -166,7 +167,7 @@ def event_test(client, say, event):

say(results, thread_ts=thread_ts)
else:
say(cfg.DAILY_LIMIT_REACHED_MESSAGE, thread_ts=thread_ts)
say(f"""I'm sorry for any inconvenience, but it appears you've gone over your daily token limit. Don't worry, you'll be able to use our service again in approximately {usage_cheker['time_left']}.Thank you for your patience and understanding.""", thread_ts=thread_ts)


@app.event("app_home_opened")
Expand Down
13 changes: 12 additions & 1 deletion src/sherpa_ai/database/user_usage_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ def get_last_reset_info(self, combined_id):
return {"id": last_reset_id, "timestamp": last_reset_timestamp}
else:
return None
def seconds_to_hms(self,seconds):
remaining_seconds = 24 * 3600 - seconds # Calculate the remaining seconds to reach 24 hours
hours = remaining_seconds // 3600
minutes = (remaining_seconds % 3600) // 60
seconds = remaining_seconds % 60

return f"""{hours}hours : {minutes}min : {seconds}sec"""


def check_usage(self, user_id, combined_id, token_ammount):
user_is_whitelisted = self.is_in_whitelist(user_id)
Expand All @@ -141,14 +149,14 @@ def check_usage(self, user_id, combined_id, token_ammount):
"token-left": self.max_daily_token,
"can_excute": True,
"message": "",
"time_left":""
}
else:
last_reset_info = self.get_last_reset_info(combined_id=combined_id)
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"]

# If more than 24 hours have passed since last reset
if time_since_last_reset != 0 and time_since_last_reset > 86400:
print(f"TIMESTAMP DIFFERENT: {time_since_last_reset}")
Expand All @@ -157,6 +165,7 @@ def check_usage(self, user_id, combined_id, token_ammount):
"token-left": self.max_daily_token,
"can_excute": True,
"message": "",
"time_left":self.seconds_to_hms(time_since_last_reset)
}
else:
total_token_since_last_reset = self.get_sum_of_tokens_since_last_reset(
Expand All @@ -169,6 +178,7 @@ def check_usage(self, user_id, combined_id, token_ammount):
- total_token_since_last_reset,
"can_excute": False,
"message": "daily usage limit exceeded. you can try after 24 hours",
"time_left":self.seconds_to_hms(time_since_last_reset)
}
else:
self.add_data(combined_id=combined_id, token=token_ammount)
Expand All @@ -178,6 +188,7 @@ def check_usage(self, user_id, combined_id, token_ammount):
"current_token": token_ammount,
"can_excute": True,
"message": "",
"time_left":self.seconds_to_hms(time_since_last_reset)
}

def get_all_data(self):
Expand Down