RathodHarish commited on
Commit
9f62323
·
verified ·
1 Parent(s): e6a79ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -37
app.py CHANGED
@@ -130,25 +130,46 @@ def create_salesforce_reports(df):
130
  usage_report_metadata = {
131
  "reportMetadata": {
132
  "name": f"SmartLog_Usage_Report_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
133
- "reportType": "SmartLog__c",
 
 
 
 
134
  "reportFormat": "SUMMARY",
 
135
  "reportFilters": [
136
- {"column": "Status__c", "operator": "equals", "value": "Active"},
137
- {"column": "Timestamp__c", "operator": "greaterOrEqual", "value": "THIS_MONTH"}
138
- ],
139
- "reportColumns": [
140
- {"field": "Device_Id__c"},
141
- {"field": "Log_Type__c"},
142
- {"field": "Status__c"},
143
- {"field": "Timestamp__c"},
144
- {"field": "Usage_Hours__c", "aggregateTypes": ["Sum"]},
145
- {"field": "Downtime__c", "aggregateTypes": ["Sum"]},
146
- {"field": "AMC_Date__c"}
147
  ],
 
148
  "groupingsDown": [
149
- {"field": "Device_Id__c", "sortOrder": "Asc", "dateGranularity": "None"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  ],
151
- "folderId": LABOPS_REPORTS_FOLDER_ID
 
152
  }
153
  }
154
  usage_result = sf.restful('analytics/reports', method='POST', json=usage_report_metadata)
@@ -159,19 +180,37 @@ def create_salesforce_reports(df):
159
  amc_report_metadata = {
160
  "reportMetadata": {
161
  "name": f"SmartLog_AMC_Reminders_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
162
- "reportType": "SmartLog__c",
 
 
 
 
163
  "reportFormat": "TABULAR",
 
164
  "reportFilters": [
165
- {"column": "Status__c", "operator": "equals", "value": "Active"},
166
- {"column": "AMC_Date__c", "operator": "greaterOrEqual", "value": "TODAY"},
167
- {"column": "AMC_Date__c", "operator": "lessOrEqual", "value": "NEXT_N_DAYS:30"}
 
 
 
 
 
 
 
 
 
 
 
 
168
  ],
169
- "reportColumns": [
170
- {"field": "Device_Id__c"},
171
- {"field": "AMC_Date__c"},
172
- {"field": "Status__c"}
173
  ],
174
- "folderId": LABOPS_REPORTS_FOLDER_ID
 
175
  }
176
  }
177
  amc_result = sf.restful('analytics/reports', method='POST', json=amc_report_metadata)
@@ -195,14 +234,14 @@ def save_to_salesforce(df, summary, anomalies, amc_reminders, insights):
195
  # Validate and map picklist values
196
  status = str(row['status'])
197
  log_type = str(row['log_type'])
198
-
199
  # Map Status__c
200
  if status not in status_values:
201
  status = picklist_mapping['Status__c'].get(status.lower(), status_values[0] if status_values else None)
202
  if status is None:
203
  logging.warning(f"Skipping record with invalid Status__c: {row['status']}")
204
  continue
205
-
206
  # Map Log_Type__c
207
  if log_type not in log_type_values:
208
  log_type = picklist_mapping['Log_Type__c'].get(log_type.lower(), log_type_values[0] if log_type_values else None)
@@ -228,7 +267,7 @@ def save_to_salesforce(df, summary, anomalies, amc_reminders, insights):
228
  'AMC_Date__c': amc_date_str
229
  }
230
  records.append(record)
231
-
232
  # Bulk insert to reduce API calls
233
  if records:
234
  sf.bulk.SmartLog__c.insert(records)
@@ -391,7 +430,7 @@ async def process_logs(file_obj, progress=gr.Progress()):
391
 
392
  file_name = file_obj.name
393
  logging.info(f"Processing file: {file_name}")
394
-
395
  if not file_name.endswith(".csv"):
396
  return "Please upload a CSV file.", "", None, "", "", "", None, "", ""
397
 
@@ -469,39 +508,39 @@ try:
469
  with gr.Column(scale=2):
470
  with gr.Group(elem_classes="dashboard-container"):
