Jacob Schmidt aa144c1349 Fix: Improve data handling and error logging
This commit improves the application's robustness by enhancing data handling and adding error logging.

Specifically, it:
- Updates the project version to 1.0.4 in `pyproject.toml` and `setup.py`.
- Removes redis from requirements.txt
- Improves data parsing in `KeyService.parse_hash_data` to handle None values and simplify the logic.
- Adds error logging to `KeyService.parse_hash_data` and `KeyService.process_list_values` to catch and log parsing errors.
- Modifies `KeyService.process_list_values` to ensure the return value is always a list.
- Adds try-except blocks in `get_keys` and `get_key_value` routes to handle potential exceptions during key processing and log warnings.
2025-04-13 16:48:32 -05:00

127 lines
4.3 KiB
Python

from flask import Blueprint, render_template, jsonify, request
from .database import DatabaseClient
from .services import KeyService
from .logger import logger
routes = Blueprint('routes', __name__)
@routes.route('/')
def index():
return render_template('index.html')
@routes.route('/api/keys', methods=['GET'])
def get_keys():
logger.debug("API endpoint /api/keys called")
try:
client = DatabaseClient.create_client()
if not client:
return jsonify({
'success': False,
'error': 'Could not connect to Firefly database',
'connection_status': False
})
pattern = request.args.get('pattern', '*')
keys = client.string_ops.keys(pattern)
if not keys:
return jsonify({
'success': True,
'data': {'strings': [], 'lists': [], 'hashes': []},
'connection_status': True
})
strings = []
lists = []
hashes = []
for key in keys:
key_type = DatabaseClient.get_key_type(client, key)
try:
if key_type == "string":
value = client.string_ops.string_get(key)
if value is not None:
strings.append({"key": key, "value": value})
elif key_type == "list":
list_values = client.list_ops.list_range(key, 0, -1)
if list_values is not None:
processed_value = KeyService.process_list_values(list_values)
lists.append({"key": key, "value": processed_value})
elif key_type == "hash":
hash_values = client.hash_ops.hash_get_all(key)
if hash_values is not None:
processed_value = KeyService.parse_hash_data(hash_values)
hashes.append({"key": key, "value": processed_value})
except Exception as e:
logger.warning(f"Error processing key {key}: {e}")
return jsonify({
'success': True,
'data': {
'strings': strings,
'lists': lists,
'hashes': hashes
},
'connection_status': True
})
except Exception as e:
logger.error(f"Error getting keys: {e}")
return jsonify({
'success': False,
'error': str(e),
'connection_status': False
})
@routes.route('/api/key/<key>', methods=['GET'])
def get_key_value(key):
try:
client = DatabaseClient.create_client()
if not client:
return jsonify({
'success': False,
'error': 'Could not connect to Firefly database',
'connection_status': False
})
key_type = DatabaseClient.get_key_type(client, key)
if not key_type:
return jsonify({
'success': False,
'error': 'Key not found or type not determined',
'connection_status': True
}), 404
value = None
try:
if key_type == "string":
value = client.string_ops.string_get(key)
elif key_type == "hash":
hash_values = client.hash_ops.hash_get_all(key)
value = KeyService.parse_hash_data(hash_values)
elif key_type == "list":
list_values = client.list_ops.list_range(key, 0, -1)
value = KeyService.process_list_values(list_values)
except Exception as e:
logger.warning(f"Error processing key {key}: {e}")
return jsonify({
'success': False,
'error': f'Error processing key: {str(e)}',
'connection_status': True
}), 500
return jsonify({
'success': True,
'data': {
'key': key,
'type': key_type,
'value': value
},
'connection_status': True
})
except Exception as e:
logger.error(f"Error getting key value: {e}")
return jsonify({
'success': False,
'error': str(e),
'connection_status': False
})