feat: Add InvestorInsightCache model and implement caching for investor insights

This commit is contained in:
bolade
2025-10-17 23:15:57 +01:00
parent 390ecee9ed
commit 228a67cc49
9 changed files with 233 additions and 162 deletions
+56 -14
View File
@@ -1,7 +1,8 @@
from datetime import datetime, timedelta
from typing import Optional
from db.db import get_db
from db.models import InvestorTable, ProjectTable
from db.models import InvestorInsightCache, InvestorTable, ProjectTable
from fastapi import APIRouter, Depends, HTTPException
from schemas.insight_schema import InsightResponse
from services.compatibility_score import (
@@ -39,19 +40,60 @@ async def get_insights(
status_code=404, detail=f"Investor with id {investor_id} not found"
)
# Initialize the query processor for insights
query_processor = QueryProcessor()
# Get investment pattern analysis and market position using web search
insights = await query_processor.get_investor_insights(
investor_name=investor.name,
investor_website=investor.website,
investor_description=investor.description,
investor_headquarters=investor.headquarters,
investment_thesis=investor.investment_thesis,
portfolio_highlights=investor.portfolio_highlights,
# Check if we have cached insights
cached_insights = (
db.query(InvestorInsightCache)
.filter(InvestorInsightCache.investor_id == investor_id)
.first()
)
# Determine if cache needs refresh (older than 1 month)
needs_refresh = True
if cached_insights:
# Calculate if cache is older than 1 month
cache_age = (
datetime.now(cached_insights.last_refreshed.tzinfo)
- cached_insights.last_refreshed
)
needs_refresh = cache_age > timedelta(days=30)
# Fetch new insights if needed
if needs_refresh:
# Initialize the query processor for insights
query_processor = QueryProcessor()
# Get investment pattern analysis and market position using web search
insights = await query_processor.get_investor_insights(
investor_name=investor.name,
investor_website=investor.website,
investor_description=investor.description,
investor_headquarters=investor.headquarters,
investment_thesis=investor.investment_thesis,
portfolio_highlights=investor.portfolio_highlights,
)
# Update or create cache entry
if cached_insights:
# Update existing cache
cached_insights.investment_pattern_analysis = insights[
"investment_pattern_analysis"
]
cached_insights.market_position = insights["market_position"]
cached_insights.last_refreshed = datetime.now(
cached_insights.last_refreshed.tzinfo
)
else:
# Create new cache entry
cached_insights = InvestorInsightCache(
investor_id=investor_id,
investment_pattern_analysis=insights["investment_pattern_analysis"],
market_position=insights["market_position"],
)
db.add(cached_insights)
db.commit()
db.refresh(cached_insights)
# Calculate compatibility score if project_id is provided
compatibility_score = None
if project_id:
@@ -74,7 +116,7 @@ async def get_insights(
compatibility_score = "Select a project to see compatibility analysis"
return InsightResponse(
investment_pattern_analysis=insights["investment_pattern_analysis"],
market_position=insights["market_position"],
investment_pattern_analysis=cached_insights.investment_pattern_analysis,
market_position=cached_insights.market_position,
compatibility_score=compatibility_score,
)