workout-tracker / tracker /orchestrate.py
ronheichman's picture
Deploy workout tracker
b28b9ac verified
Raw
History Blame Contribute Delete
2.41 kB
"""CLI orchestration: weekly planning and state snapshots (uses services, not HTTP)."""
from __future__ import annotations
import json
import click
from sqlmodel import Session
from tracker.agent_profile import resolve_agent_profile
from tracker.config import load_config
from tracker.db import get_engine_for_config
from tracker.planner_schemas import ScheduleWorkoutsInput
from tracker.planner_service import (
build_tracker_state,
fetch_calendar_view,
run_schedule_workouts,
)
@click.group()
def cli() -> None:
"""Tracker orchestration commands."""
@cli.command("weekly-plan")
@click.option("--days-ahead", default=7, type=int)
@click.option("--duration-minutes", default=45, type=int)
@click.option("--apply-schedule/--no-apply-schedule", default=False)
def weekly_plan(days_ahead: int, duration_minutes: int, apply_schedule: bool) -> None:
"""Load profile, show calendar window, optionally schedule workouts in Google Calendar."""
cfg = load_config()
profile = resolve_agent_profile(cfg)
click.echo(json.dumps({"profile": profile.model_dump(mode="json")}, indent=2))
cal = fetch_calendar_view(days_ahead)
click.echo(json.dumps({"calendar_events": len(cal.events), "free_blocks": len(cal.free_blocks)}))
if apply_schedule:
body = ScheduleWorkoutsInput(days_ahead=days_ahead, duration_minutes=duration_minutes)
with Session(get_engine_for_config(cfg)) as session:
created, err = run_schedule_workouts(session, cfg, body)
if err is not None:
click.echo(err, err=True)
raise SystemExit(1)
click.echo(json.dumps([e.model_dump() for e in created], indent=2))
else:
click.echo("(use --apply-schedule to create calendar events)")
@cli.command("day-status")
def day_status() -> None:
"""Print tracker state JSON (weight, last workout, progression)."""
cfg = load_config()
with Session(get_engine_for_config(cfg)) as session:
state = build_tracker_state(session, cfg)
click.echo(state.model_dump_json(indent=2))
@cli.command("sync-state")
def sync_state() -> None:
"""Same snapshot as day-status."""
cfg = load_config()
with Session(get_engine_for_config(cfg)) as session:
state = build_tracker_state(session, cfg)
click.echo(state.model_dump_json(indent=2))
def main() -> None:
cli()
if __name__ == "__main__":
main()