"""Application Logic for tool_log.""" from datetime import datetime from prs.constants import tools from prs.constants import error from prs.models import collection_society from prs.models import tool_log from prs.utils import shared_utils def initialize_tool(tool_name, tool_param_data): """Initialize the tool_log. Args: tool_name (String): Name of the tool for which we need to initialize the tool log. tool_param_data (Dict): dict of params to be logged in tool_log. Returns: Response: Response containing the log id, which allows all tool updates per session to be written to the same log row. """ country_id = tool_param_data.get('country_id') society_id = tools.WORLDWIDE_SOCIETY_ID comment = tool_param_data.get('manual_label') label_ids = tool_param_data.get('eligible_vendor_list') if country_id and country_id != tools.WORLDWIDE_COUNTRY_ID: collection_society_data = collection_society.\ get_society_id_by_country_id(country_id) if not collection_society_data: return collection_society_data society_id = collection_society_data.message['society_id'] log_values = { 'tool_name': tool_name, 'society_id': society_id, 'time_start': datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'input_text': str(label_ids), 'tool_switches': str(tool_param_data), 'status': -1, 'comment': comment, 'progress': 'Initializing', 'user_ip': '127.0.0.1' } return tool_log.initialize_tool_log(log_values) def complete_tool(log_values, progress_message, tool_log_status): """Finish the logging for a single tool_log run. Args: log_values (dict): Contains the log data that needs to be updated in complete_tool_failure. progress_message (String): Progress message for failure. tool_log_status (String): Complete tool with success or failure message. """ log_values['progress'] = progress_message log_values['status'] = 1 if tool_log_status != error.TOOL_SHUTDOWN_SUCCESS_STATUS: log_values['num_rows'] = 0 log_values['status'] = 2 log_values['return_message'] = progress_message log_values['time_end'] = shared_utils.get_current_datetime( '%Y-%m-%d %H:%M:%S') tool_log.update_tool_log(log_values)