welcome ghaymah
هذا الالتزام موجود في:
17
q1-deploy-monitor/Dockerfile
Normal file
17
q1-deploy-monitor/Dockerfile
Normal file
@@ -0,0 +1,17 @@
|
||||
# Use an official lightweight Python image
|
||||
FROM python:3.9-slim
|
||||
|
||||
# Set the working directory inside the container
|
||||
WORKDIR /app
|
||||
|
||||
# Install required dependencies
|
||||
RUN pip install --no-cache-dir fastapi uvicorn
|
||||
|
||||
# Copy all project files into the container
|
||||
COPY . /app
|
||||
|
||||
# Expose the application port
|
||||
EXPOSE 8000
|
||||
|
||||
# Command to run the application using Uvicorn server
|
||||
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
31
q1-deploy-monitor/app.py
Normal file
31
q1-deploy-monitor/app.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import time
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# Global counter to track total requests
|
||||
request_count = 0
|
||||
|
||||
@app.middleware("http")
|
||||
async def count_requests(request: Request, call_next):
|
||||
global request_count
|
||||
request_count += 1
|
||||
start_time = time.time()
|
||||
|
||||
response = await call_next(request)
|
||||
|
||||
process_time = (time.time() - start_time) * 1000
|
||||
response.headers["X-Process-Time"] = f"{process_time:.2f}ms"
|
||||
return response
|
||||
|
||||
# 3. Health Endpoint required by the task
|
||||
@app.get("/health")
|
||||
def health_check():
|
||||
return {"status": "healthy", "requests": request_count}
|
||||
|
||||
# 5. Dashboard Endpoint serving the HTML file
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
def get_dashboard():
|
||||
with open("dashboard.html", "r", encoding="utf-8") as f:
|
||||
return f.read()
|
||||
53
q1-deploy-monitor/dashboard.html
Normal file
53
q1-deploy-monitor/dashboard.html
Normal file
@@ -0,0 +1,53 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Ghaymah App Dashboard</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; background: #0f172a; color: #fff; text-align: center; padding-top: 50px; }
|
||||
.card { background: #1e293b; padding: 20px; margin: 15px auto; width: 320px; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.3); }
|
||||
h3 { color: #38bdf8; margin-bottom: 5px; }
|
||||
p { font-size: 20px; font-weight: bold; margin: 0; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Application Monitoring Dashboard</h1>
|
||||
|
||||
<div class="card">
|
||||
<h3>Status</h3>
|
||||
<p id="status" style="color: #4ade80;">Loading...</p>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h3>Total Requests</h3>
|
||||
<p id="count">0</p>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h3>Response Time</h3>
|
||||
<p id="time">0 ms</p>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
async function fetchMetrics() {
|
||||
const start = performance.now();
|
||||
try {
|
||||
const response = await fetch('/health');
|
||||
const end = performance.now();
|
||||
const data = await response.json();
|
||||
|
||||
document.getElementById('status').innerText = data.status;
|
||||
document.getElementById('count').innerText = data.requests;
|
||||
document.getElementById('time').innerText = (end - start).toFixed(2) + ' ms';
|
||||
} catch (error) {
|
||||
document.getElementById('status').innerText = 'Down';
|
||||
document.getElementById('status').style.color = '#f87171';
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch metrics every 30 seconds as required
|
||||
setInterval(fetchMetrics, 30000);
|
||||
fetchMetrics();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
20
q1-deploy-monitor/health-check.sh
Normal file
20
q1-deploy-monitor/health-check.sh
Normal file
@@ -0,0 +1,20 @@
|
||||
#!/bin/bash
|
||||
|
||||
URL="http://localhost:8000/health"
|
||||
|
||||
echo "Starting application monitoring. Checking every 30 seconds..."
|
||||
|
||||
while true; do
|
||||
# Send HTTP GET request and get the status code
|
||||
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" $URL)
|
||||
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
|
||||
if [ "$RESPONSE" -eq 200 ]; then
|
||||
echo "[$TIMESTAMP] Status: HEALTHY (HTTP 200)"
|
||||
else
|
||||
echo "[$TIMESTAMP] Status: DOWN (HTTP $RESPONSE)"
|
||||
fi
|
||||
|
||||
# Wait for 30 seconds before the next check
|
||||
sleep 30
|
||||
done
|
||||
المرجع في مشكلة جديدة
حظر مستخدم