Compare commits

...
3 Commits
Author SHA1 Message Date
Ian Roddis a617b935d3 Fixing formatting of magnification 2026-08-04 09:02:38 -03:00
Ian Roddis d108cb7149 Making long running operations asynchronous 2026-07-31 10:40:54 -03:00
Ian Roddis 373a2c6acf Adding favicon 2026-07-31 10:09:41 -03:00
13 changed files with 645 additions and 101 deletions

No files matched your search

+5 -1
View File
@@ -30,7 +30,11 @@
"Bash(lsof -ti:8000 -sTCP:LISTEN)", "Bash(lsof -ti:8000 -sTCP:LISTEN)",
"Bash(xargs -r kill)", "Bash(xargs -r kill)",
"Bash(rm -f /tmp/fg_cookies.txt /private/tmp/claude-501/-Users-roddis-dev-armament-fitguide/7873fd9c-2f2b-4583-877e-72fc88718f24/scratchpad/smoke_sort.py)", "Bash(rm -f /tmp/fg_cookies.txt /private/tmp/claude-501/-Users-roddis-dev-armament-fitguide/7873fd9c-2f2b-4583-877e-72fc88718f24/scratchpad/smoke_sort.py)",
"Bash(curl -s -o /dev/null -w \"%{http_code}\\\\n\" http://localhost:8000/login)" "Bash(curl -s -o /dev/null -w \"%{http_code}\\\\n\" http://localhost:8000/login)",
"Bash(curl -s -b /tmp/fg_cookies.txt http://localhost:8000__TRACKED_VAR__)",
"Bash(sqlite3 app.db \"PRAGMA foreign_keys=ON; SELECT scope_model_id FROM augmented_parts WHERE id=1862;\")",
"Bash(sqlite3 app.db \"SELECT id, name FROM scope_models WHERE name='Viper';\")",
"Bash(sqlite3 app.db \"SELECT COUNT\\(*\\) FROM augmented_parts WHERE scope_model_id=\\(SELECT id FROM scope_models WHERE name='Viper'\\);\")"
] ]
} }
} }
+19
View File
@@ -2,6 +2,7 @@ from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.models.attachment_point import AttachmentPoint from app.models.attachment_point import AttachmentPoint
from app.models.attachment_point_name import AttachmentPointName
_RELATIONSHIPS = [ _RELATIONSHIPS = [
"attachment_point_name", "attachment_point_name",
@@ -27,6 +28,24 @@ async def list_for_augmented_part(
return list(result.scalars().all()) return list(result.scalars().all())
async def list_by_augmented_parts_and_name(
db: AsyncSession, augmented_part_ids: list[int], name: str
) -> list[AttachmentPoint]:
"""AttachmentPoints named ``name`` for the given augmented parts, fetched in
one query."""
if not augmented_part_ids:
return []
result = await db.execute(
select(AttachmentPoint)
.join(AttachmentPoint.attachment_point_name)
.where(
AttachmentPoint.augmented_part_id.in_(augmented_part_ids),
AttachmentPointName.name == name,
)
)
return list(result.scalars().all())
async def list_all( async def list_all(
db: AsyncSession, skip: int = 0, limit: int = 100 db: AsyncSession, skip: int = 0, limit: int = 100
) -> list[AttachmentPoint]: ) -> list[AttachmentPoint]:
+74
View File
@@ -0,0 +1,74 @@
"""Lightweight in-process tracking for long-running admin operations.
Jobs live in a module-level dict, so this only works correctly for a single
OS process (the app's current deployment: one uvicorn worker, no multi-process
or multi-replica setup — see ``entrypoint.sh`` / ``docker-compose.yml``). If
the app is ever scaled to multiple workers/replicas, this needs to move to
shared storage (a DB table, Redis) so a poll request landing on a different
process can still see the job.
"""
from __future__ import annotations
import asyncio
import uuid
from collections.abc import Awaitable, Callable
from dataclasses import dataclass, field
from datetime import UTC, datetime
_MAX_FINISHED_AGE_SECONDS = 3600
@dataclass
class Job:
id: str
kind: str
status: str = "running" # running | done | error
message: str = "Working…"
result: object | None = None
created_at: datetime = field(default_factory=lambda: datetime.now(UTC))
finished_at: datetime | None = None
_jobs: dict[str, Job] = {}
def get(job_id: str) -> Job | None:
return _jobs.get(job_id)
def _evict_stale() -> None:
now = datetime.now(UTC)
stale = [
job_id
for job_id, job in _jobs.items()
if job.finished_at
and (now - job.finished_at).total_seconds() > _MAX_FINISHED_AGE_SECONDS
]
for job_id in stale:
del _jobs[job_id]
def start(kind: str, run: Callable[[Job], Awaitable[None]]) -> Job:
"""Create a job, launch ``run(job)`` in the background, and return it immediately.
``run`` is responsible for updating ``job.status``/``job.message``/``job.result``
to reflect the outcome, including catching its own errors. Any exception that
escapes anyway is caught here as a last resort so the job doesn't hang forever
in "running" state.
"""
_evict_stale()
job = Job(id=uuid.uuid4().hex, kind=kind)
_jobs[job.id] = job
async def _runner() -> None:
try:
await run(job)
except Exception as exc:
job.status = "error"
job.message = f"{kind} failed: {exc}"
finally:
job.finished_at = datetime.now(UTC)
asyncio.create_task(_runner())
return job
+30 -19
View File
@@ -7,6 +7,7 @@ the parts of that type that fit the row's scope.
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.crud import attachment_point as attachment_point_crud
from app.crud import augmented_part as augmented_part_crud from app.crud import augmented_part as augmented_part_crud
from app.crud import fit as fit_crud from app.crud import fit as fit_crud
from app.crud import part_type as part_type_crud from app.crud import part_type as part_type_crud
@@ -14,6 +15,7 @@ from app.crud import scope_info as scope_info_crud
from app.models.fit import FitLabel from app.models.fit import FitLabel
SCOPE_TYPE_NAME = "Scope" SCOPE_TYPE_NAME = "Scope"
OBJECTIVE_ATTACHMENT_POINT_NAME = "Objective"
# "exact" outranks "possible" when the same part fits a scope via several fits. # "exact" outranks "possible" when the same part fits a scope via several fits.
_LABEL_RANK = {FitLabel.EXACT.value: 2, FitLabel.POSSIBLE.value: 1} _LABEL_RANK = {FitLabel.EXACT.value: 2, FitLabel.POSSIBLE.value: 1}
@@ -43,6 +45,7 @@ async def build(
{ {
"scope": AugmentedPart, "scope": AugmentedPart,
"info": ScopeInfo | None, "info": ScopeInfo | None,
"objective_diameter_mm": float | None,
"cells": {part_type_id: [{"part": AugmentedPart, "label": str}]}, "cells": {part_type_id: [{"part": AugmentedPart, "label": str}]},
}, },
..., ...,
@@ -50,12 +53,8 @@ async def build(
} }
""" """
part_types = await part_type_crud.list_all(db, limit=1000) part_types = await part_type_crud.list_all(db, limit=1000)
scope_type = next( scope_type = next((pt for pt in part_types if pt.name == SCOPE_TYPE_NAME), None)
(pt for pt in part_types if pt.name == SCOPE_TYPE_NAME), None column_types = [pt for pt in part_types if scope_type is None or pt.id != scope_type.id]
)
column_types = [
pt for pt in part_types if scope_type is None or pt.id != scope_type.id
]
if scope_type is None: if scope_type is None:
return {"column_types": column_types, "rows": []} return {"column_types": column_types, "rows": []}
@@ -73,9 +72,7 @@ async def build(
if not scope_ids: if not scope_ids:
return {"column_types": column_types, "rows": []} return {"column_types": column_types, "rows": []}
fits = await fit_crud.list_for_parts( fits = await fit_crud.list_for_parts(db, list(scope_ids), exclude_label=FitLabel.NONE)
db, list(scope_ids), exclude_label=FitLabel.NONE
)
# Resolve the "other" (non-scope) part on each fit in a single batched query. # Resolve the "other" (non-scope) part on each fit in a single batched query.
other_ids: set[int] = set() other_ids: set[int] = set()
@@ -83,20 +80,31 @@ async def build(
scope_id, other_id = _scope_and_other(fit, scope_ids) scope_id, other_id = _scope_and_other(fit, scope_ids)
if scope_id is not None and other_id not in scope_ids: if scope_id is not None and other_id not in scope_ids:
other_ids.add(other_id) other_ids.add(other_id)
other_parts = { other_parts = {p.id: p for p in await augmented_part_crud.get_many(db, list(other_ids))}
p.id: p for p in await augmented_part_crud.get_many(db, list(other_ids))
}
infos = { infos = {
info.augmented_part_id: info info.augmented_part_id: info
for info in await scope_info_crud.list_by_augmented_parts(db, list(scope_ids)) for info in await scope_info_crud.list_by_augmented_parts(db, list(scope_ids))
} }
# The lens outside diameter lives on the "Objective" attachment point's
# StaticConnection, not on ScopeInfo (which only carries the objective
# *thread* spec) — see app/services/scope_import.py's column-mapping doc.
objective_diameters: dict[int, float] = {}
objective_aps = await attachment_point_crud.list_by_augmented_parts_and_name(
db, list(scope_ids), OBJECTIVE_ATTACHMENT_POINT_NAME
)
for ap in objective_aps:
if ap.static_connection is not None:
objective_diameters[ap.augmented_part_id] = ap.static_connection.od_mm
elif ap.iso_thread is not None:
objective_diameters[ap.augmented_part_id] = ap.iso_thread.diameter_mm
elif ap.unc_thread is not None:
objective_diameters[ap.augmented_part_id] = float(ap.unc_thread.diameter_fraction)
# cell_map[scope_id][type_id][other_part_id] -> {"part", "label"}, dedup'd so a # cell_map[scope_id][type_id][other_part_id] -> {"part", "label"}, dedup'd so a
# part appears once per cell, keeping its strongest fit label. # part appears once per cell, keeping its strongest fit label.
cell_map: dict[int, dict[int, dict[int, dict]]] = { cell_map: dict[int, dict[int, dict[int, dict]]] = {sid: {} for sid in scope_ids}
sid: {} for sid in scope_ids
}
for fit in fits: for fit in fits:
scope_id, other_id = _scope_and_other(fit, scope_ids) scope_id, other_id = _scope_and_other(fit, scope_ids)
if scope_id is None: if scope_id is None:
@@ -113,15 +121,18 @@ async def build(
rows = [] rows = []
for scope in scopes: for scope in scopes:
cells = { cells = {
type_id: sorted( type_id: sorted(entries.values(), key=lambda e: e["part"].description.lower())
entries.values(), key=lambda e: e["part"].description.lower()
)
for type_id, entries in cell_map[scope.id].items() for type_id, entries in cell_map[scope.id].items()
} }
if fits_part_type_id is not None and fits_part_type_id not in cells: if fits_part_type_id is not None and fits_part_type_id not in cells:
continue # scope has no fit for the required accessory type continue # scope has no fit for the required accessory type
rows.append( rows.append(
{"scope": scope, "info": infos.get(scope.id), "cells": cells} {
"scope": scope,
"info": infos.get(scope.id),
"objective_diameter_mm": objective_diameters.get(scope.id),
"cells": cells,
}
) )
return {"column_types": column_types, "rows": rows} return {"column_types": column_types, "rows": rows}
Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

+11 -5
View File
@@ -1,11 +1,17 @@
{%- macro mag(v) -%}{{ v | int if v == v | int else v | round(1) }}{%- endmacro -%} {%- macro mag(v) -%}{{ v | int if v == v | int else v | round(1) }}{%- endmacro -%}
{%- macro magnification(info) -%} {%- macro magnification_range(info) -%}
{%- if info and info.magnification_min is not none and info.magnification_max is not none -%} {%- if info and info.magnification_min is not none and info.magnification_max is not none -%}
{{ mag(info.magnification_min) }}–{{ mag(info.magnification_max) }}× {{ mag(info.magnification_min) }}-{{ mag(info.magnification_max) }}
{%- elif info and info.magnification_min is not none -%} {%- elif info and info.magnification_min is not none -%}
{{ mag(info.magnification_min) }}× {{ mag(info.magnification_min) }}
{%- elif info and info.magnification_max is not none -%} {%- elif info and info.magnification_max is not none -%}
{{ mag(info.magnification_max) }}× {{ mag(info.magnification_max) }}
{%- endif -%}
{%- endmacro -%}
{%- macro magnification(info, objective_diameter_mm) -%}
{%- set range = magnification_range(info) -%}
{%- if range -%}
{{ range }}x{% if objective_diameter_mm is not none %}{{ mag(objective_diameter_mm) }}{% endif %}
{%- else -%} {%- else -%}
<span class="muted">—</span> <span class="muted">—</span>
{%- endif -%} {%- endif -%}
@@ -37,7 +43,7 @@
<td>{{ scope.manufacturer.name if scope.manufacturer else "—" }}</td> <td>{{ scope.manufacturer.name if scope.manufacturer else "—" }}</td>
<td>{{ scope.scope_model.name if scope.scope_model else "—" }}</td> <td>{{ scope.scope_model.name if scope.scope_model else "—" }}</td>
<td>{{ scope.sub_part_type.name if scope.sub_part_type else "—" }}</td> <td>{{ scope.sub_part_type.name if scope.sub_part_type else "—" }}</td>
<td>{{ magnification(info) }}</td> <td>{{ magnification(info, row.objective_diameter_mm) }}</td>
<td> <td>
{% if info and info.objective_thread_pitch %} {% if info and info.objective_thread_pitch %}
{{ info.objective_thread_pitch }} {{ info.objective_thread_pitch }}
+7
View File
@@ -0,0 +1,7 @@
<div id="{{ container_id }}"
hx-get="{{ poll_url }}"
hx-trigger="load delay:1s"
hx-swap="outerHTML"
class="{{ extra_class or '' }}">
<p class="muted">⏳ {{ job.message }}</p>
</div>
@@ -0,0 +1,5 @@
<span id="{{ container_id }}"
hx-get="{{ poll_url }}"
hx-trigger="load delay:1s"
hx-swap="outerHTML"
class="muted">⏳ {{ job.message }}</span>
@@ -1,3 +1,4 @@
<div id="{{ container_id or 'scope-import-result-body' }}">
{% if error %} {% if error %}
<div class="card"> <div class="card">
<p style="color:var(--color-danger,#c00)">{{ error }}</p> <p style="color:var(--color-danger,#c00)">{{ error }}</p>
@@ -39,3 +40,4 @@
{% endif %} {% endif %}
</div> </div>
{% endif %} {% endif %}
</div>
+1
View File
@@ -4,6 +4,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %}{{ app_name }}{% endblock %}</title> <title>{% block title %}{{ app_name }}{% endblock %}</title>
<link rel="icon" href="/static/favicon.ico" sizes="any">
<link rel="stylesheet" href="/static/css/style.css"> <link rel="stylesheet" href="/static/css/style.css">
<script src="https://unpkg.com/htmx.org@1.9.12" <script src="https://unpkg.com/htmx.org@1.9.12"
integrity="sha384-ujb1lZYygJmzgSwoxRggbCHcjc0rB2XoQrxeTUQyRjrOnlCoYta87iKBWq3EsdM2" integrity="sha384-ujb1lZYygJmzgSwoxRggbCHcjc0rB2XoQrxeTUQyRjrOnlCoYta87iKBWq3EsdM2"
+284 -75
View File
@@ -16,7 +16,7 @@ from fastapi import (
from fastapi.responses import HTMLResponse, RedirectResponse, Response from fastapi.responses import HTMLResponse, RedirectResponse, Response
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.core.database import get_db from app.core.database import AsyncSessionLocal, get_db
from app.crud import audit as audit_crud from app.crud import audit as audit_crud
from app.crud import augmented_part as augmented_part_crud from app.crud import augmented_part as augmented_part_crud
from app.crud import augmented_part_note as augmented_part_note_crud from app.crud import augmented_part_note as augmented_part_note_crud
@@ -44,6 +44,7 @@ from app.models.unc_thread import UncSeries
from app.models.user import User from app.models.user import User
from app.services import augmented_part_sync as augmented_part_sync_service from app.services import augmented_part_sync as augmented_part_sync_service
from app.services import fit_matching as fit_matching_service from app.services import fit_matching as fit_matching_service
from app.services import jobs
from app.services import odata as odata_service from app.services import odata as odata_service
from app.services import scope_import as scope_import_service from app.services import scope_import as scope_import_service
from app.web.templates import templates from app.web.templates import templates
@@ -1469,11 +1470,21 @@ async def scope_import_page(
) )
_SCOPE_IMPORT_STATUS_ID = "scope-import-status"
def _scope_import_progress_ctx(job: jobs.Job) -> dict:
return {
"container_id": _SCOPE_IMPORT_STATUS_ID,
"poll_url": f"/admin/scope-import/jobs/{job.id}",
"job": job,
}
@router.post("/scope-import", response_class=HTMLResponse) @router.post("/scope-import", response_class=HTMLResponse)
async def upload_scope_import( async def upload_scope_import(
request: Request, request: Request,
file: UploadFile = File(...), file: UploadFile = File(...),
db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("augmented_parts:write")), actor: User = Depends(web_require("augmented_parts:write")),
): ):
filename = file.filename or "" filename = file.filename or ""
@@ -1481,33 +1492,74 @@ async def upload_scope_import(
return templates.TemplateResponse( return templates.TemplateResponse(
request, request,
"admin/_scope_import_result.html", "admin/_scope_import_result.html",
{"error": "Expected an .xlsx spreadsheet."}, {
"error": "Expected an .xlsx spreadsheet.",
"container_id": _SCOPE_IMPORT_STATUS_ID,
},
) )
data = await file.read() data = await file.read()
try: actor_id = actor.id
result = await scope_import_service.import_from_bytes(db, data)
except Exception as exc: # noqa: BLE001 async def _run(job: jobs.Job) -> None:
async with AsyncSessionLocal() as db:
try:
result = await scope_import_service.import_from_bytes(db, data)
except Exception as exc: # noqa: BLE001
job.status = "error"
job.message = f"Could not read spreadsheet: {exc}"
return
actor_row = await user_crud.get(db, actor_id)
await audit_crud.log(
db,
actor=actor_row,
action="update",
resource_type="AugmentedPart",
resource_id=None,
description=(
f"Scope import ({filename}): {result.created} created, "
f"{result.updated} updated, {result.skipped} skipped, "
f"{result.errors} errors"
),
)
job.status = "done"
job.result = result
job = jobs.start("scope_import", _run)
return templates.TemplateResponse(
request, "admin/_job_progress.html", _scope_import_progress_ctx(job)
)
@router.get("/scope-import/jobs/{job_id}", response_class=HTMLResponse)
async def scope_import_job_status(
request: Request,
job_id: str,
_: User = Depends(web_require("augmented_parts:write")),
):
job = jobs.get(job_id)
if job is None:
return templates.TemplateResponse( return templates.TemplateResponse(
request, request,
"admin/_scope_import_result.html", "admin/_scope_import_result.html",
{"error": f"Could not read spreadsheet: {exc}"}, {
"error": "Import job not found — it may have expired.",
"container_id": _SCOPE_IMPORT_STATUS_ID,
},
)
if job.status == "running":
return templates.TemplateResponse(
request, "admin/_job_progress.html", _scope_import_progress_ctx(job)
)
if job.status == "error":
return templates.TemplateResponse(
request,
"admin/_scope_import_result.html",
{"error": job.message, "container_id": _SCOPE_IMPORT_STATUS_ID},
) )
await audit_crud.log(
db,
actor=actor,
action="update",
resource_type="AugmentedPart",
resource_id=None,
description=(
f"Scope import ({filename}): {result.created} created, "
f"{result.updated} updated, {result.skipped} skipped, "
f"{result.errors} errors"
),
)
return templates.TemplateResponse( return templates.TemplateResponse(
request, request,
"admin/_scope_import_result.html", "admin/_scope_import_result.html",
{"result": result}, {"result": job.result, "container_id": _SCOPE_IMPORT_STATUS_ID},
) )
@@ -1645,38 +1697,89 @@ async def create_augmented_part(
) )
_AUGMENTED_PARTS_SYNC_TARGET_ID = "augmented-part-list"
@router.post("/augmented-parts/sync", response_class=HTMLResponse) @router.post("/augmented-parts/sync", response_class=HTMLResponse)
async def sync_augmented_parts( async def sync_augmented_parts(
request: Request, request: Request,
db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("augmented_parts:write")), actor: User = Depends(web_require("augmented_parts:write")),
): ):
try: actor_id = actor.id
result = await augmented_part_sync_service.sync(db)
await audit_crud.log( async def _run(job: jobs.Job) -> None:
db, async with AsyncSessionLocal() as db:
actor=actor, try:
action="update", result = await augmented_part_sync_service.sync(db)
resource_type="AugmentedPart", except Exception as exc:
resource_id=None, job.status = "error"
description=( job.message = f"Sync failed: {exc}"
f"Sync: {result.created} created, {result.deleted} deleted, " return
f"{result.skipped} skipped (no type match)" actor_row = await user_crud.get(db, actor_id)
), await audit_crud.log(
) db,
sync_result = ( actor=actor_row,
f"Sync complete: {result.created} created, {result.deleted} deleted" action="update",
+ ( resource_type="AugmentedPart",
f", {result.skipped} skipped (type not recognised)" resource_id=None,
if result.skipped description=(
else "" f"Sync: {result.created} created, {result.deleted} deleted, "
f"{result.skipped} skipped (no type match)"
),
) )
+ "." job.status = "done"
job.message = (
f"Sync complete: {result.created} created, {result.deleted} deleted"
+ (
f", {result.skipped} skipped (type not recognised)"
if result.skipped
else ""
)
+ "."
)
job = jobs.start("augmented_part_sync", _run)
return templates.TemplateResponse(
request,
"admin/_job_progress.html",
{
"container_id": _AUGMENTED_PARTS_SYNC_TARGET_ID,
"poll_url": f"/admin/augmented-parts/sync/jobs/{job.id}",
"job": job,
},
)
@router.get("/augmented-parts/sync/jobs/{job_id}", response_class=HTMLResponse)
async def augmented_parts_sync_job_status(
request: Request,
job_id: str,
db: AsyncSession = Depends(get_db),
_: User = Depends(web_require("augmented_parts:write")),
):
job = jobs.get(job_id)
if job is None:
form_ctx = await _augmented_part_form_ctx(db)
return templates.TemplateResponse(
request,
"admin/_augmented_parts_table.html",
{
**form_ctx,
**await _default_augmented_parts_table_ctx(db),
"sync_result": None,
"sync_error": "Sync job not found — it may have expired.",
},
)
if job.status == "running":
return templates.TemplateResponse(
request,
"admin/_job_progress.html",
{
"container_id": _AUGMENTED_PARTS_SYNC_TARGET_ID,
"poll_url": f"/admin/augmented-parts/sync/jobs/{job_id}",
"job": job,
},
) )
sync_error = None
except Exception as exc:
sync_result = None
sync_error = f"Sync failed: {exc}"
form_ctx = await _augmented_part_form_ctx(db) form_ctx = await _augmented_part_form_ctx(db)
return templates.TemplateResponse( return templates.TemplateResponse(
request, request,
@@ -1684,8 +1787,8 @@ async def sync_augmented_parts(
{ {
**form_ctx, **form_ctx,
**await _default_augmented_parts_table_ctx(db), **await _default_augmented_parts_table_ctx(db),
"sync_result": sync_result, "sync_result": job.message if job.status == "done" else None,
"sync_error": sync_error, "sync_error": job.message if job.status == "error" else None,
}, },
) )
@@ -2355,32 +2458,94 @@ async def augmented_part_fits(
return templates.TemplateResponse(request, "admin/_augmented_part_fits.html", ctx) return templates.TemplateResponse(request, "admin/_augmented_part_fits.html", ctx)
def _augmented_part_fits_target_id(augmented_part_id: int) -> str:
return f"ap-{augmented_part_id}-fits"
@router.post( @router.post(
"/augmented-parts/{augmented_part_id}/fits/recompute", response_class=HTMLResponse "/augmented-parts/{augmented_part_id}/fits/recompute", response_class=HTMLResponse
) )
async def recompute_augmented_part_fits( async def recompute_augmented_part_fits(
request: Request, request: Request,
augmented_part_id: int, augmented_part_id: int,
actor: User = Depends(web_require("fits:write")),
):
actor_id = actor.id
async def _run(job: jobs.Job) -> None:
async with AsyncSessionLocal() as db:
result = await fit_matching_service.recompute_for_part(
db, augmented_part_id
)
actor_row = await user_crud.get(db, actor_id)
await audit_crud.log(
db,
actor=actor_row,
action="update",
resource_type="Fit",
resource_id=augmented_part_id,
description=(
f"Recomputed fits for AugmentedPart #{augmented_part_id}: "
f"{result.created} created, {result.removed} removed"
),
)
job.status = "done"
job.message = f"{result.created} created, {result.removed} removed."
job = jobs.start("fit_recompute_part", _run)
return templates.TemplateResponse(
request,
"admin/_job_progress.html",
{
"container_id": _augmented_part_fits_target_id(augmented_part_id),
"poll_url": f"/admin/augmented-parts/{augmented_part_id}/fits/recompute/jobs/{job.id}",
"job": job,
"extra_class": "ap-inline-section",
},
)
@router.get(
"/augmented-parts/{augmented_part_id}/fits/recompute/jobs/{job_id}",
response_class=HTMLResponse,
)
async def augmented_part_fits_recompute_job_status(
request: Request,
augmented_part_id: int,
job_id: str,
db: AsyncSession = Depends(get_db), db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("fits:write")), actor: User = Depends(web_require("fits:write")),
): ):
result = await fit_matching_service.recompute_for_part(db, augmented_part_id) job = jobs.get(job_id)
await audit_crud.log( if job is None:
db, ctx = await _augmented_part_fits_ctx(
actor=actor, db,
action="update", augmented_part_id,
resource_type="Fit", can_write=_can_write_fits(actor),
resource_id=augmented_part_id, error="Recompute job not found — it may have expired.",
description=( )
f"Recomputed fits for AugmentedPart #{augmented_part_id}: " return templates.TemplateResponse(
f"{result.created} created, {result.removed} removed" request, "admin/_augmented_part_fits.html", ctx
), )
) if job.status == "running":
return templates.TemplateResponse(
request,
"admin/_job_progress.html",
{
"container_id": _augmented_part_fits_target_id(augmented_part_id),
"poll_url": (
f"/admin/augmented-parts/{augmented_part_id}/fits/recompute/jobs/{job_id}"
),
"job": job,
"extra_class": "ap-inline-section",
},
)
ctx = await _augmented_part_fits_ctx( ctx = await _augmented_part_fits_ctx(
db, db,
augmented_part_id, augmented_part_id,
can_write=_can_write_fits(actor), can_write=_can_write_fits(actor),
message=f"{result.created} created, {result.removed} removed.", message=job.message if job.status == "done" else None,
error=job.message if job.status == "error" else None,
) )
return templates.TemplateResponse(request, "admin/_augmented_part_fits.html", ctx) return templates.TemplateResponse(request, "admin/_augmented_part_fits.html", ctx)
@@ -2588,29 +2753,73 @@ async def delete_augmented_part_fit(
return templates.TemplateResponse(request, "admin/_augmented_part_fits.html", ctx) return templates.TemplateResponse(request, "admin/_augmented_part_fits.html", ctx)
_ALL_FITS_RECOMPUTE_STATUS_ID = "fits-recompute-status"
@router.post("/augmented-parts/fits/recompute", response_class=HTMLResponse) @router.post("/augmented-parts/fits/recompute", response_class=HTMLResponse)
async def recompute_all_fits( async def recompute_all_fits(
request: Request, request: Request,
db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("fits:write")), actor: User = Depends(web_require("fits:write")),
): ):
result = await fit_matching_service.recompute_all(db) actor_id = actor.id
await audit_crud.log(
db, async def _run(job: jobs.Job) -> None:
actor=actor, async with AsyncSessionLocal() as db:
action="update", result = await fit_matching_service.recompute_all(db)
resource_type="Fit", actor_row = await user_crud.get(db, actor_id)
resource_id=None, await audit_crud.log(
description=( db,
f"Recomputed fits for all parts: {result.created} created, " actor=actor_row,
f"{result.removed} removed" action="update",
), resource_type="Fit",
) resource_id=None,
return HTMLResponse( description=(
f"Recompute complete: {result.created} created, {result.removed} removed." f"Recomputed fits for all parts: {result.created} created, "
f"{result.removed} removed"
),
)
job.status = "done"
job.message = (
f"Recompute complete: {result.created} created, "
f"{result.removed} removed."
)
job = jobs.start("fit_recompute_all", _run)
return templates.TemplateResponse(
request,
"admin/_job_progress_inline.html",
{
"container_id": _ALL_FITS_RECOMPUTE_STATUS_ID,
"poll_url": f"/admin/augmented-parts/fits/recompute/jobs/{job.id}",
"job": job,
},
) )
@router.get(
"/augmented-parts/fits/recompute/jobs/{job_id}", response_class=HTMLResponse
)
async def recompute_all_fits_job_status(
request: Request,
job_id: str,
_: User = Depends(web_require("fits:write")),
):
job = jobs.get(job_id)
if job is None:
return HTMLResponse("Recompute job not found — it may have expired.")
if job.status == "running":
return templates.TemplateResponse(
request,
"admin/_job_progress_inline.html",
{
"container_id": _ALL_FITS_RECOMPUTE_STATUS_ID,
"poll_url": f"/admin/augmented-parts/fits/recompute/jobs/{job_id}",
"job": job,
},
)
return HTMLResponse(job.message)
# --- Augmented Part Notes ------------------------------------------------- # --- Augmented Part Notes -------------------------------------------------
+156
View File
@@ -0,0 +1,156 @@
import asyncio
import re
import pytest
from app.crud import role as role_crud
from app.models.attachment_point import AttachmentPoint
from app.models.attachment_point_name import AttachmentPointName
from app.models.augmented_part import AugmentedPart
from app.models.part_type import PartType
from tests.test_scope_import import _scope_row, _workbook
_JOB_URL_RE = re.compile(r'hx-get="([^"]+/jobs/[^"]+)"')
async def _web_login(client, email, password="secret123"):
resp = await client.post("/login", data={"email": email, "password": password})
assert resp.status_code == 303
async def _poll_until_done(client, url, max_tries=50):
"""Follow the same htmx polling loop a browser would, until no further
``hx-get`` job-status link is present in the response."""
body = ""
for _ in range(max_tries):
resp = await client.get(url)
assert resp.status_code == 200
body = resp.text
match = _JOB_URL_RE.search(body)
if not match:
return body
url = match.group(1)
await asyncio.sleep(0.02)
raise AssertionError(f"job did not finish after {max_tries} polls: {body!r}")
@pytest.mark.asyncio
async def test_scope_import_runs_in_background(
client, seeded, make_user, db_sessionmaker, monkeypatch
):
monkeypatch.setattr("app.web.admin.AsyncSessionLocal", db_sessionmaker)
async with db_sessionmaker() as db:
perms = seeded["perms"]
editor = await role_crud.create(
db,
name="scope-import-editor",
permission_ids=[perms["augmented_parts:write"].id],
)
editor_id = editor.id
await make_user("importer@example.com", role_ids=[editor_id])
await _web_login(client, "importer@example.com")
data = _workbook([_scope_row(product_number="BG-SCOPE-1")])
resp = await client.post(
"/admin/scope-import",
files={"file": ("scopes.xlsx", data, "application/octet-stream")},
)
assert resp.status_code == 200
assert "scope-import-status" in resp.text
match = _JOB_URL_RE.search(resp.text)
assert match, resp.text
final_body = await _poll_until_done(client, match.group(1))
assert "1</strong> created" in final_body
@pytest.mark.asyncio
async def test_augmented_parts_sync_runs_in_background(
client, seeded, make_user, db_sessionmaker, monkeypatch
):
monkeypatch.setattr("app.web.admin.AsyncSessionLocal", db_sessionmaker)
async with db_sessionmaker() as db:
perms = seeded["perms"]
editor = await role_crud.create(
db, name="sync-editor", permission_ids=[perms["augmented_parts:write"].id]
)
editor_id = editor.id
await make_user("syncer@example.com", role_ids=[editor_id])
await _web_login(client, "syncer@example.com")
resp = await client.post("/admin/augmented-parts/sync")
assert resp.status_code == 200
assert 'id="augmented-part-list"' in resp.text
match = _JOB_URL_RE.search(resp.text)
assert match, resp.text
final_body = await _poll_until_done(client, match.group(1))
assert "Sync complete" in final_body
@pytest.mark.asyncio
async def test_fit_recompute_for_part_runs_in_background(
client, seeded, make_user, db_sessionmaker, monkeypatch
):
monkeypatch.setattr("app.web.admin.AsyncSessionLocal", db_sessionmaker)
async with db_sessionmaker() as db:
apn = AttachmentPointName(name="Objective")
part_t = PartType(name="Scope")
db.add_all([apn, part_t])
await db.flush()
part = AugmentedPart(
description="Test scope",
colour_id=1,
manufacturer_id=1,
part_type_id=part_t.id,
)
db.add(part)
await db.flush()
db.add(
AttachmentPoint(augmented_part_id=part.id, attachment_point_name_id=apn.id)
)
await db.commit()
part_id = part.id
perms = seeded["perms"]
editor = await role_crud.create(
db,
name="fit-recompute-editor",
permission_ids=[perms["fits:write"].id, perms["augmented_parts:read"].id],
)
editor_id = editor.id
await make_user("recomputer@example.com", role_ids=[editor_id])
await _web_login(client, "recomputer@example.com")
resp = await client.post(f"/admin/augmented-parts/{part_id}/fits/recompute")
assert resp.status_code == 200
assert f'id="ap-{part_id}-fits"' in resp.text
match = _JOB_URL_RE.search(resp.text)
assert match, resp.text
final_body = await _poll_until_done(client, match.group(1))
assert "created" in final_body and "removed" in final_body
@pytest.mark.asyncio
async def test_fit_recompute_all_runs_in_background(
client, seeded, make_user, db_sessionmaker, monkeypatch
):
monkeypatch.setattr("app.web.admin.AsyncSessionLocal", db_sessionmaker)
async with db_sessionmaker() as db:
perms = seeded["perms"]
editor = await role_crud.create(
db, name="fit-recompute-all-editor", permission_ids=[perms["fits:write"].id]
)
editor_id = editor.id
await make_user("recomputerall@example.com", role_ids=[editor_id])
await _web_login(client, "recomputerall@example.com")
resp = await client.post("/admin/augmented-parts/fits/recompute")
assert resp.status_code == 200
assert "fits-recompute-status" in resp.text
match = _JOB_URL_RE.search(resp.text)
assert match, resp.text
final_body = await _poll_until_done(client, match.group(1))
assert "Recompute complete" in final_body
+51 -1
View File
@@ -1,9 +1,12 @@
import pytest import pytest
from app.models.attachment_point import AttachmentPoint
from app.models.attachment_point_name import AttachmentPointName
from app.models.augmented_part import AugmentedPart from app.models.augmented_part import AugmentedPart
from app.models.fit import Fit, FitLabel from app.models.fit import Fit, FitLabel
from app.models.part_type import PartType from app.models.part_type import PartType
from app.models.scope_info import ScopeInfo from app.models.scope_info import ScopeInfo
from app.models.static_connection import StaticConnection
from app.models.sub_part_type import SubPartType from app.models.sub_part_type import SubPartType
from app.services import scope_guide from app.services import scope_guide
@@ -199,7 +202,54 @@ async def test_scopes_page_renders_rows_and_fits(
assert "Flip cover 50mm" in body # a matching part in a fit column assert "Flip cover 50mm" in body # a matching part in a fit column
assert "fit-badge-exact" in body # the exact fit badge assert "fit-badge-exact" in body # the exact fit badge
assert "M52x0.75" in body # scope info: objective thread assert "M52x0.75" in body # scope info: objective thread
assert "3–18×" in body # magnification range, integral values un-padded assert "3-18" in body # magnification range, integral values un-padded
@pytest.mark.asyncio
async def test_build_includes_objective_diameter(db_sessionmaker):
async with db_sessionmaker() as db:
ids = await _seed_guide_data(db)
apn = AttachmentPointName(name="Objective")
db.add(apn)
await db.flush()
db.add(
AttachmentPoint(
augmented_part_id=ids["s1"].id,
attachment_point_name_id=apn.id,
static_connection=StaticConnection(od_mm=50, od_in=1.9685),
)
)
await db.commit()
guide = await scope_guide.build(db)
rows = {r["scope"].description: r for r in guide["rows"]}
assert rows["Alpha 3-18x50"]["objective_diameter_mm"] == 50
assert rows["Zeta 1-6x24"]["objective_diameter_mm"] is None
@pytest.mark.asyncio
async def test_scopes_page_renders_objective_diameter_in_magnification(
client, seeded, make_user, db_sessionmaker
):
async with db_sessionmaker() as db:
ids = await _seed_guide_data(db)
apn = AttachmentPointName(name="Objective")
db.add(apn)
await db.flush()
db.add(
AttachmentPoint(
augmented_part_id=ids["s1"].id,
attachment_point_name_id=apn.id,
static_connection=StaticConnection(od_mm=50, od_in=1.9685),
)
)
await db.commit()
await make_user("reader4@example.com", role_ids=[seeded["roles"]["user"].id])
await _web_login(client, "reader4@example.com")
resp = await client.get("/scopes")
assert resp.status_code == 200
assert "3-18x50" in resp.text
@pytest.mark.asyncio @pytest.mark.asyncio