From 2789bbeb14c4cae244d3b0281a63c883648b1d86 Mon Sep 17 00:00:00 2001 From: Dylan De Faoite Date: Sat, 31 Jan 2026 15:46:53 +0000 Subject: [PATCH] feat: add summary endpoint --- server/app.py | 16 +++++++++++++++- server/stat_gen.py | 18 ++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/server/app.py b/server/app.py index b579bb7..580396a 100644 --- a/server/app.py +++ b/server/app.py @@ -10,7 +10,9 @@ app = Flask(__name__) CORS(app, resources={r"/*": {"origins": "http://localhost:5173"}}) # Global State -stat_obj = None +posts_df = pd.read_json('posts.jsonl', lines=True) +comments_df = pd.read_json('comments.jsonl', lines=True) +stat_obj = StatGen(posts_df, comments_df) @app.route('/upload', methods=['POST']) def upload_data(): @@ -89,6 +91,18 @@ def search_dataset(): query = data["query"] return jsonify(stat_obj.filter_events(query).to_dict(orient='records')), 200 +@app.route('/stats/summary', methods=["GET"]) +def get_summary(): + if stat_obj is None: + return jsonify({"error": "No data uploaded"}), 400 + + try: + return jsonify(stat_obj.get_summary()), 200 + except ValueError as e: + 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('/reset', methods=["GET"]) def reset_dataset(): if stat_obj is None: diff --git a/server/stat_gen.py b/server/stat_gen.py index 2280174..738c5d3 100644 --- a/server/stat_gen.py +++ b/server/stat_gen.py @@ -88,7 +88,7 @@ class StatGen: self.df .groupby('date') .size() - .reset_index(name='posts_count') + .reset_index(name='event_count') ) def filter_events(self, search_query: str) -> pd.DataFrame: @@ -96,4 +96,18 @@ class StatGen: return self.df def reset_dataset(self) -> None: - self.df = self.original_df.copy(deep=True) \ No newline at end of file + self.df = self.original_df.copy(deep=True) + + def get_summary(self) -> dict: + return { + "total_events": int(len(self.df)), + "total_posts": int((self.df["type"] == "post").sum()), + "total_comments": int((self.df["type"] == "comment").sum()), + "unique_users": int(self.df["author"].nunique()), + "time_range": { + "start": int(self.df["dt"].min().timestamp()), + "end": int(self.df["dt"].max().timestamp()) + } + } + +