From bfcc224a0fedfa04fcc85dd4249f289d9e9e72d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Sun, 17 Aug 2025 17:12:51 +0200 Subject: [PATCH] poprawki i optymalizacje kodu --- app.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 45443ce..1df551e 100644 --- a/app.py +++ b/app.py @@ -2379,20 +2379,27 @@ def admin_panel(): total_expense = db.session.query(func.sum(Expense.amount)).scalar() or 0 avg_list_expense = round(total_expense / list_count, 2) if list_count else 0 + if db.engine.name == "sqlite": + timestamp_diff = func.strftime("%s", Item.purchased_at) - func.strftime("%s", Item.added_at) + elif db.engine.name in ("postgresql", "postgres"): + timestamp_diff = func.extract("epoch", Item.purchased_at) - func.extract("epoch", Item.added_at) + elif db.engine.name in ("mysql", "mariadb"): + timestamp_diff = func.timestampdiff(text("SECOND"), Item.added_at, Item.purchased_at) + else: + timestamp_diff = None + time_to_purchase = ( - db.session.query( - func.avg( - func.strftime("%s", Item.purchased_at) - - func.strftime("%s", Item.added_at) - ) - ) + db.session.query(func.avg(timestamp_diff)) .filter( Item.purchased == True, Item.purchased_at.isnot(None), Item.added_at.isnot(None), ) .scalar() + if timestamp_diff is not None + else None ) + avg_hours_to_purchase = round(time_to_purchase / 3600, 2) if time_to_purchase else 0 first_list = db.session.query(func.min(ShoppingList.created_at)).scalar()