more png
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
Wolfgang Hottgenroth 2025-03-12 16:04:34 +01:00
parent a63776fb3f
commit 92ef3e6a85

View File

@ -3,6 +3,7 @@ from loguru import logger
import json import json
import plotly.express as px import plotly.express as px
import plotly.graph_objects as po import plotly.graph_objects as po
import matplotlib.pyplot as plt
import pandas as pd import pandas as pd
import psycopg import psycopg
import sqlalchemy import sqlalchemy
@ -118,3 +119,45 @@ def ntpserver():
dbh.close() dbh.close()
def get_dataframe():
@app.route('/plot.png')
def plot_png():
dbh = psycopg.connect()
engine = sqlalchemy.create_engine("postgresql+psycopg://", creator=lambda: dbh)
query = """
select time_bucket('5 minutes', time) as bucket,
device,
avg(cast(values->'rootdisp'->>'value' as float)) as rootdisp,
max(cast(values->'stratum'->>'value' as int)) as stratum
from measurements
where time >= date_trunc('day', now()) AND time < date_trunc('day', now()) + '1 day'::interval and
application = 'TSM' and attributes->>'Label' = 'david'
group by bucket, device
order by bucket, device
"""
df = pd.read_sql(query, con=engine)
fig, ax1 = plt.subplots()
ax1.plot(df['bucket'], df['rootdisp'], 'r-', label='Root Dispersion')
ax1.set_xlabel('Time')
ax1.set_ylabel('Root Dispersion (ms)', color='r')
ax1.tick_params(axis='y', labelcolor='r')
ax2 = ax1.twinx()
ax2.plot(df['bucket'], df['stratum'], 'b-', label='Stratum')
ax2.set_ylabel('Stratum', color='b')
ax2.tick_params(axis='y', labelcolor='b')
ax2.set_yticks(range(int(df['stratum'].min()), int(df['stratum'].max()) + 1))
fig.suptitle('NTP Server Numbers')
fig.tight_layout()
img_io = io.BytesIO()
plt.savefig(img_io, format='png')
img_io.seek(0)
plt.close(fig)
return Response(img_io, mimetype='image/png')