Files
ai/object/geo_centroids.go
5885db5777 feat(traffic): native request-geo aggregate + public /v1/traffic/globe (#103)
In-process, memory-bounded minute-ring folds each inbound /v1 API hit into a
(country, region, service-class) counter — AGGREGATES ONLY, never an IP: Record
has no IP parameter, geo comes from the edge CF-IPCountry / CF-Region-Code
headers, CF-Connecting-IP is never read. A slot is reset on ring reuse, so memory
is bounded to 24h of minute buckets; zero DB writes on the hot path (an O(1) map
bump under a short lock, tapped by a BeforeRouter filter).

GET /v1/traffic/globe returns {window, points[{country,region?,lat,lon,count,
byService}], totals{rps_1m,rpm_60m,top_countries}} with lat/lon from an embedded
country + US-state centroid table (no external geo service). Public: auth-exempt
(a non get-/update- controller name the authz filter passes through) and
balance-exempt — same class as /v1/router/stats?scope=platform. Powers the
world.hanzo.ai "Hanzo mode" live-traffic globe.

Tests: aggregation/grouping/by-service, throughput rates, ring rotation reset,
window filtering, unknown-country counts-total-not-points, the no-IP invariant
(marshal + dotted-quad scan), and the wire-shape contract.


Co-authored-by: hanzo-dev <[email protected]>
2026-07-16 15:18:31 -07:00

129 lines
8.9 KiB
Go

// Copyright 2023-2026 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package object
// Embedded geo centroid tables for the live-traffic globe. Deliberately small and
// self-contained: NO external geo service, NO network call, NO per-IP lookup — the
// aggregate keys on the edge-supplied ISO country (and optional subdivision) code,
// and this table turns that code into an approximate map position. Country
// centroids are approximate geographic centers; subdivision centroids cover US
// states (the one subdivision Cloudflare's CF-Region-Code commonly carries) so
// within-US traffic spreads instead of stacking on one national dot. Everything
// else falls back to the country centroid — always honest at country granularity.
// countryCentroid maps an ISO-3166 alpha-2 country code (upper) to {lat, lon}.
var countryCentroid = map[string][2]float64{
"AD": {42.55, 1.60}, "AE": {23.42, 53.85}, "AF": {33.94, 67.71}, "AG": {17.06, -61.80},
"AI": {18.22, -63.07}, "AL": {41.15, 20.17}, "AM": {40.07, 45.04}, "AO": {-11.20, 17.87},
"AR": {-38.42, -63.62}, "AS": {-14.27, -170.13}, "AT": {47.52, 14.55}, "AU": {-25.27, 133.78},
"AW": {12.52, -69.97}, "AZ": {40.14, 47.58}, "BA": {43.92, 17.68}, "BB": {13.19, -59.54},
"BD": {23.68, 90.36}, "BE": {50.50, 4.47}, "BF": {12.24, -1.56}, "BG": {42.73, 25.49},
"BH": {25.93, 50.64}, "BI": {-3.37, 29.92}, "BJ": {9.31, 2.32}, "BM": {32.32, -64.76},
"BN": {4.54, 114.73}, "BO": {-16.29, -63.59}, "BR": {-14.24, -51.93}, "BS": {25.03, -77.40},
"BT": {27.51, 90.43}, "BW": {-22.33, 24.68}, "BY": {53.71, 27.95}, "BZ": {17.19, -88.50},
"CA": {56.13, -106.35}, "CD": {-4.04, 21.76}, "CF": {6.61, 20.94}, "CG": {-0.23, 15.83},
"CH": {46.82, 8.23}, "CI": {7.54, -5.55}, "CL": {-35.68, -71.54}, "CM": {7.37, 12.35},
"CN": {35.86, 104.20}, "CO": {4.57, -74.30}, "CR": {9.75, -83.75}, "CU": {21.52, -77.78},
"CV": {16.00, -24.01}, "CW": {12.17, -68.99}, "CY": {35.13, 33.43}, "CZ": {49.82, 15.47},
"DE": {51.17, 10.45}, "DJ": {11.83, 42.59}, "DK": {56.26, 9.50}, "DM": {15.41, -61.37},
"DO": {18.74, -70.16}, "DZ": {28.03, 1.66}, "EC": {-1.83, -78.18}, "EE": {58.60, 25.01},
"EG": {26.82, 30.80}, "EH": {24.22, -12.89}, "ER": {15.18, 39.78}, "ES": {40.46, -3.75},
"ET": {9.15, 40.49}, "FI": {61.92, 25.75}, "FJ": {-16.58, 179.41}, "FK": {-51.80, -59.52},
"FM": {7.43, 150.55}, "FO": {61.89, -6.91}, "FR": {46.23, 2.21}, "GA": {-0.80, 11.61},
"GB": {55.38, -3.44}, "GD": {12.12, -61.68}, "GE": {42.32, 43.36}, "GF": {3.93, -53.13},
"GG": {49.47, -2.59}, "GH": {7.95, -1.02}, "GI": {36.14, -5.35}, "GL": {71.71, -42.60},
"GM": {13.44, -15.31}, "GN": {9.95, -9.70}, "GP": {16.27, -61.55}, "GQ": {1.65, 10.27},
"GR": {39.07, 21.82}, "GT": {15.78, -90.23}, "GU": {13.44, 144.79}, "GW": {11.80, -15.18},
"GY": {4.86, -58.93}, "HK": {22.32, 114.17}, "HN": {15.20, -86.24}, "HR": {45.10, 15.20},
"HT": {18.97, -72.29}, "HU": {47.16, 19.50}, "ID": {-0.79, 113.92}, "IE": {53.41, -8.24},
"IL": {31.05, 34.85}, "IM": {54.24, -4.55}, "IN": {20.59, 78.96}, "IQ": {33.22, 43.68},
"IR": {32.43, 53.69}, "IS": {64.96, -19.02}, "IT": {41.87, 12.57}, "JE": {49.21, -2.13},
"JM": {18.11, -77.30}, "JO": {30.59, 36.24}, "JP": {36.20, 138.25}, "KE": {-0.02, 37.91},
"KG": {41.20, 74.77}, "KH": {12.57, 104.99}, "KI": {-3.37, -168.73}, "KM": {-11.65, 43.33},
"KN": {17.36, -62.78}, "KP": {40.34, 127.51}, "KR": {35.91, 127.77}, "KW": {29.31, 47.48},
"KY": {19.31, -81.25}, "KZ": {48.02, 66.92}, "LA": {19.86, 102.50}, "LB": {33.85, 35.86},
"LC": {13.91, -60.98}, "LI": {47.17, 9.56}, "LK": {7.87, 80.77}, "LR": {6.43, -9.43},
"LS": {-29.61, 28.23}, "LT": {55.17, 23.88}, "LU": {49.82, 6.13}, "LV": {56.88, 24.60},
"LY": {26.34, 17.23}, "MA": {31.79, -7.09}, "MC": {43.75, 7.41}, "MD": {47.41, 28.37},
"ME": {42.71, 19.37}, "MG": {-18.77, 46.87}, "MH": {7.13, 171.18}, "MK": {41.61, 21.75},
"ML": {17.57, -3.996}, "MM": {21.91, 95.96}, "MN": {46.86, 103.85}, "MO": {22.20, 113.54},
"MP": {15.18, 145.75}, "MQ": {14.64, -61.02}, "MR": {21.01, -10.94}, "MS": {16.74, -62.19},
"MT": {35.94, 14.38}, "MU": {-20.35, 57.55}, "MV": {3.20, 73.22}, "MW": {-13.25, 34.30},
"MX": {23.63, -102.55}, "MY": {4.21, 101.98}, "MZ": {-18.67, 35.53}, "NA": {-22.96, 18.49},
"NC": {-20.90, 165.62}, "NE": {17.61, 8.08}, "NF": {-29.04, 167.95}, "NG": {9.08, 8.68},
"NI": {12.87, -85.21}, "NL": {52.13, 5.29}, "NO": {60.47, 8.47}, "NP": {28.39, 84.12},
"NR": {-0.52, 166.93}, "NZ": {-40.90, 174.89}, "OM": {21.51, 55.92}, "PA": {8.54, -80.78},
"PE": {-9.19, -75.02}, "PF": {-17.68, -149.41}, "PG": {-6.31, 143.96}, "PH": {12.88, 121.77},
"PK": {30.38, 69.35}, "PL": {51.92, 19.15}, "PM": {46.94, -56.27}, "PR": {18.22, -66.59},
"PS": {31.95, 35.23}, "PT": {39.40, -8.22}, "PW": {7.51, 134.58}, "PY": {-23.44, -58.44},
"QA": {25.35, 51.18}, "RE": {-21.12, 55.54}, "RO": {45.94, 24.97}, "RS": {44.02, 21.01},
"RU": {61.52, 105.32}, "RW": {-1.94, 29.87}, "SA": {23.89, 45.08}, "SB": {-9.65, 160.16},
"SC": {-4.68, 55.49}, "SD": {12.86, 30.22}, "SE": {60.13, 18.64}, "SG": {1.35, 103.82},
"SI": {46.15, 14.99}, "SK": {48.67, 19.70}, "SL": {8.46, -11.78}, "SM": {43.94, 12.46},
"SN": {14.50, -14.45}, "SO": {5.15, 46.20}, "SR": {3.92, -56.03}, "SS": {6.877, 31.31},
"ST": {0.19, 6.61}, "SV": {13.79, -88.90}, "SX": {18.04, -63.06}, "SY": {34.80, 38.997},
"SZ": {-26.52, 31.47}, "TC": {21.69, -71.80}, "TD": {15.45, 18.73}, "TG": {8.62, 0.82},
"TH": {15.87, 100.99}, "TJ": {38.86, 71.28}, "TL": {-8.87, 125.73}, "TM": {38.97, 59.56},
"TN": {33.89, 9.54}, "TO": {-21.18, -175.20}, "TR": {38.96, 35.24}, "TT": {10.69, -61.22},
"TV": {-7.11, 177.65}, "TW": {23.70, 120.96}, "TZ": {-6.37, 34.89}, "UA": {48.38, 31.17},
"UG": {1.37, 32.29}, "US": {37.09, -95.71}, "UY": {-32.52, -55.77}, "UZ": {41.38, 64.59},
"VA": {41.90, 12.45}, "VC": {12.98, -61.29}, "VE": {6.42, -66.59}, "VG": {18.42, -64.64},
"VI": {18.34, -64.90}, "VN": {14.06, 108.28}, "VU": {-15.38, 166.96}, "WS": {-13.76, -172.10},
"XK": {42.60, 20.90}, "YE": {15.55, 48.52}, "YT": {-12.83, 45.17}, "ZA": {-30.56, 22.94},
"ZM": {-13.13, 27.85}, "ZW": {-19.02, 29.15},
}
// subdivisionCentroid maps "US-<state>" (ISO 3166-2:US) to {lat, lon}. Cloudflare's
// CF-Region-Code carries the 2-letter US state code, so US traffic resolves to the
// state center; any country whose subdivision is absent here falls back to the
// country centroid. Extensible: add "<CC>-<REGION>" rows as other subdivisions
// become useful without touching the aggregation logic.
var subdivisionCentroid = map[string][2]float64{
"US-AL": {32.81, -86.79}, "US-AK": {61.37, -152.40}, "US-AZ": {33.73, -111.43},
"US-AR": {34.97, -92.37}, "US-CA": {36.12, -119.68}, "US-CO": {39.06, -105.31},
"US-CT": {41.60, -72.76}, "US-DE": {39.32, -75.51}, "US-DC": {38.90, -77.03},
"US-FL": {27.77, -81.69}, "US-GA": {33.04, -83.64}, "US-HI": {21.09, -157.50},
"US-ID": {44.24, -114.48}, "US-IL": {40.35, -88.99}, "US-IN": {39.85, -86.26},
"US-IA": {42.01, -93.21}, "US-KS": {38.53, -96.73}, "US-KY": {37.67, -84.67},
"US-LA": {31.17, -91.87}, "US-ME": {44.69, -69.38}, "US-MD": {39.06, -76.80},
"US-MA": {42.23, -71.53}, "US-MI": {43.33, -84.54}, "US-MN": {45.69, -93.90},
"US-MS": {32.74, -89.68}, "US-MO": {38.46, -92.29}, "US-MT": {46.92, -110.45},
"US-NE": {41.13, -98.27}, "US-NV": {38.31, -117.06}, "US-NH": {43.45, -71.56},
"US-NJ": {40.30, -74.52}, "US-NM": {34.84, -106.25}, "US-NY": {42.17, -74.95},
"US-NC": {35.63, -79.81}, "US-ND": {47.53, -99.78}, "US-OH": {40.39, -82.76},
"US-OK": {35.57, -96.93}, "US-OR": {44.57, -122.07}, "US-PA": {40.59, -77.21},
"US-RI": {41.68, -71.51}, "US-SC": {33.86, -80.95}, "US-SD": {44.30, -99.44},
"US-TN": {35.75, -86.69}, "US-TX": {31.05, -97.56}, "US-UT": {40.15, -111.86},
"US-VT": {44.05, -72.71}, "US-VA": {37.77, -78.17}, "US-WA": {47.40, -121.49},
"US-WV": {38.49, -80.95}, "US-WI": {44.27, -89.62}, "US-WY": {42.76, -107.30},
}
// centroid resolves an approximate map position for a (country, region) group.
// It prefers the subdivision centroid when a subdivision code is present and known,
// then falls back to the country centroid. ok=false when neither resolves (the
// group is dropped from the plotted points but still counted in the throughput
// totals — some inbound traffic is simply not geo-resolvable, and we never fake it).
func centroid(country, region string) (lat, lon float64, ok bool) {
if region != "" {
if c, has := subdivisionCentroid[country+"-"+region]; has {
return c[0], c[1], true
}
}
if c, has := countryCentroid[country]; has {
return c[0], c[1], true
}
return 0, 0, false
}