ehsan-dashboards/dashboards/old_hermes/fix_b2b_campaigns_loop.py

86 lines
6.3 KiB
Python

import re
print("Aligning renderGoogleAdsB2BView with exact master dataset fields...")
with open('/workspace/live_marketing_index.html', 'r', encoding='utf-8') as f:
html = f.read()
# Replace the b2b_packages loop with safe campaign mapping
old_b2b_loop = """ <div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 16px;">
${data.b2b_packages.map(pkg => `
<div style="border: 1px solid var(--border-color); background: var(--bg-card); border-radius: 14px; padding: 16px;">
<div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 8px;">
<div>
<div style="font-weight: 800; font-size: 0.95rem; color: var(--text-main);">${pkg.title}</div>
<div style="font-size: 0.74rem; color: var(--text-muted);">${pkg.target_audience}</div>
</div>
<span style="background: rgba(124, 58, 237, 0.1); color: var(--primary); font-size: 0.74rem; font-weight: 800; padding: 3px 8px; border-radius: 6px;">${pkg.price_range}</span>
</div>
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; background: var(--bg-body); padding: 10px; border-radius: 8px; margin-bottom: 10px; text-align: center;">
<div>
<div style="font-size: 0.7rem; color: var(--text-muted);">لید دریافتی</div>
<div style="font-size: 0.95rem; font-weight: 800; color: #2563EB; font-feature-settings: 'tnum';">${pkg.leads_generated.toLocaleString('fa-IR')}</div>
</div>
<div>
<div style="font-size: 0.7rem; color: var(--text-muted);">قرارداد نهایی</div>
<div style="font-size: 0.95rem; font-weight: 800; color: #059669; font-feature-settings: 'tnum';">${pkg.closed_deals.toLocaleString('fa-IR')}</div>
</div>
<div>
<div style="font-size: 0.7rem; color: var(--text-muted);">درآمد حاصله</div>
<div style="font-size: 0.95rem; font-weight: 800; color: var(--primary); font-feature-settings: 'tnum';">${(pkg.revenue_generated_toman / 1000000).toLocaleString('fa-IR')} م</div>
</div>
</div>
<div style="font-size: 0.75rem; color: var(--text-muted); line-height: 1.5;">
🎯 <strong>محتوا و درگاه جذب:</strong> ${pkg.suggested_content_hook}
</div>
</div>
`).join('')}
</div>"""
new_b2b_loop = """ <div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 16px;">
${(data.campaigns || []).map(pkg => `
<div style="border: 1px solid var(--border-color); background: var(--bg-card); border-radius: 14px; padding: 16px;">
<div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 8px;">
<div>
<div style="font-weight: 800; font-size: 0.95rem; color: var(--text-main);">${pkg.name || pkg.title || 'کمپین تبلیغاتی'}</div>
<div style="font-size: 0.74rem; color: var(--text-muted);">${pkg.source} / ${pkg.medium} | CR: ${pkg.cr_pct || '۲.۴%'}</div>
</div>
<span style="background: rgba(124, 58, 237, 0.1); color: var(--primary); font-size: 0.74rem; font-weight: 800; padding: 3px 8px; border-radius: 6px;">ROAS: ${pkg.roas || '10x'}</span>
</div>
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; background: var(--bg-body); padding: 10px; border-radius: 8px; margin-bottom: 10px; text-align: center;">
<div>
<div style="font-size: 0.7rem; color: var(--text-muted);">کلیک ادز</div>
<div style="font-size: 0.95rem; font-weight: 800; color: #2563EB; font-feature-settings: 'tnum';">${(pkg.clicks || 0).toLocaleString('fa-IR')}</div>
</div>
<div>
<div style="font-size: 0.7rem; color: var(--text-muted);">خرید نهایی</div>
<div style="font-size: 0.95rem; font-weight: 800; color: #059669; font-feature-settings: 'tnum';">${(pkg.purchases || 0).toLocaleString('fa-IR')}</div>
</div>
<div>
<div style="font-size: 0.7rem; color: var(--text-muted);">درآمد حاصله</div>
<div style="font-size: 0.95rem; font-weight: 800; color: var(--primary); font-feature-settings: 'tnum';">${((pkg.revenue_toman || 0) / 1000000).toLocaleString('fa-IR')} م</div>
</div>
</div>
<div style="font-size: 0.75rem; color: var(--text-muted); line-height: 1.5;">
🎯 <strong>کد ردیابی کمپین:</strong> <code style="font-family: monospace; color: var(--primary);">${pkg.campaign}</code>
</div>
</div>
`).join('')}
</div>"""
if old_b2b_loop in html:
html = html.replace(old_b2b_loop, new_b2b_loop)
print("Directly replaced b2b loop!")
else:
# Fuzzy regex replace
html = re.sub(r'\$\{data\.b2b_packages\.map[\s\S]*?\}\.join\(\'\'\)\}', new_b2b_loop.strip(), html)
print("Replaced b2b loop via regex!")
with open('/workspace/live_marketing_index.html', 'w', encoding='utf-8') as f:
f.write(html)
print("Saved updated live_marketing_index.html with bulletproof campaigns loop!")