Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -132,10 +132,13 @@ LABOPS_REPORTS_FOLDER_ID = get_folder_id('LabOps Reports')
|
|
| 132 |
# Send AMC reminder emails
|
| 133 |
def send_amc_reminder_emails(reminders_df):
|
| 134 |
if reminders_df.empty:
|
| 135 |
-
logging.info("No AMC reminders to send via email.")
|
| 136 |
return "No AMC reminder emails sent (no reminders found)."
|
| 137 |
|
| 138 |
try:
|
|
|
|
|
|
|
|
|
|
| 139 |
# Set up the SMTP server
|
| 140 |
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
| 141 |
server.starttls()
|
|
@@ -144,7 +147,7 @@ def send_amc_reminder_emails(reminders_df):
|
|
| 144 |
email_results = []
|
| 145 |
for _, row in reminders_df.iterrows():
|
| 146 |
device_id = row['device_id']
|
| 147 |
-
amc_date = row['amc_date'].strftime('%Y-%m-%d')
|
| 148 |
|
| 149 |
# Create the email
|
| 150 |
msg = MIMEMultipart()
|
|
@@ -175,7 +178,7 @@ LabOps Team
|
|
| 175 |
logging.error(f"Failed to send AMC reminder emails: {str(e)}")
|
| 176 |
return f"Failed to send AMC reminder emails: {str(e)}"
|
| 177 |
|
| 178 |
-
#
|
| 179 |
def create_salesforce_reports(df):
|
| 180 |
if sf is None:
|
| 181 |
return "Salesforce connection not available."
|
|
@@ -183,39 +186,43 @@ def create_salesforce_reports(df):
|
|
| 183 |
return "Cannot create reports: 'LabOps Reports' folder not found in Salesforce."
|
| 184 |
|
| 185 |
try:
|
| 186 |
-
#
|
| 187 |
usage_report_metadata = {
|
| 188 |
"reportMetadata": {
|
| 189 |
"name": f"SmartLog_Usage_Report_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 190 |
-
"
|
|
|
|
| 191 |
"reportFormat": "SUMMARY",
|
| 192 |
-
"
|
| 193 |
-
{"field": "Device_Id__c"},
|
| 194 |
-
{"field": "Usage_Hours__c", "aggregateTypes": ["Sum"]}
|
| 195 |
],
|
| 196 |
"groupingsDown": [
|
| 197 |
-
{"field": "Device_Id__c", "sortOrder": "Asc"}
|
| 198 |
],
|
| 199 |
"folderId": LABOPS_REPORTS_FOLDER_ID
|
| 200 |
}
|
| 201 |
}
|
|
|
|
| 202 |
usage_result = sf.restful('analytics/reports', method='POST', json=usage_report_metadata)
|
| 203 |
usage_report_id = usage_result['id']
|
| 204 |
logging.info(f"Usage Report created: {usage_report_id}")
|
| 205 |
|
| 206 |
-
#
|
| 207 |
amc_report_metadata = {
|
| 208 |
"reportMetadata": {
|
| 209 |
"name": f"SmartLog_AMC_Reminders_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 210 |
-
"
|
|
|
|
| 211 |
"reportFormat": "TABULAR",
|
| 212 |
-
"
|
| 213 |
-
{"field": "Device_Id__c"},
|
| 214 |
-
{"field": "AMC_Date__c"}
|
| 215 |
],
|
| 216 |
"folderId": LABOPS_REPORTS_FOLDER_ID
|
| 217 |
}
|
| 218 |
}
|
|
|
|
| 219 |
amc_result = sf.restful('analytics/reports', method='POST', json=amc_report_metadata)
|
| 220 |
amc_report_id = amc_result['id']
|
| 221 |
logging.info(f"AMC Reminders Report created: {amc_report_id}")
|
|
@@ -225,7 +232,7 @@ def create_salesforce_reports(df):
|
|
| 225 |
logging.error(f"Failed to create Salesforce reports: {str(e)}")
|
| 226 |
return f"Failed to create reports: {str(e)}"
|
| 227 |
|
| 228 |
-
# Save results to Salesforce SmartLog__c
|
| 229 |
def save_to_salesforce(df, summary, anomalies, amc_reminders, insights):
|
| 230 |
if sf is None:
|
| 231 |
return "Salesforce connection not available."
|
|
@@ -332,10 +339,12 @@ def check_amc_reminders(df, current_date):
|
|
| 332 |
df["days_to_amc"] = (df["amc_date"] - current_date).dt.days
|
| 333 |
reminders = df[(df["days_to_amc"] >= 0) & (df["days_to_amc"] <= 30)][["device_id", "amc_date"]]
|
| 334 |
if reminders.empty:
|
|
|
|
| 335 |
return "No AMC reminders due within the next 30 days.", reminders
|
| 336 |
reminder_lines = ["Upcoming AMC Reminders:"]
|
| 337 |
for _, row in reminders.head(5).iterrows():
|
| 338 |
reminder_lines.append(f"- Device ID: {row['device_id']}, AMC Date: {row['amc_date']}")
|
|
|
|
| 339 |
return "\n".join(reminder_lines), reminders
|
| 340 |
except Exception as e:
|
| 341 |
logging.error(f"AMC reminder generation failed: {str(e)}")
|
|
@@ -459,7 +468,7 @@ async def process_logs(file_obj):
|
|
| 459 |
if df.empty:
|
| 460 |
return "No data available.", None, None, None, None, None, None, None, None, None
|
| 461 |
|
| 462 |
-
# Run tasks concurrently
|
| 463 |
with ThreadPoolExecutor(max_workers=4) as executor:
|
| 464 |
future_summary = executor.submit(summarize_logs, df)
|
| 465 |
future_anomalies = executor.submit(detect_anomalies, df)
|
|
|
|
| 132 |
# Send AMC reminder emails
|
| 133 |
def send_amc_reminder_emails(reminders_df):
|
| 134 |
if reminders_df.empty:
|
| 135 |
+
logging.info("No AMC reminders to send via email. Dataframe is empty.")
|
| 136 |
return "No AMC reminder emails sent (no reminders found)."
|
| 137 |
|
| 138 |
try:
|
| 139 |
+
# Log the reminders for debugging
|
| 140 |
+
logging.info(f"Found {len(reminders_df)} AMC reminders to send: {reminders_df.to_dict()}")
|
| 141 |
+
|
| 142 |
# Set up the SMTP server
|
| 143 |
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
| 144 |
server.starttls()
|
|
|
|
| 147 |
email_results = []
|
| 148 |
for _, row in reminders_df.iterrows():
|
| 149 |
device_id = row['device_id']
|
| 150 |
+
amc_date = row['amc_date'].strftime('%Y-%m-%d') if pd.notna(row['amc_date']) else "N/A"
|
| 151 |
|
| 152 |
# Create the email
|
| 153 |
msg = MIMEMultipart()
|
|
|
|
| 178 |
logging.error(f"Failed to send AMC reminder emails: {str(e)}")
|
| 179 |
return f"Failed to send AMC reminder emails: {str(e)}"
|
| 180 |
|
| 181 |
+
# Updated Salesforce report creation with correct field API names
|
| 182 |
def create_salesforce_reports(df):
|
| 183 |
if sf is None:
|
| 184 |
return "Salesforce connection not available."
|
|
|
|
| 186 |
return "Cannot create reports: 'LabOps Reports' folder not found in Salesforce."
|
| 187 |
|
| 188 |
try:
|
| 189 |
+
# Usage Report (Summary Report)
|
| 190 |
usage_report_metadata = {
|
| 191 |
"reportMetadata": {
|
| 192 |
"name": f"SmartLog_Usage_Report_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 193 |
+
"developerName": f"SmartLog_Usage_Report_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 194 |
+
"reportType": "CustomEntity$SmartLog__c",
|
| 195 |
"reportFormat": "SUMMARY",
|
| 196 |
+
"columns": [
|
| 197 |
+
{"field": "SmartLog__c.Device_Id__c"},
|
| 198 |
+
{"field": "SmartLog__c.Usage_Hours__c", "aggregateTypes": ["Sum"]}
|
| 199 |
],
|
| 200 |
"groupingsDown": [
|
| 201 |
+
{"field": "SmartLog__c.Device_Id__c", "sortOrder": "Asc", "dateGranularity": "None"}
|
| 202 |
],
|
| 203 |
"folderId": LABOPS_REPORTS_FOLDER_ID
|
| 204 |
}
|
| 205 |
}
|
| 206 |
+
logging.info(f"Creating Usage Report with metadata: {json.dumps(usage_report_metadata, indent=2)}")
|
| 207 |
usage_result = sf.restful('analytics/reports', method='POST', json=usage_report_metadata)
|
| 208 |
usage_report_id = usage_result['id']
|
| 209 |
logging.info(f"Usage Report created: {usage_report_id}")
|
| 210 |
|
| 211 |
+
# AMC Reminders Report (Tabular Report)
|
| 212 |
amc_report_metadata = {
|
| 213 |
"reportMetadata": {
|
| 214 |
"name": f"SmartLog_AMC_Reminders_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 215 |
+
"developerName": f"SmartLog_AMC_Reminders_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 216 |
+
"reportType": "CustomEntity$SmartLog__c",
|
| 217 |
"reportFormat": "TABULAR",
|
| 218 |
+
"columns": [
|
| 219 |
+
{"field": "SmartLog__c.Device_Id__c"},
|
| 220 |
+
{"field": "SmartLog__c.AMC_Date__c"}
|
| 221 |
],
|
| 222 |
"folderId": LABOPS_REPORTS_FOLDER_ID
|
| 223 |
}
|
| 224 |
}
|
| 225 |
+
logging.info(f"Creating AMC Reminders Report with metadata: {json.dumps(amc_report_metadata, indent=2)}")
|
| 226 |
amc_result = sf.restful('analytics/reports', method='POST', json=amc_report_metadata)
|
| 227 |
amc_report_id = amc_result['id']
|
| 228 |
logging.info(f"AMC Reminders Report created: {amc_report_id}")
|
|
|
|
| 232 |
logging.error(f"Failed to create Salesforce reports: {str(e)}")
|
| 233 |
return f"Failed to create reports: {str(e)}"
|
| 234 |
|
| 235 |
+
# Save results to Salesforce SmartLog__c
|
| 236 |
def save_to_salesforce(df, summary, anomalies, amc_reminders, insights):
|
| 237 |
if sf is None:
|
| 238 |
return "Salesforce connection not available."
|
|
|
|
| 339 |
df["days_to_amc"] = (df["amc_date"] - current_date).dt.days
|
| 340 |
reminders = df[(df["days_to_amc"] >= 0) & (df["days_to_amc"] <= 30)][["device_id", "amc_date"]]
|
| 341 |
if reminders.empty:
|
| 342 |
+
logging.info("No AMC reminders found within the next 30 days.")
|
| 343 |
return "No AMC reminders due within the next 30 days.", reminders
|
| 344 |
reminder_lines = ["Upcoming AMC Reminders:"]
|
| 345 |
for _, row in reminders.head(5).iterrows():
|
| 346 |
reminder_lines.append(f"- Device ID: {row['device_id']}, AMC Date: {row['amc_date']}")
|
| 347 |
+
logging.info(f"Found {len(reminders)} AMC reminders: {reminder_lines}")
|
| 348 |
return "\n".join(reminder_lines), reminders
|
| 349 |
except Exception as e:
|
| 350 |
logging.error(f"AMC reminder generation failed: {str(e)}")
|
|
|
|
| 468 |
if df.empty:
|
| 469 |
return "No data available.", None, None, None, None, None, None, None, None, None
|
| 470 |
|
| 471 |
+
# Run tasks concurrently
|
| 472 |
with ThreadPoolExecutor(max_workers=4) as executor:
|
| 473 |
future_summary = executor.submit(summarize_logs, df)
|
| 474 |
future_anomalies = executor.submit(detect_anomalies, df)
|