-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.py
More file actions
38 lines (35 loc) · 1.42 KB
/
dashboard.py
File metadata and controls
38 lines (35 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import logging
from pyspark.sql import DataFrame
from config import Config
logger = logging.getLogger(__name__)
# Generate a summary report as a CSV file for visualization in BI tools
def generate_summary_report(df: DataFrame, report_filename="summary_report.csv"):
report_path = os.path.join(Config.REPORT_OUTPUT_PATH, report_filename)
try:
df.coalesce(1).write.mode("overwrite").csv(report_path, header=True)
logger.info("Summary report generated at: %s", report_path)
except Exception as e:
logger.error("Failed to generate summary report: %s", str(e))
# Function to update a rudimentary HTML dashboard based on streaming data
def update_dashboard(df: DataFrame, dashboard_file="dashboard.html"):
report_path = os.path.join(Config.REPORT_OUTPUT_PATH, dashboard_file)
try:
# Convert a sample of data to HTML for display
summary_html = df.limit(10).toPandas().to_html(index=False)
html_content = f"""
<html>
<head>
<title>User Activity Dashboard</title>
</head>
<body>
<h1>Real-Time User Activity Dashboard</h1>
{summary_html}
</body>
</html>
"""
with open(report_path, "w") as f:
f.write(html_content)
logger.info("Dashboard updated at: %s", report_path)
except Exception as e:
logger.error("Failed to update dashboard: %s", str(e))