Add basis workspace filters, stale-on-reimport, and variant usage metrics
This commit is contained in:
@@ -17,7 +17,7 @@ from typing import Any
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models import TryoutImportSnapshot, TryoutSnapshotQuestion, Website
|
||||
from app.models import Item, TryoutImportSnapshot, TryoutSnapshotQuestion, Website
|
||||
|
||||
SOURCE_FORMAT = "sejoli_json"
|
||||
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
|
||||
@@ -292,7 +292,8 @@ async def import_tryout_json_snapshot(payload: dict[str, Any], website_id: int,
|
||||
new_questions += 1
|
||||
continue
|
||||
|
||||
if existing.content_checksum != question["content_checksum"]:
|
||||
content_changed = existing.content_checksum != question["content_checksum"]
|
||||
if content_changed:
|
||||
existing.question_title = question["title"] or question["question"]
|
||||
existing.question_html = question["question"]
|
||||
existing.explanation_html = question["explanation"]
|
||||
@@ -313,6 +314,18 @@ async def import_tryout_json_snapshot(payload: dict[str, Any], website_id: int,
|
||||
existing.is_active = True
|
||||
existing.last_seen_at = now
|
||||
|
||||
# If source content changed, mark AI children derived from this source as stale.
|
||||
if content_changed:
|
||||
stale_variants_result = await db.execute(
|
||||
select(Item).where(
|
||||
Item.generated_by == "ai",
|
||||
Item.source_snapshot_question_id == existing.id,
|
||||
Item.variant_status.in_(["draft", "approved", "active"]),
|
||||
)
|
||||
)
|
||||
for variant in stale_variants_result.scalars().all():
|
||||
variant.variant_status = "stale"
|
||||
|
||||
removed_questions = 0
|
||||
for source_question_id, existing in existing_questions.items():
|
||||
if existing.is_active and source_question_id not in incoming_ids:
|
||||
@@ -320,6 +333,15 @@ async def import_tryout_json_snapshot(payload: dict[str, Any], website_id: int,
|
||||
existing.latest_snapshot_id = snapshot.id
|
||||
existing.last_seen_at = now
|
||||
removed_questions += 1
|
||||
stale_removed_result = await db.execute(
|
||||
select(Item).where(
|
||||
Item.generated_by == "ai",
|
||||
Item.source_snapshot_question_id == existing.id,
|
||||
Item.variant_status.in_(["draft", "approved", "active"]),
|
||||
)
|
||||
)
|
||||
for variant in stale_removed_result.scalars().all():
|
||||
variant.variant_status = "stale"
|
||||
|
||||
imported_tryouts.append(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user