Update utils.py
Browse files
utils.py
CHANGED
|
@@ -5,20 +5,25 @@ from reportlab.lib import colors
|
|
| 5 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 6 |
import pandas as pd
|
| 7 |
from datetime import datetime
|
|
|
|
| 8 |
|
| 9 |
-
def fetch_salesforce_data(sf: Salesforce, query: str) -> list:
|
| 10 |
-
"""Fetch data from Salesforce using SOQL query."""
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def detect_anomalies(log_text: str, anomaly_detector) -> str:
|
| 19 |
"""Detect anomalies in log text using Hugging Face model."""
|
| 20 |
try:
|
| 21 |
-
result = anomaly_detector(log_text)
|
| 22 |
return result[0]["label"] # Returns 'POSITIVE' for anomaly, 'NEGATIVE' for normal
|
| 23 |
except Exception as e:
|
| 24 |
print(f"Error detecting anomaly: {e}")
|
|
@@ -40,11 +45,12 @@ def generate_pdf_report(df: pd.DataFrame, lab_site: str, equipment_type: str, da
|
|
| 40 |
# Data Table
|
| 41 |
data = [["Equipment", "Timestamp", "Status", "Usage Count", "Anomaly"]]
|
| 42 |
for _, row in df.iterrows():
|
|
|
|
| 43 |
data.append([
|
| 44 |
row["Equipment__c"],
|
| 45 |
-
|
| 46 |
row["Status__c"],
|
| 47 |
-
row["Usage_Count__c"],
|
| 48 |
row["Anomaly"]
|
| 49 |
])
|
| 50 |
|
|
|
|
| 5 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 6 |
import pandas as pd
|
| 7 |
from datetime import datetime
|
| 8 |
+
import time
|
| 9 |
|
| 10 |
+
def fetch_salesforce_data(sf: Salesforce, query: str, retries=3) -> list:
|
| 11 |
+
"""Fetch data from Salesforce using SOQL query with retry logic."""
|
| 12 |
+
for attempt in range(retries):
|
| 13 |
+
try:
|
| 14 |
+
result = sf.query_all(query)
|
| 15 |
+
return result["records"]
|
| 16 |
+
except Exception as e:
|
| 17 |
+
if attempt == retries - 1:
|
| 18 |
+
print(f"Error fetching Salesforce data after {retries} attempts: {e}")
|
| 19 |
+
return []
|
| 20 |
+
time.sleep(2) # Wait before retrying
|
| 21 |
+
return []
|
| 22 |
|
| 23 |
def detect_anomalies(log_text: str, anomaly_detector) -> str:
|
| 24 |
"""Detect anomalies in log text using Hugging Face model."""
|
| 25 |
try:
|
| 26 |
+
result = anomaly_detector(log_text, clean_up_tokenization_spaces=True)
|
| 27 |
return result[0]["label"] # Returns 'POSITIVE' for anomaly, 'NEGATIVE' for normal
|
| 28 |
except Exception as e:
|
| 29 |
print(f"Error detecting anomaly: {e}")
|
|
|
|
| 45 |
# Data Table
|
| 46 |
data = [["Equipment", "Timestamp", "Status", "Usage Count", "Anomaly"]]
|
| 47 |
for _, row in df.iterrows():
|
| 48 |
+
timestamp = row["Log_Timestamp__c"].strftime('%Y-%m-%d %H:%M:%S') if pd.notnull(row["Log_Timestamp__c"]) else "N/A"
|
| 49 |
data.append([
|
| 50 |
row["Equipment__c"],
|
| 51 |
+
timestamp,
|
| 52 |
row["Status__c"],
|
| 53 |
+
str(row["Usage_Count__c"]),
|
| 54 |
row["Anomaly"]
|
| 55 |
])
|
| 56 |
|