update applying current ai insight from dashboard-cpsp

This commit is contained in:
Alberto-Audrix committed 2026-10-05 14:25:22 +07:00
1 parent 31272035e9
commit b87f20e3b3
27 files changed
+5300 -343

No files matched your search

+25 -10
View File
@@ -421,21 +421,36 @@ def _upsert_kpis(cycle: Cycle, rows: list[dict[str, Any]]) -> int:
def _upsert_ai_insights(cycle: Cycle, rows: list[dict[str, Any]]) -> int:
import json
count = 0
for raw in rows:
row_date = _as_date(raw.get("date"))
section = str(raw.get("section") or "")[:100]
session = str(raw.get("session") or "")[:100]
if row_date is None:
continue
row_date = _as_date(raw.get("date")) or cycle.start_date
topic = str(raw.get("topic") or "")[:64]
report_type = str(raw.get("report_type") or "page")[:32]
report_period = str(raw.get("report_period") or "current")[:64]
insight_text = str(raw.get("insight_text") or "")
summary = str(raw.get("summary") or "")
alert = str(raw.get("alert") or "")[:100]
raw_citations = raw.get("citations")
if isinstance(raw_citations, (list, dict)):
citations_str = json.dumps(raw_citations, ensure_ascii=False)
elif isinstance(raw_citations, str):
citations_str = raw_citations
else:
citations_str = "[]"
AIInsight.objects.update_or_create(
cycle=cycle,
date=row_date,
section=section,
session=session,
kandang=cycle.kandang,
topic=topic,
report_type=report_type,
report_period=report_period,
defaults={
"insight_text": str(raw.get("insight_text") or ""),
"alert": str(raw.get("alert") or "")[:100],
"date": row_date,
"insight_text": insight_text,
"summary": summary,
"alert": alert,
"citations": citations_str,
},
)
count += 1