RathodHarish commited on
Commit
a814e38
·
verified ·
1 Parent(s): ecc4ac2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -22
app.py CHANGED
@@ -38,7 +38,7 @@ SMTP_SERVER = os.getenv('SMTP_SERVER', 'smtp.gmail.com')
38
  SMTP_PORT = int(os.getenv('SMTP_PORT', 587))
39
  SMTP_USERNAME = os.getenv('harishkumarr@sathkrutha.com') # e.g., your-email@gmail.com
40
  SMTP_PASSWORD = os.getenv('Harish@048') # App-specific password if using Gmail
41
- EMAIL_FROM = os.getenv('EMAIL_FROM', SMTP_USERNAME)
42
  EMAIL_TO = "harishkumarr@sathkrutha.com"
43
 
44
  # Try to import reportlab
@@ -108,6 +108,27 @@ picklist_mapping = {
108
  }
109
  }
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  # Send AMC reminder emails
112
  def send_amc_reminder_emails(reminders_df):
113
  if reminders_df.empty:
@@ -165,29 +186,35 @@ LabOps Team
165
  def create_salesforce_reports(df):
166
  if sf is None:
167
  return "Salesforce connection not available."
 
 
 
168
  try:
169
  # Usage Report
170
  usage_report_metadata = {
171
  "reportMetadata": {
172
  "name": f"SmartLog_Usage_Report_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
173
- "reportType": {"type": "SmartLog__c"},
174
  "reportFormat": "SUMMARY",
175
- "reportBooleanFilter": "",
176
  "reportFilters": [
177
  {"column": "Status__c", "operator": "equals", "value": "Active"},
178
  {"column": "Timestamp__c", "operator": "greaterOrEqual", "value": "THIS_MONTH"}
179
  ],
180
- "reportColumns": [
181
- {"column": "Device_Id__c"},
182
- {"column": "Log_Type__c"},
183
- {"column": "Status__c"},
184
- {"column": "Timestamp__c"},
185
- {"column": "Usage_Hours__c", "aggregateTypes": ["Sum"]},
186
- {"column": "Downtime__c", "aggregateTypes": ["Sum"]},
187
- {"column": "AMC_Date__c"}
 
 
 
 
 
188
  ],
189
- "groupingsDown": [{"name": "Device_Id__c", "sortOrder": "Asc"}],
190
- "folderName": "LabOps Reports"
191
  }
192
  }
193
  usage_result = sf.restful('analytics/reports', method='POST', json=usage_report_metadata)
@@ -198,20 +225,19 @@ def create_salesforce_reports(df):
198
  amc_report_metadata = {
199
  "reportMetadata": {
200
  "name": f"SmartLog_AMC_Reminders_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
201
- "reportType": {"type": "SmartLog__c"},
202
  "reportFormat": "TABULAR",
203
- "reportBooleanFilter": "",
204
  "reportFilters": [
205
  {"column": "Status__c", "operator": "equals", "value": "Active"},
206
  {"column": "AMC_Date__c", "operator": "greaterOrEqual", "value": "TODAY"},
207
  {"column": "AMC_Date__c", "operator": "lessOrEqual", "value": "NEXT_N_DAYS:30"}
208
  ],
209
- "reportColumns": [
210
- {"column": "Device_Id__c"},
211
- {"column": "AMC_Date__c"},
212
- {"column": "Status__c"}
213
  ],
214
- "folderName": "LabOps Reports"
215
  }
216
  }
217
  amc_result = sf.restful('analytics/reports', method='POST', json=amc_report_metadata)
@@ -325,8 +351,8 @@ def check_amc_reminders(df, current_date, progress=gr.Progress()):
325
  return "AMC reminders require 'device_id' and 'amc_date' columns.", pd.DataFrame()
326
  df["amc_date"] = pd.to_datetime(df["amc_date"], errors='coerce')
327
  current_date = pd.to_datetime(current_date)
328
- df["days_to_amc"] = (df["days_to_amc"] >= 0) & (df["days_to_amc"] <= 30)
329
- reminders = df[df["days_to_amc"]][["device_id", "amc_date"]]
330
  if reminders.empty:
331
  return "No AMC reminders due within the next 30 days.", reminders
332
  reminder_lines = ["Upcoming AMC Reminders:"]
 
38
  SMTP_PORT = int(os.getenv('SMTP_PORT', 587))
39
  SMTP_USERNAME = os.getenv('harishkumarr@sathkrutha.com') # e.g., your-email@gmail.com
40
  SMTP_PASSWORD = os.getenv('Harish@048') # App-specific password if using Gmail
