Initial commit

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-26 22:27:27 -08:00
parent 4387f6df92
commit 492e1fd68f
32 changed files with 2608 additions and 0 deletions

14
backend/Dockerfile Normal file
View File

@@ -0,0 +1,14 @@
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
# Run as a non-root user — no reason for a web process to have root privileges
RUN adduser --disabled-password --no-create-home appuser
USER appuser
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

22
backend/database.py Normal file
View File

@@ -0,0 +1,22 @@
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import DeclarativeBase, sessionmaker
DATABASE_URL = os.environ["DATABASE_URL"]
engine = create_engine(DATABASE_URL, pool_pre_ping=True)
SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False)
class Base(DeclarativeBase):
pass
def get_db():
"""FastAPI dependency — yields a DB session and closes it when done."""
db = SessionLocal()
try:
yield db
finally:
db.close()

24
backend/main.py Normal file
View File

@@ -0,0 +1,24 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from routers import eggs, flock, feed, stats
app = FastAPI(title="Eggtracker API")
# Allow requests from the Nginx frontend (same host, different port internally)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(eggs.router)
app.include_router(flock.router)
app.include_router(feed.router)
app.include_router(stats.router)
@app.get("/api/health")
def health():
return {"status": "ok"}

35
backend/models.py Normal file
View File

@@ -0,0 +1,35 @@
from datetime import date, datetime
from sqlalchemy import Integer, Date, DateTime, Text, Numeric, func
from sqlalchemy.orm import Mapped, mapped_column
from database import Base
class EggCollection(Base):
__tablename__ = "egg_collections"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
date: Mapped[date] = mapped_column(Date, nullable=False, index=True)
eggs: Mapped[int] = mapped_column(Integer, nullable=False)
notes: Mapped[str] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
class FlockHistory(Base):
__tablename__ = "flock_history"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
date: Mapped[date] = mapped_column(Date, nullable=False, index=True)
chicken_count: Mapped[int] = mapped_column(Integer, nullable=False)
notes: Mapped[str] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
class FeedPurchase(Base):
__tablename__ = "feed_purchases"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
date: Mapped[date] = mapped_column(Date, nullable=False, index=True)
bags: Mapped[float] = mapped_column(Numeric(5, 2), nullable=False)
price_per_bag: Mapped[float] = mapped_column(Numeric(10, 2), nullable=False)
notes: Mapped[str] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())

6
backend/requirements.txt Normal file
View File

@@ -0,0 +1,6 @@
fastapi==0.115.0
uvicorn==0.32.0
sqlalchemy==2.0.36
pymysql==1.1.1
cryptography==43.0.3
pydantic==2.9.2

View File

68
backend/routers/eggs.py Normal file
View File

@@ -0,0 +1,68 @@
from datetime import date
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
from database import get_db
from models import EggCollection
from schemas import EggCollectionCreate, EggCollectionUpdate, EggCollectionOut
router = APIRouter(prefix="/api/eggs", tags=["eggs"])
@router.get("", response_model=list[EggCollectionOut])
def list_eggs(
start: Optional[date] = None,
end: Optional[date] = None,
db: Session = Depends(get_db),
):
q = select(EggCollection).order_by(EggCollection.date.desc())
if start:
q = q.where(EggCollection.date >= start)
if end:
q = q.where(EggCollection.date <= end)
return db.scalars(q).all()
@router.post("", response_model=EggCollectionOut, status_code=201)
def create_egg_collection(body: EggCollectionCreate, db: Session = Depends(get_db)):
record = EggCollection(**body.model_dump())
db.add(record)
try:
db.commit()
except IntegrityError:
db.rollback()
raise HTTPException(status_code=409, detail=f"An entry for {body.date} already exists. Edit it from the History page.")
db.refresh(record)
return record
@router.put("/{record_id}", response_model=EggCollectionOut)
def update_egg_collection(
record_id: int,
body: EggCollectionUpdate,
db: Session = Depends(get_db),
):
record = db.get(EggCollection, record_id)
if not record:
raise HTTPException(status_code=404, detail="Record not found")
for field, value in body.model_dump(exclude_none=True).items():
setattr(record, field, value)
try:
db.commit()
except IntegrityError:
db.rollback()
raise HTTPException(status_code=409, detail=f"An entry for that date already exists.")
db.refresh(record)
return record
@router.delete("/{record_id}", status_code=204)
def delete_egg_collection(record_id: int, db: Session = Depends(get_db)):
record = db.get(EggCollection, record_id)
if not record:
raise HTTPException(status_code=404, detail="Record not found")
db.delete(record)
db.commit()

59
backend/routers/feed.py Normal file
View File

