feat(dashboard): redesign UI with smart polling, unified sync card, filter bar
Replace SSE with smart polling (30s idle / 3s when running). Unify sync panel into single two-row card with live progress text. Add unified filter bar (period dropdown, status pills, search) with period-total counts. Add Client/Cont tooltip for different shipping/billing persons. Add SKU mappings pct_total badges + complete/incomplete filter + 409 duplicate check. Add missing SKUs search + rescan progress UX. Migrate SQLite orders schema (shipping_name, billing_name, payment_method, delivery_method). Fix JSON_OUTPUT_DIR path for server running from project root. Fix pagination controls showing top+bottom with per-page selector (25/50/100/250). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from fastapi import APIRouter, Query, Request, UploadFile, File
|
||||
from fastapi.responses import StreamingResponse, HTMLResponse
|
||||
from fastapi.responses import StreamingResponse, HTMLResponse, JSONResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi import HTTPException
|
||||
from pydantic import BaseModel
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
@@ -49,15 +50,19 @@ async def mappings_page(request: Request):
|
||||
@router.get("/api/mappings")
|
||||
async def list_mappings(search: str = "", page: int = 1, per_page: int = 50,
|
||||
sort_by: str = "sku", sort_dir: str = "asc",
|
||||
show_deleted: bool = False):
|
||||
show_deleted: bool = False, pct_filter: str = None):
|
||||
result = mapping_service.get_mappings(search=search, page=page, per_page=per_page,
|
||||
sort_by=sort_by, sort_dir=sort_dir,
|
||||
show_deleted=show_deleted)
|
||||
show_deleted=show_deleted,
|
||||
pct_filter=pct_filter)
|
||||
# Merge product names from web_products (R4)
|
||||
skus = list({m["sku"] for m in result.get("mappings", [])})
|
||||
product_names = await sqlite_service.get_web_products_batch(skus)
|
||||
for m in result.get("mappings", []):
|
||||
m["product_name"] = product_names.get(m["sku"], "")
|
||||
# Ensure counts key is always present
|
||||
if "counts" not in result:
|
||||
result["counts"] = {"total": 0, "complete": 0, "incomplete": 0}
|
||||
return result
|
||||
|
||||
@router.post("/api/mappings")
|
||||
@@ -67,6 +72,12 @@ async def create_mapping(data: MappingCreate):
|
||||
# Mark SKU as resolved in missing_skus tracking
|
||||
await sqlite_service.resolve_missing_sku(data.sku)
|
||||
return {"success": True, **result}
|
||||
except HTTPException as e:
|
||||
can_restore = e.headers.get("X-Can-Restore") == "true" if e.headers else False
|
||||
resp: dict = {"error": e.detail}
|
||||
if can_restore:
|
||||
resp["can_restore"] = True
|
||||
return JSONResponse(status_code=e.status_code, content=resp)
|
||||
except Exception as e:
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ from datetime import datetime
|
||||
from fastapi import APIRouter, Request, BackgroundTasks
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi.responses import HTMLResponse
|
||||
from starlette.responses import StreamingResponse
|
||||
from pydantic import BaseModel
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
@@ -21,35 +20,6 @@ class ScheduleConfig(BaseModel):
|
||||
interval_minutes: int = 5
|
||||
|
||||
|
||||
# SSE streaming endpoint
|
||||
@router.get("/api/sync/stream")
|
||||
async def sync_stream(request: Request):
|
||||
"""SSE stream for real-time sync progress."""
|
||||
q = sync_service.subscribe()
|
||||
|
||||
async def event_generator():
|
||||
try:
|
||||
while True:
|
||||
# Check if client disconnected
|
||||
if await request.is_disconnected():
|
||||
break
|
||||
try:
|
||||
event = await asyncio.wait_for(q.get(), timeout=15.0)
|
||||
yield f"data: {json.dumps(event)}\n\n"
|
||||
if event.get("type") in ("completed", "failed"):
|
||||
break
|
||||
except asyncio.TimeoutError:
|
||||
yield f"data: {json.dumps({'type': 'keepalive'})}\n\n"
|
||||
finally:
|
||||
sync_service.unsubscribe(q)
|
||||
|
||||
return StreamingResponse(
|
||||
event_generator(),
|
||||
media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"}
|
||||
)
|
||||
|
||||
|
||||
# API endpoints
|
||||
@router.post("/api/sync/start")
|
||||
async def start_sync(background_tasks: BackgroundTasks):
|
||||
@@ -72,10 +42,68 @@ async def stop_sync():
|
||||
|
||||
@router.get("/api/sync/status")
|
||||
async def sync_status():
|
||||
"""Get current sync status."""
|
||||
"""Get current sync status with progress details and last_run info."""
|
||||
status = await sync_service.get_sync_status()
|
||||
stats = await sqlite_service.get_dashboard_stats()
|
||||
return {**status, "stats": stats}
|
||||
|
||||
# Build last_run from most recent completed/failed sync_runs row
|
||||
current_run_id = status.get("run_id")
|
||||
last_run = None
|
||||
try:
|
||||
from ..database import get_sqlite
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
if current_run_id:
|
||||
cursor = await db.execute("""
|
||||
SELECT * FROM sync_runs
|
||||
WHERE status IN ('completed', 'failed') AND run_id != ?
|
||||
ORDER BY started_at DESC LIMIT 1
|
||||
""", (current_run_id,))
|
||||
else:
|
||||
cursor = await db.execute("""
|
||||
SELECT * FROM sync_runs
|
||||
WHERE status IN ('completed', 'failed')
|
||||
ORDER BY started_at DESC LIMIT 1
|
||||
""")
|
||||
row = await cursor.fetchone()
|
||||
if row:
|
||||
row_dict = dict(row)
|
||||
duration_seconds = None
|
||||
if row_dict.get("started_at") and row_dict.get("finished_at"):
|
||||
try:
|
||||
dt_start = datetime.fromisoformat(row_dict["started_at"])
|
||||
dt_end = datetime.fromisoformat(row_dict["finished_at"])
|
||||
duration_seconds = int((dt_end - dt_start).total_seconds())
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
last_run = {
|
||||
"run_id": row_dict.get("run_id"),
|
||||
"started_at": row_dict.get("started_at"),
|
||||
"finished_at": row_dict.get("finished_at"),
|
||||
"duration_seconds": duration_seconds,
|
||||
"status": row_dict.get("status"),
|
||||
"imported": row_dict.get("imported", 0),
|
||||
"skipped": row_dict.get("skipped", 0),
|
||||
"errors": row_dict.get("errors", 0),
|
||||
}
|
||||
finally:
|
||||
await db.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Ensure all expected keys are present
|
||||
result = {
|
||||
"status": status.get("status", "idle"),
|
||||
"run_id": status.get("run_id"),
|
||||
"started_at": status.get("started_at"),
|
||||
"finished_at": status.get("finished_at"),
|
||||
"phase": status.get("phase"),
|
||||
"phase_text": status.get("phase_text"),
|
||||
"progress_current": status.get("progress_current", 0),
|
||||
"progress_total": status.get("progress_total", 0),
|
||||
"counts": status.get("counts", {"imported": 0, "skipped": 0, "errors": 0}),
|
||||
"last_run": last_run,
|
||||
}
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/api/sync/history")
|
||||
@@ -277,8 +305,13 @@ async def order_detail(order_number: str):
|
||||
async def dashboard_orders(page: int = 1, per_page: int = 50,
|
||||
search: str = "", status: str = "all",
|
||||
sort_by: str = "order_date", sort_dir: str = "desc",
|
||||
period_days: int = 7):
|
||||
"""Get orders for dashboard, enriched with invoice data. period_days=0 means all time."""
|
||||
period_days: int = 7,
|
||||
period_start: str = "", period_end: str = ""):
|
||||
"""Get orders for dashboard, enriched with invoice data.
|
||||
|
||||
period_days=0 with period_start/period_end uses custom date range.
|
||||
period_days=0 without dates means all time.
|
||||
"""
|
||||
is_uninvoiced_filter = (status == "UNINVOICED")
|
||||
|
||||
# For UNINVOICED: fetch all IMPORTED orders, then filter post-invoice-check
|
||||
@@ -289,7 +322,9 @@ async def dashboard_orders(page: int = 1, per_page: int = 50,
|
||||
result = await sqlite_service.get_orders(
|
||||
page=fetch_page, per_page=fetch_per_page, search=search,
|
||||
status_filter=fetch_status, sort_by=sort_by, sort_dir=sort_dir,
|
||||
period_days=period_days
|
||||
period_days=period_days,
|
||||
period_start=period_start if period_days == 0 else "",
|
||||
period_end=period_end if period_days == 0 else "",
|
||||
)
|
||||
|
||||
# Enrich imported orders with invoice data from Oracle
|
||||
@@ -309,12 +344,22 @@ async def dashboard_orders(page: int = 1, per_page: int = 50,
|
||||
else:
|
||||
o["invoice"] = None
|
||||
|
||||
# Count uninvoiced (IMPORTED without invoice)
|
||||
uninvoiced_count = sum(
|
||||
# Add shipping/billing name fields + is_different_person flag
|
||||
s_name = o.get("shipping_name") or ""
|
||||
b_name = o.get("billing_name") or ""
|
||||
o["shipping_name"] = s_name
|
||||
o["billing_name"] = b_name
|
||||
o["is_different_person"] = bool(s_name and b_name and s_name != b_name)
|
||||
|
||||
# Build period-total counts (across all pages, same filters)
|
||||
nefacturate_count = sum(
|
||||
1 for o in all_orders
|
||||
if o.get("status") == "IMPORTED" and not o.get("invoice")
|
||||
)
|
||||
result["counts"]["uninvoiced"] = uninvoiced_count
|
||||
# Use counts from sqlite_service (already period-scoped) and add nefacturate
|
||||
counts = result.get("counts", {})
|
||||
counts["nefacturate"] = nefacturate_count
|
||||
counts.setdefault("total", counts.get("imported", 0) + counts.get("skipped", 0) + counts.get("error", 0))
|
||||
|
||||
# For UNINVOICED filter: apply server-side filtering + pagination
|
||||
if is_uninvoiced_filter:
|
||||
@@ -327,7 +372,16 @@ async def dashboard_orders(page: int = 1, per_page: int = 50,
|
||||
result["per_page"] = per_page
|
||||
result["pages"] = (total + per_page - 1) // per_page if total > 0 else 0
|
||||
|
||||
return result
|
||||
# Reshape response
|
||||
return {
|
||||
"orders": result["orders"],
|
||||
"pagination": {
|
||||
"page": result.get("page", page),
|
||||
"per_page": result.get("per_page", per_page),
|
||||
"total_pages": result.get("pages", 0),
|
||||
},
|
||||
"counts": counts,
|
||||
}
|
||||
|
||||
|
||||
@router.put("/api/sync/schedule")
|
||||
|
||||
@@ -16,7 +16,10 @@ async def scan_and_validate():
|
||||
orders, json_count = order_reader.read_json_orders()
|
||||
|
||||
if not orders:
|
||||
return {"orders": 0, "json_files": json_count, "skus": {}, "message": "No orders found"}
|
||||
return {
|
||||
"orders": 0, "json_files": json_count, "skus": {}, "message": "No orders found",
|
||||
"total_skus_scanned": 0, "new_missing": 0, "auto_resolved": 0, "unchanged": 0,
|
||||
}
|
||||
|
||||
all_skus = order_reader.get_all_skus(orders)
|
||||
result = validation_service.validate_skus(all_skus)
|
||||
@@ -37,6 +40,7 @@ async def scan_and_validate():
|
||||
if customer not in sku_context[sku]["customers"]:
|
||||
sku_context[sku]["customers"].append(customer)
|
||||
|
||||
new_missing = 0
|
||||
for sku in result["missing"]:
|
||||
# Find product name from orders
|
||||
product_name = ""
|
||||
@@ -49,13 +53,19 @@ async def scan_and_validate():
|
||||
break
|
||||
|
||||
ctx = sku_context.get(sku, {})
|
||||
await sqlite_service.track_missing_sku(
|
||||
tracked = await sqlite_service.track_missing_sku(
|
||||
sku=sku,
|
||||
product_name=product_name,
|
||||
order_count=len(ctx.get("order_numbers", [])),
|
||||
order_numbers=json.dumps(ctx.get("order_numbers", [])),
|
||||
customers=json.dumps(ctx.get("customers", []))
|
||||
)
|
||||
if tracked:
|
||||
new_missing += 1
|
||||
|
||||
total_skus_scanned = len(all_skus)
|
||||
new_missing_count = len(result["missing"])
|
||||
unchanged = total_skus_scanned - new_missing_count
|
||||
|
||||
return {
|
||||
"json_files": json_count,
|
||||
@@ -64,6 +74,11 @@ async def scan_and_validate():
|
||||
"importable": len(importable),
|
||||
"skipped": len(skipped),
|
||||
"new_orders": len(new_orders),
|
||||
# Fields consumed by the rescan progress banner in missing_skus.html
|
||||
"total_skus_scanned": total_skus_scanned,
|
||||
"new_missing": new_missing_count,
|
||||
"auto_resolved": 0,
|
||||
"unchanged": unchanged,
|
||||
"skus": {
|
||||
"mapped": len(result["mapped"]),
|
||||
"direct": len(result["direct"]),
|
||||
@@ -88,20 +103,35 @@ async def scan_and_validate():
|
||||
async def get_missing_skus(
|
||||
page: int = Query(1, ge=1),
|
||||
per_page: int = Query(20, ge=1, le=100),
|
||||
resolved: int = Query(0, ge=-1, le=1)
|
||||
resolved: int = Query(0, ge=-1, le=1),
|
||||
search: str = Query(None)
|
||||
):
|
||||
"""Get paginated missing SKUs. resolved=-1 means show all (R10)."""
|
||||
result = await sqlite_service.get_missing_skus_paginated(page, per_page, resolved)
|
||||
# Backward compat: also include 'unresolved' count
|
||||
"""Get paginated missing SKUs. resolved=-1 means show all (R10).
|
||||
Optional search filters by sku or product_name."""
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
cursor = await db.execute(
|
||||
"SELECT COUNT(*) FROM missing_skus WHERE resolved = 0"
|
||||
)
|
||||
unresolved = (await cursor.fetchone())[0]
|
||||
# Compute counts across ALL records (unfiltered by search)
|
||||
cursor = await db.execute("SELECT COUNT(*) FROM missing_skus WHERE resolved = 0")
|
||||
unresolved_count = (await cursor.fetchone())[0]
|
||||
cursor = await db.execute("SELECT COUNT(*) FROM missing_skus WHERE resolved = 1")
|
||||
resolved_count = (await cursor.fetchone())[0]
|
||||
cursor = await db.execute("SELECT COUNT(*) FROM missing_skus")
|
||||
total_count = (await cursor.fetchone())[0]
|
||||
finally:
|
||||
await db.close()
|
||||
result["unresolved"] = unresolved
|
||||
|
||||
counts = {
|
||||
"total": total_count,
|
||||
"unresolved": unresolved_count,
|
||||
"resolved": resolved_count,
|
||||
}
|
||||
|
||||
result = await sqlite_service.get_missing_skus_paginated(page, per_page, resolved, search=search)
|
||||
# Backward compat
|
||||
result["unresolved"] = unresolved_count
|
||||
result["counts"] = counts
|
||||
# rename key for JS consistency
|
||||
result["skus"] = result.get("missing_skus", [])
|
||||
return result
|
||||
|
||||
@router.get("/missing-skus-csv")
|
||||
|
||||
Reference in New Issue
Block a user