Co-authored-by: OpenAI/GPT-5.6-Terra <vibecoder@antoineve.me> Co-authored-by: OpenAI/GPT-5.6-Luna <vibecoder@antoineve.me> Co-authored-by: MiniMax/MiniMax-M3 <vibecoder@antoineve.me> Co-authored-by: DeepSeek/DeepSeek-v4-Flash <vibecoder@antoineve.me>
66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
from datetime import date, timedelta
|
|
|
|
import sqlalchemy as sa
|
|
from flask import Blueprint, render_template
|
|
|
|
from app import db
|
|
from app.business.leave_calc import compute_leave_used, get_or_create_balance
|
|
from app.business.time_calc import minutes_to_str, work_minutes_reference
|
|
from app.business.travel_calc import compute_co2_grams, compute_km_for_entry
|
|
from app.config_loader import get_journeys, get_vehicles
|
|
from app.models import WorkEntry
|
|
|
|
bp = Blueprint("dashboard", __name__)
|
|
|
|
|
|
@bp.route("/")
|
|
def index():
|
|
today = date.today()
|
|
year = today.year
|
|
|
|
monday = today - timedelta(days=today.weekday())
|
|
sunday = monday + timedelta(days=6)
|
|
week_entries = db.session.scalars(
|
|
sa.select(WorkEntry).where(WorkEntry.date.between(monday, sunday))
|
|
).all()
|
|
|
|
week_actual = sum(e.total_minutes() for e in week_entries)
|
|
week_ref = sum(work_minutes_reference(e.day_type) for e in week_entries)
|
|
week_balance = week_actual - week_ref
|
|
|
|
month_start = today.replace(day=1)
|
|
month_entries = db.session.scalars(
|
|
sa.select(WorkEntry).where(WorkEntry.date.between(month_start, today))
|
|
).all()
|
|
|
|
vehicles = get_vehicles()
|
|
journeys = get_journeys()
|
|
|
|
month_km = {}
|
|
month_co2 = 0.0
|
|
for entry in month_entries:
|
|
km = compute_km_for_entry(entry.journey_profile_id, journeys, entry.motor_vehicle_id)
|
|
for v, d in km.items():
|
|
month_km[v] = month_km.get(v, 0) + d
|
|
month_co2 += compute_co2_grams(km, vehicles)
|
|
|
|
balance = get_or_create_balance(year)
|
|
used = compute_leave_used(year)
|
|
|
|
today_entry = db.session.scalar(sa.select(WorkEntry).where(WorkEntry.date == today))
|
|
|
|
return render_template(
|
|
"dashboard.html",
|
|
today=today,
|
|
today_entry=today_entry,
|
|
journeys=journeys,
|
|
vehicles=vehicles,
|
|
week_actual_str=minutes_to_str(week_actual),
|
|
week_balance=week_balance,
|
|
week_balance_str=minutes_to_str(abs(week_balance)),
|
|
month_km=month_km,
|
|
month_co2_kg=round(month_co2 / 1000, 2),
|
|
balance=balance,
|
|
used=used,
|
|
)
|