RathodHarish commited on
Commit
db570c6
·
verified ·
1 Parent(s): f0a177c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -33
app.py CHANGED
@@ -11,21 +11,10 @@ import io
11
  import time
12
  import asyncio
13
  from simple_salesforce import Salesforce
14
- import smtplib
15
- from email.mime.text import MIMEText
16
 
17
  # Configure logging
18
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
19
 
20
- # Email configuration
21
- EMAIL_CONFIG = {
22
- 'sender_email': 'your-email@gmail.com', # Replace with your Gmail address
23
- 'app_password': 'your-app-password', # Replace with your Gmail App Password
24
- 'recipient_email': 'recipient@example.com', # Replace with recipient email
25
- 'smtp_server': 'smtp.gmail.com',
26
- 'smtp_port': 587
27
- }
28
-
29
  # Salesforce configuration
30
  try:
31
  sf = Salesforce(
@@ -211,22 +200,6 @@ def save_to_salesforce(df, reminders_df):
211
  except Exception as e:
212
  logging.error(f"Failed to save to Salesforce: {str(e)}")
213
 
214
- # Send email alert
215
- def send_email_alert(subject, body):
216
- try:
217
- msg = MIMEText(body)
218
- msg['Subject'] = subject
219
- msg['From'] = EMAIL_CONFIG['sender_email']
220
- msg['To'] = EMAIL_CONFIG['recipient_email']
221
-
222
- with smtplib.SMTP(EMAIL_CONFIG['smtp_server'], EMAIL_CONFIG['smtp_port']) as server:
223
- server.starttls()
224
- server.login(EMAIL_CONFIG['sender_email'], EMAIL_CONFIG['app_password'])
225
- server.send_message(msg)
226
- logging.info(f"Email alert sent successfully to {EMAIL_CONFIG['recipient_email']}")
227
- except Exception as e:
228
- logging.error(f"Failed to send email alert: {str(e)}")
229
-
230
  # Summarize logs
231
  def summarize_logs(df):
232
  try:
@@ -250,9 +223,7 @@ def detect_anomalies(df):
250
  anomalies = df[df["anomaly"] == -1][["device_id", "usage_hours", "downtime", "timestamp"]]
251
  if anomalies.empty:
252
  return "No anomalies detected.", anomalies
253
- anomaly_summary = "\n".join([f"- Device ID: {row['device_id']}, Usage: {row['usage_hours']}, Downtime: {row['downtime']}, Timestamp: {row['timestamp']}" for _, row in anomalies.head(5).iterrows()])
254
- send_email_alert("Anomaly Detection Alert", f"Anomalies detected:\n{anomaly_summary}")
255
- return anomaly_summary, anomalies
256
  except Exception as e:
257
  logging.error(f"Anomaly detection failed: {str(e)}")
258
  return f"Anomaly detection failed: {str(e)}", pd.DataFrame()
@@ -268,9 +239,7 @@ def check_amc_reminders(df, current_date):
268
  reminders = df[(df["days_to_amc"] >= 0) & (df["days_to_amc"] <= 30)][["device_id", "log_type", "status", "timestamp", "usage_hours", "downtime", "amc_date"]]
269
  if reminders.empty:
270
  return "No AMC reminders due within the next 30 days.", reminders
271
- reminder_summary = "\n".join([f"- Device ID: {row['device_id']}, AMC Date: {row['amc_date']}" for _, row in reminders.head(5).iterrows()])
272
- send_email_alert("AMC Reminder Alert", f"AMC reminders due within 30 days:\n{reminder_summary}")
273
- return reminder_summary, reminders
274
  except Exception as e:
275
  logging.error(f"AMC reminder generation failed: {str(e)}")
276
  return f"AMC reminder generation failed: {str(e)}", pd.DataFrame()
 
11
  import time
12
  import asyncio
13
  from simple_salesforce import Salesforce
 
 
14
 
15
  # Configure logging
16
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
17
 
 
 
 
 
 
 
 
 
 
18
  # Salesforce configuration
19
  try:
20
  sf = Salesforce(
 
200
  except Exception as e:
201
  logging.error(f"Failed to save to Salesforce: {str(e)}")
202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  # Summarize logs
204
  def summarize_logs(df):
205
  try:
 
223
  anomalies = df[df["anomaly"] == -1][["device_id", "usage_hours", "downtime", "timestamp"]]
224
  if anomalies.empty:
225
  return "No anomalies detected.", anomalies
226
+ return "\n".join([f"- Device ID: {row['device_id']}, Usage: {row['usage_hours']}, Downtime: {row['downtime']}, Timestamp: {row['timestamp']}" for _, row in anomalies.head(5).iterrows()]), anomalies
 
 
227
  except Exception as e:
228
  logging.error(f"Anomaly detection failed: {str(e)}")
229
  return f"Anomaly detection failed: {str(e)}", pd.DataFrame()
 
239
  reminders = df[(df["days_to_amc"] >= 0) & (df["days_to_amc"] <= 30)][["device_id", "log_type", "status", "timestamp", "usage_hours", "downtime", "amc_date"]]
240
  if reminders.empty:
241
  return "No AMC reminders due within the next 30 days.", reminders
242
+ return "\n".join([f"- Device ID: {row['device_id']}, AMC Date: {row['amc_date']}" for _, row in reminders.head(5).iterrows()]), reminders
 
 
243
  except Exception as e:
244
  logging.error(f"AMC reminder generation failed: {str(e)}")
245
  return f"AMC reminder generation failed: {str(e)}", pd.DataFrame()