diff --git a/src/app.py b/src/app.py index c800fc8..7776651 100644 --- a/src/app.py +++ b/src/app.py @@ -36,6 +36,7 @@ def read_log(log_file, lines): log_file = replace_placeholders(log_file) return subprocess.check_output(['tail', '-n', str(lines), log_file]).decode('utf-8') + @app.route('/get_logs', methods=['GET']) def get_logs(): tags = request.args.getlist("tags") # Get tags in the order they appear in the query @@ -54,7 +55,7 @@ def get_logs(): config["tags"] = [tag for tag in config["tags"] if tag["tag"] != custom_tag["tag"]] config["tags"].append(custom_tag) - response_list = [] # Use a list to store the responses in order + response_text = "" # Initialize an empty string for the plain text response tags_found = False # Process each tag in the given order @@ -63,37 +64,39 @@ def get_logs(): # Search for the tag in commands and logs tag_data = next((item for item in config["tags"] if item["tag"] == tag), None) if not tag_data: - response_list.append({tag: "Tag not found"}) + response_text += f"Tag '{tag}' not found.\n\n" continue tags_found = True # At least one valid tag is found # Determine if the tag is a command or a log + response_text += f"===== {tag.upper()} =====\n" if tag_data["type"] == "command": - response_list.append({tag: f"```\n{os.popen(tag_data['content']).read()}\n```"}) + response_text += f"{os.popen(tag_data['content']).read()}\n" elif tag_data["type"] == "log": log_file = tag_data["content"] # Replace any placeholders in the log file path log_file = replace_placeholders(log_file) if "*" in log_file: # Handle wildcard in log file path - response_list.append({tag: f"```\n{read_log_from_dir(*split_directory_and_file(log_file), tag_data['lines'])}\n```"}) + response_text += f"{read_log_from_dir(*split_directory_and_file(log_file), tag_data['lines'])}\n" else: # Regular log file - response_list.append({tag: f"```\n{read_log(log_file, tag_data['lines'])}\n```"}) + response_text += f"{read_log(log_file, tag_data['lines'])}\n" + response_text += "\n" # Add spacing between entries except Exception as e: # Log the error and proceed - response_list.append({tag: f"Error: {str(e)}"}) + response_text += f"Error for tag '{tag}': {str(e)}\n\n" # If no tags were found, return HTTP 400 with an error message if not tags_found: - return jsonify({"error": "None of the requested tags were found."}), 400 + return "None of the requested tags were found.", 400 except Exception as e: - return jsonify({"error": str(e)}), 500 + return f"Error: {str(e)}", 500 - # Return the response as a list to guarantee order - return jsonify(response_list) + # Return the plain text response + return response_text, 200, {'Content-Type': 'text/plain'} if __name__ == '__main__': port = int(os.environ.get("TICKET_AI_PORT", 5000)) # Default to 5000 if no environment variable is set