AI platform insights monitoring and website analysis monitoring services added
This commit is contained in:
@@ -265,7 +265,27 @@ class OnboardingProgress:
|
||||
# Log database save confirmation
|
||||
logger.info(f"✅ DATABASE: API key for {provider} saved to database for user {self.user_id}")
|
||||
elif step.step_number == 2: # Website Analysis
|
||||
self.db_service.save_website_analysis(self.user_id, step.data, db)
|
||||
# Transform frontend data structure to match database schema
|
||||
# Frontend sends: { website: "url", analysis: {...} }
|
||||
# Database expects: { website_url: "url", ...analysis (flattened) }
|
||||
analysis_for_db = {}
|
||||
if step.data:
|
||||
# Extract website_url from 'website' or 'website_url' field
|
||||
website_url = step.data.get('website') or step.data.get('website_url')
|
||||
if website_url:
|
||||
analysis_for_db['website_url'] = website_url
|
||||
# Flatten nested 'analysis' object if it exists
|
||||
if 'analysis' in step.data and isinstance(step.data['analysis'], dict):
|
||||
analysis_for_db.update(step.data['analysis'])
|
||||
# Also include any other top-level fields (except 'website' and 'analysis')
|
||||
for key, value in step.data.items():
|
||||
if key not in ['website', 'website_url', 'analysis']:
|
||||
analysis_for_db[key] = value
|
||||
# Ensure status is set
|
||||
if 'status' not in analysis_for_db:
|
||||
analysis_for_db['status'] = 'completed'
|
||||
|
||||
self.db_service.save_website_analysis(self.user_id, analysis_for_db, db)
|
||||
logger.info(f"✅ DATABASE: Website analysis saved to database for user {self.user_id}")
|
||||
elif step.step_number == 3: # Research Preferences
|
||||
self.db_service.save_research_preferences(self.user_id, step.data, db)
|
||||
|
||||
@@ -336,8 +336,13 @@ class OnboardingDatabaseService:
|
||||
).first()
|
||||
|
||||
if existing:
|
||||
# Update existing
|
||||
existing.website_url = normalized.get('website_url', existing.website_url)
|
||||
# Update existing - only update website_url if normalized value is not empty
|
||||
# This prevents overwriting a valid URL with an empty string when step.data
|
||||
# doesn't include the website field
|
||||
normalized_url = normalized.get('website_url', '').strip() if normalized.get('website_url') else ''
|
||||
if normalized_url:
|
||||
existing.website_url = normalized_url
|
||||
# If normalized_url is empty, keep existing.website_url unchanged
|
||||
existing.writing_style = normalized.get('writing_style')
|
||||
existing.content_characteristics = normalized.get('content_characteristics')
|
||||
existing.target_audience = normalized.get('target_audience')
|
||||
@@ -522,6 +527,52 @@ class OnboardingDatabaseService:
|
||||
logger.error(f"Error getting research preferences: {e}")
|
||||
return None
|
||||
|
||||
def get_competitor_analysis(self, user_id: str, db: Session = None) -> Optional[List[Dict[str, Any]]]:
|
||||
"""Get competitor analysis data for user from onboarding."""
|
||||
session_db = db or self.db
|
||||
if not session_db:
|
||||
raise ValueError("Database session required")
|
||||
|
||||
try:
|
||||
from models.onboarding import CompetitorAnalysis
|
||||
|
||||
session = self.get_session_by_user(user_id, session_db)
|
||||
if not session:
|
||||
return None
|
||||
|
||||
# Query CompetitorAnalysis table
|
||||
competitor_records = session_db.query(CompetitorAnalysis).filter(
|
||||
CompetitorAnalysis.session_id == session.id
|
||||
).all()
|
||||
|
||||
if not competitor_records:
|
||||
return None
|
||||
|
||||
# Convert to list of dicts
|
||||
competitors = []
|
||||
for record in competitor_records:
|
||||
analysis_data = record.analysis_data or {}
|
||||
competitors.append({
|
||||
"url": record.competitor_url,
|
||||
"domain": record.competitor_domain or record.competitor_url,
|
||||
"title": analysis_data.get("title", record.competitor_domain or ""),
|
||||
"summary": analysis_data.get("summary", ""),
|
||||
"relevance_score": analysis_data.get("relevance_score", 0.5),
|
||||
"highlights": analysis_data.get("highlights", []),
|
||||
"favicon": analysis_data.get("favicon"),
|
||||
"image": analysis_data.get("image"),
|
||||
"published_date": analysis_data.get("published_date"),
|
||||
"author": analysis_data.get("author"),
|
||||
"competitive_insights": analysis_data.get("competitive_analysis", {}),
|
||||
"content_insights": analysis_data.get("content_insights", {})
|
||||
})
|
||||
|
||||
return competitors
|
||||
|
||||
except SQLAlchemyError as e:
|
||||
logger.error(f"Error getting competitor analysis: {e}")
|
||||
return None
|
||||
|
||||
def get_persona_data(self, user_id: str, db: Session = None) -> Optional[Dict[str, Any]]:
|
||||
"""Get persona data for user."""
|
||||
session_db = db or self.db
|
||||
|
||||
Reference in New Issue
Block a user