41
+ EMAIL_FROM = os.getenv('LabOpsDashboard', SMTP_USERNAME)
42
  EMAIL_TO = "harishkumarr@sathkrutha.com"
43
 
44
  # Try to import reportlab
 
108
  }
109
  }
110
 
111
+ # Fetch folder ID for "LabOps Reports"
112
+ def get_folder_id(folder_name):
113
+ if sf is None:
114
+ return None
115
+ try:
116
+ query = f"SELECT Id FROM Folder WHERE Name = '{folder_name}' AND Type = 'Report'"
117
+ result = sf.query(query)
118
+ if result['totalSize'] > 0:
119
+ folder_id = result['records'][0]['Id']
120
+ logging.info(f"Found folder ID for '{folder_name}': {folder_id}")
121
+ return folder_id
122
+ else:
123
+ logging.error(f"Folder '{folder_name}' not found in Salesforce.")
124
+ return None
125
+ except Exception as e:
126
+ logging.error(f"Failed to fetch folder ID for '{folder_name}': {str(e)}")
127
+ return None
128
+
129
+ # Cache the folder ID at startup
130
+ LABOPS_REPORTS_FOLDER_ID = get_folder_id('LabOps Reports')
131
+
132
  # Send AMC reminder emails
133
  def send_amc_reminder_emails(reminders_df):
134
  if reminders_df.empty:
 
186
  def create_salesforce_reports(df):
187
  if sf is None:
188
  return "Salesforce connection not available."
189
+ if not LABOPS_REPORTS_FOLDER_ID:
190
+ return "Cannot create reports: 'LabOps Reports' folder not found in Salesforce."
191
+
192
  try:
193
  # Usage Report
194
  usage_report_metadata = {
195
  "reportMetadata": {
196
  "name": f"SmartLog_Usage_Report_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
197
+ "reportType": "SmartLog__c",
198
  "reportFormat": "SUMMARY",
 
199
  "reportFilters": [
200
  {"column": "Status__c", "operator": "equals", "value": "Active"},
201
  {"column": "Timestamp__c", "operator": "greaterOrEqual", "value": "THIS_MONTH"}
202
  ],
203
+ "detailColumns": [
204
+ "Device_Id__c",
205
+ "Log_Type__c",
206
+ "Status__c",
207
+ "Timestamp__c",
208
+ "AMC_Date__c"
209
+ ],
210
+ "aggregates": [
211
+ {"column": "Usage_Hours__c", "aggregate": "Sum"},
212
+ {"column": "Downtime__c", "aggregate": "Sum"}
213
+ ],
214
+ "groupingsDown": [
215
+ {"name": "Device_Id__c", "sortOrder": "Asc", "dateGranularity": "None"}
216
  ],
217
+ "folderId": LABOPS_REPORTS_FOLDER_ID
 
218
  }
219
  }
220
  usage_result = sf.restful('analytics/reports', method='POST', json=usage_report_metadata)
 
225
  amc_report_metadata = {
226
  "reportMetadata": {
227
  "name": f"SmartLog_AMC_Reminders_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
228
+ "reportType": "SmartLog__c",
229
  "reportFormat": "TABULAR",
 
230
  "reportFilters": [
231
  {"column": "Status__c", "operator": "equals", "value": "Active"},
232
  {"column": "AMC_Date__c", "operator": "greaterOrEqual", "value": "TODAY"},
233
  {"column": "AMC_Date__c", "operator": "lessOrEqual", "value": "NEXT_N_DAYS:30"}
234
  ],
235
+ "detailColumns": [
236
+ "Device_Id__c",
237
+ "AMC_Date__c",
238
+ "Status__c"
239
  ],
240
+ "folderId": LABOPS_REPORTS_FOLDER_ID
241
  }
242
  }
243
  amc_result = sf.restful('analytics/reports', method='POST', json=amc_report_metadata)
 
351
  return "AMC reminders require 'device_id' and 'amc_date' columns.", pd.DataFrame()
352
  df["amc_date"] = pd.to_datetime(df["amc_date"], errors='coerce')
353
  current_date = pd.to_datetime(current_date)
354
+ df["days_to_amc"] = (df["amc_date"] - current_date).dt.days
355
+ reminders = df[(df["days_to_amc"] >= 0) & (df["days_to_amc"] <= 30)][["device_id", "amc_date"]]
356
  if reminders.empty:
357
  return "No AMC reminders due within the next 30 days.", reminders
358
  reminder_lines = ["Upcoming AMC Reminders:"]