fix segfault in draw_plot_png_bytes

This commit is contained in:
forest 2021-01-04 15:18:23 -06:00
parent 4833c6250b
commit 3fdd78df2e

View File

@ -3,8 +3,9 @@ import matplotlib.pyplot as pyplot
import matplotlib.dates as mdates import matplotlib.dates as mdates
from functools import reduce from functools import reduce
import requests import requests
#import json import json
from datetime import datetime from datetime import datetime
from threading import Lock
from io import BytesIO from io import BytesIO
from flask import Blueprint from flask import Blueprint
from flask import current_app from flask import current_app
@ -15,6 +16,7 @@ from werkzeug.exceptions import abort
from capsulflask.db import get_model from capsulflask.db import get_model
from capsulflask.auth import account_required from capsulflask.auth import account_required
mutex = Lock()
bp = Blueprint("metrics", __name__, url_prefix="/metrics") bp = Blueprint("metrics", __name__, url_prefix="/metrics")
durations = dict( durations = dict(
@ -116,6 +118,7 @@ def get_plot_bytes(metric, capsulid, duration, size):
return (502, None) return (502, None)
series = prometheus_response.json()["data"]["result"] series = prometheus_response.json()["data"]["result"]
if len(series) == 0: if len(series) == 0:
now_timestamp = datetime.timestamp(datetime.now()) now_timestamp = datetime.timestamp(datetime.now())
series = [ series = [
@ -129,13 +132,19 @@ def get_plot_bytes(metric, capsulid, duration, size):
series[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]) mutex.acquire()
try:
plot_bytes = draw_plot_png_bytes(time_series_data, scale=scales[metric], size_x=sizes[size][0], size_y=sizes[size][1])
finally:
mutex.release()
return (200, plot_bytes) return (200, plot_bytes)
def draw_plot_png_bytes(data, scale, size_x=3, size_y=1): def draw_plot_png_bytes(data, scale, size_x=3, size_y=1):
#current_app.logger.info(json.dumps(data, indent=4, default=str))
pyplot.style.use("seaborn-dark") pyplot.style.use("seaborn-dark")
fig, my_plot = pyplot.subplots(figsize=(size_x, size_y)) fig, my_plot = pyplot.subplots(figsize=(size_x, size_y))