Files
bambuddy/backend/app/models/print_log.py
T
maziggy 856b849ffa fix(stats): per-event aggregation so reprints add to Quick Stats instead of overwriting (#1378)
Statistics now aggregate over PrintLogEntry (one row per print event,
  the same table backing the global Print Log) rather than PrintArchive
  (one row per file). A reprint creates a new PrintLogEntry instead of
  overwriting the source archive's runtime fields, so:

  - a 100 g successful print + a 10 g failed reprint correctly sums to
    110 g / 2 prints / 1 successful / 1 failed in Quick Stats and the
    Prometheus /metrics endpoint (previously the failed reprint silently
    replaced the source archive's data; totals dropped from 100 g to 10 g)
  - the archive's card cost/energy_kwh are preserved on reprints (only
    the first run writes them); per-run actuals live on PrintLogEntry
  - failed/cancelled/stopped reprints record partial-aware filament: sum
    of tracked spool deltas when inventory is set up, else estimate
    scaled to progress%, else None — prevents the full slicer estimate
    from inflating totals on a print that stopped at 10 % progress

  PrintLogEntry gains six columns: archive_id (nullable FK, ON DELETE
  SET NULL so log entries survive archive deletion preserving #1343
  soft-delete-vs-stats decoupling), cost, energy_kwh, energy_cost,
  failure_reason, created_by_id. Idempotent SQLite + Postgres migrations.

  New per-archive surface:

  - archive list response carries run_count / last_run_at /
    total_filament_actual_grams / successful_run_count / failed_run_count
    via a single batch JOIN, no N+1
  - new GET /archives/{id}/runs endpoint returns every PrintLogEntry for
    the archive (ARCHIVES_READ permission, newest-first ordering)
  - archive cards render an orange "N prints" badge for archives with
    more than one run; clicking the badge opens a dedicated PrintLogModal
    with date/status/duration/filament/cost columns plus failure_reason
    under failed runs. Also reachable via the context menu's new "Print
    Log" entry (works for single-run archives too), and embedded at the
    top of the Edit Archive modal for context.

  The purge_stats=true delete path now hard-deletes linked PrintLogEntry
  rows up front so the archive's contribution truly leaves the totals;
  without it, ON DELETE SET NULL would orphan the runs and leave them
  counting toward stats.
2026-05-16 11:34:40 +02:00

45 lines
2.2 KiB
Python

from datetime import datetime
from sqlalchemy import DateTime, Float, ForeignKey, Integer, String, func
from sqlalchemy.orm import Mapped, mapped_column
from backend.app.core.database import Base
class PrintLogEntry(Base):
"""Independent print log entry. Written when print events occur.
This is a separate table from archives/queue — clearing the log
never touches archives or queue items.
archive_id is a nullable FK so log entries survive archive deletion (ON
DELETE SET NULL). Aggregating runs per archive — for the per-archive
"Print Log" view and for statistics that should not double-count
overwritten archives (#1378) — is done via WHERE archive_id = X.
"""
__tablename__ = "print_log_entries"
id: Mapped[int] = mapped_column(primary_key=True)
archive_id: Mapped[int | None] = mapped_column(
ForeignKey("print_archives.id", ondelete="SET NULL"), nullable=True, index=True
)
print_name: Mapped[str | None] = mapped_column(String(255))
printer_name: Mapped[str | None] = mapped_column(String(255))
printer_id: Mapped[int | None] = mapped_column(Integer)
status: Mapped[str] = mapped_column(String(20)) # completed, failed, stopped, cancelled, skipped
started_at: Mapped[datetime | None] = mapped_column(DateTime)
completed_at: Mapped[datetime | None] = mapped_column(DateTime)
duration_seconds: Mapped[int | None] = mapped_column(Integer)
filament_type: Mapped[str | None] = mapped_column(String(50))
filament_color: Mapped[str | None] = mapped_column(String(50))
filament_used_grams: Mapped[float | None] = mapped_column(Float)
cost: Mapped[float | None] = mapped_column(Float)
energy_kwh: Mapped[float | None] = mapped_column(Float)
energy_cost: Mapped[float | None] = mapped_column(Float)
failure_reason: Mapped[str | None] = mapped_column(String(100))
thumbnail_path: Mapped[str | None] = mapped_column(String(500))
created_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
created_by_username: Mapped[str | None] = mapped_column(String(100))
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())