Skip to content

Commit

Permalink
Fix scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
justinh-rahb committed Sep 28, 2023
1 parent b93c984 commit 26a4863
Showing 1 changed file with 45 additions and 42 deletions.
87 changes: 45 additions & 42 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,47 +39,49 @@ def refresh_and_fetch_csv(query_id):


def job():
time_period = datetime.now().strftime("%B %Y")
for query_id, title in zip(settings.query_ids, settings.titles):
data = refresh_and_fetch_csv(query_id)
if data is not None:
# Convert to XLSX
xlsx_file = f"{title} {time_period}.xlsx"
with pd.ExcelWriter(xlsx_file, engine='openpyxl') as writer:
# Start writing CSV data from the third row
data.to_excel(writer, index=False, startrow=2) # 0-indexed, so startrow=2 means the third row
# Check if today is the day of the month to run the job
if datetime.today().day == settings.day_of_month:
time_period = datetime.now().strftime("%B %Y")
for query_id, title in zip(settings.query_ids, settings.titles):
data = refresh_and_fetch_csv(query_id)
if data is not None:
# Convert to XLSX
xlsx_file = f"{title} {time_period}.xlsx"
with pd.ExcelWriter(xlsx_file, engine='openpyxl') as writer:
# Start writing CSV data from the third row
data.to_excel(writer, index=False, startrow=2) # 0-indexed, so startrow=2 means the third row

# Get the active worksheet
sheet = writer.sheets['Sheet1']

# Set column widths
for column in sheet.columns:
max_length = max(len(str(cell.value)) for cell in column)
sheet.column_dimensions[column[0].column_letter].width = max_length

# Define a cell style for bold and larger font
bold_large_font = styles.Font(bold=True, size=14)

# Insert the title, time period, and total count in the specified cells
title_cell = sheet['B1']
title_cell.value = title
title_cell.font = bold_large_font

date_cell = sheet['D1']
date_cell.value = time_period
date_cell.font = bold_large_font

total_cell = sheet['F1']
total_cell.value = f"Total Count: {len(data)}"
total_cell.font = bold_large_font

# Send email with XLSX attached
dynamic_subject = settings.subject.replace("{{time_period}}", time_period)
dynamic_content = settings.content.replace("{{time_period}}", time_period)
send_email(settings.sendgrid_api_key, settings.from_email, settings.to_emails, dynamic_subject, dynamic_content, xlsx_file)

# Get the active worksheet
sheet = writer.sheets['Sheet1']

# Set column widths
for column in sheet.columns:
max_length = max(len(str(cell.value)) for cell in column)
sheet.column_dimensions[column[0].column_letter].width = max_length

# Define a cell style for bold and larger font
bold_large_font = styles.Font(bold=True, size=14)

# Insert the title, time period, and total count in the specified cells
title_cell = sheet['B1']
title_cell.value = title
title_cell.font = bold_large_font

date_cell = sheet['D1']
date_cell.value = time_period
date_cell.font = bold_large_font

total_cell = sheet['F1']
total_cell.value = f"Total Count: {len(data)}"
total_cell.font = bold_large_font

# Send email with XLSX attached
dynamic_subject = settings.subject.replace("{{time_period}}", time_period)
dynamic_content = settings.content.replace("{{time_period}}", time_period)
send_email(settings.sendgrid_api_key, settings.from_email, settings.to_emails, dynamic_subject, dynamic_content, xlsx_file)

# Remove the XLSX file after sending the email
os.remove(xlsx_file)
# Remove the XLSX file after sending the email
os.remove(xlsx_file)


def main():
Expand All @@ -91,8 +93,9 @@ def main():
if args.now:
job()
else:
# Schedule the job to run once a month
schedule.every().month.at(f"{settings.day_of_month} {settings.hour_of_day}:00").do(job)
# Schedule the job to run every day at the specified hour,
# but the tasks inside the job will only execute if the day of the month matches the specified day in the settings.
schedule.every().day.at(f"{settings.hour_of_day}:00").do(job)

# Keep the script running
while True:
Expand Down

0 comments on commit 26a4863

Please sign in to comment.