Emmanuel Frimpong Asante
"Update system"
c514c85
raw
history blame
5.51 kB
# controllers/data_logging.py
from flask import request, jsonify
from models.schemas.log_schema import create_log
from models.schemas.health_schema import create_health_record
from models.schemas.inventory_schema import create_inventory_item, update_inventory_item
from models.schemas.usage_schema import create_usage_record
# Log a general user action (e.g., a user performs an action in the system)
def log_user_action(user_id, message, level="INFO", metadata=None):
"""
Logs a general user action.
:param user_id: The ID of the user performing the action.
:param message: Description of the action.
:param level: Log level (default is "INFO").
:param metadata: Additional information about the action.
:return: JSON response indicating success or failure.
"""
try:
ip_address = request.remote_addr # Get the user's IP address from the request
log_id = create_log(level, message, metadata, ip_address, user_id)
return jsonify({"message": "Action logged successfully", "log_id": log_id}), 201
except Exception as e:
return jsonify({"error": str(e)}), 500
# Log a health record for a poultry bird
def log_health_record(user_id, poultry_id, disease_name, status, recommendation, image_path=None):
"""
Logs a health record for a poultry bird.
:param user_id: The ID of the user managing the poultry.
:param poultry_id: The unique identifier for the poultry bird.
:param disease_name: The name of the detected disease.
:param status: The health status of the bird (e.g., "Critical", "No issue").
:param recommendation: The recommended treatment or action.
:param image_path: Optional. The path to an image related to the health record.
:return: JSON response indicating success or failure.
"""
try:
record_id = create_health_record(user_id, poultry_id, disease_name, status, recommendation, image_path)
# Log the health record creation action
log_message = f"Health record created for poultry ID {poultry_id} - Disease: {disease_name}"
log_user_action(user_id, log_message, "INFO")
return jsonify({"message": "Health record logged successfully", "record_id": record_id}), 201
except Exception as e:
return jsonify({"error": str(e)}), 500
# Log an addition to the inventory
def log_inventory_addition(user_id, item_name, category, quantity, unit, description=None):
"""
Logs an inventory addition (e.g., adding feed, medicine).
:param user_id: The ID of the user managing the inventory.
:param item_name: The name of the inventory item.
:param category: The category of the item (e.g., "Feed", "Medicine").
:param quantity: The quantity added.
:param unit: The unit of measurement (e.g., "kg", "liters").
:param description: Optional. Additional notes or description of the item.
:return: JSON response indicating success or failure.
"""
try:
item_id = create_inventory_item(item_name, category, quantity, unit, user_id, description=description)
# Log the inventory addition action
log_message = f"Inventory item added: {item_name}, Quantity: {quantity} {unit}"
log_user_action(user_id, log_message, "INFO")
return jsonify({"message": "Inventory item logged successfully", "item_id": item_id}), 201
except Exception as e:
return jsonify({"error": str(e)}), 500
# Log updates to an inventory item (e.g., usage or quantity adjustment)
def log_inventory_update(user_id, item_id, updated_fields):
"""
Logs an update to an inventory item (e.g., when quantity is used).
:param user_id: The ID of the user managing the inventory.
:param item_id: The ID of the inventory item being updated.
:param updated_fields: The fields to be updated (e.g., "used_quantity", "quantity").
:return: JSON response indicating success or failure.
"""
try:
update_success = update_inventory_item(item_id, updated_fields)
if update_success:
# Log the inventory update action
log_message = f"Inventory item updated: {item_id} - {updated_fields}"
log_user_action(user_id, log_message, "INFO")
return jsonify({"message": "Inventory item updated successfully"}), 200
else:
return jsonify({"message": "Inventory item update failed"}), 400
except Exception as e:
return jsonify({"error": str(e)}), 500
# Log assistant usage by a user
def log_assistant_usage(user_id, manager_id, interaction_type, metadata=None):
"""
Logs the usage of the assistant by a user.
:param user_id: The ID of the user interacting with the assistant.
:param manager_id: The ID of the manager overseeing the user.
:param interaction_type: The type of interaction (e.g., "disease_detection", "inventory_management").
:param metadata: Optional. Additional details about the interaction.
:return: JSON response indicating success or failure.
"""
try:
usage_record_id = create_usage_record(user_id, manager_id, interaction_type, metadata)
# Log the assistant usage action
log_message = f"Assistant interaction logged: {interaction_type} by user {user_id}"
log_user_action(user_id, log_message, "INFO", metadata)
return jsonify({"message": "Assistant usage logged successfully", "usage_record_id": usage_record_id}), 201
except Exception as e:
return jsonify({"error": str(e)}), 500