67 lines
2.8 KiB
Python
Executable File
67 lines
2.8 KiB
Python
Executable File
from flask import jsonify
|
|
from datetime import date, timedelta
|
|
from app.delivery_status import deliverystatus
|
|
from app import db
|
|
|
|
from app.classes.delivery import (Delivery_Delivery,
|
|
Delivery_Delivery_schema,
|
|
)
|
|
from app.classes.service import Service_Service
|
|
from app.classes.auto import Auto_Delivery
|
|
from app.classes.transactions import Transaction
|
|
from datetime import date, timedelta, datetime
|
|
from zoneinfo import ZoneInfo
|
|
|
|
|
|
# --- NEW EFFICIENT ENDPOINT ---
|
|
@deliverystatus.route("/stats/sidebar-counts", methods=["GET"])
|
|
def get_sidebar_counts():
|
|
"""
|
|
Efficiently gets all counts needed for the navigation sidebar in a single request.
|
|
This combines logic from all the individual /count/* endpoints.
|
|
"""
|
|
try:
|
|
eastern = ZoneInfo("America/New_York")
|
|
now_local = datetime.now(eastern).replace(tzinfo=None) # naive local time
|
|
today_date = datetime.now(eastern).date() # local date
|
|
|
|
# Replicate the logic from each of your /count/* endpoints
|
|
today_count = db.session.query(Delivery_Delivery).filter(Delivery_Delivery.delivery_status == 2).count()
|
|
|
|
tomorrow_count = db.session.query(Delivery_Delivery).filter(Delivery_Delivery.delivery_status == 3).count()
|
|
|
|
waiting_count = db.session.query(Delivery_Delivery).filter(Delivery_Delivery.delivery_status == 0).count()
|
|
|
|
pending_count = db.session.query(Delivery_Delivery).filter(Delivery_Delivery.delivery_status == 9).count()
|
|
|
|
automatic_count = db.session.query(Auto_Delivery).filter(Auto_Delivery.estimated_gallons_left <= 80).count()
|
|
|
|
upcoming_service_count = db.session.query(Service_Service).filter(Service_Service.scheduled_date >= now_local).count()
|
|
|
|
start_of_today = datetime.combine(today_date, datetime.min.time())
|
|
start_of_tomorrow = datetime.combine(today_date + timedelta(days=1), datetime.min.time())
|
|
today_service_count = db.session.query(Service_Service).filter(
|
|
Service_Service.scheduled_date >= start_of_today,
|
|
Service_Service.scheduled_date < start_of_tomorrow
|
|
).count()
|
|
|
|
transaction_count = db.session.query(Transaction).filter(Transaction.transaction_type == 0).count()
|
|
|
|
return jsonify({
|
|
"ok": True,
|
|
"counts": {
|
|
"today": today_count,
|
|
"tomorrow": tomorrow_count,
|
|
"waiting": waiting_count,
|
|
"pending": pending_count,
|
|
"automatic": automatic_count,
|
|
"upcoming_service": upcoming_service_count,
|
|
"today_service": today_service_count,
|
|
"transaction": transaction_count,
|
|
}
|
|
}), 200
|
|
|
|
except Exception as e:
|
|
# Basic error handling
|
|
return jsonify({"ok": False, "error": str(e)}), 500
|