updated speed logger

This commit is contained in:
2025-05-13 02:27:03 +02:00
parent 88b3439822
commit 5081df993f
10 changed files with 233 additions and 73 deletions

65
app.py
View File

@@ -1,38 +1,67 @@
from flask import Flask, render_template
from flask import Flask, render_template, request
import sqlite3
import pandas as pd
from config import DB_PATH
from datetime import datetime
import math
app = Flask(__name__)
def load_data():
# Connect to SQLite database
# 1) grab raw
conn = sqlite3.connect(DB_PATH)
query = "SELECT * FROM speed_tests"
df = pd.read_sql(query, conn)
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['recorded_at'].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)
print(f"down last 10: {chart_data['down_90th'][-10:]}")
print(f"down last 10: {chart_data['up_90th'][-10:]}")
print(math.isnan(chart_data['down_90th'][-1]) or math.isnan(chart_data['up_90th'][-1]))
# Convert timestamps to human-readable format
df['datetime'] = pd.to_datetime(df['timestamp'], unit='s')
# Suppose your DataFrame is called `df`
df["timestamp"] = df["timestamp"].apply(lambda ts: datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S"))
#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)
# Collect the data for charts
chart_data = {
"times": df['datetime'].dt.strftime('%Y-%m-%d %H:%M:%S').tolist(),
"down_90th": df['down_90th'].tolist(),
"up_90th": df['up_90th'].tolist()
}
return render_template('index.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(port=5001)
app.run(host='0.0.0.0', port=5001)