poprawki i optymalizacje kodu
This commit is contained in:
19
app.py
19
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()
|
||||
|
Reference in New Issue
Block a user