Skip to content

Commit

Permalink
Merge branch 'develop' into 'main'
Browse files Browse the repository at this point in the history
Develop

See merge request tmllull/la-viciacion!7
  • Loading branch information
tmllull committed Sep 6, 2024
2 parents 82b7112 + ff22331 commit d23cd99
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
27 changes: 19 additions & 8 deletions api/app/crud/time_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,27 @@ def get_forgotten_timer_by_user(db: Session, user: models.User):
return active_time_entry


def get_older_timers(db: Session) -> list[models.TimeEntry]:
def get_older_active_timers(
db: Session, user: models.User = None
) -> list[models.TimeEntry]:
current_time = datetime.datetime.now()
time_threshold = current_time - datetime.timedelta(minutes=5)
active_time_entry = (
db.query(models.TimeEntry)
.filter(models.TimeEntry.duration.is_(None))
.filter(models.TimeEntry.start < time_threshold)
.all()
)
return active_time_entry
if user is None:
active_time_entries = (
db.query(models.TimeEntry)
.filter(models.TimeEntry.duration.is_(None))
.filter(models.TimeEntry.start < time_threshold)
.all()
)
else:
active_time_entries = (
db.query(models.TimeEntry)
.filter(models.TimeEntry.user_clockify_id == user.clockify_id)
.filter(models.TimeEntry.duration.is_(None))
.filter(models.TimeEntry.start < time_threshold)
.all()
)
return active_time_entries


def get_weekly_hours(
Expand Down
14 changes: 9 additions & 5 deletions api/app/utils/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,20 @@ async def sync_data(
logger.info("Current season: " + str(config.CURRENT_SEASON))
logger.info("Silent mode: " + str(silent))
logger.info("Sync clockify entries...")
delete_older_timers(db)
# delete_older_timers(db)
try:
if user_clfy_id is not None:
user_db = users.get_user_by_clockify_id(db, user_clfy_id)
if user_db:
users_db = [user_db]
logger.info("Delete older timers for " + str(user_db.name) + "...")
# delete_older_active_timers(db, user_db)
else:
logger.warning("User not found")
return
# else:
# logger.info("Delete older timers for all users...")
# delete_older_active_timers(db)
for user in users_db:
if user.name is not None and user.name != "":
user_name = str(user.name)
Expand Down Expand Up @@ -511,13 +516,12 @@ async def check_forgotten_timer(db: Session, user: models.User):
await utils.send_message_to_user(user.telegram_id, msg)


def delete_older_timers(db: Session):
logger.info("Delete older timers...")
def delete_older_active_timers(db: Session, user: models.User = None):
current_time = datetime.datetime.now().time()
minutes = current_time.minute
active_timers = time_entries.get_older_timers(db)
active_timers = time_entries.get_older_active_timers(db, user)
for timer in active_timers:
logger.info("Deleting timer " + str(timer.id))
# logger.info("Deleting timer " + str(timer.id))
db.delete(timer)
db.commit()

Expand Down
7 changes: 5 additions & 2 deletions api/app/utils/my_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ async def send_message(
openai=False,
system_prompt=prompts.DEFAULT_SYSTEM_PROMPT,
):
logger.info("Sending message...")
if not silent:
logger.info("Preparing message...")
if openai:
try:
# logger.debug("Original message: " + msg)
Expand All @@ -271,10 +271,11 @@ async def send_message(
logger.info(completion.choices[0].message.content)
msg = completion.choices[0].message.content
except Exception as e:
logger.info("Error generatins completion: " + str(e))
logger.info("Error generating completion: " + str(e))
bot = telegram.Bot(config.TELEGRAM_TOKEN)
async with bot:
try:
logger.info("Sending message...")
if image is None:
await bot.send_message(
text=msg,
Expand All @@ -291,6 +292,8 @@ async def send_message(
logger.info("Message sent successfully!")
except Exception as e:
logger.error("Error sending telegram message: " + str(e))
else:
logger.info("Silent mode. Message not sent.")


async def send_message_to_user(user_telegram_id, msg):
Expand Down

0 comments on commit d23cd99

Please sign in to comment.