feat: posts_per_day and comments_per_day endpoints in Flask

This commit is contained in:
2026-01-27 13:20:51 +00:00
parent 2482c1da1c
commit 8394673b3c

View File

@@ -7,6 +7,10 @@ app = Flask(__name__)
# Allow for CORS from localhost:5173 # Allow for CORS from localhost:5173
CORS(app, resources={r"/*": {"origins": "http://localhost:5173"}}) CORS(app, resources={r"/*": {"origins": "http://localhost:5173"}})
# Global State
posts_df = None
comments_df = None
@app.route('/upload', methods=['POST']) @app.route('/upload', methods=['POST'])
def upload_data(): def upload_data():
if "posts" not in request.files or "comments" not in request.files: if "posts" not in request.files or "comments" not in request.files:
@@ -22,6 +26,7 @@ def upload_data():
return jsonify({"error": "Invalid file type. Only .jsonl files are allowed."}), 400 return jsonify({"error": "Invalid file type. Only .jsonl files are allowed."}), 400
try: try:
global posts_df, comments_df
posts_df = pd.read_json(post_file, lines=True) posts_df = pd.read_json(post_file, lines=True)
comments_df = pd.read_json(comment_file, lines=True) comments_df = pd.read_json(comment_file, lines=True)
except ValueError as e: except ValueError as e:
@@ -31,5 +36,45 @@ def upload_data():
return jsonify({"message": "File uploaded successfully", "posts_count": len(posts_df), "comments_count": len(comments_df)}), 200 return jsonify({"message": "File uploaded successfully", "posts_count": len(posts_df), "comments_count": len(comments_df)}), 200
@app.route('/stats/posts_per_day', methods=['GET'])
def posts_per_day():
if posts_df is None:
return jsonify({"error": "No data uploaded"}), 400
try:
posts_df['date'] = pd.to_datetime(posts_df['timestamp'], unit='s').dt.date
posts_per_day = (
posts_df
.groupby('date')
.size()
.reset_index(name='posts_count')
)
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
return jsonify(posts_per_day.to_dict(orient='records')), 200
@app.route('/stats/comments_per_day', methods=['GET'])
def comments_per_day():
if comments_df is None:
return jsonify({"error": "No data uploaded"}), 400
try:
comments_df['date'] = pd.to_datetime(comments_df['timestamp'], unit='s').dt.date
comments_per_day = (
comments_df
.groupby('date')
.size()
.reset_index(name='comments_count')
)
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
return jsonify(comments_per_day.to_dict(orient='records')), 200
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True) app.run(debug=True)