diff --git a/meshview/store.py b/meshview/store.py index 7f5e98b..c85122c 100644 --- a/meshview/store.py +++ b/meshview/store.py @@ -1,3 +1,4 @@ +import logging from datetime import datetime, timedelta from sqlalchemy import Text, and_, cast, func, or_, select @@ -6,6 +7,8 @@ from sqlalchemy.orm import lazyload from meshview import database, models from meshview.models import Node, Packet, PacketSeen, Traceroute +logger = logging.getLogger(__name__) + async def get_node(node_id): async with database.async_session() as session: @@ -176,7 +179,7 @@ async def get_mqtt_neighbors(since): async def get_total_node_count(channel: str = None) -> int: try: async with database.async_session() as session: - now_us = int(datetime.utcnow().timestamp() * 1_000_000) + now_us = int(datetime.now(datetime.UTC).timestamp() * 1_000_000) cutoff_us = now_us - 86400 * 1_000_000 q = select(func.count(Node.id)).where(Node.last_seen_us > cutoff_us) @@ -193,7 +196,7 @@ async def get_total_node_count(channel: str = None) -> int: async def get_top_traffic_nodes(): try: async with database.async_session() as session: - now_us = int(datetime.utcnow().timestamp() * 1_000_000) + now_us = int(datetime.now(datetime.UTC).timestamp() * 1_000_000) cutoff_us = now_us - 86400 * 1_000_000 total_packets_sent = func.count(func.distinct(Packet.id)).label("total_packets_sent") total_times_seen = func.count(PacketSeen.packet_id).label("total_times_seen") @@ -241,7 +244,7 @@ async def get_top_traffic_nodes(): async def get_node_traffic(node_id: int): try: async with database.async_session() as session: - now_us = int(datetime.utcnow().timestamp() * 1_000_000) + now_us = int(datetime.now(datetime.UTC).timestamp() * 1_000_000) cutoff_us = now_us - 86400 * 1_000_000 packet_count = func.count().label("packet_count") @@ -293,7 +296,11 @@ async def get_nodes(node_id=None, role=None, channel=None, hw_model=None, days_a # Apply filters based on provided parameters if node_id is not None: - query = query.where(Node.node_id == node_id) + try: + node_id_int = int(node_id) + except (TypeError, ValueError): + node_id_int = node_id + query = query.where(Node.node_id == node_id_int) if role is not None: query = query.where(Node.role == role.upper()) # Ensure role is uppercase if channel is not None: @@ -302,7 +309,7 @@ async def get_nodes(node_id=None, role=None, channel=None, hw_model=None, days_a query = query.where(Node.hw_model == hw_model) if days_active is not None: - now_us = int(datetime.utcnow().timestamp() * 1_000_000) + now_us = int(datetime.now(datetime.UTC).timestamp() * 1_000_000) cutoff_us = now_us - int(timedelta(days_active).total_seconds() * 1_000_000) query = query.where(Node.last_seen_us > cutoff_us) @@ -318,7 +325,7 @@ async def get_nodes(node_id=None, role=None, channel=None, hw_model=None, days_a return nodes # Return the list of nodes except Exception: - print("error reading DB") # Consider using logging instead of print + logger.exception("error reading DB") return [] # Return an empty list in case of failure @@ -330,7 +337,7 @@ async def get_packet_stats( to_node: int | None = None, from_node: int | None = None, ): - now = datetime.now() + now = datetime.now(datetime.UTC) if period_type == "hour": start_time = now - timedelta(hours=length)