Afficher la distribution locale des prix

This commit is contained in:
Hermes Agent
2026-09-12 19:57:27 +02:00
parent 48ab7af525
commit a37e40e640

23
app.py
View File

@@ -83,7 +83,10 @@ function render(d){
const stations=(d.stations||[]).map(s=>`<div class="station"><strong>${s.name}</strong><span class="muted">${s.city||''} · ${s.distance_km} km · màj ${s.updated||'N/A'}</span><span class="big">${s.price.toFixed(3)} €/L</span><span class="muted">${s.address||''}</span></div>`).join(''); const stations=(d.stations||[]).map(s=>`<div class="station"><strong>${s.name}</strong><span class="muted">${s.city||''} · ${s.distance_km} km · màj ${s.updated||'N/A'}</span><span class="big">${s.price.toFixed(3)} €/L</span><span class="muted">${s.address||''}</span></div>`).join('');
const reasons=(d.reasons||[]).map(x=>`<span class="pill">${x}</span>`).join(''); const reasons=(d.reasons||[]).map(x=>`<span class="pill">${x}</span>`).join('');
const news=(d.news||[]).map(n=>`<li>${n}</li>`).join(''); const news=(d.news||[]).map(n=>`<li>${n}</li>`).join('');
document.getElementById('result').innerHTML=`<div class="eyebrow">Recommandation</div><h2 class="score ${klass(d.action)}">${d.label}</h2><p>${d.summary}</p><div>${reasons}</div><h3>Stations les moins chères</h3><div class="stations">${stations||'<span class="muted">Aucune station locale trouvée.</span>'}</div><h3>Signaux externes</h3><p class="muted">Marché: ${d.market?.summary||'indisponible'} · IA: ${d.ai_used?'utilisée':'non utilisée'}</p>${news?`<ul class="news">${news}</ul>`:''}`; const stats=d.local_stats||{};
const buckets=(stats.price_buckets||[]).map(b=>`<span class="pill">${b.price.toFixed(3)} €/L · ${b.count}</span>`).join('');
const tie=stats.best_tie_count>1?`<p class="muted">${stats.best_tie_count} stations sont à égalité au meilleur prix affiché (${stats.best.toFixed(3)} €/L). Les stations ci-dessous sont les moins chères, pas toutes les stations du rayon.</p>`:'';
document.getElementById('result').innerHTML=`<div class="eyebrow">Recommandation</div><h2 class="score ${klass(d.action)}">${d.label}</h2><p>${d.summary}</p><div>${reasons}</div><h3>Résumé local</h3><p class="muted">${stats.count||0} stations comparées · min ${stats.min?.toFixed(3)||'N/A'} €/L · médiane ${stats.median?.toFixed(3)||'N/A'} €/L · max ${stats.max?.toFixed(3)||'N/A'} €/L</p><div>${buckets}</div>${tie}<h3>Stations les moins chères</h3><div class="stations">${stations||'<span class="muted">Aucune station locale trouvée.</span>'}</div><h3>Signaux externes</h3><p class="muted">Marché: ${d.market?.summary||'indisponible'} · IA: ${d.ai_used?'utilisée':'non utilisée'}</p>${news?`<ul class="news">${news}</ul>`:''}`;
} }
async function geocode(){ async function geocode(){
const q=document.getElementById('place').value.trim(); const q=document.getElementById('place').value.trim();
@@ -342,6 +345,11 @@ def advice(lat: float, lon: float, fuel: str, radius_km: float) -> dict[str, Any
f"Moyenne locale: {local_avg:.3f} €/L sur {len(stations)} stations." f"Moyenne locale: {local_avg:.3f} €/L sur {len(stations)} stations."
) )
ai_text, ai_used = ollama_summary({"label": label, "best": best.price, "avg": local_avg, "spread": spread, "market": market, "reasons": reasons}) ai_text, ai_used = ollama_summary({"label": label, "best": best.price, "avg": local_avg, "spread": spread, "market": market, "reasons": reasons})
price_counts: dict[str, int] = {}
for p in prices:
key = f"{p:.3f}"
price_counts[key] = price_counts.get(key, 0) + 1
best_tie_count = sum(1 for s in stations if round(s.price, 3) == round(best.price, 3))
return { return {
"generated_at": now_iso(), "generated_at": now_iso(),
"fuel": fuel, "fuel": fuel,
@@ -353,6 +361,19 @@ def advice(lat: float, lon: float, fuel: str, radius_km: float) -> dict[str, Any
"market": market, "market": market,
"news": news[:4], "news": news[:4],
"ai_used": ai_used, "ai_used": ai_used,
"local_stats": {
"count": len(stations),
"best": round(best.price, 3),
"best_tie_count": best_tie_count,
"average": round(local_avg, 3),
"median": round(median, 3),
"min": round(min(prices), 3),
"max": round(max(prices), 3),
"price_buckets": [
{"price": float(price), "count": count}
for price, count in sorted(price_counts.items(), key=lambda item: float(item[0]))[:12]
],
},
"stations": [s.__dict__ for s in stations[:8]], "stations": [s.__dict__ for s in stations[:8]],
} }