Commit 3780ce34 authored by vaisakh.nair's avatar vaisakh.nair 🎯

added docstrings

parent a8a54ebb
......@@ -13,12 +13,9 @@ def generate_daily_report():
# Generate the daily report
report_path = report_generator.create_excel_report()
# saving file locally in one more specified file path
report_generator.save_report_locally(filepath=report_path)
# Send the email with the report
# please comment out if you are running the create_report.py file without a email method.
#report_generator.send_email_from_ut(filepath=report_path)
report_generator.send_email_from_ut(filepath=report_path)
return report_path
......
template_file: D:\vision_utilities\vision_utilities\report_generator\templates\daily_report_jk_template.xlsx
folder_path: D:\vision_utilities\vision_utilities\report_generator\report_bkp
folder_path: D:\vision_utilities\vision_utilities\report_generator\daily_reports
# logo_image: path/to/logo.png
cameras:
......
......@@ -35,6 +35,13 @@ class DailyReportGenerator:
def get_shift_name(self, timestamp):
"""
Maps the shift names to the desired timings.
Returns:
str: The shift names.
"""
hour = timestamp.hour
if 6 <= hour < 14:
return 'Shift - A'
......@@ -45,10 +52,30 @@ class DailyReportGenerator:
def get_packer_name(self, camera_name):
"""
Fetches the camera mapping fromt he config file/any for of input file given.
Returns: The mappings
"""
return self.camera_mappings.get(camera_name, 'Unknown')
def get_count(self, start_time, end_time, camera_name):
"""
Fetches the count difference between the first and last document within the specified time range and camera name.
Args:
start_time (datetime): The start time of the time range.
end_time (datetime): The end time of the time range.
camera_name (str): The name of the camera.
Returns:
int: The count difference between the first and last document within the specified time range and camera name.
"""
query = {
'timestamp': {'$gte': start_time, '$lte': end_time},
'cameraName': camera_name
......@@ -71,6 +98,18 @@ class DailyReportGenerator:
def create_excel_report(self):
"""
Generates an Excel report based on the provided data and saves it in the specified folder.
Returns:
str: The absolute path of the generated report file.
"""
reports_folder = self.config['folder_path']
if not os.path.exists(reports_folder):
os.makedirs(reports_folder)
report_file = 'daily_report.xlsx'
......@@ -141,6 +180,8 @@ class DailyReportGenerator:
print_area = f"A1:{openpyxl.utils.get_column_letter(max_column)}{max_row}"
sheet.print_area = print_area
report_file = os.path.join(reports_folder, report_file)
# Save the report file
wb.save(report_file)
......@@ -150,23 +191,15 @@ class DailyReportGenerator:
return file_path
def save_report_locally(self, filepath):
# Create the 'daily_reports' folder if it doesn't exist
folder_path = self.config['folder_path']
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# Get the filename from the filepath
filename = os.path.basename(filepath)
# Save the report file in the 'daily_reports' folder
destination = os.path.join(folder_path, filename)
os.rename(filepath, destination)
def get_receiver_emails(self):
return destination
"""
Retrieves the email addresses of receivers from the MongoDB collection.
Returns:
list: A list of email addresses of receivers.
def get_receiver_emails(self):
"""
# Connect to MongoDB and retrieve the email addresses from the collection
client = MongoClient(os.environ["MONGO_CLIENT"])
db = client[os.environ["MONGO_DATABASE"]]
......@@ -182,6 +215,18 @@ class DailyReportGenerator:
def send_email_from_ut(self,filename="daily_report.xlsx", filepath=None):
"""
Sends an email to the receiver list with the specified file attachment and payload.
Args:
filename (str, optional): The name of the file attachment. Defaults to "daily_report.xlsx".
filepath (str, optional): The file path of the attachment. Defaults to None.
Returns:
bool: True if the email was successfully sent, False otherwise.
"""
logging.info("Sending email to {}".format(self.get_receiver_emails()))
payload = dict()
payload['from_name'] = os.environ["FROM_ADDRESS"]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment