From 8833b5b3715dd80a47ecc3e620aaf0c989e99661 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Fri, 8 Jul 2022 21:41:39 +0200 Subject: [PATCH] Add outgoing activity logs to stats --- app/utils/stats.py | 62 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/app/utils/stats.py b/app/utils/stats.py index 8bf2efb..ca5e477 100644 --- a/app/utils/stats.py +++ b/app/utils/stats.py @@ -6,6 +6,7 @@ from sqlalchemy import case from sqlalchemy import func from sqlalchemy import or_ from sqlalchemy import select +from sqlalchemy.orm import joinedload from tabulate import tabulate from app import models @@ -94,7 +95,12 @@ async def get_outgoing_activity_stats( ).where(f) ) ).one() - return OutgoingActivityStatsItem(**dict(row)) + return OutgoingActivityStatsItem( + total_count=row.total_count or 0, + waiting_count=row.waiting_count or 0, + sent_count=row.sent_count or 0, + errored_count=row.errored_count or 0, + ) from_inbox = await _get_stats(models.OutgoingActivity.inbox_object_id.is_not(None)) from_outbox = await _get_stats( @@ -115,12 +121,28 @@ async def get_outgoing_activity_stats( def print_stats() -> None: async def _get_stats(): - async with async_session() as session: - dat = await get_outgoing_activity_stats(session) + async with async_session() as db_session: + outgoing_activity_stats = await get_outgoing_activity_stats(db_session) - return dat + outgoing_activities = ( + ( + await db_session.scalars( + select(models.OutgoingActivity) + .options( + joinedload(models.OutgoingActivity.inbox_object), + joinedload(models.OutgoingActivity.outbox_object), + ) + .order_by(models.OutgoingActivity.last_try.desc()) + .limit(10) + ) + ) + .unique() + .all() + ) - outgoing_activity_stats = asyncio.run(_get_stats()) + return outgoing_activity_stats, outgoing_activities + + outgoing_activity_stats, outgoing_activities = asyncio.run(_get_stats()) disk_usage_stats = get_disk_usage_stats() print() @@ -139,7 +161,6 @@ def print_stats() -> None: headers=["Disk usage", "size"], ) ) - print() print( tabulate( @@ -155,3 +176,32 @@ def print_stats() -> None: ) ) print() + print("Outgoing activities log") + print("=======================") + print() + print( + tabulate( + [ + ( + row.anybox_object.ap_id, + humanize.naturaltime(row.last_try), + row.recipient, + row.tries, + row.last_status_code, + row.is_sent, + row.is_errored, + ) + for row in outgoing_activities + ], + headers=[ + "Object", + "last try", + "recipient", + "tries", + "status code", + "sent", + "errored", + ], + ) + ) + print()