471
  gr.Markdown("<div class='dashboard-title'>Analysis Results</div>")
472
-
473
  with gr.Group(elem_classes="dashboard-section"):
474
  gr.Markdown("### Step 1: Summary Report")
475
  summary_output = gr.Markdown()
476
-
477
  with gr.Group(elem_classes="dashboard-section"):
478
  gr.Markdown("### Step 2: Log Preview")
479
  preview_output = gr.Markdown()
480
-
481
  with gr.Group(elem_classes="dashboard-section"):
482
  gr.Markdown("### Step 3: Usage Chart")
483
  chart_output = gr.Plot()
484
-
485
  with gr.Group(elem_classes="dashboard-section"):
486
  gr.Markdown("### Step 4: Anomaly Detection")
487
  anomaly_output = gr.Markdown()
488
-
489
  with gr.Group(elem_classes="dashboard-section"):
490
  gr.Markdown("### Step 5: AMC Reminders")
491
  amc_output = gr.Markdown()
492
-
493
  with gr.Group(elem_classes="dashboard-section"):
494
  gr.Markdown("### Step 6: Insights (AI)")
495
  insights_output = gr.Markdown()
496
-
497
  with gr.Group(elem_classes="dashboard-section"):
498
  gr.Markdown("### Salesforce Integration")
499
  salesforce_output = gr.Markdown()
500
-
501
  with gr.Group(elem_classes="dashboard-section"):
502
  gr.Markdown("### Salesforce Reports")
503
  report_output = gr.Markdown()
504
-
505
  with gr.Group(elem_classes="dashboard-section"):
506
  gr.Markdown("### Download Report")
507
  pdf_output = gr.File(label="Download Analysis Report as PDF")
@@ -535,4 +574,4 @@ if __name__ == "__main__":
535
  except Exception as e:
536
  logging.error(f"Failed to launch Gradio interface: {str(e)}")
537
  print(f"Error launching app: {str(e)}")
538
- raise e
 
130
  usage_report_metadata = {
131
  "reportMetadata": {
132
  "name": f"SmartLog_Usage_Report_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
133
+ "developerName": f"SmartLog_Usage_Report_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
134
+ "reportType": {
135
+ "type": "CustomObject",
136
+ "value": "SmartLog__c"
137
+ },
138
  "reportFormat": "SUMMARY",
139
+ "reportBooleanFilter": None,
140
  "reportFilters": [
141
+ {
142
+ "column": "SmartLog__c.Status__c",
143
+ "operator": "equals",
144
+ "value": "Active"
145
+ },
146
+ {
147
+ "column": "SmartLog__c.Timestamp__c",
148
+ "operator": "greaterOrEqual",
149
+ "value": "THIS_MONTH"
150
+ }
 
151
  ],
152
+ "aggregates": ["s!SmartLog__c.Usage_Hours__c", "s!SmartLog__c.Downtime__c"],
153
  "groupingsDown": [
154
+ {
155
+ "name": "Device_Id__c",
156
+ "field": "SmartLog__c.Device_Id__c",
157
+ "sortOrder": "Asc",
158
+ "sortAggregate": None,
159
+ "dateGranularity": "None"
160
+ }
161
+ ],
162
+ "detailColumns": [
163
+ "SmartLog__c.Device_Id__c",
164
+ "SmartLog__c.Log_Type__c",
165
+ "SmartLog__c.Status__c",
166
+ "SmartLog__c.Timestamp__c",
167
+ "SmartLog__c.Usage_Hours__c",
168
+ "SmartLog__c.Downtime__c",
169
+ "SmartLog__c.AMC_Date__c"
170
  ],
171
+ "folderId": LABOPS_REPORTS_FOLDER_ID,
172
+ "currency": None
173
  }
174
  }
175
  usage_result = sf.restful('analytics/reports', method='POST', json=usage_report_metadata)
 
