Skip to content

Commit

Permalink
report interval
Browse files Browse the repository at this point in the history
  • Loading branch information
Proteseus committed Oct 20, 2023
1 parent be3ead5 commit 5f99bb7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ptb20.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async def start(update: Update, context: CallbackContext):
commands=[
BotCommand('generate_report_subs', 'Generate subs report'),
BotCommand('generate_report_orders', 'Generate orders report'),
BotCommand('generate_report_all_orders', 'Generate all orders report'),
BotCommand('delete_subscriber', 'Delete subscriber')
],
scope=BotCommandScopeChat(chat_id=update.effective_chat.id)
Expand Down Expand Up @@ -533,6 +534,21 @@ async def generate_report_ord(update: Update, context: CallbackContext):
else:
await update.message.reply_text("YOU ARE NOT AN ADMIN")

async def generate_report_all_ord(update: Update, context: CallbackContext):
"""Generate report"""
if str(update.effective_chat.id) == os.getenv('USERNAME') or str(update.effective_chat.id) == os.getenv('USERNAME_Y') or str(update.effective_chat.id) == os.getenv('USERNAME_S'):
# if str(update.effective_user.id) == os.getenv('USERNAME'):
order = session.query(Order)

if order:
subprocess.run(['python3', 'reports.py', f'{update.effective_chat.id}', 'ord_all'])
logger.info("Report generated.")
else:
await update.message.reply_text("No subscribers to report on.")
logger.info("Report not generated. No subscribers to report on.")
else:
await update.message.reply_text("YOU ARE NOT AN ADMIN")

async def get_chat_id(update, context):
chat_id = update.message.chat_id
user_id = update.message.from_user.id
Expand Down Expand Up @@ -718,6 +734,7 @@ def main():
application.add_handler(CommandHandler("delete_subscriber", delete_subscriber))
application.add_handler(CommandHandler("generate_report_subs", generate_report_sub))
application.add_handler(CommandHandler("generate_report_orders", generate_report_ord))
application.add_handler(CommandHandler("generate_report_all_orders", generate_report_all_ord))
application.add_handler(CommandHandler("change_language", change_language))
application.add_handler(CommandHandler("contact_us", contact_us))
application.add_handler(CommandHandler("about", about))
Expand Down
28 changes: 28 additions & 0 deletions reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import glob
import time
import asyncio
from datetime import datetime, timedelta

from db import create_user_order, add_order, session
from model import Order, Trackable
Expand Down Expand Up @@ -34,8 +35,32 @@ def iterate_subscribers(csv_file_path_subs):
csv_writer.writerows(data)

def iterate_orders(csv_file_path_orders):
delete_old_csv_files()
# orders = session.query(Trackable).all()

now = datetime.now()
start_time = datetime(now.year, now.month, now.day, 22, 0, 0) - timedelta(days=1)
end_time = datetime(now.year, now.month, now.day, 22, 0, 0)
orders = session.query(Trackable).filter(Trackable.date >= start_time, Trackable.date <= end_time)


data = []
for order in orders:
order_data = []
for column in Trackable.__table__.columns:
order_data.append(str(getattr(order, column.name)))
data.append(order_data)

# Write the data to a CSV file
with open(csv_file_path_orders, 'w', newline='') as file:
csv_writer = csv.writer(file)
csv_writer.writerow([column.name for column in Trackable.__table__.columns])
csv_writer.writerows(data)

def iterate_all_orders(csv_file_path_orders):
delete_old_csv_files()
orders = session.query(Trackable).all()

data = []
for order in orders:
order_data = []
Expand Down Expand Up @@ -70,3 +95,6 @@ async def send_csv_to_user(csv_file_path, user):
elif opt == 'ord':
iterate_orders(csv_file_path_orders)
asyncio.run(send_csv_to_user(csv_file_path_orders, user))
elif opt == 'ord_all':
iterate_all_orders(csv_file_path_orders)
asyncio.run(send_csv_to_user(csv_file_path_orders, user))

0 comments on commit 5f99bb7

Please sign in to comment.