File size: 5,513 Bytes
c514c85
 
6513666
 
 
 
 
 
 
c514c85
6513666
 
 
 
 
 
 
 
 
 
 
c514c85
6513666
 
 
 
 
 
c514c85
6513666
 
 
 
 
 
 
 
 
 
 
 
 
 
c514c85
6513666
 
 
 
 
 
 
c514c85
6513666
 
 
 
 
 
 
 
 
 
 
 
 
 
c514c85
6513666
 
 
 
 
 
 
c514c85
6513666
 
 
 
 
 
 
 
 
 
 
 
c514c85
6513666
 
 
 
 
 
 
 
 
c514c85
6513666
 
 
 
 
 
 
 
 
 
 
 
c514c85
6513666
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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