feat: add grouped time analysis endpoint

This commit is contained in:
2026-01-31 16:46:15 +00:00
parent 70cd4393a5
commit 657bd37cdd
2 changed files with 33 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ from flask_cors import CORS
from server.stat_gen import StatGen
import pandas as pd
import traceback
app = Flask(__name__)
@@ -102,6 +103,19 @@ def get_summary():
return jsonify({"error": f"Malformed or missing data: {str(e)}"}), 400
except Exception as e:
return jsonify({"error": f"An unexpected error occurred: {str(e)}"}), 500
@app.route("/stats/time", methods=["GET"])
def get_time_analysis():
if stat_obj is None:
return jsonify({"error": "No data uploaded"}), 400
try:
return jsonify(stat_obj.time_analysis()), 200
except ValueError as e:
return jsonify({"error": f"Malformed or missing data: {str(e)}"}), 400
except Exception as e:
print(traceback.format_exc())
return jsonify({"error": f"An unexpected error occurred: {str(e)}"}), 500
@app.route('/reset', methods=["GET"])
def reset_dataset():

View File

@@ -38,25 +38,35 @@ class StatGen:
df["weekday"] = df["dt"].dt.day_name()
## Public
def get_heatmap(self) -> pd.DataFrame:
def time_analysis(self) -> pd.DataFrame:
per_day = (
self.df.groupby("date")
.size()
.reset_index(name="count")
)
weekday_order = [
"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"
]
self.df["weekday"] = pd.Categorical(
self.df["weekday"],
categories=weekday_order,
ordered=True
)
return (
heatmap = (
self.df
.groupby(["weekday", "hour"], observed=True)
.assign(weekday=pd.Categorical(self.df["weekday"], weekday_order, ordered=True))
.groupby(["weekday", "hour"])
.size()
.unstack(fill_value=0)
.reindex(columns=range(24), fill_value=0)
)
heatmap.columns = heatmap.columns.map(str)
burst_index = per_day["count"].std() / max(per_day["count"].mean(), 1)
return {
"events_per_day": per_day.to_dict(orient="records"),
"weekday_hour_heatmap": heatmap.reset_index().to_dict(orient="records"),
"burstiness": round(burst_index, 2)
}
def get_word_frequencies(self, limit: int = 100) -> pd.DataFrame:
texts = (