-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.py
More file actions
executable file
·560 lines (484 loc) · 18 KB
/
dashboard.py
File metadata and controls
executable file
·560 lines (484 loc) · 18 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/usr/bin/env python3
"""
Simple Dashboard for Mini Data Warehouse
Creates HTML dashboard with key metrics and visualizations
"""
import psycopg2
import json
from datetime import datetime, timedelta
import logging
import base64
import io
import matplotlib
matplotlib.use('Agg') # Use non-interactive backend
import matplotlib.pyplot as plt
import pandas as pd
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class DashboardGenerator:
def __init__(self, connection_params=None):
if connection_params is None:
self.connection_params = {
'host': 'localhost',
'port': '5432',
'database': 'warehouse',
'user': 'admin',
'password': 'secret'
}
else:
self.connection_params = connection_params
self.metrics = {}
def connect_db(self):
"""Connect to PostgreSQL database"""
try:
conn = psycopg2.connect(**self.connection_params)
return conn
except psycopg2.Error as e:
logger.error(f"Database connection failed: {e}")
return None
def get_key_metrics(self):
"""Get key business metrics"""
conn = self.connect_db()
if not conn:
return {}
try:
with conn.cursor() as cur:
# Total customers
cur.execute("SELECT COUNT(*) FROM customers")
total_customers = cur.fetchone()[0]
# Total products
cur.execute("SELECT COUNT(*) FROM products")
total_products = cur.fetchone()[0]
# Total orders
cur.execute("SELECT COUNT(*) FROM orders")
total_orders = cur.fetchone()[0]
# Total revenue
cur.execute("SELECT SUM(total_amount) FROM orders")
total_revenue = cur.fetchone()[0] or 0
# Average order value
cur.execute("SELECT AVG(total_amount) FROM orders")
avg_order_value = cur.fetchone()[0] or 0
# Top category by revenue
cur.execute("""
SELECT p.category, SUM(oi.quantity * oi.unit_price) as revenue
FROM products p
JOIN order_items oi ON p.product_id = oi.product_id
GROUP BY p.category
ORDER BY revenue DESC
LIMIT 1
""")
top_category_result = cur.fetchone()
top_category = top_category_result[0] if top_category_result else 'N/A'
top_category_revenue = top_category_result[1] if top_category_result else 0
# Recent orders (last 7 days)
cur.execute("""
SELECT COUNT(*) FROM orders
WHERE order_date >= CURRENT_DATE - INTERVAL '7 days'
""")
recent_orders = cur.fetchone()[0]
return {
'total_customers': total_customers,
'total_products': total_products,
'total_orders': total_orders,
'total_revenue': float(total_revenue),
'avg_order_value': float(avg_order_value),
'top_category': top_category,
'top_category_revenue': float(top_category_revenue),
'recent_orders': recent_orders
}
except psycopg2.Error as e:
logger.error(f"Error getting key metrics: {e}")
return {}
finally:
conn.close()
def get_sales_trends(self):
"""Get sales trends by month"""
conn = self.connect_db()
if not conn:
return [], []
try:
with conn.cursor() as cur:
cur.execute("""
SELECT
DATE_TRUNC('month', order_date) as month,
COUNT(*) as order_count,
SUM(total_amount) as revenue
FROM orders
GROUP BY DATE_TRUNC('month', order_date)
ORDER BY month
""")
results = cur.fetchall()
months = [row[0].strftime('%Y-%m') for row in results]
revenues = [float(row[2]) for row in results]
return months, revenues
except psycopg2.Error as e:
logger.error(f"Error getting sales trends: {e}")
return [], []
finally:
conn.close()
def get_category_performance(self):
"""Get performance by product category"""
conn = self.connect_db()
if not conn:
return [], []
try:
with conn.cursor() as cur:
cur.execute("""
SELECT
p.category,
COUNT(oi.order_item_id) as items_sold,
SUM(oi.quantity * oi.unit_price) as revenue
FROM products p
JOIN order_items oi ON p.product_id = oi.product_id
GROUP BY p.category
ORDER BY revenue DESC
""")
results = cur.fetchall()
categories = [row[0] for row in results]
revenues = [float(row[2]) for row in results]
return categories, revenues
except psycopg2.Error as e:
logger.error(f"Error getting category performance: {e}")
return [], []
finally:
conn.close()
def get_top_customers(self, limit=10):
"""Get top customers by total spending"""
conn = self.connect_db()
if not conn:
return []
try:
with conn.cursor() as cur:
cur.execute("""
SELECT
c.first_name || ' ' || c.last_name as customer_name,
c.country,
COUNT(o.order_id) as order_count,
SUM(o.total_amount) as total_spent
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name, c.country
ORDER BY total_spent DESC
LIMIT %s
""", (limit,))
return cur.fetchall()
except psycopg2.Error as e:
logger.error(f"Error getting top customers: {e}")
return []
finally:
conn.close()
def create_chart(self, chart_type, data, title, xlabel='', ylabel=''):
"""Create a chart and return as base64 encoded string"""
plt.figure(figsize=(10, 6))
plt.style.use('default')
if chart_type == 'line':
labels, values = data
plt.plot(labels, values, marker='o', linewidth=2, markersize=6)
plt.xticks(rotation=45)
elif chart_type == 'bar':
labels, values = data
bars = plt.bar(labels, values, color=['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FECA57'])
# Add value labels on bars
for bar, value in zip(bars, values):
height = bar.get_height()
plt.text(bar.get_x() + bar.get_width()/2., height,
f'${value:,.0f}', ha='center', va='bottom')
plt.xticks(rotation=45)
elif chart_type == 'pie':
labels, values = data
colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FECA57']
plt.pie(values, labels=labels, autopct='%1.1f%%', colors=colors)
plt.title(title, fontsize=14, fontweight='bold')
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.tight_layout()
# Convert to base64
buffer = io.BytesIO()
plt.savefig(buffer, format='png', dpi=100, bbox_inches='tight')
buffer.seek(0)
image_base64 = base64.b64encode(buffer.getvalue()).decode()
plt.close()
return image_base64
def generate_html_dashboard(self):
"""Generate complete HTML dashboard"""
logger.info("Generating dashboard...")
# Get data
metrics = self.get_key_metrics()
months, revenues = self.get_sales_trends()
categories, cat_revenues = self.get_category_performance()
top_customers = self.get_top_customers()
# Create charts
revenue_chart = self.create_chart(
'line',
(months, revenues),
'Monthly Revenue Trends',
'Month',
'Revenue ($)'
)
category_chart = self.create_chart(
'bar',
(categories, cat_revenues),
'Revenue by Category',
'Category',
'Revenue ($)'
)
category_pie_chart = self.create_chart(
'pie',
(categories, cat_revenues),
'Revenue Distribution by Category'
)
# Generate HTML
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mini Data Warehouse Dashboard</title>
<style>
body {{
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}}
.container {{
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}}
.header {{
background: rgba(255, 255, 255, 0.95);
border-radius: 15px;
padding: 20px;
text-align: center;
margin-bottom: 30px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}}
.header h1 {{
color: #333;
margin: 0;
font-size: 2.5em;
font-weight: 300;
}}
.header p {{
color: #666;
margin: 10px 0 0 0;
font-size: 1.1em;
}}
.metrics-grid {{
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
margin-bottom: 30px;
}}
.metric-card {{
background: rgba(255, 255, 255, 0.95);
border-radius: 15px;
padding: 25px;
text-align: center;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease;
}}
.metric-card:hover {{
transform: translateY(-5px);
}}
.metric-value {{
font-size: 2.5em;
font-weight: bold;
color: #4ECDC4;
margin: 0;
}}
.metric-label {{
font-size: 1em;
color: #666;
margin: 5px 0 0 0;
text-transform: uppercase;
letter-spacing: 1px;
}}
.charts-section {{
display: grid;
grid-template-columns: 1fr 1fr;
gap: 30px;
margin-bottom: 30px;
}}
.chart-card {{
background: rgba(255, 255, 255, 0.95);
border-radius: 15px;
padding: 25px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}}
.chart-card h3 {{
margin: 0 0 20px 0;
color: #333;
text-align: center;
}}
.chart-img {{
width: 100%;
border-radius: 10px;
}}
.full-width {{
grid-column: 1 / -1;
}}
.customers-table {{
background: rgba(255, 255, 255, 0.95);
border-radius: 15px;
padding: 25px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
margin-top: 30px;
}}
.customers-table h3 {{
margin: 0 0 20px 0;
color: #333;
text-align: center;
}}
table {{
width: 100%;
border-collapse: collapse;
}}
th, td {{
padding: 12px;
text-align: left;
border-bottom: 1px solid #ddd;
}}
th {{
background-color: #f8f9fa;
font-weight: 600;
color: #333;
}}
.footer {{
text-align: center;
color: rgba(255, 255, 255, 0.8);
margin-top: 40px;
padding: 20px;
}}
@media (max-width: 768px) {{
.charts-section {{
grid-template-columns: 1fr;
}}
.metric-value {{
font-size: 2em;
}}
.header h1 {{
font-size: 2em;
}}
}}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>📊 Mini Data Warehouse</h1>
<p>Business Intelligence Dashboard • Generated on {timestamp}</p>
</div>
<div class="metrics-grid">
<div class="metric-card">
<div class="metric-value">{total_customers:,}</div>
<div class="metric-label">Total Customers</div>
</div>
<div class="metric-card">
<div class="metric-value">{total_products:,}</div>
<div class="metric-label">Products</div>
</div>
<div class="metric-card">
<div class="metric-value">{total_orders:,}</div>
<div class="metric-label">Total Orders</div>
</div>
<div class="metric-card">
<div class="metric-value">${total_revenue:,.0f}</div>
<div class="metric-label">Total Revenue</div>
</div>
<div class="metric-card">
<div class="metric-value">${avg_order_value:.0f}</div>
<div class="metric-label">Avg Order Value</div>
</div>
<div class="metric-card">
<div class="metric-value">{recent_orders:,}</div>
<div class="metric-label">Recent Orders (7d)</div>
</div>
</div>
<div class="charts-section">
<div class="chart-card">
<img src="data:image/png;base64,{revenue_chart}" alt="Revenue Trends" class="chart-img">
</div>
<div class="chart-card">
<img src="data:image/png;base64,{category_chart}" alt="Category Performance" class="chart-img">
</div>
</div>
<div class="chart-card full-width">
<img src="data:image/png;base64,{category_pie_chart}" alt="Revenue Distribution" class="chart-img">
</div>
<div class="customers-table">
<h3>🏆 Top Customers</h3>
<table>
<thead>
<tr>
<th>Customer Name</th>
<th>Country</th>
<th>Orders</th>
<th>Total Spent</th>
</tr>
</thead>
<tbody>
{customer_rows}
</tbody>
</table>
</div>
<div class="footer">
<p>Mini Data Warehouse Dashboard • PostgreSQL + Python • Star Schema Analytics</p>
</div>
</div>
</body>
</html>
"""
# Generate customer table rows
customer_rows = ""
for customer in top_customers:
customer_rows += f"""
<tr>
<td>{customer[0]}</td>
<td>{customer[1]}</td>
<td>{customer[2]}</td>
<td>${customer[3]:,.2f}</td>
</tr>"""
# Fill template
html_content = html_template.format(
timestamp=datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
total_customers=metrics.get('total_customers', 0),
total_products=metrics.get('total_products', 0),
total_orders=metrics.get('total_orders', 0),
total_revenue=metrics.get('total_revenue', 0),
avg_order_value=metrics.get('avg_order_value', 0),
recent_orders=metrics.get('recent_orders', 0),
revenue_chart=revenue_chart,
category_chart=category_chart,
category_pie_chart=category_pie_chart,
customer_rows=customer_rows
)
return html_content
def save_dashboard(self, filename='dashboard.html'):
"""Save dashboard to HTML file"""
html_content = self.generate_html_dashboard()
with open(filename, 'w') as f:
f.write(html_content)
logger.info(f"Dashboard saved to {filename}")
return filename
def main():
"""Main function to generate dashboard"""
import argparse
parser = argparse.ArgumentParser(description='Generate dashboard for Mini Data Warehouse')
parser.add_argument('--output', '-o', default='dashboard.html', help='Output HTML file')
args = parser.parse_args()
generator = DashboardGenerator()
try:
filename = generator.save_dashboard(args.output)
print(f"✅ Dashboard generated successfully: {filename}")
print(f"📝 Open {filename} in your web browser to view the dashboard")
return 0
except Exception as e:
logger.error(f"Error generating dashboard: {e}")
return 1
if __name__ == "__main__":
exit(main())