make metrics show up gray when prometheus returns no data

This commit is contained in:
forest 2020-05-15 12:24:09 -05:00
parent 0434c4d43b
commit 67120e9461
1 changed files with 20 additions and 9 deletions

View File

@ -33,6 +33,8 @@ green = (121/255, 240/255, 50/255)
blue = (70/255, 150/255, 255/255) blue = (70/255, 150/255, 255/255)
red = (255/255, 50/255, 8/255) red = (255/255, 50/255, 8/255)
gray = (128/255, 128/255, 128/255)
@bp.route("/html/<string:metric>/<string:capsulid>/<string:duration>") @bp.route("/html/<string:metric>/<string:capsulid>/<string:duration>")
@account_required @account_required
def display_metric(metric, capsulid, duration): def display_metric(metric, capsulid, duration):
@ -113,12 +115,18 @@ def get_plot_bytes(metric, capsulid, duration, size):
if prometheus_response.status_code >= 300: if prometheus_response.status_code >= 300:
return (502, None) return (502, None)
if len(prometheus_response.json()["data"]["result"]) == 0: series = prometheus_response.json()["data"]["result"]
return (404, None) if len(series) == 0:
now_timestamp = datetime.timestamp(datetime.now())
series = [
dict(
values=[[now_timestamp - interval_seconds, float(0)],[now_timestamp, float(0)]]
)
]
time_series_data = list(map( time_series_data = list(map(
lambda x: (datetime.fromtimestamp(x[0]), float(x[1])), lambda x: (datetime.fromtimestamp(x[0]), float(x[1])),
prometheus_response.json()["data"]["result"][0]["values"] series[0]["values"]
)) ))
plot_bytes = draw_plot_png_bytes(time_series_data, scale=scales[metric], size_x=sizes[size][0], size_y=sizes[size][1]) plot_bytes = draw_plot_png_bytes(time_series_data, scale=scales[metric], size_x=sizes[size][0], size_y=sizes[size][1])
@ -182,15 +190,18 @@ def draw_plot_png_bytes(data, scale, size_x=3, size_y=1):
max_value = reduce(lambda a, b: a if a > b else b, y, scale) max_value = reduce(lambda a, b: a if a > b else b, y, scale)
average=(sum(y)/len(y))/scale if len(data) > 2:
average=average*1.25+0.1 average=(sum(y)/len(y))/scale
average=average*1.25+0.1
bg_color=color_gradient(average) bg_color=color_gradient(average)
average -= 0.1 average -= 0.1
fill_color=color_gradient(average) fill_color=color_gradient(average)
highlight_color=lerp_rgb_tuples(fill_color, (1,1,1), 0.5) highlight_color=lerp_rgb_tuples(fill_color, (1,1,1), 0.5)
else:
bg_color = fill_color = highlight_color = gray
my_plot.fill_between( x, max_value, color=bg_color, alpha=0.13) my_plot.fill_between( x, max_value, color=bg_color, alpha=0.13)
my_plot.fill_between( x, y, color=highlight_color, alpha=0.3) my_plot.fill_between( x, y, color=highlight_color, alpha=0.3)