-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization_engine.py
More file actions
579 lines (493 loc) · 20.6 KB
/
visualization_engine.py
File metadata and controls
579 lines (493 loc) · 20.6 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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
import matplotlib
matplotlib.use('Agg') # Use non-interactive backend
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import pandas as pd
import numpy as np
import json
import base64
from io import BytesIO
from typing import Dict, List, Optional, Any, Tuple
from datetime import datetime
import logging
logger = logging.getLogger(__name__)
class DataVisualizationEngine:
"""
Data visualization engine for generating charts and graphs from Jira analytics.
Supports both static (matplotlib/seaborn) and interactive (plotly) visualizations.
"""
def __init__(self):
"""Initialize the visualization engine."""
# Set style for matplotlib/seaborn
plt.style.use('default')
sns.set_palette("husl")
# Configure plotly default template
self.plotly_template = "plotly_white"
def generate_velocity_chart(self, sprint_metrics: List[Dict[str, Any]],
chart_type: str = 'plotly') -> Dict[str, Any]:
"""
Generate velocity chart from sprint metrics.
Args:
sprint_metrics: List of sprint metrics dictionaries
chart_type: 'plotly' or 'matplotlib'
Returns:
Dictionary with chart data and metadata
"""
if not sprint_metrics:
return {'error': 'No sprint metrics data available'}
# Prepare data
df = pd.DataFrame(sprint_metrics)
if chart_type == 'plotly':
return self._create_plotly_velocity_chart(df)
else:
return self._create_matplotlib_velocity_chart(df)
def _create_plotly_velocity_chart(self, df: pd.DataFrame) -> Dict[str, Any]:
"""Create interactive velocity chart using Plotly."""
fig = make_subplots(
rows=2, cols=1,
subplot_titles=('Sprint Velocity (Story Points)', 'Completion Rate (%)'),
vertical_spacing=0.1
)
# Velocity chart
fig.add_trace(
go.Scatter(
x=df['sprint_name'],
y=df['velocity'],
mode='lines+markers',
name='Velocity',
line=dict(color='#1f77b4', width=3),
marker=dict(size=8)
),
row=1, col=1
)
# Add planned vs completed points
fig.add_trace(
go.Bar(
x=df['sprint_name'],
y=df['planned_points'],
name='Planned Points',
marker_color='lightblue',
opacity=0.7
),
row=1, col=1
)
fig.add_trace(
go.Bar(
x=df['sprint_name'],
y=df['completed_points'],
name='Completed Points',
marker_color='darkblue'
),
row=1, col=1
)
# Completion rate chart
fig.add_trace(
go.Scatter(
x=df['sprint_name'],
y=df['completion_rate'],
mode='lines+markers',
name='Completion Rate',
line=dict(color='#ff7f0e', width=3),
marker=dict(size=8)
),
row=2, col=1
)
# Update layout
fig.update_layout(
title='Sprint Velocity Analysis',
template=self.plotly_template,
height=600,
showlegend=True
)
fig.update_xaxes(title_text="Sprint", row=2, col=1)
fig.update_yaxes(title_text="Story Points", row=1, col=1)
fig.update_yaxes(title_text="Completion Rate (%)", row=2, col=1)
return {
'chart_type': 'plotly',
'chart_data': fig.to_json(),
'chart_html': fig.to_html(include_plotlyjs='cdn'),
'summary': {
'avg_velocity': df['velocity'].mean(),
'avg_completion_rate': df['completion_rate'].mean(),
'total_sprints': len(df)
}
}
def _create_matplotlib_velocity_chart(self, df: pd.DataFrame) -> Dict[str, Any]:
"""Create static velocity chart using Matplotlib."""
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 10))
# Velocity chart
ax1.plot(df['sprint_name'], df['velocity'], marker='o', linewidth=2, markersize=6)
ax1.set_title('Sprint Velocity (Story Points)')
ax1.set_ylabel('Story Points')
ax1.grid(True, alpha=0.3)
ax1.tick_params(axis='x', rotation=45)
# Completion rate chart
ax2.plot(df['sprint_name'], df['completion_rate'], marker='s', color='orange', linewidth=2, markersize=6)
ax2.set_title('Sprint Completion Rate (%)')
ax2.set_xlabel('Sprint')
ax2.set_ylabel('Completion Rate (%)')
ax2.grid(True, alpha=0.3)
ax2.tick_params(axis='x', rotation=45)
plt.tight_layout()
# Convert to base64 string
buffer = BytesIO()
plt.savefig(buffer, format='png', dpi=300, bbox_inches='tight')
buffer.seek(0)
image_base64 = base64.b64encode(buffer.getvalue()).decode()
plt.close()
return {
'chart_type': 'matplotlib',
'chart_image': image_base64,
'summary': {
'avg_velocity': df['velocity'].mean(),
'avg_completion_rate': df['completion_rate'].mean(),
'total_sprints': len(df)
}
}
def generate_defect_analysis_charts(self, defect_metrics: Dict[str, Any],
chart_type: str = 'plotly') -> Dict[str, Any]:
"""
Generate defect analysis charts.
Args:
defect_metrics: Defect metrics dictionary
chart_type: 'plotly' or 'matplotlib'
Returns:
Dictionary with chart data and metadata
"""
if chart_type == 'plotly':
return self._create_plotly_defect_charts(defect_metrics)
else:
return self._create_matplotlib_defect_charts(defect_metrics)
def _create_plotly_defect_charts(self, metrics: Dict[str, Any]) -> Dict[str, Any]:
"""Create interactive defect analysis charts using Plotly."""
fig = make_subplots(
rows=2, cols=2,
subplot_titles=(
'Defect Status Distribution',
'Defects by Priority',
'Defects by Component',
'Defect Rate Overview'
),
specs=[[{"type": "pie"}, {"type": "bar"}],
[{"type": "bar"}, {"type": "indicator"}]]
)
# Status distribution pie chart
status_data = {
'Open': metrics['open_defects'],
'Resolved': metrics['resolved_defects']
}
fig.add_trace(
go.Pie(
labels=list(status_data.keys()),
values=list(status_data.values()),
name="Status"
),
row=1, col=1
)
# Priority distribution bar chart
if metrics['defects_by_priority']:
priorities = list(metrics['defects_by_priority'].keys())
priority_counts = list(metrics['defects_by_priority'].values())
fig.add_trace(
go.Bar(
x=priorities,
y=priority_counts,
name="Priority",
marker_color='red'
),
row=1, col=2
)
# Component distribution bar chart
if metrics['defects_by_component']:
components = list(metrics['defects_by_component'].keys())[:10] # Top 10
component_counts = list(metrics['defects_by_component'].values())[:10]
fig.add_trace(
go.Bar(
x=components,
y=component_counts,
name="Component",
marker_color='orange'
),
row=2, col=1
)
# Defect rate indicator
fig.add_trace(
go.Indicator(
mode="gauge+number",
value=metrics['defect_rate'],
title={'text': "Defect Rate (%)"},
gauge={'axis': {'range': [None, 50]},
'bar': {'color': "darkred"},
'steps': [
{'range': [0, 10], 'color': "lightgreen"},
{'range': [10, 25], 'color': "yellow"},
{'range': [25, 50], 'color': "red"}],
'threshold': {'line': {'color': "red", 'width': 4},
'thickness': 0.75, 'value': 20}}
),
row=2, col=2
)
fig.update_layout(
title='Defect Analysis Dashboard',
template=self.plotly_template,
height=800,
showlegend=False
)
return {
'chart_type': 'plotly',
'chart_data': fig.to_json(),
'chart_html': fig.to_html(include_plotlyjs='cdn'),
'summary': metrics
}
def _create_matplotlib_defect_charts(self, metrics: Dict[str, Any]) -> Dict[str, Any]:
"""Create static defect analysis charts using Matplotlib."""
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(15, 12))
# Status distribution pie chart
status_data = {
'Open': metrics['open_defects'],
'Resolved': metrics['resolved_defects']
}
ax1.pie(status_data.values(), labels=status_data.keys(), autopct='%1.1f%%')
ax1.set_title('Defect Status Distribution')
# Priority distribution bar chart
if metrics['defects_by_priority']:
priorities = list(metrics['defects_by_priority'].keys())
priority_counts = list(metrics['defects_by_priority'].values())
ax2.bar(priorities, priority_counts, color='red', alpha=0.7)
ax2.set_title('Defects by Priority')
ax2.set_ylabel('Count')
ax2.tick_params(axis='x', rotation=45)
# Component distribution bar chart
if metrics['defects_by_component']:
components = list(metrics['defects_by_component'].keys())[:10]
component_counts = list(metrics['defects_by_component'].values())[:10]
ax3.bar(components, component_counts, color='orange', alpha=0.7)
ax3.set_title('Defects by Component (Top 10)')
ax3.set_ylabel('Count')
ax3.tick_params(axis='x', rotation=45)
# Defect rate text display
ax4.text(0.5, 0.5, f"Defect Rate\n{metrics['defect_rate']:.1f}%",
ha='center', va='center', fontsize=20,
bbox=dict(boxstyle="round,pad=0.3", facecolor="lightblue"))
ax4.set_xlim(0, 1)
ax4.set_ylim(0, 1)
ax4.axis('off')
ax4.set_title('Overall Defect Rate')
plt.tight_layout()
# Convert to base64 string
buffer = BytesIO()
plt.savefig(buffer, format='png', dpi=300, bbox_inches='tight')
buffer.seek(0)
image_base64 = base64.b64encode(buffer.getvalue()).decode()
plt.close()
return {
'chart_type': 'matplotlib',
'chart_image': image_base64,
'summary': metrics
}
def generate_lead_time_chart(self, lead_time_metrics: Dict[str, Any],
chart_type: str = 'plotly') -> Dict[str, Any]:
"""
Generate lead time analysis chart.
Args:
lead_time_metrics: Lead time metrics dictionary
chart_type: 'plotly' or 'matplotlib'
Returns:
Dictionary with chart data and metadata
"""
if chart_type == 'plotly':
return self._create_plotly_lead_time_chart(lead_time_metrics)
else:
return self._create_matplotlib_lead_time_chart(lead_time_metrics)
def _create_plotly_lead_time_chart(self, metrics: Dict[str, Any]) -> Dict[str, Any]:
"""Create interactive lead time chart using Plotly."""
# Lead time by issue type
if metrics['lead_time_by_type_hours']:
issue_types = list(metrics['lead_time_by_type_hours'].keys())
lead_times = [t/24 for t in metrics['lead_time_by_type_hours'].values()] # Convert to days
fig = go.Figure()
fig.add_trace(
go.Bar(
x=issue_types,
y=lead_times,
name='Lead Time (Days)',
marker_color='steelblue'
)
)
# Add average line
avg_lead_time_days = metrics['avg_lead_time_hours'] / 24
fig.add_hline(
y=avg_lead_time_days,
line_dash="dash",
line_color="red",
annotation_text=f"Average: {avg_lead_time_days:.1f} days"
)
fig.update_layout(
title='Lead Time Analysis by Issue Type',
xaxis_title='Issue Type',
yaxis_title='Lead Time (Days)',
template=self.plotly_template
)
return {
'chart_type': 'plotly',
'chart_data': fig.to_json(),
'chart_html': fig.to_html(include_plotlyjs='cdn'),
'summary': {
'avg_lead_time_days': avg_lead_time_days,
'median_lead_time_days': metrics['median_lead_time_hours'] / 24,
'p95_lead_time_days': metrics['percentile_95_lead_time_hours'] / 24
}
}
else:
return {'error': 'No lead time data available'}
def _create_matplotlib_lead_time_chart(self, metrics: Dict[str, Any]) -> Dict[str, Any]:
"""Create static lead time chart using Matplotlib."""
if metrics['lead_time_by_type_hours']:
fig, ax = plt.subplots(figsize=(12, 6))
issue_types = list(metrics['lead_time_by_type_hours'].keys())
lead_times = [t/24 for t in metrics['lead_time_by_type_hours'].values()] # Convert to days
bars = ax.bar(issue_types, lead_times, color='steelblue', alpha=0.7)
# Add average line
avg_lead_time_days = metrics['avg_lead_time_hours'] / 24
ax.axhline(y=avg_lead_time_days, color='red', linestyle='--',
label=f'Average: {avg_lead_time_days:.1f} days')
ax.set_title('Lead Time Analysis by Issue Type')
ax.set_xlabel('Issue Type')
ax.set_ylabel('Lead Time (Days)')
ax.legend()
ax.grid(True, alpha=0.3)
plt.xticks(rotation=45)
plt.tight_layout()
# Convert to base64 string
buffer = BytesIO()
plt.savefig(buffer, format='png', dpi=300, bbox_inches='tight')
buffer.seek(0)
image_base64 = base64.b64encode(buffer.getvalue()).decode()
plt.close()
return {
'chart_type': 'matplotlib',
'chart_image': image_base64,
'summary': {
'avg_lead_time_days': avg_lead_time_days,
'median_lead_time_days': metrics['median_lead_time_hours'] / 24,
'p95_lead_time_days': metrics['percentile_95_lead_time_hours'] / 24
}
}
else:
return {'error': 'No lead time data available'}
def generate_trend_chart(self, trend_data: Dict[str, Any],
chart_type: str = 'plotly') -> Dict[str, Any]:
"""
Generate trend analysis chart.
Args:
trend_data: Trend analysis data
chart_type: 'plotly' or 'matplotlib'
Returns:
Dictionary with chart data and metadata
"""
if not trend_data.get('trend_data'):
return {'error': 'No trend data available'}
df = pd.DataFrame(trend_data['trend_data'])
df['period_start'] = pd.to_datetime(df['period_start'])
if chart_type == 'plotly':
return self._create_plotly_trend_chart(df, trend_data)
else:
return self._create_matplotlib_trend_chart(df, trend_data)
def _create_plotly_trend_chart(self, df: pd.DataFrame, trend_data: Dict[str, Any]) -> Dict[str, Any]:
"""Create interactive trend chart using Plotly."""
fig = go.Figure()
# Main trend line
fig.add_trace(
go.Scatter(
x=df['period_start'],
y=df['value'],
mode='lines+markers',
name=trend_data['metric_type'].title(),
line=dict(width=3),
marker=dict(size=8)
)
)
# Add trend line
if len(df) >= 2:
x_numeric = np.arange(len(df))
slope = trend_data['trend_analysis']['slope']
intercept = df['value'].iloc[0] - slope * 0
trend_line = slope * x_numeric + intercept
fig.add_trace(
go.Scatter(
x=df['period_start'],
y=trend_line,
mode='lines',
name='Trend',
line=dict(dash='dash', color='red')
)
)
fig.update_layout(
title=f'{trend_data["metric_type"].title()} Trend Analysis',
xaxis_title='Time Period',
yaxis_title=trend_data['metric_type'].title(),
template=self.plotly_template
)
return {
'chart_type': 'plotly',
'chart_data': fig.to_json(),
'chart_html': fig.to_html(include_plotlyjs='cdn'),
'summary': trend_data['trend_analysis']
}
def _create_matplotlib_trend_chart(self, df: pd.DataFrame, trend_data: Dict[str, Any]) -> Dict[str, Any]:
"""Create static trend chart using Matplotlib."""
fig, ax = plt.subplots(figsize=(12, 6))
# Main trend line
ax.plot(df['period_start'], df['value'], marker='o', linewidth=2, markersize=6)
# Add trend line
if len(df) >= 2:
x_numeric = np.arange(len(df))
slope = trend_data['trend_analysis']['slope']
intercept = df['value'].iloc[0] - slope * 0
trend_line = slope * x_numeric + intercept
ax.plot(df['period_start'], trend_line, '--', color='red', alpha=0.7, label='Trend')
ax.legend()
ax.set_title(f'{trend_data["metric_type"].title()} Trend Analysis')
ax.set_xlabel('Time Period')
ax.set_ylabel(trend_data['metric_type'].title())
ax.grid(True, alpha=0.3)
plt.xticks(rotation=45)
plt.tight_layout()
# Convert to base64 string
buffer = BytesIO()
plt.savefig(buffer, format='png', dpi=300, bbox_inches='tight')
buffer.seek(0)
image_base64 = base64.b64encode(buffer.getvalue()).decode()
plt.close()
return {
'chart_type': 'matplotlib',
'chart_image': image_base64,
'summary': trend_data['trend_analysis']
}
def save_chart_to_file(self, chart_data: Dict[str, Any], file_path: str) -> bool:
"""
Save chart to file.
Args:
chart_data: Chart data dictionary
file_path: Path to save the file
Returns:
True if successful, False otherwise
"""
try:
if chart_data['chart_type'] == 'matplotlib':
# Save base64 image to file
image_data = base64.b64decode(chart_data['chart_image'])
with open(file_path, 'wb') as f:
f.write(image_data)
elif chart_data['chart_type'] == 'plotly':
# Save HTML file
with open(file_path, 'w') as f:
f.write(chart_data['chart_html'])
logger.info(f"Chart saved to {file_path}")
return True
except Exception as e:
logger.error(f"Failed to save chart to {file_path}: {e}")
return False