Create utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from simple_salesforce import Salesforce
|
2 |
+
from reportlab.lib.pagesizes import letter
|
3 |
+
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
|
4 |
+
from reportlab.lib import colors
|
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}")
|
30 |
+
return "NEGATIVE"
|
31 |
+
|
32 |
+
def generate_pdf_report(df: pd.DataFrame, lab_site: str, equipment_type: str, date_range: list) -> str:
|
33 |
+
"""Generate a PDF report from dashboard data."""
|
34 |
+
pdf_file = "LabOps_Report.pdf"
|
35 |
+
doc = SimpleDocTemplate(pdf_file, pagesize=letter)
|
36 |
+
elements = []
|
37 |
+
styles = getSampleStyleSheet()
|
38 |
+
|
39 |
+
# Title
|
40 |
+
elements.append(Paragraph(f"LabOps Dashboard Report - {datetime.now().strftime('%Y-%m-%d')}", styles["Title"]))
|
41 |
+
|
42 |
+
# Filters
|
43 |
+
elements.append(Paragraph(f"Lab: {lab_site} | Equipment: {equipment_type} | Date Range: {date_range[0]} to {date_range[1]}", styles["Normal"]))
|
44 |
+
|
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 |
+
|
57 |
+
table = Table(data)
|
58 |
+
table.setStyle(TableStyle([
|
59 |
+
("BACKGROUND", (0, 0), (-1, 0), colors.grey),
|
60 |
+
("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),
|
61 |
+
("ALIGN", (0, 0), (-1, -1), "CENTER"),
|
62 |
+
("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
|
63 |
+
("FONTSIZE", (0, 0), (-1, 0), 14),
|
64 |
+
("BOTTOMPADDING", (0, 0), (-1, 0), 12),
|
65 |
+
("BACKGROUND", (0, 1), (-1, -1), colors.beige),
|
66 |
+
("GRID", (0, 0), (-1, -1), 1, colors.black)
|
67 |
+
]))
|
68 |
+
elements.append(table)
|
69 |
+
|
70 |
+
doc.build(elements)
|
71 |
+
return pdf_file
|