api update

This commit is contained in:
Mateusz Gruszczyński
2026-02-25 10:02:37 +01:00
parent 9ccb1651b6
commit 1d8071966b
3 changed files with 257 additions and 69 deletions

111
api.py
View File

@@ -139,7 +139,7 @@ def get_countries():
'countries': config.COMMON_COUNTRIES
})
@api_blueprint.route('/api/cache/status', methods=['GET'])
@api_blueprint.route('/api/cache/redis/status', methods=['GET'])
def cache_status():
if not redis_cache:
return jsonify({
@@ -148,19 +148,35 @@ def cache_status():
'message': 'Redis cache is not enabled'
})
health = None
try:
health = redis_cache.health_check()
country_keys_count = 0
config_keys_count = 0
total_size_bytes = 0
except Exception as health_error:
print(f"[REDIS] Health check failed: {health_error}", flush=True)
health = {'connected': False, 'status': 'disconnected', 'error': str(health_error)}
country_keys_count = 0
config_keys_count = 0
total_size_bytes = 0
patterns = [
("geoban:country:*", "country"),
("geoip:config:*", "config"),
("geoban:config:*", "config")
]
for pattern, key_type in patterns:
try:
pattern_country = "geoban:country:*"
cursor = 0
while True:
cursor, keys = redis_cache.redis_client.scan(cursor, match=pattern_country, count=1000)
country_keys_count += len(keys)
cursor, keys = redis_cache.redis_client.scan(cursor, match=pattern, count=1000)
key_count = len(keys)
if key_type == "country":
country_keys_count += key_count
else:
config_keys_count += key_count
for key in keys:
try:
size = redis_cache.redis_client.memory_usage(key)
@@ -168,65 +184,26 @@ def cache_status():
total_size_bytes += size
except:
pass
if cursor == 0:
break
pattern_config = "geoip:config:*"
cursor = 0
while True:
cursor, keys = redis_cache.redis_client.scan(cursor, match=pattern_config, count=1000)
config_keys_count += len(keys)
for key in keys:
try:
size = redis_cache.redis_client.memory_usage(key)
if size:
total_size_bytes += size
except:
pass
if cursor == 0:
break
pattern_config_new = "geoban:config:*"
cursor = 0
while True:
cursor, keys = redis_cache.redis_client.scan(cursor, match=pattern_config_new, count=1000)
config_keys_count += len(keys)
for key in keys:
try:
size = redis_cache.redis_client.memory_usage(key)
if size:
total_size_bytes += size
except:
pass
if cursor == 0:
break
except Exception as e:
print(f"[REDIS] Error counting keys: {e}", flush=True)
import traceback
traceback.print_exc()
return jsonify({
'success': True,
'enabled': True,
'health': health,
'stats': {
'country_keys': country_keys_count,
'config_keys': config_keys_count,
'total_keys': country_keys_count + config_keys_count,
'total_size_mb': round(total_size_bytes / 1024 / 1024, 2),
'memory_used_mb': health.get('memory_used_mb', 0),
'total_keys_in_db': health.get('keys', 0)
}
})
except Exception as e:
import traceback
traceback.print_exc()
return jsonify({
'success': False,
'enabled': True,
'error': str(e)
}), 500
except Exception as pattern_error:
print(f"[REDIS] Pattern '{pattern}' failed: {pattern_error}", flush=True)
continue
return jsonify({
'success': True,
'enabled': True,
'health': health,
'stats': {
'country_keys': country_keys_count,
'config_keys': config_keys_count,
'total_keys': country_keys_count + config_keys_count,
'total_size_mb': round(total_size_bytes / 1024 / 1024, 2),
'memory_used_mb': health.get('memory_used_mb', 0) if isinstance(health, dict) else 0,
'total_keys_in_db': health.get('keys', 0) if isinstance(health, dict) else 0
}
})
@api_blueprint.route('/api/cache/flush', methods=['POST'])
def cache_flush():
@@ -700,7 +677,7 @@ def generate_config():
clear_progress()
return jsonify({'success': False, 'error': str(e)}), 500
@api_blueprint.route('/api/database/sqlite/status', methods=['GET'])
@api_blueprint.route('/api/cache/sqlite/status', methods=['GET'])
def sqlite_status():
"""Get SQLite cache database statistics"""
db_path = config.GEOIP_DB_DIR / 'networks_cache.db'