Added fire stats in zone to API
This commit is contained in:
parent
9502ea3604
commit
f144c986b7
4 changed files with 68 additions and 26 deletions
|
|
@ -29,7 +29,25 @@ const findFiresInRegion = (zone) => {
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
const countFires = (regions, stringsToRemove) => {
|
export const countRealFires = (firesCursor) => {
|
||||||
|
const realFires = [];
|
||||||
|
firesCursor.forEach((fire) => {
|
||||||
|
const union = firesUnion([fire]);
|
||||||
|
const falsePos = whichAreFalsePositives(FalsePositives, union);
|
||||||
|
const industries = whichAreFalsePositives(Industries, union);
|
||||||
|
if (falsePos.count() === 0 && industries.count() === 0) {
|
||||||
|
realFires.push(fire);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// group fires
|
||||||
|
const realUnion = firesUnion(realFires);
|
||||||
|
const unionCount = realUnion[0] &&
|
||||||
|
realUnion[0].geometry &&
|
||||||
|
realUnion[0].geometry.coordinates ? realUnion[0].geometry.coordinates.length : 0;
|
||||||
|
return unionCount;
|
||||||
|
};
|
||||||
|
|
||||||
|
const countFiresInRegions = (regions, stringsToRemove) => {
|
||||||
let total = 0;
|
let total = 0;
|
||||||
const fireStats = {};
|
const fireStats = {};
|
||||||
|
|
||||||
|
|
@ -41,25 +59,9 @@ const countFires = (regions, stringsToRemove) => {
|
||||||
const fires = findFiresInRegion(region);
|
const fires = findFiresInRegion(region);
|
||||||
// TODO Also check neighbour fires (when better implementation of that part)
|
// TODO Also check neighbour fires (when better implementation of that part)
|
||||||
const initialCount = fires.count();
|
const initialCount = fires.count();
|
||||||
let count = initialCount;
|
if (initialCount > 0) {
|
||||||
if (count > 0) {
|
const unionCount = countRealFires(fires);
|
||||||
const realFires = [];
|
if (debug) console.log(`${regionName} initial: ${initialCount}, union calc: ${unionCount}`);
|
||||||
fires.forEach((fire) => {
|
|
||||||
const union = firesUnion([fire]);
|
|
||||||
const falsePos = whichAreFalsePositives(FalsePositives, union);
|
|
||||||
const industries = whichAreFalsePositives(Industries, union);
|
|
||||||
if (falsePos.count() === 0 && industries.count() === 0) {
|
|
||||||
realFires.push(fire);
|
|
||||||
} else {
|
|
||||||
count -= 1;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// group fires
|
|
||||||
const realUnion = firesUnion(realFires);
|
|
||||||
const unionCount = realUnion[0] &&
|
|
||||||
realUnion[0].geometry &&
|
|
||||||
realUnion[0].geometry.coordinates ? realUnion[0].geometry.coordinates.length : 0;
|
|
||||||
if (debug) console.log(`${regionName} initial: ${initialCount}, first calc: ${count} union calc: ${unionCount}`);
|
|
||||||
if (unionCount > 0) {
|
if (unionCount > 0) {
|
||||||
total += unionCount;
|
total += unionCount;
|
||||||
fireStats[regionName] = { count: unionCount, code: regionCode };
|
fireStats[regionName] = { count: unionCount, code: regionCode };
|
||||||
|
|
@ -72,4 +74,4 @@ const countFires = (regions, stringsToRemove) => {
|
||||||
return { total, fires: fireStats };
|
return { total, fires: fireStats };
|
||||||
};
|
};
|
||||||
|
|
||||||
export default countFires;
|
export default countFiresInRegions;
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { Meteor } from 'meteor/meteor';
|
||||||
import tbuffer from '@turf/buffer';
|
import tbuffer from '@turf/buffer';
|
||||||
import emojiFlags from 'emoji-flags';
|
import emojiFlags from 'emoji-flags';
|
||||||
import tweet from '/imports/modules/server/twitter';
|
import tweet from '/imports/modules/server/twitter';
|
||||||
import countFires from './countFires';
|
import countFiresInRegions from './countFires';
|
||||||
|
|
||||||
const debug = false;
|
const debug = false;
|
||||||
|
|
||||||
|
|
@ -103,7 +103,7 @@ const correctTweetSize = (tweetText) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const tweetEuropeFires = () => {
|
const tweetEuropeFires = () => {
|
||||||
const resultEu = countFires(europe, [[' ', '']]);
|
const resultEu = countFiresInRegions(europe, [[' ', '']]);
|
||||||
|
|
||||||
if (resultEu.total > 0) {
|
if (resultEu.total > 0) {
|
||||||
let tweetText = `${tweetHeaders[0].trim()} ${composeEuropeTweet(resultEu.total, resultEu.fires, false)}. More info: https://fires.comunes.org/fires`;
|
let tweetText = `${tweetHeaders[0].trim()} ${composeEuropeTweet(resultEu.total, resultEu.fires, false)}. More info: https://fires.comunes.org/fires`;
|
||||||
|
|
@ -126,8 +126,8 @@ const tweetEuropeFires = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const tweetIberiaFires = () => {
|
const tweetIberiaFires = () => {
|
||||||
const resultEs = countFires(autonomies, stringsToRemoveIberia);
|
const resultEs = countFiresInRegions(autonomies, stringsToRemoveIberia);
|
||||||
const resultPt = countFires(portugal, stringsToRemoveIberia);
|
const resultPt = countFiresInRegions(portugal, stringsToRemoveIberia);
|
||||||
|
|
||||||
const fires = Object.assign(resultEs.fires, resultPt.fires);
|
const fires = Object.assign(resultEs.fires, resultPt.fires);
|
||||||
const total = resultEs.total + resultPt.total;
|
const total = resultEs.total + resultPt.total;
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,12 @@
|
||||||
/* eslint-disable import/no-absolute-path */
|
/* eslint-disable import/no-absolute-path */
|
||||||
|
|
||||||
import { Meteor } from 'meteor/meteor';
|
import { Meteor } from 'meteor/meteor';
|
||||||
|
import { NumberBetween } from '/imports/modules/server/other-checks';
|
||||||
import Fires from '/imports/api/Fires/Fires';
|
import Fires from '/imports/api/Fires/Fires';
|
||||||
|
import { check } from 'meteor/check';
|
||||||
import SiteSettings from '/imports/api/SiteSettings/SiteSettings';
|
import SiteSettings from '/imports/api/SiteSettings/SiteSettings';
|
||||||
import ActiveFiresCollection from '/imports/api/ActiveFires/ActiveFires';
|
import ActiveFiresCollection from '/imports/api/ActiveFires/ActiveFires';
|
||||||
|
import { countRealFires } from '/imports/api/ActiveFires/server/countFires';
|
||||||
|
|
||||||
Meteor.startup(() => {
|
Meteor.startup(() => {
|
||||||
const uptime = new Date();
|
const uptime = new Date();
|
||||||
|
|
@ -19,7 +22,7 @@ Meteor.startup(() => {
|
||||||
// Generates: POST on /api/users and GET, DELETE /api/users/:id for
|
// Generates: POST on /api/users and GET, DELETE /api/users/:id for
|
||||||
// Meteor.users collection
|
// Meteor.users collection
|
||||||
/* apiV1.addCollection(Meteor.users, {
|
/* apiV1.addCollection(Meteor.users, {
|
||||||
* excludedEndpoints: ['getAll', 'put'],
|
* excludedEndpoints: ['getAll', 'put', 'delete', 'patch', 'get'],
|
||||||
* routeOptions: {
|
* routeOptions: {
|
||||||
* authRequired: true
|
* authRequired: true
|
||||||
* },
|
* },
|
||||||
|
|
@ -75,4 +78,40 @@ Meteor.startup(() => {
|
||||||
return { ms: new Date() - uptime };
|
return { ms: new Date() - uptime };
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Maps to: /api/v1/fires-in/:lat/:lng/:km
|
||||||
|
// Ex: http://127.0.0.1:3000/api/v1/fires-in/38.736946/-9.142685/100
|
||||||
|
apiV1.addRoute('fires-in/:token/:lat/:lng/:km', { authRequired: false }, {
|
||||||
|
get: function get() {
|
||||||
|
const lat = Number(this.urlParams.lat);
|
||||||
|
const lng = Number(this.urlParams.lng);
|
||||||
|
const km = Number(this.urlParams.km);
|
||||||
|
const { token } = this.urlParams;
|
||||||
|
check(lng, NumberBetween(-180, 180));
|
||||||
|
check(lat, NumberBetween(-90, 90));
|
||||||
|
check(km, NumberBetween(0, 100));
|
||||||
|
check(token, String);
|
||||||
|
|
||||||
|
console.log(`Query for fires in ${lat}, ${lng} in ${km} km radius`);
|
||||||
|
|
||||||
|
if (token !== Meteor.settings.private.internalApiToken) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const fires = ActiveFiresCollection.find({
|
||||||
|
ourid: {
|
||||||
|
$near: {
|
||||||
|
$geometry: {
|
||||||
|
type: 'Point',
|
||||||
|
coordinates: [lng, lat]
|
||||||
|
},
|
||||||
|
$maxDistance: km * 1000,
|
||||||
|
$minDistance: 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return { total: fires.count(), real: countRealFires(fires) };
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@
|
||||||
},
|
},
|
||||||
"proxies_count": 0,
|
"proxies_count": 0,
|
||||||
"ironPassword": "SomePasswordAlLeast32LongYouKnowHowToCount",
|
"ironPassword": "SomePasswordAlLeast32LongYouKnowHowToCount",
|
||||||
|
"internalApiToken": "someSimilarPasswordForUseOurAPIinternally",
|
||||||
"cron": {
|
"cron": {
|
||||||
"debug": false
|
"debug": false
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue