-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.py
More file actions
362 lines (322 loc) · 20.1 KB
/
Copy pathdashboard.py
File metadata and controls
362 lines (322 loc) · 20.1 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
"""
CI/CD Pipeline Analytics - Light Theme Dashboard
Real-time data from Supabase
"""
import dash
from dash import dcc, html, Input, Output, callback, ctx
import plotly.graph_objects as go
import pandas as pd
from datetime import datetime
from config import supabase
from auth import decode_token
from urllib.parse import parse_qs
# Will be set when integrated with Flask
dash_app = None
current_user_id = None
# Light Theme
THEME = {
'bg': '#f8fafc', 'card': '#ffffff', 'card_border': '#e2e8f0',
'accent': '#6366f1', 'accent2': '#8b5cf6',
'success': '#22c55e', 'error': '#ef4444', 'warning': '#f59e0b', 'info': '#3b82f6',
'text': '#1e293b', 'text_dim': '#64748b', 'border': '#e2e8f0',
}
def fetch_jobs(user_id=None):
# Fetch jobs for specific user if user_id provided
query = supabase.table('jobs').select('*').order('created_at', desc=True)
if user_id:
query = query.eq('user_id', user_id)
result = query.execute()
if result.data:
df = pd.DataFrame(result.data)
df['created_at'] = pd.to_datetime(df['created_at'])
df['started_at'] = pd.to_datetime(df['started_at'])
df['finished_at'] = pd.to_datetime(df['finished_at'])
df['date'] = df['created_at'].dt.date
df['hour'] = df['created_at'].dt.hour
df['day_name'] = df['created_at'].dt.day_name()
df['duration'] = (df['finished_at'] - df['started_at']).dt.total_seconds()
return df
return pd.DataFrame()
def fetch_logs(user_id=None):
if user_id:
# Get logs only for this user's jobs
jobs_result = supabase.table('jobs').select('id').eq('user_id', user_id).execute()
if jobs_result.data:
job_ids = [job['id'] for job in jobs_result.data]
result = supabase.table('job_logs').select('*').in_('job_id', job_ids).order('created_at', desc=True).limit(50).execute()
else:
result = type('obj', (object,), {'data': []})()
else:
result = supabase.table('job_logs').select('*').order('created_at', desc=True).limit(50).execute()
return pd.DataFrame(result.data) if result.data else pd.DataFrame()
def fetch_users():
result = supabase.table('users').select('*').execute()
return pd.DataFrame(result.data) if result.data else pd.DataFrame()
def create_dashboard(flask_app):
"""Create and integrate Dash dashboard with Flask app"""
global dash_app
dash_app = dash.Dash(
__name__,
server=flask_app,
url_base_pathname='/dashboard/',
title="Pipeline Analytics"
)
dash_app.index_string = '''
<!DOCTYPE html>
<html>
<head>
{%metas%}<title>{%title%}</title>{%favicon%}{%css%}
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: 'Inter', -apple-system, sans-serif; background: #f8fafc; color: #1e293b; }
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: #f1f5f9; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #94a3b8; }
.card { background: #ffffff; border: 1px solid #e2e8f0; border-radius: 12px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
.card:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.08); }
.pulse { animation: pulse 2s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }
.badge { padding: 4px 10px; border-radius: 6px; font-size: 11px; font-weight: 500; }
</style>
</head>
<body>{%app_entry%}<footer>{%config%}{%scripts%}{%renderer%}</footer></body>
</html>
'''
dash_app.layout = html.Div([
# Header
html.Div([
html.Div([
html.H1("Pipeline Analytics", style={'fontSize': '20px', 'fontWeight': '600', 'color': THEME['text']}),
html.Span("Real-time CI/CD Metrics", style={'color': THEME['text_dim'], 'fontSize': '13px', 'marginLeft': '16px'})
], style={'display': 'flex', 'alignItems': 'center'}),
html.Div([
html.A("← Back to App", href="/", style={
'color': THEME['accent'], 'textDecoration': 'none', 'fontSize': '13px', 'fontWeight': '500'
}),
], style={'display': 'flex', 'alignItems': 'center'})
], style={'display': 'flex', 'justifyContent': 'space-between', 'padding': '20px 32px',
'background': '#fff', 'borderBottom': f'1px solid {THEME["border"]}'}),
# KPI Cards
html.Div(id='kpi-cards', style={'display': 'grid', 'gridTemplateColumns': 'repeat(6, 1fr)', 'gap': '16px', 'padding': '24px 32px', 'minHeight': '100px'}),
# Row 1
html.Div([
html.Div([
html.Div("Pipeline Throughput", style={'fontSize': '14px', 'fontWeight': '600', 'marginBottom': '16px', 'color': THEME['text']}),
dcc.Graph(id='throughput-chart', config={'displayModeBar': False}, style={'height': '220px'})
], className='card', style={'padding': '20px', 'flex': '2', 'minHeight': '280px'}),
html.Div([
html.Div("Status Distribution", style={'fontSize': '14px', 'fontWeight': '600', 'marginBottom': '16px', 'color': THEME['text']}),
dcc.Graph(id='status-pie', config={'displayModeBar': False}, style={'height': '220px'})
], className='card', style={'padding': '20px', 'flex': '1', 'minHeight': '280px'}),
], style={'display': 'flex', 'gap': '16px', 'padding': '0 32px 16px'}),
# Row 2
html.Div([
html.Div([
html.Div("Weekly Activity Heatmap", style={'fontSize': '14px', 'fontWeight': '600', 'marginBottom': '16px', 'color': THEME['text']}),
dcc.Graph(id='heatmap-chart', config={'displayModeBar': False}, style={'height': '200px'})
], className='card', style={'padding': '20px', 'flex': '1', 'minHeight': '260px'}),
html.Div([
html.Div("Duration by Status", style={'fontSize': '14px', 'fontWeight': '600', 'marginBottom': '16px', 'color': THEME['text']}),
dcc.Graph(id='duration-chart', config={'displayModeBar': False}, style={'height': '200px'})
], className='card', style={'padding': '20px', 'flex': '1', 'minHeight': '260px'}),
html.Div([
html.Div("Hourly Distribution", style={'fontSize': '14px', 'fontWeight': '600', 'marginBottom': '16px', 'color': THEME['text']}),
dcc.Graph(id='hourly-chart', config={'displayModeBar': False}, style={'height': '200px'})
], className='card', style={'padding': '20px', 'flex': '1', 'minHeight': '260px'}),
], style={'display': 'flex', 'gap': '16px', 'padding': '0 32px 16px'}),
# Row 3
html.Div([
html.Div([
html.Div("Repository Performance", style={'fontSize': '14px', 'fontWeight': '600', 'marginBottom': '16px', 'color': THEME['text']}),
dcc.Graph(id='repo-chart', config={'displayModeBar': False}, style={'height': '200px'})
], className='card', style={'padding': '20px', 'flex': '1', 'minHeight': '260px'}),
html.Div([
html.Div("Success vs Failure Trend", style={'fontSize': '14px', 'fontWeight': '600', 'marginBottom': '16px', 'color': THEME['text']}),
dcc.Graph(id='trend-chart', config={'displayModeBar': False}, style={'height': '200px'})
], className='card', style={'padding': '20px', 'flex': '1', 'minHeight': '260px'}),
], style={'display': 'flex', 'gap': '16px', 'padding': '0 32px 16px'}),
# Row 4
html.Div([
html.Div([
html.Div("Recent Pipelines", style={'fontSize': '14px', 'fontWeight': '600', 'marginBottom': '16px', 'color': THEME['text']}),
html.Div(id='jobs-table', style={'minHeight': '200px'})
], className='card', style={'padding': '20px', 'flex': '2'}),
html.Div([
html.Div([
html.Span("Live Logs", style={'fontSize': '14px', 'fontWeight': '600', 'color': THEME['text']}),
html.Span("●", className='pulse', style={'color': THEME['success'], 'marginLeft': '8px', 'fontSize': '8px'})
], style={'marginBottom': '16px'}),
html.Div(id='logs-feed', style={'height': '280px', 'overflowY': 'auto'})
], className='card', style={'padding': '20px', 'flex': '1'}),
], style={'display': 'flex', 'gap': '16px', 'padding': '0 32px 24px'}),
], style={'minHeight': '100vh', 'background': THEME['bg']},
id='main-container')
# Register callbacks
register_callbacks(dash_app)
return dash_app
def register_callbacks(app):
@app.callback(
[Output('kpi-cards', 'children'), Output('throughput-chart', 'figure'), Output('status-pie', 'figure'),
Output('heatmap-chart', 'figure'), Output('duration-chart', 'figure'), Output('hourly-chart', 'figure'),
Output('repo-chart', 'figure'), Output('trend-chart', 'figure'),
Output('jobs-table', 'children'), Output('logs-feed', 'children')],
[Input('main-container', 'id')],
prevent_initial_call=False
)
def update_all(container_id):
global current_user_id
# Try to get user_id from URL query parameter (token)
user_id = None
try:
from flask import request
token = request.args.get('token')
if token:
payload = decode_token(token)
user_id = payload.get('user_id')
current_user_id = user_id
except:
user_id = current_user_id
df = fetch_jobs(user_id)
logs = fetch_logs(user_id)
users = fetch_users()
def empty_fig(h=200):
fig = go.Figure()
fig.add_annotation(text='No data available', x=0.5, y=0.5, xref='paper', yref='paper',
showarrow=False, font=dict(size=14, color=THEME['text_dim']))
fig.update_layout(
paper_bgcolor='#fff', plot_bgcolor='#fff', height=h,
margin=dict(l=20, r=20, t=20, b=20),
xaxis=dict(visible=False, showgrid=False),
yaxis=dict(visible=False, showgrid=False)
)
return fig
if df.empty:
kpis = [kpi_card("Total Jobs", 0, THEME['accent']), kpi_card("Success Rate", "0%", THEME['success']),
kpi_card("Failed", 0, THEME['error']), kpi_card("Running", 0, THEME['info']),
kpi_card("Avg Duration", "0s", THEME['warning']), kpi_card("Users", len(users), THEME['accent2'])]
return kpis, empty_fig(220), empty_fig(220), empty_fig(200), empty_fig(200), empty_fig(200), empty_fig(200), empty_fig(200), \
html.P("No jobs yet - create a pipeline to see data here", style={'color': THEME['text_dim'], 'textAlign': 'center', 'padding': '40px'}), \
html.P("No logs yet", style={'color': THEME['text_dim'], 'textAlign': 'center', 'padding': '40px'})
# Stats
total = len(df)
success = len(df[df['status'] == 'success'])
failed = len(df[df['status'] == 'failed'])
running = len(df[df['status'] == 'running'])
rate = f"{success/total*100:.0f}%" if total else "0%"
avg_dur = f"{df['duration'].dropna().mean():.0f}s" if df['duration'].notna().any() else "0s"
kpis = [kpi_card("Total Jobs", total, THEME['accent']), kpi_card("Success Rate", rate, THEME['success']),
kpi_card("Failed", failed, THEME['error']), kpi_card("Running", running, THEME['info']),
kpi_card("Avg Duration", avg_dur, THEME['warning']), kpi_card("Users", len(users), THEME['accent2'])]
# 1. Throughput Area
daily = df.groupby('date').size().reset_index(name='count')
throughput = go.Figure()
throughput.add_trace(go.Scatter(x=daily['date'], y=daily['count'], mode='lines', fill='tozeroy',
line=dict(color=THEME['accent'], width=2), fillcolor='rgba(99,102,241,0.1)'))
throughput.update_layout(paper_bgcolor='#fff', plot_bgcolor='#fff', height=220, margin=dict(l=40,r=20,t=10,b=40),
xaxis=dict(showgrid=False, tickfont=dict(size=11, color=THEME['text_dim'])),
yaxis=dict(showgrid=True, gridcolor='#f1f5f9', tickfont=dict(size=11, color=THEME['text_dim'])),
uirevision='constant')
# 2. Status Pie
status_counts = df['status'].value_counts()
colors_map = {'success': THEME['success'], 'failed': THEME['error'], 'running': THEME['info'],
'pending': THEME['text_dim'], 'cancelled': THEME['warning']}
status_pie = go.Figure(go.Pie(values=status_counts.values, labels=status_counts.index, hole=0.5,
marker=dict(colors=[colors_map.get(s, THEME['text_dim']) for s in status_counts.index]),
textinfo='percent', textfont=dict(size=12, color='#fff')))
status_pie.update_layout(paper_bgcolor='#fff', height=220, margin=dict(l=20,r=20,t=10,b=10),
legend=dict(orientation='h', y=-0.1, font=dict(size=11, color=THEME['text_dim'])),
uirevision='constant')
# 3. Heatmap
days_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
heatmap_data = df.groupby(['day_name', 'hour']).size().unstack(fill_value=0).reindex(days_order, fill_value=0)
heatmap = go.Figure(go.Heatmap(z=heatmap_data.values, x=list(range(24)), y=['Mon','Tue','Wed','Thu','Fri','Sat','Sun'],
colorscale=[[0, '#f1f5f9'], [0.5, '#a5b4fc'], [1, THEME['accent']]], showscale=False))
heatmap.update_layout(paper_bgcolor='#fff', plot_bgcolor='#fff', height=200, margin=dict(l=40,r=20,t=10,b=30),
xaxis=dict(tickfont=dict(size=10, color=THEME['text_dim']), dtick=4),
yaxis=dict(tickfont=dict(size=10, color=THEME['text_dim'])))
# 4. Duration Box
dur_df = df[df['duration'].notna()]
if not dur_df.empty:
duration = go.Figure()
for status in ['success', 'failed']:
subset = dur_df[dur_df['status'] == status]
if not subset.empty:
duration.add_trace(go.Box(y=subset['duration'], name=status.title(),
marker_color=colors_map.get(status), boxpoints=False))
duration.update_layout(paper_bgcolor='#fff', plot_bgcolor='#fff', height=200, margin=dict(l=50,r=20,t=10,b=30),
showlegend=False, yaxis=dict(title=dict(text='Seconds', font=dict(size=11, color=THEME['text_dim'])),
gridcolor='#f1f5f9',
tickfont=dict(size=10, color=THEME['text_dim'])))
else:
duration = empty_fig()
# 5. Hourly Bar
hourly = df.groupby('hour').size().reindex(range(24), fill_value=0)
hourly_chart = go.Figure(go.Bar(x=list(range(24)), y=hourly.values, marker_color=THEME['accent']))
hourly_chart.update_layout(paper_bgcolor='#fff', plot_bgcolor='#fff', height=200, margin=dict(l=40,r=20,t=10,b=30),
xaxis=dict(tickfont=dict(size=10, color=THEME['text_dim']), dtick=4),
yaxis=dict(showgrid=True, gridcolor='#f1f5f9', tickfont=dict(size=10, color=THEME['text_dim'])))
# 6. Repo Bar
repo_counts = df['repo_url'].value_counts().head(5)
repo_names = [r.split('/')[-1][:15] if r else 'Unknown' for r in repo_counts.index]
repo_chart = go.Figure(go.Bar(y=repo_names, x=repo_counts.values, orientation='h', marker_color=THEME['accent']))
repo_chart.update_layout(paper_bgcolor='#fff', plot_bgcolor='#fff', height=200, margin=dict(l=100,r=20,t=10,b=30),
xaxis=dict(showgrid=True, gridcolor='#f1f5f9', tickfont=dict(size=10, color=THEME['text_dim'])),
yaxis=dict(tickfont=dict(size=11, color=THEME['text'])))
# 7. Trend Line
daily_stats = df.groupby('date').agg(success=('status', lambda x: (x=='success').sum()),
failed=('status', lambda x: (x=='failed').sum())).reset_index()
trend = go.Figure()
trend.add_trace(go.Scatter(x=daily_stats['date'], y=daily_stats['success'], name='Success', mode='lines+markers',
line=dict(color=THEME['success'], width=2), marker=dict(size=6)))
trend.add_trace(go.Scatter(x=daily_stats['date'], y=daily_stats['failed'], name='Failed', mode='lines+markers',
line=dict(color=THEME['error'], width=2), marker=dict(size=6)))
trend.update_layout(paper_bgcolor='#fff', plot_bgcolor='#fff', height=200, margin=dict(l=40,r=20,t=10,b=40),
legend=dict(orientation='h', y=1.1, font=dict(size=11, color=THEME['text_dim'])),
xaxis=dict(showgrid=False, tickfont=dict(size=10, color=THEME['text_dim'])),
yaxis=dict(showgrid=True, gridcolor='#f1f5f9', tickfont=dict(size=10, color=THEME['text_dim'])))
# Jobs Table
table = create_table(df.head(8))
# Logs Feed
if not logs.empty:
log_colors = {'info': THEME['info'], 'error': THEME['error'], 'warn': THEME['warning']}
logs_feed = [html.Div([
html.Span("●", style={'color': log_colors.get(row['level'], THEME['text_dim']), 'marginRight': '8px', 'fontSize': '8px'}),
html.Span(str(row['message'])[:50], style={'fontSize': '12px', 'color': THEME['text']})
], style={'padding': '8px 0', 'borderBottom': f'1px solid {THEME["border"]}'}) for _, row in logs.head(20).iterrows()]
else:
logs_feed = [html.P("Waiting for logs...", style={'color': THEME['text_dim'], 'textAlign': 'center', 'padding': '40px'})]
return kpis, throughput, status_pie, heatmap, duration, hourly_chart, repo_chart, trend, table, logs_feed
def kpi_card(label, value, color):
return html.Div([
html.Div(str(value), style={'fontSize': '28px', 'fontWeight': '700', 'color': color}),
html.Div(label, style={'fontSize': '12px', 'color': THEME['text_dim'], 'marginTop': '4px'})
], className='card', style={'padding': '20px', 'textAlign': 'center'})
def create_table(df):
if df.empty:
return html.P("No jobs", style={'color': THEME['text_dim'], 'textAlign': 'center', 'padding': '40px'})
colors = {'success': THEME['success'], 'failed': THEME['error'], 'running': THEME['info'],
'pending': THEME['text_dim'], 'cancelled': THEME['warning']}
return html.Div([
html.Div([
html.Span("●", style={'color': colors.get(r['status'], THEME['text_dim']), 'marginRight': '12px', 'fontSize': '10px'}),
html.Span(r['repo_url'].split('/')[-1] if r['repo_url'] else '-', style={'flex': '1', 'fontWeight': '500', 'fontSize': '13px'}),
html.Span(r['branch'], style={'width': '100px', 'fontSize': '12px', 'color': THEME['text_dim']}),
html.Span(r['status'], className='badge', style={
'backgroundColor': f"{colors.get(r['status'], THEME['text_dim'])}15",
'color': colors.get(r['status'], THEME['text_dim']), 'width': '80px', 'textAlign': 'center'
}),
html.Span(r['created_at'].strftime('%H:%M') if pd.notna(r['created_at']) else '',
style={'width': '60px', 'fontSize': '12px', 'color': THEME['text_dim'], 'textAlign': 'right'})
], style={'display': 'flex', 'alignItems': 'center', 'padding': '12px 0', 'borderBottom': f'1px solid {THEME["border"]}'})
for _, r in df.iterrows()
])
if __name__ == '__main__':
# Standalone mode for testing
from flask import Flask
test_app = Flask(__name__)
create_dashboard(test_app)
print("\n 📊 Pipeline Analytics (Light Theme)")
print(f" → http://localhost:8050/dashboard/\n")
test_app.run(debug=True, port=8050)