From 2c957a64a29e6d584760314d82c74a95cc2a9710 Mon Sep 17 00:00:00 2001 From: edo-neo Date: Mon, 2 Dec 2024 12:24:02 +0100 Subject: [PATCH] Log management ok --- src/components/archive_synchronizer.py | 61 +++++++++++++++++--------- src/lib/db/models/log.py | 4 +- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/components/archive_synchronizer.py b/src/components/archive_synchronizer.py index 6e22717..03c5fde 100644 --- a/src/components/archive_synchronizer.py +++ b/src/components/archive_synchronizer.py @@ -4,6 +4,7 @@ import re import sys import threading import time +from datetime import datetime from pathlib import Path import requests @@ -12,8 +13,8 @@ import requests from google.api_core.exceptions import Forbidden from google.cloud import storage from requests import JSONDecodeError - from lib.db import Archive, db +from lib.db.models import Log from PyQt5.QtCore import QThread from requests.adapters import HTTPAdapter, Retry from urllib3.exceptions import InsecureRequestWarning @@ -187,11 +188,11 @@ class ArchiveSynchronizer(Component): def remote_fetch(self, remote_path=None, local_path=None): """ - download a single file from the server. + Download a single file from the server. - :param remote_path: path of where to download the file from - :param local_path: path of where to save the file to - :return: a dictionary with errors if any occur. + :param remote_path: Path of where to download the file from + :param local_path: Path of where to save the file to + :return: A dictionary with errors if any occur. """ if remote_path is None: raise ValueError("remote_path cannot be None") @@ -199,48 +200,68 @@ class ArchiveSynchronizer(Component): raise ValueError("local_path cannot be None") call_url = f"https://dev.r5portal.it/{remote_path}" + log_info_type = "Download" + log_time = datetime.now() + log_info = f"Attempted to download from {call_url}" + try: if not self.simulate: with requests.Session() as s: self.log.info(f"Fetching file from: {call_url}") - # Make the HTTP GET request to fetch the file response = s.get(call_url, timeout=5, verify=False) - # Log response details self.log.info(f"HTTP Status Code: {response.status_code}") - #self.log.info(f"Response Headers: {response.headers}") - #self.log.info(f"Response Content: {response.content}") - # Handle HTTP errors if response.status_code == 404: - self.log.warning(f"File not found: {call_url}. Please check the URL path.") + log_info += " - File not found" + self.log.warning(log_info) + self.log_to_db(log_time, log_info_type, log_info) return {"error": "File not found"} - elif response.status_code == 403 or response.status_code == 401: - self.log.warning(f"Access forbidden or not logged in for file: {call_url}") + elif response.status_code in [403, 401]: + log_info += " - Access forbidden or not logged in" + self.log.warning(log_info) + self.log_to_db(log_time, log_info_type, log_info) return {"error": "Access forbidden or not logged in"} elif response.status_code != 200: - self.log.error(f"Unexpected HTTP response status: {response.status_code} for URL: {call_url}") + log_info += f" - Unexpected HTTP response status: {response.status_code}" + self.log.error(log_info) + self.log_to_db(log_time, log_info_type, log_info) return {"error": "Unexpected HTTP response status"} - # Ensure the directory exists os.makedirs(local_path, exist_ok=True) - # Save the file to the local path local_file_path = os.path.join(local_path, os.path.basename(remote_path)) with open(local_file_path, "wb") as f: f.write(response.content) - self.log.info(f"File downloaded successfully: {local_file_path}") + log_info += f" - File downloaded successfully: {local_file_path}" + self.log.info(log_info) + self.log_to_db(log_time, log_info_type, log_info) return {"downloaded_file": local_file_path} except requests.ConnectionError as e: - self.log.error(f"Connection error occurred while fetching the file: {str(e)}") + log_info += f" - Connection error: {str(e)}" + self.log.error(log_info) + self.log_to_db(log_time, log_info_type, log_info) return {"error": "Connection error"} except requests.Timeout as e: - self.log.error(f"Timeout error occurred while fetching the file: {str(e)}") + log_info += f" - Timeout error: {str(e)}" + self.log.error(log_info) + self.log_to_db(log_time, log_info_type, log_info) return {"error": "Timeout error"} except Exception as e: - self.log.error(f"An unexpected error occurred: {str(e)}") + log_info += f" - Unexpected error: {str(e)}" + self.log.error(log_info) + self.log_to_db(log_time, log_info_type, log_info) return {"error": "Unexpected error"} + def log_to_db(self, log_time, log_info_type, log_info): + """Save log information to the database.""" + try: + # Use the Log class instead of the log instance + new_log_entry = Log(time=log_time, info_type=log_info_type, info=log_info) + new_log_entry.save() # Save the log entry to the database + except Exception as e: + self.log.error(f"Failed to save log to database: {str(e)}") + self.log.error(traceback.format_exc()) diff --git a/src/lib/db/models/log.py b/src/lib/db/models/log.py index d3d231b..4a187eb 100755 --- a/src/lib/db/models/log.py +++ b/src/lib/db/models/log.py @@ -5,7 +5,7 @@ from peewee import AutoField, DateTimeField, TextField from .base_model import BaseModel, db -log = logging.getLogger("db_log") +db_logger = logging.getLogger("db_log") class Log(BaseModel): @@ -18,7 +18,7 @@ class Log(BaseModel): @db.atomic() def log(cls, info_type, info=None): cls.create(info_type=info_type, info=info) - log.info(f"{info_type}: {info}") + db_logger.info(f"{info_type}: {info}") class Meta: table_name = "log"