Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def process_equipment_utilization(equipment_id, project_id, usage_hours, idle_hours,
|
| 2 |
movement_frequency, cost_per_hour, report_link, last_maintenance):
|
| 3 |
last_maintenance_str = last_maintenance.strftime('%Y-%m-%d') if last_maintenance else None
|
| 4 |
|
| 5 |
-
# Lookup Equipment ID by Name
|
| 6 |
equipment_sf_id = get_equipment_id_by_name(equipment_id)
|
| 7 |
if not equipment_sf_id:
|
| 8 |
return {"error": f"Equipment name '{equipment_id}' not found in Salesforce."}
|
| 9 |
|
| 10 |
-
# Lookup Project ID by Name
|
| 11 |
project_sf_id = get_project_id_by_name(project_id)
|
| 12 |
if not project_sf_id:
|
| 13 |
return {"error": f"Project name '{project_id}' not found in Salesforce."}
|
| 14 |
|
| 15 |
-
#
|
| 16 |
ai_suggestion = "Move"
|
| 17 |
-
suggestion_confidence =
|
| 18 |
-
utilization_score =
|
| 19 |
|
| 20 |
-
|
| 21 |
-
record = {
|
| 22 |
"Equipment_ID__c": equipment_sf_id,
|
| 23 |
"Project_ID__c": project_sf_id,
|
| 24 |
"Usage_Hours__c": usage_hours,
|
|
@@ -31,16 +94,15 @@ def process_equipment_utilization(equipment_id, project_id, usage_hours, idle_ho
|
|
| 31 |
"Report_Link__c": report_link
|
| 32 |
}
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
return {"error": str(e)}
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
+
import io
|
| 3 |
+
import logging
|
| 4 |
+
import requests
|
| 5 |
+
import traceback
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from simple_salesforce import Salesforce
|
| 9 |
+
|
| 10 |
+
# Configure logging
|
| 11 |
+
logging.basicConfig(
|
| 12 |
+
level=logging.INFO,
|
| 13 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 14 |
+
handlers=[logging.FileHandler('app_log.txt'), logging.StreamHandler()]
|
| 15 |
+
)
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
+
|
| 18 |
+
# Salesforce credentials
|
| 19 |
+
SALESFORCE_USERNAME = "vaneshdevarapalli866@agentforce.com"
|
| 20 |
+
SALESFORCE_PASSWORD = "vanesh@331"
|
| 21 |
+
SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
|
| 22 |
+
|
| 23 |
+
# Connect to Salesforce
|
| 24 |
+
sf = Salesforce(
|
| 25 |
+
username=SALESFORCE_USERNAME,
|
| 26 |
+
password=SALESFORCE_PASSWORD,
|
| 27 |
+
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 28 |
+
domain="login"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Lookup helper functions
|
| 32 |
+
def get_equipment_id_by_name(equipment_name):
|
| 33 |
+
try:
|
| 34 |
+
query = f"SELECT Id FROM Equipment__c WHERE Name = '{equipment_name}' LIMIT 1"
|
| 35 |
+
result = sf.query(query)
|
| 36 |
+
if result["totalSize"] > 0:
|
| 37 |
+
return result["records"][0]["Id"]
|
| 38 |
+
else:
|
| 39 |
+
logger.error(f"No Equipment found with name: {equipment_name}")
|
| 40 |
+
return None
|
| 41 |
+
except Exception as e:
|
| 42 |
+
logger.error(f"Error fetching Equipment ID: {e}")
|
| 43 |
+
return None
|
| 44 |
+
|
| 45 |
+
def get_project_id_by_name(project_name):
|
| 46 |
+
try:
|
| 47 |
+
query = f"SELECT Id FROM Project__c WHERE Name = '{project_name}' LIMIT 1"
|
| 48 |
+
result = sf.query(query)
|
| 49 |
+
if result["totalSize"] > 0:
|
| 50 |
+
return result["records"][0]["Id"]
|
| 51 |
+
else:
|
| 52 |
+
logger.error(f"No Project found with name: {project_name}")
|
| 53 |
+
return None
|
| 54 |
+
except Exception as e:
|
| 55 |
+
logger.error(f"Error fetching Project ID: {e}")
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
+
# CSV generation
|
| 59 |
+
def generate_csv_report(data_dict):
|
| 60 |
+
df = pd.DataFrame([data_dict])
|
| 61 |
+
buffer = io.StringIO()
|
| 62 |
+
df.to_csv(buffer, index=False)
|
| 63 |
+
buffer.seek(0)
|
| 64 |
+
return buffer.getvalue()
|
| 65 |
+
|
| 66 |
+
# Record creation
|
| 67 |
def process_equipment_utilization(equipment_id, project_id, usage_hours, idle_hours,
|
| 68 |
movement_frequency, cost_per_hour, report_link, last_maintenance):
|
| 69 |
last_maintenance_str = last_maintenance.strftime('%Y-%m-%d') if last_maintenance else None
|
| 70 |
|
|
|
|
| 71 |
equipment_sf_id = get_equipment_id_by_name(equipment_id)
|
| 72 |
if not equipment_sf_id:
|
| 73 |
return {"error": f"Equipment name '{equipment_id}' not found in Salesforce."}
|
| 74 |
|
|
|
|
| 75 |
project_sf_id = get_project_id_by_name(project_id)
|
| 76 |
if not project_sf_id:
|
| 77 |
return {"error": f"Project name '{project_id}' not found in Salesforce."}
|
| 78 |
|
| 79 |
+
# Placeholder AI values (since endpoint removed)
|
| 80 |
ai_suggestion = "Move"
|
| 81 |
+
suggestion_confidence = 90
|
| 82 |
+
utilization_score = 88
|
| 83 |
|
| 84 |
+
salesforce_record = {
|
|
|
|
| 85 |
"Equipment_ID__c": equipment_sf_id,
|
| 86 |
"Project_ID__c": project_sf_id,
|
| 87 |
"Usage_Hours__c": usage_hours,
|
|
|
|
| 94 |
"Report_Link__c": report_link
|
| 95 |
}
|
| 96 |
|
| 97 |
+
response = sf.Equipment_Utilization_Record__c.create(salesforce_record)
|
| 98 |
+
logger.info(f"Salesforce Equipment Utilization Record created: {response}")
|
| 99 |
+
csv_content = generate_csv_report(salesforce_record)
|
| 100 |
+
return {
|
| 101 |
+
"Salesforce_Record_Id": response["id"],
|
| 102 |
+
"status": "Success",
|
| 103 |
+
"AI_Suggestion": ai_suggestion,
|
| 104 |
+
"Suggestion_Confidence": suggestion_confidence,
|
| 105 |
+
"Utilization_Score": utilization_score,
|
| 106 |
+
"Report_Link": report_link,
|
| 107 |
+
"CSV_Report": csv_content
|
| 108 |
+
}
|
|
|