Commit f8e51d3c authored by vaisakh.nair's avatar vaisakh.nair 🎯

coverted to a class

parent 548ff9ba
......@@ -5,13 +5,14 @@ import schedule
import time
import os
# Connect to MongoDB
client = MongoClient(os.environ["MONGO_CLIENT"])
db = client[os.environ["MONGO_DATABASE"]]
collection = db[os.environ["MONGO_COLLECTION"]]
class DailyReportGenerator:
def __init__(self):
# Connect to MongoDB
client = MongoClient(os.environ["MONGO_CLIENT"])
db = client[os.environ["MONGO_DATABASE"]]
self.collection = db[os.environ["MONGO_COLLECTION"]]
# Define function to get shift name
def get_shift_name(timestamp):
def get_shift_name(self, timestamp):
hour = timestamp.hour
if 6 <= hour < 14:
return 'Shift A'
......@@ -20,8 +21,7 @@ def get_shift_name(timestamp):
else:
return 'Shift C'
# Define function to map packer names
def map_packer_name(camera_name):
def map_packer_name(self, camera_name):
packer_mapping = {
'camera_41': 'Packer 1',
'camera_42': 'Packer 2',
......@@ -32,26 +32,24 @@ def map_packer_name(camera_name):
}
return packer_mapping.get(camera_name, 'Unknown')
# Retrieve count using query
def get_count(start_time, end_time, camera_name):
def get_count(self, start_time, end_time, camera_name):
query = {
'timestamp': {'$gte': start_time, '$lte': end_time},
'cameraName': camera_name
}
count = collection.count_documents(query)
count = self.collection.count_documents(query)
return count
# Create Excel report
def create_excel_report():
def create_excel_report(self):
data = []
current_time = datetime.now()
start_time = datetime(current_time.year, current_time.month, current_time.day - 1, 6, 0, 0)
end_time = datetime(current_time.year, current_time.month, current_time.day, 6, 0, 0)
for camera_name in ['camera_41', 'camera_42', 'camera_44', 'camera_45', 'camera_46', 'camera_47']:
shift_name = get_shift_name(start_time)
packer_name = map_packer_name(camera_name)
count = get_count(start_time, end_time, camera_name)
shift_name = self.get_shift_name(start_time)
packer_name = self.map_packer_name(camera_name)
count = self.get_count(start_time, end_time, camera_name)
data.append({
'Date': start_time.date(),
......@@ -65,10 +63,13 @@ def create_excel_report():
df.to_excel(writer, index=False, sheet_name='Report')
writer.save()
# Schedule the report generation
schedule.every().day.at('08:00').do(create_excel_report)
def schedule_report_generation(self):
schedule.every().day.at('08:00').do(self.create_excel_report)
# Run the scheduler
while True:
while True:
schedule.run_pending()
time.sleep(1)
# Create an instance of DailyReportGenerator and start scheduling report generation
report_generator = DailyReportGenerator()
report_generator.schedule_report_generation()
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