65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
from flask import Flask, render_template, request
|
|
import sqlite3
|
|
import pandas as pd
|
|
from config import DB_PATH
|
|
import math
|
|
|
|
app = Flask(__name__)
|
|
|
|
def load_data():
|
|
# 1) grab raw
|
|
conn = sqlite3.connect(DB_PATH)
|
|
df = pd.read_sql("SELECT * FROM speed_tests", conn)
|
|
conn.close()
|
|
|
|
# 2) parse epoch→UTC datetimes, then shift to Europe/Berlin
|
|
df['datetime'] = (
|
|
pd.to_datetime(df['timestamp'], unit='s', utc=True)
|
|
.dt.tz_convert('Europe/Berlin')
|
|
)
|
|
|
|
# 3) for your table display, format as naive strings
|
|
df['recorded_at'] = df['datetime'].dt.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
return df
|
|
|
|
|
|
def get_aggregated_data(df: pd.DataFrame, interval: str = '5min'):
|
|
|
|
# ensure 'datetime' is the index
|
|
df = df.set_index('datetime')
|
|
|
|
# 1) resample into N-minute bins, mean
|
|
agg = (
|
|
df
|
|
.resample(interval)
|
|
.agg({
|
|
'down_90th': 'mean',
|
|
'up_90th': 'mean'
|
|
})
|
|
.reset_index()
|
|
)
|
|
|
|
# 3) output for Chart.js
|
|
return {
|
|
"times": agg['datetime'].tolist(),
|
|
"down_90th": agg['down_90th'].round(2).tolist(),
|
|
"up_90th": agg['up_90th'].round(2).tolist()
|
|
}
|
|
|
|
|
|
@app.route('/')
|
|
def index():
|
|
df = load_data()
|
|
agg = request.args.get("agg", "5min")
|
|
chart_data = get_aggregated_data(df, agg)
|
|
|
|
|
|
#if math.isnan(chart_data['down_90th'][-1]) or math.isnan(chart_data['up_90th'][-1]):
|
|
#return render_template('local.html', data=df.to_dict(orient='records'), chart_data=chart_data)
|
|
|
|
return render_template('index.html', aggregation=agg, data=df.to_dict(orient='records'), chart_data=chart_data)
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5001)
|