124 lines
4.0 KiB
Python
124 lines
4.0 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db import SessionLocal, init_db
|
|
from app.models import Comment, ContentItem, Hotspot, Report, Task, utc_now
|
|
|
|
|
|
FIXTURE_PATH = Path(__file__).resolve().parent / "fixtures" / "demo_seed.json"
|
|
|
|
|
|
def seed_demo_data(session: Session, fixture_path: Path = FIXTURE_PATH) -> str:
|
|
data = json.loads(fixture_path.read_text(encoding="utf-8"))
|
|
task_data = data["task"]
|
|
task_id = task_data["id"]
|
|
existing = session.scalar(select(Task.id).where(Task.id == task_id))
|
|
if existing:
|
|
return task_id
|
|
|
|
task = Task(
|
|
id=task_id,
|
|
platform=task_data["platform"],
|
|
status="success",
|
|
current_stage="success",
|
|
last_progress_at=utc_now(),
|
|
hotspot_limit=task_data["hotspot_limit"],
|
|
item_limit_per_hotspot=task_data["item_limit_per_hotspot"],
|
|
comment_limit_per_item=task_data["comment_limit_per_item"],
|
|
total_items_count=len(data["items"]),
|
|
processed_items_count=len(data["items"]),
|
|
successful_items_count=len(data["items"]),
|
|
failed_items_count=0,
|
|
analysis_success_rate=1.0,
|
|
analysis_status="normal",
|
|
)
|
|
session.add(task)
|
|
|
|
for hotspot_data in data["hotspots"]:
|
|
session.add(
|
|
Hotspot(
|
|
id=hotspot_data["id"],
|
|
task_id=task_id,
|
|
platform=task.platform,
|
|
rank=hotspot_data["rank"],
|
|
title=hotspot_data["title"],
|
|
heat_value=hotspot_data.get("heat_value"),
|
|
source_hot_id=None,
|
|
raw_data="{}",
|
|
)
|
|
)
|
|
|
|
for item_data in data["items"]:
|
|
session.add(
|
|
ContentItem(
|
|
id=item_data["id"],
|
|
task_id=task_id,
|
|
hotspot_id=item_data["hotspot_id"],
|
|
platform=task.platform,
|
|
source_item_id=f"demo-item-{item_data['id']}",
|
|
item_type=item_data["item_type"],
|
|
title=item_data["title"],
|
|
summary=item_data.get("summary"),
|
|
url=None,
|
|
status="success",
|
|
raw_data="{}",
|
|
)
|
|
)
|
|
|
|
for comment_data in data["comments"]:
|
|
session.add(
|
|
Comment(
|
|
id=comment_data["id"],
|
|
task_id=task_id,
|
|
hotspot_id=comment_data["hotspot_id"],
|
|
content_item_id=comment_data["content_item_id"],
|
|
platform=task.platform,
|
|
source_comment_id=None,
|
|
content=comment_data["content"],
|
|
author=None,
|
|
like_count=comment_data.get("like_count", 0),
|
|
sentiment=comment_data["sentiment"],
|
|
labels=json.dumps(comment_data["labels"], ensure_ascii=False),
|
|
reason=comment_data.get("reason"),
|
|
ai_analysis_status="success",
|
|
raw_data="{}",
|
|
ai_raw_response=None,
|
|
)
|
|
)
|
|
|
|
for report_data in data["reports"]:
|
|
session.add(
|
|
Report(
|
|
task_id=task_id,
|
|
hotspot_id=report_data.get("hotspot_id"),
|
|
content_item_id=report_data.get("content_item_id"),
|
|
report_type=report_data["report_type"],
|
|
title=report_data["title"],
|
|
metrics_json=json.dumps(report_data["metrics"], ensure_ascii=False),
|
|
typical_comments_json=json.dumps(report_data["typical_comments"], ensure_ascii=False),
|
|
summary=report_data["summary"],
|
|
markdown_content=report_data["markdown_content"],
|
|
data="{}",
|
|
markdown=report_data["markdown_content"],
|
|
)
|
|
)
|
|
|
|
session.commit()
|
|
return task_id
|
|
|
|
|
|
def main() -> None:
|
|
init_db()
|
|
with SessionLocal() as session:
|
|
task_id = seed_demo_data(session)
|
|
print(f"Seeded demo task: {task_id}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|