Spaces:
Running
Running
prince1604 commited on
Commit ·
3da7fc2
1
Parent(s): 67a97b3
Updated /api/status to accept 'domain' parameter for specific latency checks; Default checks dev.autoalt.ai
Browse files- api.py +3 -1
- src/monitor.py +22 -18
api.py
CHANGED
|
@@ -24,8 +24,10 @@ def health_check():
|
|
| 24 |
def get_system_status():
|
| 25 |
"""
|
| 26 |
Returns real-time system metadata including dynamic region and latency.
|
|
|
|
| 27 |
"""
|
| 28 |
-
|
|
|
|
| 29 |
return jsonify(stats)
|
| 30 |
|
| 31 |
@app.route('/api/seo-report', methods=['GET', 'POST'])
|
|
|
|
| 24 |
def get_system_status():
|
| 25 |
"""
|
| 26 |
Returns real-time system metadata including dynamic region and latency.
|
| 27 |
+
Accepts optional 'domain' parameter to check latency to a specific target.
|
| 28 |
"""
|
| 29 |
+
domain = request.args.get('domain')
|
| 30 |
+
stats = SystemMonitor.get_system_stats(target_url=domain)
|
| 31 |
return jsonify(stats)
|
| 32 |
|
| 33 |
@app.route('/api/seo-report', methods=['GET', 'POST'])
|
src/monitor.py
CHANGED
|
@@ -36,48 +36,52 @@ class SystemMonitor:
|
|
| 36 |
return "Unknown"
|
| 37 |
|
| 38 |
@staticmethod
|
| 39 |
-
def get_latency():
|
| 40 |
"""
|
| 41 |
-
Calculates the system's network latency by timing a request to a reliable backbone
|
| 42 |
Returns latency in milliseconds (int).
|
| 43 |
"""
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
# Measure reliable external connection time
|
| 46 |
start = time.time()
|
| 47 |
-
# Fast HEAD request
|
| 48 |
-
requests.head(
|
| 49 |
end = time.time()
|
| 50 |
|
| 51 |
# Convert to ms and round
|
| 52 |
latency_ms = int((end - start) * 1000)
|
| 53 |
return latency_ms
|
| 54 |
except Exception:
|
| 55 |
-
# Retry with a socket method if HTTP fails (
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
|
| 64 |
@staticmethod
|
| 65 |
-
def get_status():
|
| 66 |
"""
|
| 67 |
Simple self-diagnostic status.
|
| 68 |
"""
|
| 69 |
# If we can reach external network, we are operational.
|
| 70 |
-
|
| 71 |
-
latency = SystemMonitor.get_latency()
|
| 72 |
if latency < 999:
|
| 73 |
return "operational"
|
| 74 |
return "degraded_connectivity"
|
| 75 |
|
| 76 |
@staticmethod
|
| 77 |
-
def get_system_stats():
|
| 78 |
return {
|
| 79 |
"region": SystemMonitor.get_region(),
|
| 80 |
-
"latency": SystemMonitor.get_latency(),
|
| 81 |
-
"status": SystemMonitor.get_status(),
|
| 82 |
"engine": SystemMonitor._engine_version
|
| 83 |
}
|
|
|
|
| 36 |
return "Unknown"
|
| 37 |
|
| 38 |
@staticmethod
|
| 39 |
+
def get_latency(target_url=None):
|
| 40 |
"""
|
| 41 |
+
Calculates the system's network latency by timing a request to a reliable backbone or specific target.
|
| 42 |
Returns latency in milliseconds (int).
|
| 43 |
"""
|
| 44 |
+
# Default to the user's preferred check URL if no specific domain is given
|
| 45 |
+
url_to_check = target_url if target_url else "https://dev.autoalt.ai/"
|
| 46 |
+
|
| 47 |
try:
|
| 48 |
# Measure reliable external connection time
|
| 49 |
start = time.time()
|
| 50 |
+
# Fast HEAD request
|
| 51 |
+
requests.head(url_to_check, timeout=3)
|
| 52 |
end = time.time()
|
| 53 |
|
| 54 |
# Convert to ms and round
|
| 55 |
latency_ms = int((end - start) * 1000)
|
| 56 |
return latency_ms
|
| 57 |
except Exception:
|
| 58 |
+
# Retry with a socket method if HTTP fails (only for default check)
|
| 59 |
+
if not target_url:
|
| 60 |
+
try:
|
| 61 |
+
start = time.time()
|
| 62 |
+
socket.create_connection(("8.8.8.8", 53), timeout=2)
|
| 63 |
+
end = time.time()
|
| 64 |
+
return int((end - start) * 1000)
|
| 65 |
+
except:
|
| 66 |
+
return 999
|
| 67 |
+
return 999 # High latency/timeout indicator
|
| 68 |
|
| 69 |
@staticmethod
|
| 70 |
+
def get_status(target_url=None):
|
| 71 |
"""
|
| 72 |
Simple self-diagnostic status.
|
| 73 |
"""
|
| 74 |
# If we can reach external network, we are operational.
|
| 75 |
+
latency = SystemMonitor.get_latency(target_url)
|
|
|
|
| 76 |
if latency < 999:
|
| 77 |
return "operational"
|
| 78 |
return "degraded_connectivity"
|
| 79 |
|
| 80 |
@staticmethod
|
| 81 |
+
def get_system_stats(target_url=None):
|
| 82 |
return {
|
| 83 |
"region": SystemMonitor.get_region(),
|
| 84 |
+
"latency": SystemMonitor.get_latency(target_url),
|
| 85 |
+
"status": SystemMonitor.get_status(target_url),
|
| 86 |
"engine": SystemMonitor._engine_version
|
| 87 |
}
|