diff --git a/api.py b/api.py
index a0b8273..9f06a33 100644
--- a/api.py
+++ b/api.py
@@ -15,6 +15,11 @@ from datetime import datetime
from db import Database
from db.users import get_priority_users, get_priority_user_matches, get_priority_user
+# central API config
+import requests
+CENTRAL_API = os.environ.get('CONNECTD_CENTRAL_API', '')
+CENTRAL_KEY = os.environ.get('CONNECTD_API_KEY', '')
+
API_PORT = int(os.environ.get('CONNECTD_API_PORT', 8099))
# shared state (updated by daemon)
@@ -177,13 +182,29 @@ async function loadStats() {
$('status').innerHTML = 'daemon ' + (h.running ? 'ON' : 'OFF') + ' | ' + uptime + ' | ' + h.intros_today + ' today';
+ var centralHtml = '';
+ if (s.central && !s.central.error) {
+ centralHtml = '
' +
+ '
// central api
' +
+ '
' +
+ '
' + s.central.total_humans + 'humans
' +
+ '
' + s.central.total_matches.toLocaleString() + 'matches
' +
+ '
' + s.central.lost_builders + 'lost
' +
+ '
' + s.central.intros_sent + 'sent
' +
+ '
' + s.central.active_instances + 'instances
' +
+ '
';
+ }
+
$('stats').innerHTML =
+ '// local
' +
+ '' +
'
' + s.total_humans + 'humans
' +
'
' + s.total_matches + 'matches
' +
'
' + h.score_90_plus + '90+
' +
'
' + h.score_80_89 + '80+
' +
'
' + h.matches_pending + 'queue
' +
- '
' + s.sent_intros + 'sent
';
+ '
' + s.sent_intros + 'sent
' +
+ '
' + centralHtml;
}
async function loadHost() {
@@ -1028,11 +1049,30 @@ class APIHandler(BaseHTTPRequestHandler):
self._send_json({'error': str(e)}, 500)
def _handle_stats(self):
- """return database statistics"""
+ """return database statistics (local + central)"""
try:
db = Database()
stats = db.stats()
db.close()
+
+ # add central API stats if configured
+ if CENTRAL_API and CENTRAL_KEY:
+ try:
+ headers = {'X-API-Key': CENTRAL_KEY}
+ resp = requests.get(f'{CENTRAL_API}/stats', headers=headers, timeout=5)
+ if resp.status_code == 200:
+ central = resp.json()
+ stats['central'] = {
+ 'total_humans': central.get('total_humans', 0),
+ 'lost_builders': central.get('lost_builders', 0),
+ 'builders': central.get('builders', 0),
+ 'total_matches': central.get('total_matches', 0),
+ 'intros_sent': central.get('intros_sent', 0),
+ 'active_instances': central.get('active_instances', 0),
+ }
+ except Exception as ce:
+ stats['central'] = {'error': str(ce)}
+
self._send_json(stats)
except Exception as e:
self._send_json({'error': str(e)}, 500)