todos-contra-el-fuego-web/imports/api/ActiveFires/server/countFires.js
vjrj 637a749e8d meteor3: REST API async migration COMPLETE — smoke green on Meteor 3.1 + Mongo 7
All 12 Flutter REST endpoints byte-identical to the 1.6.1.1 baseline, running on
Meteor 3.1 against dockerized MongoDB 7. The 3.x jump works end-to-end.

- Rest.js: every endpoint action -> async; all Mongo calls -> *Async
  (findOneAsync/countAsync/fetchAsync/upsertAsync/removeAsync).
- Helpers to async: countRealFires (accepts array|cursor, forEachAsync->for-of),
  firesUnion (fetchAsync), fireFromHash/findOrCreateFire (await, dropped Fibers
  Promise.await), subscriptionsInsert/subscriptionsRemove, upsertFalsePositive,
  falsePositives.insert, subscriptions.update method, countFiresInRegions.
- getFires: countAsync() on a $near cursor throws on the mongodb 6 driver
  ($near not allowed in the aggregation countDocuments uses) -> fetch once and
  use array length for total.
- Route order: json-routes 3.0 matches in registration order, so
  mobile/subscriptions/all/:token/:mobileToken is now registered BEFORE
  :token/:mobileToken/:subsId (else GET .../all/x/y hit the delete-only route).
2026-07-14 06:21:35 +02:00

80 lines
2.7 KiB
JavaScript

/* eslint-disable import/no-absolute-path */
import { whichAreFalsePositives, firesUnion } from '/imports/api/FalsePositives/server/publications';
import FalsePositives from '/imports/api/FalsePositives/FalsePositives';
import Industries from '/imports/api/Industries/Industries';
import ravenLogger from '/imports/startup/server/ravenLogger';
import ActiveFires from '../ActiveFires';
const debug = 0;
const cleanProv = (prov, stringsToRemove) => {
let lprov = prov;
stringsToRemove.forEach((st) => {
lprov = lprov.replace(st[0], st[1]);
});
return lprov;
};
const findFiresInRegion = (zone) => {
const result = ActiveFires.find({
ourid: {
$geoWithin: {
$geometry: zone.geometry
}
}
});
/* console.log(`False positive total: ${falsePos.count()}`);
console.log(`False positives: ${JSON.stringify(falsePos.fetch())}`); */
return result;
};
export const countRealFires = async (firesCursor) => {
const realFires = [];
const fires = Array.isArray(firesCursor) ? firesCursor : await firesCursor.fetchAsync();
for (const fire of fires) {
if (debug) console.log(`${JSON.stringify(fire)} -----`);
const union = await firesUnion([fire]);
if (debug) console.log(`${JSON.stringify(union)} -----`);
const falsePos = whichAreFalsePositives(FalsePositives, union);
const industries = whichAreFalsePositives(Industries, union);
if (await falsePos.countAsync() === 0 && await industries.countAsync() === 0) {
realFires.push(fire);
}
}
// group fires
const realUnion = await firesUnion(realFires);
const unionCount = realUnion[0] &&
realUnion[0].geometry &&
realUnion[0].geometry.coordinates ? realUnion[0].geometry.coordinates.length : 0;
return unionCount;
};
const countFiresInRegions = async (regions, stringsToRemove) => {
let total = 0;
const fireStats = {};
for (const region of regions.features) {
const regionName = cleanProv(region.properties.name, stringsToRemove);
const regionCode = region.properties.iso_a2;
if (debug) console.log(`${regionName} -----`);
try {
const fires = findFiresInRegion(region);
// TODO Also check neighbour fires (when better implementation of that part)
const initialCount = await fires.countAsync();
if (initialCount > 0) {
const unionCount = await countRealFires(fires);
if (debug) console.log(`${regionName} initial: ${initialCount}, union calc: ${unionCount}`);
if (unionCount > 0) {
total += unionCount;
fireStats[regionName] = { count: unionCount, code: regionCode };
}
}
} catch (e) {
ravenLogger.log(e);
}
}
return { total, fires: fireStats };
};
export default countFiresInRegions;