mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 05:21:15 +00:00
7277e35a96
The Atlas server no longer keeps the admin-1 regions of every country in memory. Each country's regions are now a pre-gzipped file of their own, sent to the map as they are. **Why:** `GET /addons/atlas/regions/geo?countries=…` built `admin1Store` (every country's region text, kept until restart), then joined the requested countries, `JSON.parse`d them, and Nest stringified them again. On the 512 MB – 1 GB boxes TREK runs on, the first z≥5 view could OOM the server (same shape as #1576). **What changes:** - `server/assets/atlas/admin1/<ISO_A2>.geojson.gz`: 198 files, 14.85 MB, the same 3,447 features as `admin1.geojson.gz`. - New `GET /addons/atlas/regions/geo/:code` with a Zod contract (`regionGeoParamsSchema` in `@trek/shared`: two letters, any case). It sends the country's file as it is, with `Content-Encoding: gzip` and the current 1-day `Cache-Control`, as `countries/geo` does. A country without regions gets an empty FeatureCollection. Anything that is not two letters gets a 400. - `useAtlas.ts` fetches one URL per uncached country and still rebuilds the layer once. Each country has a stable URL, so a country evicted by `REGION_CACHE_MAX` comes back from the browser cache. - `getRegionFeatures` reads only the country it needs. `admin1Store` is removed. - `?countries=` keeps working for cached PWA clients. It joins the per-country files as bytes, without a parse. - The migrations are untouched. `admin1.geojson.gz` still ships, only for `Migration20200101021500` and `Migration20200101031500`. This costs ~15 MB of disk in the image and no RAM. `build-atlas-geo.mjs` writes both, and ATLAS-BUNDLE-008 keeps them equal. **Memory** (real `atlas-geo` module, first z≥5 view over Europe = 49 country codes, JS heap + Buffers): | | peak | kept after | |---|---|---| | before, first view (builds the store) | +372 MB | +61 MB | | before, later views | +115 MB | 0 | | after, `/regions/geo/:code` | +4.7 MB | 0 | | after, legacy `?countries=` | +59 MB | 0 | **Tests:** - New: `atlas-region-files.test.ts` (moved from `atlas.service.test.ts`, plus join and dedupe cases), the `regionGeoParamsSchema` spec, ATLAS-BUNDLE-007 (the file format the byte join depends on), ATLAS-BUNDLE-008 (bundle and per-country files are equal), ATLAS-016/017/018 (the new route over HTTP, including the 400). - The `getVisitedRegions` tests (ATLAS-UNIT-019..024) used to pass only when `getRegionGeo`'s `beforeAll` had built the store first. They now also pass on their own. - The shared, server and client gates pass. Baselines go down only where this change removed code (`no-console`, `no-explicit-any`, `no-swallowed-catch`, file sizes). **Trade-offs:** - A first zoom over Europe sends ~49 small parallel requests instead of 1. Over HTTP/1.1 the browser queues them 6 at a time. The total bytes are the same (4.4 MB gz). - The per-country URLs are unversioned and cached for a day, like `countries/geo`. After an upgrade that changes a country's regions, a browser can show the old shapes for up to 24 h. Approved on Discord in #github-pr: <paste the thread link here> Related: #1576, #2559 - [x] Bug fix - [ ] New feature - [ ] Breaking change - [ ] Documentation update - [ ] I have read the [Contributing Guidelines](https://github.com/liketrek/TREK/wiki/Contributing) - [ ] My branch is [up to date with `dev`](https://github.com/liketrek/TREK/wiki/Development-environment#3-keep-your-fork-up-to-date) - [x] This PR targets the `dev` branch, not `main` *(wiki-only PRs are exempt)* - [x] I have tested my changes locally - [x] I have added/updated tests that prove my fix is effective or that my feature works - [x] I have updated documentation if needed
60 lines
2.9 KiB
TypeScript
60 lines
2.9 KiB
TypeScript
import { getRegionGeoGz, getRegionGeoJson } from '../../../src/nest/atlas/atlas-geo';
|
|
|
|
import { describe, it, expect } from 'vitest';
|
|
import zlib from 'zlib';
|
|
|
|
// The admin-1 regions behind the Atlas region layer and region lookups, read from the
|
|
// committed geoBoundaries files (server/assets/atlas/admin1/<ISO_A2>.geojson.gz), so these
|
|
// double as a guard that the bundle ships current sub-national data (#1119).
|
|
|
|
type RegionFeature = { properties: { iso_a2: string; iso_3166_2: string; name: string } };
|
|
type RegionGeo = { type: string; features: RegionFeature[] };
|
|
|
|
const regionGeo = (codes: string[]) => JSON.parse(getRegionGeoJson(codes).toString('utf8')) as RegionGeo;
|
|
const countriesOf = (geo: RegionGeo) => new Set(geo.features.map((f) => f.properties.iso_a2));
|
|
|
|
describe('getRegionGeoJson', () => {
|
|
it('ATLAS-SVC-017: returns an empty FeatureCollection for a country with no admin-1 features', () => {
|
|
expect(regionGeo(['ZZ'])).toEqual({ type: 'FeatureCollection', features: [] });
|
|
});
|
|
|
|
it('ATLAS-SVC-018: returns the current geoBoundaries regions for a country, case-insensitively', () => {
|
|
const result = regionGeo(['no']);
|
|
|
|
expect(result.type).toBe('FeatureCollection');
|
|
expect(result.features.length).toBeGreaterThan(0);
|
|
expect(countriesOf(result)).toEqual(new Set(['NO']));
|
|
|
|
const names = result.features.map((f) => f.properties.name);
|
|
const codes = result.features.map((f) => f.properties.iso_3166_2);
|
|
// Post-2020 reform is present…
|
|
expect(codes).toContain('NO-34'); // Innlandet
|
|
expect(codes).toContain('NO-46'); // Vestland
|
|
// …and the merged-away pre-2020 counties are gone (the original #1119 bug).
|
|
expect(names).not.toContain('Oppland');
|
|
expect(names).not.toContain('Hordaland');
|
|
expect(names).not.toContain('Sogn og Fjordane');
|
|
});
|
|
|
|
it('ATLAS-SVC-048: joins several countries into one FeatureCollection, each once, skipping unknown codes', () => {
|
|
const result = regionGeo(['DE', 'zz', 'FR', 'de']);
|
|
expect(countriesOf(result)).toEqual(new Set(['DE', 'FR']));
|
|
expect(result.features.length).toBe(regionGeo(['DE']).features.length + regionGeo(['FR']).features.length);
|
|
});
|
|
});
|
|
|
|
describe('getRegionGeoGz', () => {
|
|
it("ATLAS-SVC-049: returns the country's gzipped FeatureCollection, case-insensitively", async () => {
|
|
const gz = await getRegionGeoGz('no');
|
|
expect(gz).toBeInstanceOf(Buffer);
|
|
const geo = JSON.parse(zlib.gunzipSync(gz ?? Buffer.alloc(0)).toString('utf8')) as RegionGeo;
|
|
expect(geo.type).toBe('FeatureCollection');
|
|
expect(countriesOf(geo)).toEqual(new Set(['NO']));
|
|
});
|
|
|
|
it('ATLAS-SVC-050: returns null for a country without regions and for anything that is not a two-letter code', async () => {
|
|
const codes = ['ZZ', 'NOR', '', '../admin0', '..', 'N/'];
|
|
expect(await Promise.all(codes.map((code) => getRegionGeoGz(code)))).toEqual(codes.map(() => null));
|
|
});
|
|
});
|