@@ -0,0 +1,59 @@
from datetime import date
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import select
from sqlalchemy.orm import Session
from database import get_db
from models import FeedPurchase
from schemas import FeedPurchaseCreate, FeedPurchaseUpdate, FeedPurchaseOut
router = APIRouter(prefix="/api/feed", tags=["feed"])
@router.get("", response_model=list[FeedPurchaseOut])
def list_feed_purchases(
start: Optional[date] = None,
end: Optional[date] = None,
db: Session = Depends(get_db),
):
q = select(FeedPurchase).order_by(FeedPurchase.date.desc())
if start:
q = q.where(FeedPurchase.date >= start)
if end:
q = q.where(FeedPurchase.date <= end)
return db.scalars(q).all()
@router.post("", response_model=FeedPurchaseOut, status_code=201)
def create_feed_purchase(body: FeedPurchaseCreate, db: Session = Depends(get_db)):
record = FeedPurchase(**body.model_dump())
db.add(record)
db.commit()
db.refresh(record)
return record
@router.put("/{record_id}", response_model=FeedPurchaseOut)
def update_feed_purchase(
record_id: int,
body: FeedPurchaseUpdate,
db: Session = Depends(get_db),
):
record = db.get(FeedPurchase, record_id)
if not record:
raise HTTPException(status_code=404, detail="Record not found")
for field, value in body.model_dump(exclude_none=True).items():
setattr(record, field, value)
db.commit()
db.refresh(record)
return record
@router.delete("/{record_id}", status_code=204)
def delete_feed_purchase(record_id: int, db: Session = Depends(get_db)):
record = db.get(FeedPurchase, record_id)
if not record:
raise HTTPException(status_code=404, detail="Record not found")
db.delete(record)
db.commit()

70
backend/routers/flock.py Normal file
View File

@@ -0,0 +1,70 @@
from datetime import date
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import select
from sqlalchemy.orm import Session
from database import get_db
from models import FlockHistory
from schemas import FlockHistoryCreate, FlockHistoryUpdate, FlockHistoryOut
router = APIRouter(prefix="/api/flock", tags=["flock"])
@router.get("", response_model=list[FlockHistoryOut])
def list_flock_history(db: Session = Depends(get_db)):
q = select(FlockHistory).order_by(FlockHistory.date.desc())
return db.scalars(q).all()
@router.get("/current", response_model=Optional[FlockHistoryOut])
def get_current_flock(db: Session = Depends(get_db)):
"""Returns the most recent flock entry — the current flock size."""
q = select(FlockHistory).order_by(FlockHistory.date.desc()).limit(1)
return db.scalars(q).first()
@router.get("/at/{target_date}", response_model=Optional[FlockHistoryOut])
def get_flock_at_date(target_date: date, db: Session = Depends(get_db)):
"""Returns the flock size that was in effect on a given date."""
q = (
select(FlockHistory)
.where(FlockHistory.date <= target_date)
.order_by(FlockHistory.date.desc())
.limit(1)
)
return db.scalars(q).first()
@router.post("", response_model=FlockHistoryOut, status_code=201)
def create_flock_entry(body: FlockHistoryCreate, db: Session = Depends(get_db)):
record = FlockHistory(**body.model_dump())
db.add(record)
db.commit()
db.refresh(record)
return record
@router.put("/{record_id}", response_model=FlockHistoryOut)
def update_flock_entry(
record_id: int,
body: FlockHistoryUpdate,
db: Session = Depends(get_db),
):
record = db.get(FlockHistory, record_id)
if not record:
raise HTTPException(status_code=404, detail="Record not found")
for field, value in body.model_dump(exclude_none=True).items():
setattr(record, field, value)
db.commit()
db.refresh(record)
return record
@router.delete("/{record_id}", status_code=204)
def delete_flock_entry(record_id: int, db: Session = Depends(get_db)):
record = db.get(FlockHistory, record_id)
if not record:
raise HTTPException(status_code=404, detail="Record not found")
db.delete(record)
db.commit()

207
backend/routers/stats.py Normal file
View File

@@ -0,0 +1,207 @@
import calendar
from datetime import date, timedelta
from decimal import Decimal
from fastapi import APIRouter, Depends
from sqlalchemy import select, func
from sqlalchemy.orm import Session
from database import get_db
from models import EggCollection, FlockHistory, FeedPurchase
from schemas import DashboardStats, BudgetStats, MonthlySummary
router = APIRouter(prefix="/api/stats", tags=["stats"])
def _avg_per_hen_30d(db: Session, start_30d: date) -> float | None:
"""
For each collection in the last 30 days, look up the flock size that was
in effect on that date using a correlated subquery, then average eggs/hen
across those days. This gives an accurate result even when flock size changed.
"""
flock_at_date = (
select(FlockHistory.chicken_count)
.where(FlockHistory.date <= EggCollection.date)
.order_by(FlockHistory.date.desc())
.limit(1)
.correlate(EggCollection)
.scalar_subquery()
)
rows = db.execute(
select(EggCollection.eggs, flock_at_date.label('flock_count'))
.where(EggCollection.date >= start_30d)
).all()
valid = [(r.eggs, r.flock_count) for r in rows if r.flock_count]
if not valid:
return None
return round(sum(e / f for e, f in valid) / len(valid), 3)
def _current_flock(db: Session) -> int | None:
row = db.scalars(
select(FlockHistory).order_by(FlockHistory.date.desc()).limit(1)
).first()
return row.chicken_count if row else None
def _total_eggs(db: Session, start: date | None = None, end: date | None = None) -> int:
q = select(func.coalesce(func.sum(EggCollection.eggs), 0))
if start:
q = q.where(EggCollection.date >= start)
if end:
q = q.where(EggCollection.date <= end)
return db.scalar(q)
def _total_feed_cost(db: Session, start: date | None = None, end: date | None = None):
q = select(
func.coalesce(func.sum(FeedPurchase.bags * FeedPurchase.price_per_bag), 0)
)
if start:
q = q.where(FeedPurchase.date >= start)
if end:
q = q.where(FeedPurchase.date <= end)
return db.scalar(q)
@router.get("/dashboard", response_model=DashboardStats)
def dashboard_stats(db: Session = Depends(get_db)):
today = date.today()
start_30d = today - timedelta(days=30)
start_7d = today - timedelta(days=7)
total_alltime = _total_eggs(db)
total_30d = _total_eggs(db, start=start_30d)
total_7d = _total_eggs(db, start=start_7d)
flock = _current_flock(db)
# Count how many distinct days have a collection logged
days_tracked = db.scalar(
select(func.count(func.distinct(EggCollection.date)))
)
# Average eggs per day over the last 30 days (only counting days with data)
days_with_data_30d = db.scalar(
select(func.count(func.distinct(EggCollection.date)))
.where(EggCollection.date >= start_30d)
)
avg_per_day = round(total_30d / days_with_data_30d, 2) if days_with_data_30d else None
avg_per_hen = _avg_per_hen_30d(db, start_30d)
return DashboardStats(
current_flock=flock,
total_eggs_alltime=total_alltime,
total_eggs_30d=total_30d,
total_eggs_7d=total_7d,
avg_eggs_per_day_30d=avg_per_day,
avg_eggs_per_hen_day_30d=avg_per_hen,
days_tracked=days_tracked,
)
@router.get("/monthly", response_model=list[MonthlySummary])
def monthly_stats(db: Session = Depends(get_db)):
MONTH_NAMES = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
# Monthly egg totals
egg_rows = db.execute(
select(
func.year(EggCollection.date).label('year'),
func.month(EggCollection.date).label('month'),
func.sum(EggCollection.eggs).label('total_eggs'),
func.count(EggCollection.date).label('days_logged'),
)
.group_by(func.year(EggCollection.date), func.month(EggCollection.date))
.order_by(func.year(EggCollection.date).desc(), func.month(EggCollection.date).desc())
).all()
if not egg_rows:
return []
# Monthly feed costs
feed_rows = db.execute(
select(
func.year(FeedPurchase.date).label('year'),
func.month(FeedPurchase.date).label('month'),
func.sum(FeedPurchase.bags * FeedPurchase.price_per_bag).label('feed_cost'),
)
.group_by(func.year(FeedPurchase.date), func.month(FeedPurchase.date))
).all()
feed_map = {(r.year, r.month): r.feed_cost for r in feed_rows}
results = []
for row in egg_rows:
y, m = int(row.year), int(row.month)
last_day = calendar.monthrange(y, m)[1]
month_end = date(y, m, last_day)
# Flock size in effect at the end of this month
flock_row = db.scalars(
select(FlockHistory)
.where(FlockHistory.date <= month_end)
.order_by(FlockHistory.date.desc())
.limit(1)
).first()
flock = flock_row.chicken_count if flock_row else None
total_eggs = int(row.total_eggs)
days_logged = int(row.days_logged)
avg_per_day = round(total_eggs / days_logged, 2) if days_logged else None
avg_per_hen = round(avg_per_day / flock, 3) if (avg_per_day and flock) else None
raw_feed_cost = feed_map.get((y, m))
feed_cost = round(Decimal(str(raw_feed_cost)), 2) if raw_feed_cost else None
cpe = round(Decimal(str(raw_feed_cost)) / Decimal(total_eggs), 4) if (raw_feed_cost and total_eggs) else None
cpd = round(cpe * 12, 4) if cpe else None
results.append(MonthlySummary(
year=y,
month=m,
month_label=f"{MONTH_NAMES[m - 1]} {y}",
total_eggs=total_eggs,
days_logged=days_logged,
avg_eggs_per_day=avg_per_day,
flock_at_month_end=flock,
avg_eggs_per_hen_per_day=avg_per_hen,
feed_cost=feed_cost,
cost_per_egg=cpe,
cost_per_dozen=cpd,
))
return results
@router.get("/budget", response_model=BudgetStats)
def budget_stats(db: Session = Depends(get_db)):
today = date.today()
start_30d = today - timedelta(days=30)
total_cost = _total_feed_cost(db)
total_cost_30d = _total_feed_cost(db, start=start_30d)
total_eggs = _total_eggs(db)
total_eggs_30d = _total_eggs(db, start=start_30d)
def cost_per_egg(cost, eggs):
if not eggs or not cost:
return None
return round(Decimal(str(cost)) / Decimal(eggs), 4)
def cost_per_dozen(cpe):
return round(cpe * 12, 4) if cpe else None
cpe = cost_per_egg(total_cost, total_eggs)
cpe_30d = cost_per_egg(total_cost_30d, total_eggs_30d)
return BudgetStats(
total_feed_cost=round(Decimal(str(total_cost)), 2) if total_cost else None,
total_feed_cost_30d=round(Decimal(str(total_cost_30d)), 2) if total_cost_30d else None,
total_eggs_alltime=total_eggs,
total_eggs_30d=total_eggs_30d,
cost_per_egg=cpe,
cost_per_dozen=cost_per_dozen(cpe),
cost_per_egg_30d=cpe_30d,
cost_per_dozen_30d=cost_per_dozen(cpe_30d),
)

