style(database): Changed logging
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
# db/repositories/robot_repository.py
|
||||
from typing import List, Optional
|
||||
import logging
|
||||
from db.connection import get_connection
|
||||
from model.robot import Robot
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class RobotRepository:
|
||||
def get_all(self) -> List[Robot]:
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("SELECT * FROM robots ORDER BY id")
|
||||
return [
|
||||
robots = [
|
||||
Robot(
|
||||
id=row[0],
|
||||
status=row[1],
|
||||
@@ -20,8 +23,10 @@ class RobotRepository:
|
||||
current_shelf=row[6]
|
||||
) for row in cur.fetchall()
|
||||
]
|
||||
logger.info(f"Получено {len(robots)} роботов")
|
||||
return robots
|
||||
except Exception as e:
|
||||
print(f"Ошика получения всех роботов: {e}")
|
||||
logger.error(f"Ошибка получения всех роботов: {e}")
|
||||
return []
|
||||
|
||||
def get_by_id(self, robot_id: str) -> Optional[Robot]:
|
||||
@@ -30,9 +35,13 @@ class RobotRepository:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("SELECT * FROM robots WHERE id = %s", (robot_id,))
|
||||
row = cur.fetchone()
|
||||
return Robot(*row) if row else None
|
||||
if row:
|
||||
logger.info(f"Робот {robot_id} найден")
|
||||
return Robot(*row)
|
||||
logger.warning(f"Робот {robot_id} не найден")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"Ошибка получения роботов {robot_id}: {e}")
|
||||
logger.error(f"Ошибка получения робота {robot_id}: {e}")
|
||||
return None
|
||||
|
||||
def update_robot(self, robot_id: str, status: str = None, battery_level: int = None,
|
||||
@@ -68,9 +77,14 @@ class RobotRepository:
|
||||
|
||||
cur.execute(query, params)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
success = cur.rowcount > 0
|
||||
if success:
|
||||
logger.info(f"Робот {robot_id} успешно обновлен")
|
||||
else:
|
||||
logger.warning(f"Робот {robot_id} не найден для обновления")
|
||||
return success
|
||||
except Exception as e:
|
||||
print(f"Ошибка обновления робота {robot_id}: {e}")
|
||||
logger.error(f"Ошибка обновления робота {robot_id}: {e}")
|
||||
return False
|
||||
|
||||
def get_robots_by_status(self, status: str) -> List[Robot]:
|
||||
@@ -78,9 +92,11 @@ class RobotRepository:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("SELECT * FROM robots WHERE status = %s ORDER BY id", (status,))
|
||||
return [Robot(*row) for row in cur.fetchall()]
|
||||
robots = [Robot(*row) for row in cur.fetchall()]
|
||||
logger.info(f"Получено {len(robots)} роботов со статусом {status}")
|
||||
return robots
|
||||
except Exception as e:
|
||||
print(f"Ошибка получения роботов по статусу {status}: {e}")
|
||||
logger.error(f"Ошибка получения роботов по статусу {status}: {e}")
|
||||
return []
|
||||
|
||||
def get_low_battery_robots(self, threshold: int = 20) -> List[Robot]:
|
||||
@@ -92,9 +108,11 @@ class RobotRepository:
|
||||
WHERE battery_level < %s AND status = 'active'
|
||||
ORDER BY battery_level ASC
|
||||
""", (threshold,))
|
||||
return [Robot(*row) for row in cur.fetchall()]
|
||||
robots = [Robot(*row) for row in cur.fetchall()]
|
||||
logger.info(f"Получено {len(robots)} роботов с низким зарядом (<{threshold}%)")
|
||||
return robots
|
||||
except Exception as e:
|
||||
print(f"Ошибка получения роботов с низким зарядом: {e}")
|
||||
logger.error(f"Ошибка получения роботов с низким зарядом: {e}")
|
||||
return []
|
||||
|
||||
def get_robots_in_zone(self, zone: str) -> List[Robot]:
|
||||
@@ -102,9 +120,11 @@ class RobotRepository:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("SELECT * FROM robots WHERE current_zone = %s ORDER BY id", (zone,))
|
||||
return [Robot(*row) for row in cur.fetchall()]
|
||||
robots = [Robot(*row) for row in cur.fetchall()]
|
||||
logger.info(f"Получено {len(robots)} роботов в зоне {zone}")
|
||||
return robots
|
||||
except Exception as e:
|
||||
print(f"Ошибка получения роботов в зоне {zone}: {e}")
|
||||
logger.error(f"Ошибка получения роботов в зоне {zone}: {e}")
|
||||
return []
|
||||
|
||||
def create_robot(self, robot_id: str, status: str = 'active', battery_level: int = 100) -> bool:
|
||||
@@ -116,9 +136,14 @@ class RobotRepository:
|
||||
VALUES (%s, %s, %s, CURRENT_TIMESTAMP)
|
||||
""", (robot_id, status, battery_level))
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
success = cur.rowcount > 0
|
||||
if success:
|
||||
logger.info(f"Создан новый робот {robot_id}")
|
||||
else:
|
||||
logger.warning(f"Не удалось создать робота {robot_id}")
|
||||
return success
|
||||
except Exception as e:
|
||||
print(f"Ошибка создания робота {robot_id}: {e}")
|
||||
logger.error(f"Ошибка создания робота {robot_id}: {e}")
|
||||
return False
|
||||
|
||||
def delete_robot(self, robot_id: str) -> bool:
|
||||
@@ -127,7 +152,12 @@ class RobotRepository:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("DELETE FROM robots WHERE id = %s", (robot_id,))
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
success = cur.rowcount > 0
|
||||
if success:
|
||||
logger.info(f"Робот {robot_id} удален")
|
||||
else:
|
||||
logger.warning(f"Робот {robot_id} не найден для удаления")
|
||||
return success
|
||||
except Exception as e:
|
||||
print(f"Ошибка удаления робота {robot_id}: {e}")
|
||||
logger.error(f"Ошибка удаления робота {robot_id}: {e}")
|
||||
return False
|
||||
Reference in New Issue
Block a user