Webapp toegevoegd
This commit is contained in:
55
app.py
Normal file
55
app.py
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user