109
backend/schemas.py Normal file
View File

@@ -0,0 +1,109 @@
from datetime import date, datetime
from decimal import Decimal
from typing import Optional
from pydantic import BaseModel, Field
# ── Egg Collections ───────────────────────────────────────────────────────────
class EggCollectionCreate(BaseModel):
date: date
eggs: int = Field(ge=0)
notes: Optional[str] = None
class EggCollectionUpdate(BaseModel):
date: Optional[date] = None
eggs: Optional[int] = Field(default=None, ge=0)
notes: Optional[str] = None
class EggCollectionOut(BaseModel):
id: int
date: date
eggs: int
notes: Optional[str]
created_at: datetime
model_config = {"from_attributes": True}
# ── Flock History ─────────────────────────────────────────────────────────────
class FlockHistoryCreate(BaseModel):
date: date
chicken_count: int = Field(ge=0)
notes: Optional[str] = None
class FlockHistoryUpdate(BaseModel):
date: Optional[date] = None
chicken_count: Optional[int] = Field(default=None, ge=0)
notes: Optional[str] = None
class FlockHistoryOut(BaseModel):
id: int
date: date
chicken_count: int
notes: Optional[str]
created_at: datetime
model_config = {"from_attributes": True}
# ── Feed Purchases ────────────────────────────────────────────────────────────
class FeedPurchaseCreate(BaseModel):
date: date
bags: Decimal = Field(gt=0, decimal_places=2)
price_per_bag: Decimal = Field(gt=0, decimal_places=2)
notes: Optional[str] = None
class FeedPurchaseUpdate(BaseModel):
date: Optional[date] = None
bags: Optional[Decimal] = Field(default=None, gt=0, decimal_places=2)
price_per_bag: Optional[Decimal] = Field(default=None, gt=0, decimal_places=2)
notes: Optional[str] = None
class FeedPurchaseOut(BaseModel):
id: int
date: date
bags: Decimal
price_per_bag: Decimal
notes: Optional[str]
created_at: datetime
model_config = {"from_attributes": True}
# ── Stats ─────────────────────────────────────────────────────────────────────
class MonthlySummary(BaseModel):
year: int
month: int
month_label: str
total_eggs: int
days_logged: int
avg_eggs_per_day: Optional[float]
flock_at_month_end: Optional[int]
avg_eggs_per_hen_per_day: Optional[float]
feed_cost: Optional[Decimal]
cost_per_egg: Optional[Decimal]
cost_per_dozen: Optional[Decimal]
class DashboardStats(BaseModel):
current_flock: Optional[int]
total_eggs_alltime: int
total_eggs_30d: int
total_eggs_7d: int
avg_eggs_per_day_30d: Optional[float]
avg_eggs_per_hen_day_30d: Optional[float]
days_tracked: int
class BudgetStats(BaseModel):
total_feed_cost: Optional[Decimal]
total_feed_cost_30d: Optional[Decimal]
total_eggs_alltime: int
total_eggs_30d: int
cost_per_egg: Optional[Decimal]
cost_per_dozen: Optional[Decimal]
cost_per_egg_30d: Optional[Decimal]
cost_per_dozen_30d: Optional[Decimal]