poprawki i optymalizacje kodu

This commit is contained in:
Mateusz Gruszczyński
2025-08-17 17:12:51 +02:00
parent 6a8305b640
commit bfcc224a0f

19
app.py
View File

@@ -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()