180
  amc_report_metadata = {
181
  "reportMetadata": {
182
  "name": f"SmartLog_AMC_Reminders_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
183
+ "developerName": f"SmartLog_AMC_Reminders_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
184
+ "reportType": {
185
+ "type": "CustomObject",
186
+ "value": "SmartLog__c"
187
+ },
188
  "reportFormat": "TABULAR",
189
+ "reportBooleanFilter": None,
190
  "reportFilters": [
191
+ {
192
+ "column": "SmartLog__c.Status__c",
193
+ "operator": "equals",
194
+ "value": "Active"
195
+ },
196
+ {
197
+ "column": "SmartLog__c.AMC_Date__c",
198
+ "operator": "greaterOrEqual",
199
+ "value": "TODAY"
200
+ },
201
+ {
202
+ "column": "SmartLog__c.AMC_Date__c",
203
+ "operator": "lessOrEqual",
204
+ "value": "NEXT_N_DAYS:30"
205
+ }
206
  ],
207
+ "detailColumns": [
208
+ "SmartLog__c.Device_Id__c",
209
+ "SmartLog__c.AMC_Date__c",
210
+ "SmartLog__c.Status__c"
211
  ],
212
+ "folderId": LABOPS_REPORTS_FOLDER_ID,
213
+ "currency": None
214
  }
215
  }
216
  amc_result = sf.restful('analytics/reports', method='POST', json=amc_report_metadata)
 
234
  # Validate and map picklist values
235
  status = str(row['status'])
236
  log_type = str(row['log_type'])
237
+
238
  # Map Status__c
239
  if status not in status_values:
240
  status = picklist_mapping['Status__c'].get(status.lower(), status_values[0] if status_values else None)
241
  if status is None:
242
  logging.warning(f"Skipping record with invalid Status__c: {row['status']}")
243
  continue
244
+
245
  # Map Log_Type__c
246
  if log_type not in log_type_values:
247
  log_type = picklist_mapping['Log_Type__c'].get(log_type.lower(), log_type_values[0] if log_type_values else None)
 
267
  'AMC_Date__c': amc_date_str
268
  }
269
  records.append(record)
270
+
271
  # Bulk insert to reduce API calls
272
  if records:
273
  sf.bulk.SmartLog__c.insert(records)
 
430
 
431
  file_name = file_obj.name
432
  logging.info(f"Processing file: {file_name}")
433
+
434
  if not file_name.endswith(".csv"):
435
  return "Please upload a CSV file.", "", None, "", "", "", None, "", ""
436
 
 
508
  with gr.Column(scale=2):
509
  with gr.Group(elem_classes="dashboard-container"):
510
  gr.Markdown("<div class='dashboard-title'>Analysis Results</div>")
511
+
512
  with gr.Group(elem_classes="dashboard-section"):
513
  gr.Markdown("### Step 1: Summary Report")
514
  summary_output = gr.Markdown()
515
+
516
  with gr.Group(elem_classes="dashboard-section"):
517
  gr.Markdown("### Step 2: Log Preview")
518
  preview_output = gr.Markdown()
519
+
520
  with gr.Group(elem_classes="dashboard-section"):
521
  gr.Markdown("### Step 3: Usage Chart")
522
  chart_output = gr.Plot()
523
+
524
  with gr.Group(elem_classes="dashboard-section"):
525
  gr.Markdown("### Step 4: Anomaly Detection")
526
  anomaly_output = gr.Markdown()
527
+
528
  with gr.Group(elem_classes="dashboard-section"):
529
  gr.Markdown("### Step 5: AMC Reminders")
530
  amc_output = gr.Markdown()
531
+
532
  with gr.Group(elem_classes="dashboard-section"):
533
  gr.Markdown("### Step 6: Insights (AI)")
534
  insights_output = gr.Markdown()
535
+
536
  with gr.Group(elem_classes="dashboard-section"):
537
  gr.Markdown("### Salesforce Integration")
538
  salesforce_output = gr.Markdown()
539
+
540
  with gr.Group(elem_classes="dashboard-section"):
541
  gr.Markdown("### Salesforce Reports")
542
  report_output = gr.Markdown()
543
+
544
  with gr.Group(elem_classes="dashboard-section"):
545
  gr.Markdown("### Download Report")
546
  pdf_output = gr.File(label="Download Analysis Report as PDF")
 
574
  except Exception as e:
575
  logging.error(f"Failed to launch Gradio interface: {str(e)}")
576
  print(f"Error launching app: {str(e)}")
577
+ raise e