# controllers/usage_tracking.py from flask import request, jsonify from models.schemas.usage_schema import create_usage_record, get_usage_by_user, get_usage_by_manager, \ get_usage_by_interaction_type from controllers.data_logging import log_assistant_usage # Log assistant usage by a user def log_usage(): """ Logs the usage of the assistant by a user. Expects: - user_id: The ID of the user interacting with the assistant. - manager_id: The ID of the user's manager. - interaction_type: The type of interaction (e.g., "disease_detection", "inventory_management"). - metadata: Optional. Additional details about the interaction. Returns: - JSON response with the result of the logging operation. """ try: # Extract data from the request user_id = request.json.get('user_id') manager_id = request.json.get('manager_id') interaction_type = request.json.get('interaction_type') metadata = request.json.get('metadata') if not user_id or not manager_id or not interaction_type: return jsonify({"error": "Missing required fields"}), 400 # Log assistant usage usage_record_id = create_usage_record(user_id, manager_id, interaction_type, metadata) # Log the usage in the system log log_assistant_usage(user_id, manager_id, interaction_type, 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 # Get usage records by user def get_usage_for_user(user_id): """ Retrieves all assistant usage records for a specific user. Expects: - user_id: The ID of the user. Returns: - JSON response with the list of usage records for the user. """ try: usage_records = get_usage_by_user(user_id) if not usage_records: return jsonify({"message": "No usage records found for this user"}), 404 return jsonify(usage_records), 200 except Exception as e: return jsonify({"error": str(e)}), 500 # Get usage records for all users managed by a specific manager def get_usage_for_manager(manager_id): """ Retrieves all assistant usage records for users managed by a specific manager. Expects: - manager_id: The ID of the manager. Returns: - JSON response with the list of usage records for users under the manager. """ try: usage_records = get_usage_by_manager(manager_id) if not usage_records: return jsonify({"message": "No usage records found for this manager's users"}), 404 return jsonify(usage_records), 200 except Exception as e: return jsonify({"error": str(e)}), 500 # Get usage records by interaction type def get_usage_by_type(interaction_type): """ Retrieves all assistant usage records for a specific interaction type. Expects: - interaction_type: The type of interaction (e.g., "disease_detection", "inventory_management"). Returns: - JSON response with the list of usage records for the specified interaction type. """ try: usage_records = get_usage_by_interaction_type(interaction_type) if not usage_records: return jsonify({"message": f"No usage records found for interaction type '{interaction_type}'"}), 404 return jsonify(usage_records), 200 except Exception as e: return jsonify({"error": str(e)}), 500