Skip to content

Commit

Permalink
refactor: removed outdated plots for TL/alerts
Browse files Browse the repository at this point in the history
The overview plots obviate the need for the individual (unintuitive) plots

BREAKING CHANGE: the `plot_metrics` and `plot_overview` settings are no
longer available for Tl/alerts
  • Loading branch information
sbrugman committed Jul 4, 2022
1 parent e2b9ef7 commit 1c4f072
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 183 deletions.
89 changes: 13 additions & 76 deletions popmon/visualization/alert_section_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,40 +145,20 @@ def transform(
df.columns, self.ignore_stat_endswith, self.show_stats
)

plots = []
if self.plot_overview:
plots.append(
_plot_metrics(
feature,
[m for m in metrics if not m.endswith("worst")],
dates,
df,
0,
0,
0,
0,
style="alerts",
)
plots = [
_plot_metrics(
feature,
metrics,
dates,
df,
0,
0,
0,
0,
style="alerts",
)
if self.plot_metrics:
args = [
(
feature,
metric,
dates,
df[metric],
static_bounds,
fdbounds,
self.prefix,
self.suffices,
self.last_n,
self.skip_first_n,
self.skip_last_n,
self.skip_empty_plots,
)
for metric in metrics
]
plots += parallel(_plot_metric, args)
]

# filter out potential empty plots (from skip empty plots)
if self.skip_empty_plots:
plots = [e for e in plots if len(e["plot"])]
Expand All @@ -195,46 +175,3 @@ def transform(
}
)
return sections


def _plot_metric(
feature,
metric,
dates,
values,
static_bounds,
fdbounds,
prefix,
suffices,
last_n,
skip_first_n,
skip_last_n,
skip_empty,
):
"""Split off plot histogram generation to allow for parallel processing"""
# pick up static traffic light boundaries
name = feature + ":" + metric
sbounds = static_bounds.get(name, ())
# pick up dynamic traffic light boundaries
names = [prefix + metric + suffix for suffix in suffices]
dbounds = tuple(
_prune(fdbounds[n].tolist(), last_n, skip_first_n, skip_last_n)
for n in names
if n in fdbounds.columns
)
# choose dynamic bounds if present
bounds = dbounds if len(dbounds) > 0 else sbounds
# prune dates and values
dates = _prune(dates, last_n, skip_first_n, skip_last_n)
values = _prune(values, last_n, skip_first_n, skip_last_n)

# make plot. note: slow!
plot = plot_bars_b64(
data=np.array(values),
labels=dates,
ylim=True,
bounds=bounds,
skip_empty=skip_empty,
)

return {"name": metric, "description": get_stat_description(metric), "plot": plot}
54 changes: 11 additions & 43 deletions popmon/visualization/traffic_light_section_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,35 +149,18 @@ def transform(
df.columns, self.ignore_stat_endswith, self.show_stats
)

plots = []
if self.plot_overview:
plots.append(
_plot_metrics(
feature,
metrics,
dates,
df,
self.last_n,
self.skip_first_n,
self.skip_last_n,
self.skip_empty_plots,
)
plots = [
_plot_metrics(
feature,
metrics,
dates,
df,
self.last_n,
self.skip_first_n,
self.skip_last_n,
self.skip_empty_plots,
)

if self.plot_metrics:
args = [
(
metric,
dates,
df[metric],
self.last_n,
self.skip_first_n,
self.skip_last_n,
self.skip_empty_plots,
)
for metric in metrics
]
plots += parallel(_plot_metric, args)
]

# filter out potential empty plots (from skip empty plots)
if self.skip_empty_plots:
Expand All @@ -196,21 +179,6 @@ def transform(
return sections


def _plot_metric(metric, dates, values, last_n, skip_first_n, skip_last_n, skip_empty):
"""Split off plot histogram generation to allow for parallel processing"""

# prune dates and values
dates = _prune(dates, last_n, skip_first_n, skip_last_n)
values = _prune(values, last_n, skip_first_n, skip_last_n)

# make plot. note: slow!
plot = plot_traffic_lights_b64(
data=np.array(values), labels=dates, skip_empty=skip_empty
)

return {"name": metric, "description": get_stat_description(metric), "plot": plot}


def _plot_metrics(
feature,
metrics,
Expand Down
64 changes: 0 additions & 64 deletions popmon/visualization/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,70 +226,6 @@ def plot_traffic_lights_alerts_b64(feature, data, metrics=None, labels=None):
return render_alert_aggregate_table(feature, data.astype(int), metrics, labels)


def plot_traffic_lights_b64(data, labels=None, skip_empty=True):
"""Plotting histogram data.
:param np.array data: bin values of a histogram
:param labels: common bin labels for all histograms (optional)
:param bool skip_empty: if true, skip empty plots with only nans or only zeroes (optional)
:return: base64 encoded plot image
:rtype: string
"""
# basic checks first
n = data.size # number of bins
if labels and len(labels) != n:
raise ValueError("shape mismatch: x-axis labels do not match the data shape")

# skip plot generation for empty datasets
if skip_empty:
n_data = len(data)
n_zero = n_data - np.count_nonzero(data)
n_nan = pd.isnull(data).sum()
n_inf = np.sum([np.isinf(x) for x in data if isinstance(x, float)])
if n_nan + n_zero + n_inf == n_data:
logger.debug("skipping plot with empty data.")
return ""

fig, ax = plt.subplots()

ax.yaxis.grid(True)
ax.xaxis.grid(False)

colors = ["green", "yellow", "red"]
ones = np.ones(n)

index = np.arange(n)

for i, color in enumerate(colors):
mask = data == i
ax.bar(
index[mask],
ones[mask],
width=1,
align="center",
color=color,
alpha=0.8,
edgecolor="black",
)

ax.set_yticks([])

if labels:
ax.set_xticks(index)
ax.set_xticklabels(labels, fontdict={"rotation": "vertical"})
granularity = math.ceil(len(labels) / 50)
[
l.set_visible(False)
for (i, l) in enumerate(ax.xaxis.get_ticklabels())
if i % granularity != 0
]

fig.tight_layout()

return plt_to_str(fig)


def grouped_bar_chart_b64(data, labels, legend):
"""Plotting grouped histogram data.
Expand Down

0 comments on commit 1c4f072

Please sign in to comment.