From a110fe6a6e0b36c30254e9a638dadffda760046b Mon Sep 17 00:00:00 2001 From: ewinderickx Date: Tue, 17 Mar 2026 16:23:51 +0000 Subject: [PATCH] Webapp toegevoegd --- app.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 app.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..eb0d211 --- /dev/null +++ b/app.py @@ -0,0 +1,55 @@ +from flask import Flask +import time, random, threading + +app = Flask(__name__) + +# --- Metrics (thread-safe counters) --- +lock = threading.Lock() +metrics = { + "requests_total": 0, + "errors_total": 0, + "duration_sum": 0.0, + "duration_count": 0, +} + +@app.route('/') +def home(): + start = time.time() + + # Simuleer latency + time.sleep(random.uniform(0.1, 2.0)) + + # Simuleer 10% errors + is_error = random.random() < 0.1 + duration = time.time() - start + + with lock: + metrics["requests_total"] += 1 + metrics["duration_sum"] += duration + metrics["duration_count"] += 1 + if is_error: + metrics["errors_total"] += 1 + + if is_error: + return "Oeps!", 500 + return "Welkom bij de shop!" + +@app.route('/metrics') +def get_metrics(): + with lock: + avg_duration = ( + metrics["duration_sum"] / metrics["duration_count"] + if metrics["duration_count"] > 0 else 0 + ) + error_rate = ( + metrics["errors_total"] / metrics["requests_total"] * 100 + if metrics["requests_total"] > 0 else 0 + ) + return { + "requests_total": metrics["requests_total"], + "errors_total": metrics["errors_total"], + "error_rate_pct": round(error_rate, 2), + "avg_duration_sec": round(avg_duration, 3), + } + +app.run(host='0.0.0.0', port=5000) \ No newline at end of file