Compare commits
5 Commits
v202603071
...
v202603072
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
28e44384ef | ||
|
|
b1e008a2a4 | ||
|
|
d556ceecee | ||
|
|
f215ea8e3a | ||
|
|
c30a7943aa |
@@ -208,7 +208,7 @@ scored AS (
|
|||||||
-- Supply gap (30 pts): inverted catchment venue density
|
-- Supply gap (30 pts): inverted catchment venue density
|
||||||
+ 30.0 * GREATEST(0.0, 1.0 - COALESCE(
|
+ 30.0 * GREATEST(0.0, 1.0 - COALESCE(
|
||||||
CASE WHEN catchment_population > 0
|
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
|
ELSE 0.0
|
||||||
END, 0.0) / 8.0)
|
END, 0.0) / 8.0)
|
||||||
-- Catchment gap (15 pts): distance to nearest court
|
-- Catchment gap (15 pts): distance to nearest court
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ SELECT
|
|||||||
-- Use the most common currency in the country (MIN is deterministic for single-currency countries)
|
-- Use the most common currency in the country (MIN is deterministic for single-currency countries)
|
||||||
MIN(price_currency) AS price_currency,
|
MIN(price_currency) AS price_currency,
|
||||||
SUM(population) AS total_population,
|
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
|
CURRENT_DATE AS refreshed_date
|
||||||
FROM serving.pseo_city_costs_de
|
FROM serving.pseo_city_costs_de
|
||||||
GROUP BY country_code, country_name_en, country_slug
|
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 quart import Blueprint, abort, jsonify
|
||||||
|
|
||||||
from .analytics import fetch_analytics
|
from .analytics import fetch_analytics
|
||||||
|
from .auth.routes import login_required
|
||||||
from .core import fetch_all, is_flag_enabled
|
from .core import fetch_all, is_flag_enabled
|
||||||
|
|
||||||
bp = Blueprint("api", __name__)
|
bp = Blueprint("api", __name__)
|
||||||
@@ -26,6 +27,7 @@ async def _require_maps_flag() -> None:
|
|||||||
|
|
||||||
|
|
||||||
@bp.route("/markets/countries.json")
|
@bp.route("/markets/countries.json")
|
||||||
|
@login_required
|
||||||
async def countries():
|
async def countries():
|
||||||
"""Country-level aggregates for the markets hub map."""
|
"""Country-level aggregates for the markets hub map."""
|
||||||
await _require_maps_flag()
|
await _require_maps_flag()
|
||||||
@@ -96,23 +98,3 @@ async def city_venues(country_slug: str, city_slug: str):
|
|||||||
)
|
)
|
||||||
return jsonify(rows), 200, _CACHE_HEADERS
|
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 markupsafe import Markup
|
||||||
from quart import Blueprint, abort, g, redirect, render_template, request
|
from quart import Blueprint, abort, g, redirect, render_template, request
|
||||||
|
|
||||||
|
from ..analytics import fetch_analytics
|
||||||
from ..core import (
|
from ..core import (
|
||||||
REPO_ROOT,
|
REPO_ROOT,
|
||||||
capture_waitlist_email,
|
capture_waitlist_email,
|
||||||
@@ -203,6 +204,14 @@ async def markets():
|
|||||||
)
|
)
|
||||||
|
|
||||||
articles = await _filter_articles(q, country, region)
|
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(
|
return await render_template(
|
||||||
"markets.html",
|
"markets.html",
|
||||||
@@ -212,6 +221,7 @@ async def markets():
|
|||||||
current_q=q,
|
current_q=q,
|
||||||
current_country=country,
|
current_country=country,
|
||||||
current_region=region,
|
current_region=region,
|
||||||
|
map_countries=map_countries,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -92,10 +92,8 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fetch('/api/markets/countries.json')
|
var data = {{ map_countries | tojson }};
|
||||||
.then(function(r) { return r.json(); })
|
if (data.length) {
|
||||||
.then(function(data) {
|
|
||||||
if (!data.length) return;
|
|
||||||
var maxV = Math.max.apply(null, data.map(function(d) { return d.total_venues; }));
|
var maxV = Math.max.apply(null, data.map(function(d) { return d.total_venues; }));
|
||||||
var lang = document.documentElement.lang || 'en';
|
var lang = document.documentElement.lang || 'en';
|
||||||
data.forEach(function(c) {
|
data.forEach(function(c) {
|
||||||
@@ -112,7 +110,7 @@
|
|||||||
.on('click', function() { window.location = '/' + lang + '/markets/' + c.country_slug; })
|
.on('click', function() { window.location = '/' + lang + '/markets/' + c.country_slug; })
|
||||||
.addTo(map);
|
.addTo(map);
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
@@ -87,6 +87,46 @@ async def opportunity_map():
|
|||||||
return await render_template("opportunity_map.html", countries=countries)
|
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")
|
@bp.route("/imprint")
|
||||||
async def imprint():
|
async def imprint():
|
||||||
lang = g.get("lang", "en")
|
lang = g.get("lang", "en")
|
||||||
|
|||||||
@@ -24,7 +24,10 @@
|
|||||||
|
|
||||||
<div class="card mb-4" style="padding: 1rem 1.25rem;">
|
<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>
|
<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>
|
<option value="">— choose country —</option>
|
||||||
{% for c in countries %}
|
{% for c in countries %}
|
||||||
<option value="{{ c.country_slug }}">{{ c.country_name_en }}</option>
|
<option value="{{ c.country_slug }}">{{ c.country_name_en }}</option>
|
||||||
@@ -33,6 +36,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="opportunity-map"></div>
|
<div id="opportunity-map"></div>
|
||||||
|
<div id="map-data" style="display:none;"></div>
|
||||||
|
|
||||||
<div class="mt-4 text-sm text-slate">
|
<div class="mt-4 text-sm text-slate">
|
||||||
<strong>Circle size:</strong> population |
|
<strong>Circle size:</strong> population |
|
||||||
@@ -86,18 +90,27 @@
|
|||||||
: (p || '');
|
: (p || '');
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadCountry(slug) {
|
function renderMap() {
|
||||||
oppLayer.clearLayers();
|
oppLayer.clearLayers();
|
||||||
refLayer.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')
|
refData.forEach(function(c) {
|
||||||
.then(function(r) { return r.json(); })
|
if (!c.lat || !c.lon || !c.padel_venue_count) return;
|
||||||
.then(function(data) {
|
L.marker([c.lat, c.lon], { icon: REF_ICON })
|
||||||
if (!data.length) return;
|
.bindTooltip(c.city_name + ' — ' + c.padel_venue_count + ' existing venues',
|
||||||
var maxPop = Math.max.apply(null, data.map(function(d) { return d.population || 1; }));
|
{ 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 = [];
|
var bounds = [];
|
||||||
data.forEach(function(loc) {
|
oppData.forEach(function(loc) {
|
||||||
if (!loc.lat || !loc.lon) return;
|
if (!loc.lat || !loc.lon) return;
|
||||||
var size = 8 + 40 * Math.sqrt((loc.population || 1) / maxPop);
|
var size = 8 + 40 * Math.sqrt((loc.population || 1) / maxPop);
|
||||||
var color = oppColor(loc.opportunity_score);
|
var color = oppColor(loc.opportunity_score);
|
||||||
@@ -115,24 +128,10 @@
|
|||||||
bounds.push([loc.lat, loc.lon]);
|
bounds.push([loc.lat, loc.lon]);
|
||||||
});
|
});
|
||||||
if (bounds.length) map.fitBounds(bounds, { padding: [30, 30] });
|
if (bounds.length) map.fitBounds(bounds, { padding: [30, 30] });
|
||||||
});
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
document.getElementById('opp-country-select').addEventListener('change', function() {
|
document.body.addEventListener('htmx:afterSwap', function(e) {
|
||||||
loadCountry(this.value);
|
if (e.detail.target.id === 'map-data') renderMap();
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
</script>
|
</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