Update utils/formatters.py
Browse files- utils/formatters.py +90 -1
utils/formatters.py
CHANGED
|
@@ -120,4 +120,93 @@ def format_comparison_table(cards: list) -> str:
|
|
| 120 |
category = card.get("category", "N/A")
|
| 121 |
table += f"| {name} | {rate}x | ${amount:.2f} | {category} |\n"
|
| 122 |
|
| 123 |
-
return table
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
category = card.get("category", "N/A")
|
| 121 |
table += f"| {name} | {rate}x | ${amount:.2f} | {category} |\n"
|
| 122 |
|
| 123 |
+
return table
|
| 124 |
+
|
| 125 |
+
# utils/formatters.py
|
| 126 |
+
|
| 127 |
+
def format_analytics_metrics(analytics: Dict[str, Any]) -> tuple:
|
| 128 |
+
"""
|
| 129 |
+
Format analytics data for display
|
| 130 |
+
|
| 131 |
+
Returns:
|
| 132 |
+
Tuple of (metrics_html, table_md, insights_md, forecast_md)
|
| 133 |
+
"""
|
| 134 |
+
# Format metric cards
|
| 135 |
+
metrics_html = f"""
|
| 136 |
+
<div style="display: flex; gap: 10px; flex-wrap: wrap;">
|
| 137 |
+
<div class="metric-card" style="flex: 1; min-width: 200px;">
|
| 138 |
+
<h2>${analytics['annual_savings']}</h2>
|
| 139 |
+
<p>๐ฐ Potential Annual Savings</p>
|
| 140 |
+
</div>
|
| 141 |
+
<div class="metric-card metric-card-green" style="flex: 1; min-width: 200px;">
|
| 142 |
+
<h2>{analytics['rate_increase']}%</h2>
|
| 143 |
+
<p>๐ Rewards Rate Increase</p>
|
| 144 |
+
</div>
|
| 145 |
+
<div class="metric-card metric-card-orange" style="flex: 1; min-width: 200px;">
|
| 146 |
+
<h2>{analytics['optimized_transactions']}</h2>
|
| 147 |
+
<p>โ
Optimized Transactions</p>
|
| 148 |
+
</div>
|
| 149 |
+
<div class="metric-card metric-card-blue" style="flex: 1; min-width: 200px;">
|
| 150 |
+
<h2>{analytics['optimization_score']}/100</h2>
|
| 151 |
+
<p>โญ Optimization Score</p>
|
| 152 |
+
</div>
|
| 153 |
+
</div>
|
| 154 |
+
"""
|
| 155 |
+
|
| 156 |
+
# Format spending breakdown table
|
| 157 |
+
table_rows = []
|
| 158 |
+
for cat in analytics['category_breakdown']:
|
| 159 |
+
table_rows.append(
|
| 160 |
+
f"| {cat['category']} | ${cat['monthly_spend']:.2f} | {cat['best_card']} | "
|
| 161 |
+
f"${cat['rewards']:.2f} | {cat['rate']} |"
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
table_md = f"""
|
| 165 |
+
| Category | Monthly Spend | Best Card | Rewards | Rate |
|
| 166 |
+
|----------|---------------|-----------|---------|------|
|
| 167 |
+
{chr(10).join(table_rows)}
|
| 168 |
+
| **Total** | **${analytics['total_monthly_spend']:.2f}** | - | **${analytics['total_monthly_rewards']:.2f}** | **{analytics['average_rate']}%** |
|
| 169 |
+
"""
|
| 170 |
+
|
| 171 |
+
# Format insights
|
| 172 |
+
top_cats = "\n".join([
|
| 173 |
+
f"{i+1}. {cat['name']}: ${cat['amount']:.2f} ({cat['change']})"
|
| 174 |
+
for i, cat in enumerate(analytics['top_categories'])
|
| 175 |
+
])
|
| 176 |
+
|
| 177 |
+
insights_md = f"""
|
| 178 |
+
**๐ฅ Top Spending Categories:**
|
| 179 |
+
{top_cats}
|
| 180 |
+
|
| 181 |
+
**๐ก Optimization Opportunities:**
|
| 182 |
+
- โ
You're using optimal cards {analytics['optimization_score']}% of the time
|
| 183 |
+
- ๐ฏ Switch to Chase Freedom for Q4 5% grocery bonus
|
| 184 |
+
- โ ๏ธ Amex Gold dining cap approaching ($2,000 limit)
|
| 185 |
+
- ๐ณ Consider applying for Citi Custom Cash
|
| 186 |
+
|
| 187 |
+
**๐ Best Performing Card:**
|
| 188 |
+
{analytics['category_breakdown'],[object Object],['best_card']} - ${analytics['category_breakdown'],[object Object],['rewards']:.2f} rewards earned
|
| 189 |
+
|
| 190 |
+
**๐ Year-to-Date:**
|
| 191 |
+
- Total Rewards: ${analytics['ytd_rewards']:.2f}
|
| 192 |
+
- Potential if optimized: ${analytics['ytd_potential']:.2f}
|
| 193 |
+
- **Money left on table: ${analytics['money_left']:.2f}**
|
| 194 |
+
"""
|
| 195 |
+
|
| 196 |
+
# Format forecast
|
| 197 |
+
forecast = analytics['forecast']
|
| 198 |
+
recommendations = "\n".join([f"{i+1}. {rec}" for i, rec in enumerate(analytics['recommendations'])])
|
| 199 |
+
|
| 200 |
+
forecast_md = f"""
|
| 201 |
+
### ๐ฎ Next Month Forecast
|
| 202 |
+
|
| 203 |
+
Based on your spending patterns:
|
| 204 |
+
- **Predicted Spend:** ${forecast['next_month_spend']:.2f}
|
| 205 |
+
- **Predicted Rewards:** ${forecast['next_month_rewards']:.2f}
|
| 206 |
+
- **Cards to Watch:** {', '.join(forecast['cards_to_watch'])}
|
| 207 |
+
|
| 208 |
+
**Recommendations:**
|
| 209 |
+
{recommendations}
|
| 210 |
+
"""
|
| 211 |
+
|
| 212 |
+
return metrics_html, table_md, insights_md, forecast_md
|