fix(cache): Serialize Category object to .name for stable hash
Some checks are pending
NordaBiz Tests / Unit & Integration Tests (push) Waiting to run
NordaBiz Tests / E2E Tests (Playwright) (push) Blocked by required conditions
NordaBiz Tests / Smoke Tests (Production) (push) Blocked by required conditions
NordaBiz Tests / Send Failure Notification (push) Blocked by required conditions

company.category is a SQLAlchemy Category object. json.dumps(default=str)
converted it to '<database.Category object at 0x...>' with a different
memory address on each request, causing cache hash to always differ.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Maciej Pienczyn 2026-02-07 13:58:35 +01:00
parent 4ff386fa7d
commit c99c4ac8dd

View File

@ -67,7 +67,7 @@ def _collect_seo_data(db, company) -> dict:
return {
'company_name': company.name,
'company_category': company.category,
'company_category': company.category.name if company.category else None,
'website': company.website,
'city': company.address_city,
# PageSpeed scores
@ -136,7 +136,7 @@ def _collect_gbp_data(db, company) -> dict:
return {
'company_name': company.name,
'company_category': company.category,
'company_category': company.category.name if company.category else None,
'city': company.address_city,
'completeness_score': audit.completeness_score,
# Field presence
@ -199,7 +199,7 @@ def _collect_social_data(db, company) -> dict:
return {
'company_name': company.name,
'company_category': company.category,
'company_category': company.category.name if company.category else None,
'city': company.address_city,
'platforms_present': present,
'platforms_missing': missing,
@ -596,12 +596,7 @@ def generate_analysis(company_id: int, audit_type: str, user_id: int = None, for
).first()
if cache:
logger.info(f"Cache check: stored_hash={cache.audit_data_hash[:12]}... current_hash={data_hash[:12]}... match={cache.audit_data_hash == data_hash} expires={cache.expires_at}")
if cache.audit_data_hash != data_hash:
# Debug: find which fields changed
for k, v in sorted(hash_data.items()):
field_hash = hashlib.sha256(json.dumps({k: v}, default=str).encode()).hexdigest()[:8]
logger.info(f" field_hash: {k}={field_hash} type={type(v).__name__} val={str(v)[:80]}")
logger.info(f"Cache check: stored_hash={cache.audit_data_hash[:12]}... current_hash={data_hash[:12]}... match={cache.audit_data_hash == data_hash}")
if cache and cache.audit_data_hash == data_hash and cache.expires_at and cache.expires_at > datetime.now():
logger.info(f"AI analysis cache hit for company {company_id} audit_type={audit_type}")
@ -734,7 +729,7 @@ def generate_content(company_id: int, action_type: str, context: dict = None, us
# Build context for prompt
prompt_context = {
'company_name': company.name,
'category': company.category or 'Usługi',
'category': (company.category.name if company.category else None) or 'Usługi',
'city': company.address_city or 'Polska',
'website': company.website or '',
'phone': company.phone or '',