Skip to content

Commit

Permalink
Bug Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
deexno authored Jul 27, 2023
1 parent 010150a commit 7780f66
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
63 changes: 35 additions & 28 deletions resources/fqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import uuid
from datetime import datetime

import pandas as pd


class Queue(object):
def __init__(self, file_path, order_by="created", ascending=True) -> None:
Expand All @@ -14,43 +12,52 @@ def __init__(self, file_path, order_by="created", ascending=True) -> None:
self.update_queue()

def store_queue(self):
self.queue.to_csv(
self.file_path, encoding="utf-8", sep="|", index=False
)
with open(self.file_path, "w", encoding="utf-8") as f:
for item in self.queue:
f.write(
f"{item['event']}|"
f"{item['id']}|"
f"{item['priority']}|"
f"{item['created']}\n"
)

def update_queue(self):
if not os.path.exists(self.file_path):
queue_columns = ["event", "id", "priority", "created"]
self.queue = pd.DataFrame([], columns=queue_columns)
self.queue = []
self.store_queue()
else:
self.queue = pd.read_csv(self.file_path, sep="|")

self.queue = self.queue.sort_values(
by=self.order_by, ascending=self.ascending
)
self.queue = []
with open(self.file_path, "r", encoding="utf-8") as f:
for line in f:
event, item_id, priority, created = line.strip().split("|")
self.queue.append(
{
"event": event,
"id": item_id,
"priority": priority,
"created": created,
}
)

self.queue.sort(
key=lambda item: item[self.order_by],
reverse=not self.ascending,
)

def get_queue(self):
self.update_queue()
return self.queue

def add_item(self, event, priority=0):
self.queue = pd.concat(
[
pd.DataFrame(
{
"event": [event],
"id": [uuid.uuid1()],
"priority": [priority],
"created": [datetime.now()],
}
),
self.queue,
]
).reset_index(drop=True)

new_item = {
"event": event,
"id": str(uuid.uuid1()),
"priority": priority,
"created": datetime.now(),
}
self.queue.insert(0, new_item)
self.store_queue()

def drop_item(self, id):
self.queue = self.queue[self.queue["id"] != id]
def drop_item(self, item_id):
self.queue = [item for item in self.queue if item["id"] != item_id]
self.store_queue()
4 changes: 2 additions & 2 deletions resources/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ async def post_init(bot_handler: Application) -> None:
def notifcation_listener():
while True:
try:
for id, notification in notifcation_queue.get_queue().iterrows():
for notification in notifcation_queue.get_queue():
bot_handler_job_queue.run_once(
send_automatic_notification, 3, data=notification["event"]
)
Expand Down Expand Up @@ -1489,7 +1489,7 @@ async def list_notify_queue(
try:
if is_user_authenticated(update.effective_user.id):
notifications = notifcation_queue.get_queue()
notifications_count = len(notifications.index)
notifications_count = len(notifications)

await update.message.reply_text(
translate(
Expand Down
4 changes: 0 additions & 4 deletions resources/telegram_plus_notify_listener
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ destination_log_file_path=$destination_log_folder/$destination_log_file

mkdir -p $destination_log_folder # Creates the directory if it doesn't already exist.

if [ ! -e $destination_log_file_path ]; then
echo "event|id|priority|created" > $destination_log_file_path
fi

# Save all variables in a file which is then read and processed by the Telegram bot.
if [[ ${NOTIFY_WHAT} == "SERVICE" ]]; then
echo "$notify;${NOTIFY_SERVICEDESC};${NOTIFY_PREVIOUSSERVICEHARDSHORTSTATE};${NOTIFY_SERVICESHORTSTATE};${NOTIFY_SERVICEOUTPUT}|${random_line_identifier}|0|${now}" >> $destination_log_file_path
Expand Down

0 comments on commit 7780f66

Please sign in to comment.