prince1604 commited on
Commit ·
67a97b3
1
Parent(s): f730cd8
Added /api/status endpoint with dynamic region and latency monitoring
Browse files- api.py +10 -1
- src/monitor.py +83 -0
api.py
CHANGED
|
@@ -5,6 +5,7 @@ import os
|
|
| 5 |
import random
|
| 6 |
from src.crawler import Crawler
|
| 7 |
from src.analyzer import ImageAnalyzer
|
|
|
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
CORS(app)
|
|
@@ -13,12 +14,20 @@ REPORT_FILE = 'seo_report.json'
|
|
| 13 |
|
| 14 |
@app.route('/')
|
| 15 |
def home():
|
| 16 |
-
return "Antigravity API is Running. Use /
|
| 17 |
|
| 18 |
@app.route('/health')
|
| 19 |
def health_check():
|
| 20 |
return jsonify({"status": "alive"}), 200
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
@app.route('/api/seo-report', methods=['GET', 'POST'])
|
| 23 |
def get_seo_report():
|
| 24 |
# Cache variable attached to function to persist state
|
|
|
|
| 5 |
import random
|
| 6 |
from src.crawler import Crawler
|
| 7 |
from src.analyzer import ImageAnalyzer
|
| 8 |
+
from src.monitor import SystemMonitor
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
CORS(app)
|
|
|
|
| 14 |
|
| 15 |
@app.route('/')
|
| 16 |
def home():
|
| 17 |
+
return "Antigravity API is Running. Use /api/status for system info."
|
| 18 |
|
| 19 |
@app.route('/health')
|
| 20 |
def health_check():
|
| 21 |
return jsonify({"status": "alive"}), 200
|
| 22 |
|
| 23 |
+
@app.route('/api/status', methods=['GET'])
|
| 24 |
+
def get_system_status():
|
| 25 |
+
"""
|
| 26 |
+
Returns real-time system metadata including dynamic region and latency.
|
| 27 |
+
"""
|
| 28 |
+
stats = SystemMonitor.get_system_stats()
|
| 29 |
+
return jsonify(stats)
|
| 30 |
+
|
| 31 |
@app.route('/api/seo-report', methods=['GET', 'POST'])
|
| 32 |
def get_seo_report():
|
| 33 |
# Cache variable attached to function to persist state
|
src/monitor.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import time
|
| 3 |
+
import socket
|
| 4 |
+
|
| 5 |
+
class SystemMonitor:
|
| 6 |
+
_region_cache = None
|
| 7 |
+
_engine_version = "AutoAlt Neural v2"
|
| 8 |
+
|
| 9 |
+
@staticmethod
|
| 10 |
+
def get_region():
|
| 11 |
+
"""
|
| 12 |
+
Fetches the server's current region/city using a GeoIP service.
|
| 13 |
+
Cached for the lifetime of the application to avoid rate limits.
|
| 14 |
+
"""
|
| 15 |
+
if SystemMonitor._region_cache:
|
| 16 |
+
return SystemMonitor._region_cache
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
# high-performance, free GeoIP API
|
| 20 |
+
response = requests.get("http://ip-api.com/json", timeout=3)
|
| 21 |
+
if response.status_code == 200:
|
| 22 |
+
data = response.json()
|
| 23 |
+
# User example showed "Frankfurt", which is a city.
|
| 24 |
+
# We can combine City, Country for clarity or just City.
|
| 25 |
+
city = data.get("city", "Unknown")
|
| 26 |
+
country = data.get("countryCode", "")
|
| 27 |
+
|
| 28 |
+
# Check for cloud provider hints in ISP if needed, but City is usually what people mean by 'Region' in this context
|
| 29 |
+
region = f"{city}, {country}" if country else city
|
| 30 |
+
SystemMonitor._region_cache = region
|
| 31 |
+
return region
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Region detection failed: {e}")
|
| 34 |
+
return "Unknown (Network Restricted)"
|
| 35 |
+
|
| 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 (Google).
|
| 42 |
+
Returns latency in milliseconds (int).
|
| 43 |
+
"""
|
| 44 |
+
try:
|
| 45 |
+
# Measure reliable external connection time
|
| 46 |
+
start = time.time()
|
| 47 |
+
# Fast HEAD request to Google
|
| 48 |
+
requests.head("https://www.google.com", timeout=2)
|
| 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 (sometimes faster/allowed)
|
| 56 |
+
try:
|
| 57 |
+
start = time.time()
|
| 58 |
+
socket.create_connection(("8.8.8.8", 53), timeout=2)
|
| 59 |
+
end = time.time()
|
| 60 |
+
return int((end - start) * 1000)
|
| 61 |
+
except:
|
| 62 |
+
return 999 # High latency/timeout indicator
|
| 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 |
+
# We could add more checks (disk space, memory) if needed.
|
| 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 |
+
}
|