Compare commits
8 Commits
v202603071
...
v202603072
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
28e44384ef | ||
|
|
b1e008a2a4 | ||
|
|
d556ceecee | ||
|
|
f215ea8e3a | ||
|
|
b2ffad055b | ||
|
|
544891611f | ||
|
|
c30a7943aa | ||
|
|
b071199895 |
@@ -26,6 +26,7 @@ RUN mkdir -p /app/data && chown -R appuser:appuser /app
|
||||
COPY --from=build --chown=appuser:appuser /app .
|
||||
COPY --from=css-build /app/web/src/padelnomics/static/css/output.css ./web/src/padelnomics/static/css/output.css
|
||||
COPY --chown=appuser:appuser infra/supervisor/workflows.toml ./infra/supervisor/workflows.toml
|
||||
COPY --chown=appuser:appuser content/ ./content/
|
||||
USER appuser
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
ENV DATABASE_PATH=/app/data/app.db
|
||||
|
||||
@@ -42,7 +42,7 @@ do
|
||||
# The web app detects the inode change on next query — no restart needed.
|
||||
DUCKDB_PATH="${DUCKDB_PATH:-/data/padelnomics/lakehouse.duckdb}" \
|
||||
SERVING_DUCKDB_PATH="${SERVING_DUCKDB_PATH:-/data/padelnomics/analytics.duckdb}" \
|
||||
uv run python -m padelnomics.export_serving
|
||||
uv run python src/padelnomics/export_serving.py
|
||||
|
||||
) || {
|
||||
if [ -n "${ALERT_WEBHOOK_URL:-}" ]; then
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
-- 10 pts economic context — income PPS normalised to 200 ceiling
|
||||
-- 10 pts data quality — completeness discount
|
||||
--
|
||||
-- Padelnomics Opportunity Score (Marktpotenzial-Score v3, 0–100):
|
||||
-- Padelnomics Opportunity Score (Marktpotenzial-Score v4, 0–100):
|
||||
-- "Where should I build a padel court?"
|
||||
-- Computed for ALL locations — zero-court locations score highest on supply gap.
|
||||
-- H3 catchment methodology: addressable market and supply gap use a regional
|
||||
@@ -26,7 +26,9 @@
|
||||
-- 20 pts economic power — income PPS, normalised to 35,000
|
||||
-- 30 pts supply gap — inverted catchment venue density; 0 courts = full marks
|
||||
-- 15 pts catchment gap — distance to nearest padel court
|
||||
-- 10 pts sports culture — tennis courts within 25km
|
||||
-- 10 pts market validation — country-level avg market maturity (from market_scored CTE).
|
||||
-- Replaces sports culture proxy (v3: tennis data was all zeros).
|
||||
-- ES (~60/100) → ~6 pts, SE (~35/100) → ~3.5 pts, unknown → 5 pts.
|
||||
--
|
||||
-- Consumers query directly with WHERE filters:
|
||||
-- cities API: WHERE country_slug = ? AND city_slug IS NOT NULL
|
||||
@@ -130,8 +132,8 @@ with_pricing AS (
|
||||
LEFT JOIN catchment ct
|
||||
ON b.geoname_id = ct.geoname_id
|
||||
),
|
||||
-- Both scores computed from the enriched base
|
||||
scored AS (
|
||||
-- Step 1: market score only — needed first so we can aggregate country averages.
|
||||
market_scored AS (
|
||||
SELECT *,
|
||||
-- City-level venue density (from dim_cities exact count, not dim_locations spatial 5km)
|
||||
CASE WHEN population > 0
|
||||
@@ -180,8 +182,24 @@ scored AS (
|
||||
END
|
||||
, 1)
|
||||
ELSE 0
|
||||
END AS market_score,
|
||||
-- ── Opportunity Score (Marktpotenzial-Score v3, H3 catchment) ──────────
|
||||
END AS market_score
|
||||
FROM with_pricing
|
||||
),
|
||||
-- Step 2: country-level avg market maturity — used as market validation signal (10 pts).
|
||||
-- Filter to market_score > 0 (cities with padel courts only) so zero-court locations
|
||||
-- don't dilute the country signal. ES proven demand → ~60, SE struggling → ~35.
|
||||
country_market AS (
|
||||
SELECT
|
||||
country_code,
|
||||
ROUND(AVG(market_score), 1) AS country_avg_market_score
|
||||
FROM market_scored
|
||||
WHERE market_score > 0
|
||||
GROUP BY country_code
|
||||
),
|
||||
-- Step 3: add opportunity_score using country market validation signal.
|
||||
scored AS (
|
||||
SELECT ms.*,
|
||||
-- ── Opportunity Score (Marktpotenzial-Score v4, H3 catchment) ──────────
|
||||
ROUND(
|
||||
-- Addressable market (25 pts): log-scaled catchment population, ceiling 500K
|
||||
25.0 * LEAST(1.0, LN(GREATEST(catchment_population, 1)) / LN(500000))
|
||||
@@ -190,15 +208,19 @@ scored AS (
|
||||
-- Supply gap (30 pts): inverted catchment venue density
|
||||
+ 30.0 * GREATEST(0.0, 1.0 - COALESCE(
|
||||
CASE WHEN catchment_population > 0
|
||||
THEN catchment_padel_courts::DOUBLE / catchment_population * 100000
|
||||
THEN GREATEST(catchment_padel_courts, COALESCE(city_padel_venue_count, 0))::DOUBLE / catchment_population * 100000
|
||||
ELSE 0.0
|
||||
END, 0.0) / 8.0)
|
||||
-- Catchment gap (15 pts): distance to nearest court
|
||||
+ 15.0 * COALESCE(LEAST(1.0, nearest_padel_court_km / 30.0), 0.5)
|
||||
-- Sports culture (10 pts): tennis courts within 25km
|
||||
+ 10.0 * LEAST(1.0, tennis_courts_within_25km / 10.0)
|
||||
-- Market validation (10 pts): country-level avg market maturity.
|
||||
-- Replaces sports culture (v3 tennis data was all zeros = dead code).
|
||||
-- ES (~60/100): proven demand → ~6 pts. SE (~35/100): struggling → ~3.5 pts.
|
||||
-- NULL (no courts in country yet): 0.5 neutral → 5 pts (untested, not penalised).
|
||||
+ 10.0 * COALESCE(cm.country_avg_market_score / 100.0, 0.5)
|
||||
, 1) AS opportunity_score
|
||||
FROM with_pricing
|
||||
FROM market_scored ms
|
||||
LEFT JOIN country_market cm ON ms.country_code = cm.country_code
|
||||
)
|
||||
SELECT
|
||||
s.geoname_id,
|
||||
|
||||
@@ -18,13 +18,14 @@ SELECT
|
||||
country_slug,
|
||||
COUNT(*) AS city_count,
|
||||
SUM(padel_venue_count) AS total_venues,
|
||||
ROUND(AVG(market_score), 1) AS avg_market_score,
|
||||
-- Population-weighted: large cities (Madrid, Barcelona) dominate, not hundreds of small towns
|
||||
ROUND(SUM(market_score * population) / NULLIF(SUM(population), 0), 1) AS avg_market_score,
|
||||
MAX(market_score) AS top_city_market_score,
|
||||
-- Top 5 cities by venue count (prominence), then score for internal linking
|
||||
LIST(city_slug ORDER BY padel_venue_count DESC, market_score DESC NULLS LAST)[1:5] AS top_city_slugs,
|
||||
LIST(city_name ORDER BY padel_venue_count DESC, market_score DESC NULLS LAST)[1:5] AS top_city_names,
|
||||
-- Opportunity score aggregates (NULL-safe: cities without geoname_id match excluded from AVG)
|
||||
ROUND(AVG(opportunity_score), 1) AS avg_opportunity_score,
|
||||
-- Opportunity score aggregates (population-weighted: saturated megacities dominate, not hundreds of small towns)
|
||||
ROUND(SUM(opportunity_score * population) / NULLIF(SUM(population), 0), 1) AS avg_opportunity_score,
|
||||
MAX(opportunity_score) AS top_opportunity_score,
|
||||
-- Top 5 opportunity cities by population (prominence), then opportunity score
|
||||
LIST(city_slug ORDER BY population DESC, opportunity_score DESC NULLS LAST)[1:5] AS top_opportunity_slugs,
|
||||
@@ -36,6 +37,8 @@ SELECT
|
||||
-- Use the most common currency in the country (MIN is deterministic for single-currency countries)
|
||||
MIN(price_currency) AS price_currency,
|
||||
SUM(population) AS total_population,
|
||||
ROUND(SUM(lat * population) / NULLIF(SUM(population), 0), 4) AS lat,
|
||||
ROUND(SUM(lon * population) / NULLIF(SUM(population), 0), 4) AS lon,
|
||||
CURRENT_DATE AS refreshed_date
|
||||
FROM serving.pseo_city_costs_de
|
||||
GROUP BY country_code, country_name_en, country_slug
|
||||
|
||||
@@ -8,6 +8,7 @@ daily when the pipeline runs).
|
||||
from quart import Blueprint, abort, jsonify
|
||||
|
||||
from .analytics import fetch_analytics
|
||||
from .auth.routes import login_required
|
||||
from .core import fetch_all, is_flag_enabled
|
||||
|
||||
bp = Blueprint("api", __name__)
|
||||
@@ -26,6 +27,7 @@ async def _require_maps_flag() -> None:
|
||||
|
||||
|
||||
@bp.route("/markets/countries.json")
|
||||
@login_required
|
||||
async def countries():
|
||||
"""Country-level aggregates for the markets hub map."""
|
||||
await _require_maps_flag()
|
||||
@@ -96,23 +98,3 @@ async def city_venues(country_slug: str, city_slug: str):
|
||||
)
|
||||
return jsonify(rows), 200, _CACHE_HEADERS
|
||||
|
||||
|
||||
@bp.route("/opportunity/<country_slug>.json")
|
||||
async def opportunity(country_slug: str):
|
||||
"""Location-level opportunity scores for the opportunity map."""
|
||||
await _require_maps_flag()
|
||||
assert country_slug, "country_slug required"
|
||||
rows = await fetch_analytics(
|
||||
"""
|
||||
SELECT location_name, location_slug, lat, lon,
|
||||
opportunity_score, market_score,
|
||||
nearest_padel_court_km,
|
||||
padel_venue_count, population
|
||||
FROM serving.location_profiles
|
||||
WHERE country_slug = ? AND opportunity_score > 0
|
||||
ORDER BY opportunity_score DESC
|
||||
LIMIT 500
|
||||
""",
|
||||
[country_slug],
|
||||
)
|
||||
return jsonify(rows), 200, _CACHE_HEADERS
|
||||
|
||||
@@ -9,6 +9,7 @@ from jinja2 import Environment, FileSystemLoader
|
||||
from markupsafe import Markup
|
||||
from quart import Blueprint, abort, g, redirect, render_template, request
|
||||
|
||||
from ..analytics import fetch_analytics
|
||||
from ..core import (
|
||||
REPO_ROOT,
|
||||
capture_waitlist_email,
|
||||
@@ -203,6 +204,14 @@ async def markets():
|
||||
)
|
||||
|
||||
articles = await _filter_articles(q, country, region)
|
||||
map_countries = await fetch_analytics("""
|
||||
SELECT country_code, country_name_en, country_slug,
|
||||
city_count, total_venues,
|
||||
avg_market_score, avg_opportunity_score,
|
||||
lat, lon
|
||||
FROM serving.pseo_country_overview
|
||||
ORDER BY total_venues DESC
|
||||
""")
|
||||
|
||||
return await render_template(
|
||||
"markets.html",
|
||||
@@ -212,6 +221,7 @@ async def markets():
|
||||
current_q=q,
|
||||
current_country=country,
|
||||
current_region=region,
|
||||
map_countries=map_countries,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -92,27 +92,25 @@
|
||||
});
|
||||
}
|
||||
|
||||
fetch('/api/markets/countries.json')
|
||||
.then(function(r) { return r.json(); })
|
||||
.then(function(data) {
|
||||
if (!data.length) return;
|
||||
var maxV = Math.max.apply(null, data.map(function(d) { return d.total_venues; }));
|
||||
var lang = document.documentElement.lang || 'en';
|
||||
data.forEach(function(c) {
|
||||
if (!c.lat || !c.lon) return;
|
||||
var size = 12 + 44 * Math.sqrt(c.total_venues / maxV);
|
||||
var color = scoreColor(c.avg_market_score);
|
||||
var oppColor = c.avg_opportunity_score >= 60 ? '#16A34A' : (c.avg_opportunity_score >= 30 ? '#D97706' : '#3B82F6');
|
||||
var tip = '<strong>' + c.country_name_en + '</strong><br>'
|
||||
+ c.total_venues + ' venues · ' + c.city_count + ' cities<br>'
|
||||
+ '<span style="color:' + color + ';font-weight:600;">Padelnomics Market Score: ' + c.avg_market_score + '/100</span><br>'
|
||||
+ '<span style="color:' + oppColor + ';font-weight:600;">Padelnomics Opportunity Score: ' + (c.avg_opportunity_score || 0) + '/100</span>';
|
||||
L.marker([c.lat, c.lon], { icon: makeIcon(size, color) })
|
||||
.bindTooltip(tip, { className: 'map-tooltip', direction: 'top', offset: [0, -Math.round(size / 2)] })
|
||||
.on('click', function() { window.location = '/' + lang + '/markets/' + c.country_slug; })
|
||||
.addTo(map);
|
||||
});
|
||||
var data = {{ map_countries | tojson }};
|
||||
if (data.length) {
|
||||
var maxV = Math.max.apply(null, data.map(function(d) { return d.total_venues; }));
|
||||
var lang = document.documentElement.lang || 'en';
|
||||
data.forEach(function(c) {
|
||||
if (!c.lat || !c.lon) return;
|
||||
var size = 12 + 44 * Math.sqrt(c.total_venues / maxV);
|
||||
var color = scoreColor(c.avg_market_score);
|
||||
var oppColor = c.avg_opportunity_score >= 60 ? '#16A34A' : (c.avg_opportunity_score >= 30 ? '#D97706' : '#3B82F6');
|
||||
var tip = '<strong>' + c.country_name_en + '</strong><br>'
|
||||
+ c.total_venues + ' venues · ' + c.city_count + ' cities<br>'
|
||||
+ '<span style="color:' + color + ';font-weight:600;">Padelnomics Market Score: ' + c.avg_market_score + '/100</span><br>'
|
||||
+ '<span style="color:' + oppColor + ';font-weight:600;">Padelnomics Opportunity Score: ' + (c.avg_opportunity_score || 0) + '/100</span>';
|
||||
L.marker([c.lat, c.lon], { icon: makeIcon(size, color) })
|
||||
.bindTooltip(tip, { className: 'map-tooltip', direction: 'top', offset: [0, -Math.round(size / 2)] })
|
||||
.on('click', function() { window.location = '/' + lang + '/markets/' + c.country_slug; })
|
||||
.addTo(map);
|
||||
});
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
@@ -87,6 +87,46 @@ async def opportunity_map():
|
||||
return await render_template("opportunity_map.html", countries=countries)
|
||||
|
||||
|
||||
@bp.route("/opportunity-map/data")
|
||||
async def opportunity_map_data():
|
||||
"""HTMX partial: opportunity + reference data islands for Leaflet map."""
|
||||
from ..core import is_flag_enabled
|
||||
if not await is_flag_enabled("maps", default=True):
|
||||
abort(404)
|
||||
country_slug = request.args.get("country", "")
|
||||
if not country_slug:
|
||||
return ""
|
||||
opp_points = await fetch_analytics(
|
||||
"""
|
||||
SELECT location_name, location_slug, lat, lon,
|
||||
opportunity_score, market_score,
|
||||
nearest_padel_court_km, padel_venue_count, population
|
||||
FROM serving.location_profiles
|
||||
WHERE country_slug = ? AND opportunity_score > 0
|
||||
ORDER BY opportunity_score DESC
|
||||
LIMIT 500
|
||||
""",
|
||||
[country_slug],
|
||||
)
|
||||
ref_points = await fetch_analytics(
|
||||
"""
|
||||
SELECT city_name, city_slug, lat, lon,
|
||||
city_padel_venue_count AS padel_venue_count,
|
||||
market_score, population
|
||||
FROM serving.location_profiles
|
||||
WHERE country_slug = ? AND city_slug IS NOT NULL
|
||||
ORDER BY city_padel_venue_count DESC
|
||||
LIMIT 200
|
||||
""",
|
||||
[country_slug],
|
||||
)
|
||||
return await render_template(
|
||||
"partials/opportunity_map_data.html",
|
||||
opp_points=opp_points,
|
||||
ref_points=ref_points,
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/imprint")
|
||||
async def imprint():
|
||||
lang = g.get("lang", "en")
|
||||
|
||||
@@ -24,7 +24,10 @@
|
||||
|
||||
<div class="card mb-4" style="padding: 1rem 1.25rem;">
|
||||
<label class="form-label" for="opp-country-select" style="margin-bottom: 0.5rem; display:block;">Select a country</label>
|
||||
<select id="opp-country-select" class="form-input" style="max-width: 280px;">
|
||||
<select id="opp-country-select" name="country" class="form-input" style="max-width:280px;"
|
||||
hx-get="{{ url_for('public.opportunity_map_data') }}"
|
||||
hx-target="#map-data"
|
||||
hx-trigger="change">
|
||||
<option value="">— choose country —</option>
|
||||
{% for c in countries %}
|
||||
<option value="{{ c.country_slug }}">{{ c.country_name_en }}</option>
|
||||
@@ -33,6 +36,7 @@
|
||||
</div>
|
||||
|
||||
<div id="opportunity-map"></div>
|
||||
<div id="map-data" style="display:none;"></div>
|
||||
|
||||
<div class="mt-4 text-sm text-slate">
|
||||
<strong>Circle size:</strong> population |
|
||||
@@ -86,53 +90,48 @@
|
||||
: (p || '');
|
||||
}
|
||||
|
||||
function loadCountry(slug) {
|
||||
function renderMap() {
|
||||
oppLayer.clearLayers();
|
||||
refLayer.clearLayers();
|
||||
if (!slug) return;
|
||||
var oppEl = document.getElementById('opp-data');
|
||||
var refEl = document.getElementById('ref-data');
|
||||
if (!oppEl) return;
|
||||
var oppData = JSON.parse(oppEl.textContent);
|
||||
var refData = JSON.parse(refEl.textContent);
|
||||
|
||||
fetch('/api/opportunity/' + slug + '.json')
|
||||
.then(function(r) { return r.json(); })
|
||||
.then(function(data) {
|
||||
if (!data.length) return;
|
||||
var maxPop = Math.max.apply(null, data.map(function(d) { return d.population || 1; }));
|
||||
var bounds = [];
|
||||
data.forEach(function(loc) {
|
||||
if (!loc.lat || !loc.lon) return;
|
||||
var size = 8 + 40 * Math.sqrt((loc.population || 1) / maxPop);
|
||||
var color = oppColor(loc.opportunity_score);
|
||||
var dist = loc.nearest_padel_court_km != null
|
||||
? loc.nearest_padel_court_km.toFixed(1) + ' km to nearest court'
|
||||
: 'No nearby courts';
|
||||
var mktColor = loc.market_score >= 60 ? '#16A34A' : (loc.market_score >= 30 ? '#D97706' : '#DC2626');
|
||||
var tip = '<strong>' + loc.location_name + '</strong><br>'
|
||||
+ '<span style="color:' + color + ';font-weight:600;">Padelnomics Opportunity Score: ' + loc.opportunity_score + '/100</span><br>'
|
||||
+ '<span style="color:' + mktColor + ';font-weight:600;">Padelnomics Market Score: ' + (loc.market_score || 0) + '/100</span><br>'
|
||||
+ dist + ' · Pop. ' + fmtPop(loc.population);
|
||||
L.marker([loc.lat, loc.lon], { icon: makeIcon(size, color) })
|
||||
.bindTooltip(tip, { className: 'map-tooltip', direction: 'top', offset: [0, -Math.round(size / 2)] })
|
||||
.addTo(oppLayer);
|
||||
bounds.push([loc.lat, loc.lon]);
|
||||
});
|
||||
if (bounds.length) map.fitBounds(bounds, { padding: [30, 30] });
|
||||
});
|
||||
refData.forEach(function(c) {
|
||||
if (!c.lat || !c.lon || !c.padel_venue_count) return;
|
||||
L.marker([c.lat, c.lon], { icon: REF_ICON })
|
||||
.bindTooltip(c.city_name + ' — ' + c.padel_venue_count + ' existing venues',
|
||||
{ className: 'map-tooltip', direction: 'top', offset: [0, -7] })
|
||||
.addTo(refLayer);
|
||||
});
|
||||
|
||||
// Existing venues as small gray reference dots (drawn first = behind opp dots)
|
||||
fetch('/api/markets/' + slug + '/cities.json')
|
||||
.then(function(r) { return r.json(); })
|
||||
.then(function(data) {
|
||||
data.forEach(function(c) {
|
||||
if (!c.lat || !c.lon || !c.padel_venue_count) return;
|
||||
L.marker([c.lat, c.lon], { icon: REF_ICON })
|
||||
.bindTooltip(c.city_name + ' — ' + c.padel_venue_count + ' existing venues',
|
||||
{ className: 'map-tooltip', direction: 'top', offset: [0, -7] })
|
||||
.addTo(refLayer);
|
||||
});
|
||||
});
|
||||
if (!oppData.length) return;
|
||||
var maxPop = Math.max.apply(null, oppData.map(function(d) { return d.population || 1; }));
|
||||
var bounds = [];
|
||||
oppData.forEach(function(loc) {
|
||||
if (!loc.lat || !loc.lon) return;
|
||||
var size = 8 + 40 * Math.sqrt((loc.population || 1) / maxPop);
|
||||
var color = oppColor(loc.opportunity_score);
|
||||
var dist = loc.nearest_padel_court_km != null
|
||||
? loc.nearest_padel_court_km.toFixed(1) + ' km to nearest court'
|
||||
: 'No nearby courts';
|
||||
var mktColor = loc.market_score >= 60 ? '#16A34A' : (loc.market_score >= 30 ? '#D97706' : '#DC2626');
|
||||
var tip = '<strong>' + loc.location_name + '</strong><br>'
|
||||
+ '<span style="color:' + color + ';font-weight:600;">Padelnomics Opportunity Score: ' + loc.opportunity_score + '/100</span><br>'
|
||||
+ '<span style="color:' + mktColor + ';font-weight:600;">Padelnomics Market Score: ' + (loc.market_score || 0) + '/100</span><br>'
|
||||
+ dist + ' · Pop. ' + fmtPop(loc.population);
|
||||
L.marker([loc.lat, loc.lon], { icon: makeIcon(size, color) })
|
||||
.bindTooltip(tip, { className: 'map-tooltip', direction: 'top', offset: [0, -Math.round(size / 2)] })
|
||||
.addTo(oppLayer);
|
||||
bounds.push([loc.lat, loc.lon]);
|
||||
});
|
||||
if (bounds.length) map.fitBounds(bounds, { padding: [30, 30] });
|
||||
}
|
||||
|
||||
document.getElementById('opp-country-select').addEventListener('change', function() {
|
||||
loadCountry(this.value);
|
||||
document.body.addEventListener('htmx:afterSwap', function(e) {
|
||||
if (e.detail.target.id === 'map-data') renderMap();
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
<script id="opp-data" type="application/json">{{ opp_points | tojson }}</script>
|
||||
<script id="ref-data" type="application/json">{{ ref_points | tojson }}</script>
|
||||
Reference in New Issue
Block a user