Skip to content

Commit

Permalink
Update repltools.py
Browse files Browse the repository at this point in the history
  • Loading branch information
droideck committed Jan 21, 2025
1 parent 3307dda commit 1df6501
Showing 1 changed file with 42 additions and 36 deletions.
78 changes: 42 additions & 36 deletions src/lib389/lib389/repltools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,73 +1083,71 @@ def generate_report(self, output_dir: str,
return generated_files

def _create_plotly_figure(self, results: Dict[str, Any]) -> go.Figure:
"""Create a plotly figure for visualization.
:param results: Analysis results dictionary
:returns: Plotly figure object
"""
"""Create a plotly figure for visualization."""
if not PLOTLY_AVAILABLE:
raise ImportError("Plotly is required for figure creation")

# Create figure with secondary y-axis
# Create figure with 3 subplots (global lag, durations, hop-lag)
fig = make_subplots(
rows=3, cols=1,
subplot_titles=('Replication Lag Times', 'Operation Durations', 'Per-Hop Lags'),
vertical_spacing=0.15
)

# Get unique servers and suffixes for color assignment
# Collect all (suffix, server_name) pairs for color assignment
server_suffix_pairs = set()
for csn, server_map in self.csns.items():
for rec in server_map.values():
server_suffix_pairs.add(
(rec.get('suffix', 'unknown'), rec['server_name'])
)
for key, rec in server_map.items():
if not isinstance(rec, dict) or key == '__hop_lags__':
continue

suffix_val = rec.get('suffix', 'unknown')
srv_val = rec.get('server_name', 'unknown')
server_suffix_pairs.add((suffix_val, srv_val))

# Generate colors
colors = VisualizationHelper.generate_color_palette(len(server_suffix_pairs))

# Prepare chart data with global lag/durations (row=1 and row=2)
# Prepare chart data for row=1 (global lag) & row=2 (duration)
chart_data = VisualizationHelper.prepare_chart_data(self.csns)

# Now add the third subplot data for hop-lags (row=3):
# (A) Plot Per-Hop Lags in row=3
for csn, server_map in self.csns.items():
# Get the list of hop-lags
hop_list = server_map.get('__hop_lags__', [])
for hop in hop_list:
consumer_timestamp = hop['arrival_consumer'] # numeric float UTC
consumer_dt = datetime.fromtimestamp(consumer_timestamp)
hop_lag = hop['hop_lag']
# Build a hover text string if you like
consumer_ts = hop.get('arrival_consumer', 0.0)
consumer_dt = datetime.fromtimestamp(consumer_ts)
hop_lag = hop.get('hop_lag', 0.0)

# Build hover text
hover_text = (
f"Supplier: {hop['supplier']}<br>"
f"Consumer: {hop['consumer']}<br>"
f"Supplier: {hop.get('supplier','unknown')}<br>"
f"Consumer: {hop.get('consumer','unknown')}<br>"
f"Hop Lag: {hop_lag:.3f}s<br>"
f"Arrival Time: {consumer_dt}"
)
# Add a scatter point for each hop-lag

# Add a scatter point for each hop
fig.add_trace(
go.Scatter(
x=[consumer_dt], # single point for consumer arrival
y=[hop_lag], # hop-lag in seconds
x=[consumer_dt],
y=[hop_lag],
mode='markers',
marker=dict(size=8),
name=f"{hop['supplier']}{hop['consumer']}",
name=f"{hop.get('supplier','unknown')}{hop.get('consumer','unknown')}",
text=[hover_text],
hoverinfo='text+x+y',
showlegend=False # If you want to hide repeating legends
showlegend=False # Usually hide repeated legend entries
),
row=3, col=1
)


# Add traces
# (B) Plot Global Lag (row=1) and Durations (row=2)
# data => ChartData(times, lags, durations, hover)
for idx, ((sfx, srv), data) in enumerate(sorted(chart_data.items())):
color = colors[idx % len(colors)]

# Lag time trace
# 1) Lag time trace (row=1)
fig.add_trace(
go.Scatter(
x=data.times,
Expand All @@ -1164,7 +1162,7 @@ def _create_plotly_figure(self, results: Dict[str, Any]) -> go.Figure:
row=1, col=1
)

# Duration trace
# 2) Duration trace (row=2)
fig.add_trace(
go.Scatter(
x=data.times,
Expand All @@ -1179,7 +1177,7 @@ def _create_plotly_figure(self, results: Dict[str, Any]) -> go.Figure:
row=2, col=1
)

# Update layout
# Update layout (titles, legend, size, margins)
fig.update_layout(
title={
'text': 'Replication Analysis Report',
Expand All @@ -1200,18 +1198,27 @@ def _create_plotly_figure(self, results: Dict[str, Any]) -> go.Figure:
margin=dict(t=100, r=200)
)

# Update axes
# X-axis config (row=1)
fig.update_xaxes(
title_text="Time",
gridcolor='lightgray',
row=1, col=1
)
# X-axis config (row=2) with Range Slider
fig.update_xaxes(
title_text="Time",
gridcolor='lightgray',
rangeslider_visible=True,
row=2, col=1
)
# X-axis config (row=3)
fig.update_xaxes(
title_text="Time",
gridcolor='lightgray',
row=3, col=1
)

# Y-axes
fig.update_yaxes(
title_text="Lag Time (seconds)",
gridcolor='lightgray',
Expand All @@ -1222,14 +1229,13 @@ def _create_plotly_figure(self, results: Dict[str, Any]) -> go.Figure:
gridcolor='lightgray',
row=2, col=1
)

fig.update_yaxes(
title_text="Hop Lag (seconds)",
gridcolor='lightgray',
row=3, col=1
)

# Add range selector to bottom subplot
# Range selector on the bottom subplot (row=2 or row=3—your choice).
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
Expand All @@ -1242,7 +1248,7 @@ def _create_plotly_figure(self, results: Dict[str, Any]) -> go.Figure:
]),
bgcolor='rgba(255, 255, 255, 0.8)'
),
row=2, col=1
row=2, col=1 # or row=3, col=1 if preferred
)

return fig
Expand Down

0 comments on commit 1df6501

Please sign in to comment.