Fix issue with db error
This commit is contained in:
@@ -68,10 +68,13 @@ app.get('/api/line-of-sight', async (req, res) => {
|
|||||||
|
|
||||||
// Bulk land check: use a single query for all points to avoid connection pool exhaustion
|
// Bulk land check: use a single query for all points to avoid connection pool exhaustion
|
||||||
const waterStart = Date.now();
|
const waterStart = Date.now();
|
||||||
|
const lons = pathPoints.map(p => p.lon);
|
||||||
|
const lats = pathPoints.map(p => p.lat);
|
||||||
|
|
||||||
const waterCheckQuery = `
|
const waterCheckQuery = `
|
||||||
WITH points AS (
|
WITH points AS (
|
||||||
SELECT i, ST_SetSRID(ST_MakePoint(val[1], val[2]), 4326)::geography as p_geom
|
SELECT i, ST_SetSRID(ST_MakePoint(lon, lat), 4326)::geography as p_geom
|
||||||
FROM unnest($1::float8[][]) WITH ORDINALITY AS t(val, i)
|
FROM unnest($1::float8[], $2::float8[]) WITH ORDINALITY AS t(lon, lat, i)
|
||||||
)
|
)
|
||||||
SELECT i, EXISTS (
|
SELECT i, EXISTS (
|
||||||
SELECT 1 FROM cities
|
SELECT 1 FROM cities
|
||||||
@@ -82,8 +85,7 @@ app.get('/api/line-of-sight', async (req, res) => {
|
|||||||
ORDER BY i;
|
ORDER BY i;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const pointsArray = pathPoints.map(p => [p.lon, p.lat]);
|
const waterResults = await pool.query(waterCheckQuery, [lons, lats]);
|
||||||
const waterResults = await pool.query(waterCheckQuery, [pointsArray]);
|
|
||||||
const waterChecks = waterResults.rows.map(r => !r.has_land);
|
const waterChecks = waterResults.rows.map(r => !r.has_land);
|
||||||
|
|
||||||
console.log(`Bulk water check completed in ${Date.now() - waterStart}ms for ${pathPoints.length} points.`);
|
console.log(`Bulk water check completed in ${Date.now() - waterStart}ms for ${pathPoints.length} points.`);
|
||||||
|
|||||||
Reference in New Issue
Block a user