diff --git a/imports/api/Common/FiresSchema.js b/imports/api/Common/FiresSchema.js index 5bb792b..822a41e 100644 --- a/imports/api/Common/FiresSchema.js +++ b/imports/api/Common/FiresSchema.js @@ -7,15 +7,22 @@ const firesCommonSchema = { ourid: LocationSchema, lat: Number, lon: Number, - address: { type: String, optional: true }, - scan: Number, type: String, when: Date, + + // Neighbour notified fires + + owner: { type: String, optional: true }, + dateformat: { type: String, optional: true }, + + // NASA types + address: { type: String, optional: true }, // reverse geo + scan: { type: Number, optional: true }, track: { type: Number, optional: true }, acq_date: { type: String, optional: true }, acq_time: { type: String, optional: true }, satellite: { type: String, optional: true }, - confidence: { type: Number, optional: true }, + confidence: { type: String, optional: true }, version: { type: String, optional: true }, frp: { type: Number, optional: true }, daynight: { type: String, optional: true }, @@ -23,6 +30,8 @@ const firesCommonSchema = { bright_t31: { type: Number, optional: true }, bright_ti4: { type: Number, optional: true }, bright_ti5: { type: Number, optional: true }, + + // common createdAt: defaultCreatedAt, updatedAt: defaultUpdateAt }; diff --git a/imports/api/FireAlerts/FireAlerts.js b/imports/api/FireAlerts/FireAlerts.js index 640edde..9c4a37d 100644 --- a/imports/api/FireAlerts/FireAlerts.js +++ b/imports/api/FireAlerts/FireAlerts.js @@ -2,6 +2,7 @@ import { Mongo } from 'meteor/mongo'; import SimpleSchema from 'simpl-schema'; +import firesCommonSchema from '../Common/FiresSchema'; const FireAlerts = new Mongo.Collection('avisosfuego', { idGeneration: 'MONGO' }); @@ -17,33 +18,7 @@ FireAlerts.deny({ remove: () => true }); -/* Sample: - * "_id" : ObjectId("5a059c4879095a1adba47507"), - * "chatId" : null, - * "location" : { - * "lat" : 40.2324096, - * "lon" : -3.3514863, - * "text" : null - * }, - * "aviso" : ISODate("2017-11-10T12:32:08.973Z"), - * "dateformat" : "20171110", - * "geo" : { - * "type" : "Point", - * "coordinates" : [ - * -3.3514863, - * 40.2324096 - * ] - * } - * } - * */ - - -FireAlerts.schema = new SimpleSchema({ - location: Object, - 'location.lat': Number, - 'location.lon': Number, - aviso: Date -}); +FireAlerts.schema = new SimpleSchema(firesCommonSchema); FireAlerts.attachSchema(FireAlerts.schema); diff --git a/imports/api/Fires/server/publications.js b/imports/api/Fires/server/publications.js index 6737510..de67f8b 100644 --- a/imports/api/Fires/server/publications.js +++ b/imports/api/Fires/server/publications.js @@ -5,12 +5,12 @@ import { Meteor } from 'meteor/meteor'; import { check } from 'meteor/check'; import urlEnc from '/imports/modules/url-encode'; import { Promise } from 'meteor/promise'; -import FiresCollection from '../Fires'; import NodeGeocoder from 'node-geocoder'; import { gmapServerKey } from '/imports/startup/server/IPGeocoder'; +import FiresCollection from '../Fires'; function findFire(unsealed) { - const fire = FiresCollection.find({ ourid: { type: 'Point', coordinates: [unsealed.lon, unsealed.lat] } }); + const fire = FiresCollection.find({ ourid: { type: 'Point', coordinates: [unsealed.lon, unsealed.lat] }, when: unsealed.when, type: unsealed.type }); return fire; } @@ -30,22 +30,32 @@ Meteor.publish('fireFromHash', function fireFromHash(fireEnc) { // console.log(fireEnc); const unsealed = Promise.await(urlEnc.decrypt(fireEnc)); const w = unsealed.when; + // console.log(w); unsealed.when = new Date(w); + // console.log(unsealed.when); + const c = unsealed.createdAt; + unsealed.createdAt = new Date(c); + const u = unsealed.updatedAt; + unsealed.updatedAt = new Date(u); // console.log(unsealed); FiresCollection.schema.validate(unsealed); const fire = findFire(unsealed); - try { - const rev = Promise.await(geocoder.reverse({ lat: unsealed.lat, lon: unsealed.lon })); - if (rev[0]) { - unsealed.address = rev[0].formattedAddress; + // console.log(`Found: ${fire.count()}`); + if (!unsealed.address) { + try { + const rev = Promise.await(geocoder.reverse({ lat: unsealed.lat, lon: unsealed.lon })); + if (rev[0]) { + unsealed.address = rev[0].formattedAddress; + } + // console.log(unsealed.address); + } catch (reve) { + console.error(reve); } - // console.log(unsealed.address); - } catch (reve) { - console.error(reve); } if (fire.count() === 0) { - const result = FiresCollection.upsert({ ourid: unsealed.ourid }, { $set: unsealed }, { multi: false, upsert: true }); - console.log(JSON.stringify(result)); + // const result = + FiresCollection.upsert({ ourid: unsealed.ourid, when: unsealed.when, type: unsealed.type }, { $set: unsealed }, { multi: false, upsert: true }); + // console.log(JSON.stringify(result)); } return findFire(unsealed); /* console.log(`fires: ${fire.count()}`); diff --git a/imports/startup/server/migrations.js b/imports/startup/server/migrations.js index 80c255d..d8ecff4 100644 --- a/imports/startup/server/migrations.js +++ b/imports/startup/server/migrations.js @@ -4,6 +4,7 @@ import { Meteor } from 'meteor/meteor'; import { Accounts } from 'meteor/accounts-base'; import randomHex from 'crypto-random-hex'; import UserSubsToFiresCollection from '/imports/api/Subscriptions/Subscriptions'; +import FireAlertsCollection from '/imports/api/FireAlerts/FireAlerts'; Meteor.startup(() => { // https://github.com/percolatestudio/meteor-migrations @@ -69,6 +70,18 @@ Meteor.startup(() => { } }); + Migrations.add({ + version: 4, + up: function deleteOldAlertFiresAndIndexes() { + FireAlertsCollection.remove({ createdAd: null }); + const raw = FireAlertsCollection.rawCollection(); + raw.createIndex({ ourid: '2dsphere' }); + raw.createIndex({ when: 1 }); + raw.createIndex({ updatedAt: 1 }); + raw.createIndex({ createdAt: 1 }); + raw.createIndex({ ourid: 1, type: 1 }); + } + }); // Set createdAt in users & subs Migrations.migrateTo('latest'); diff --git a/imports/ui/pages/Fires/Fires.js b/imports/ui/pages/Fires/Fires.js index 3165f9f..b7e6c09 100644 --- a/imports/ui/pages/Fires/Fires.js +++ b/imports/ui/pages/Fires/Fires.js @@ -26,14 +26,17 @@ class Fire extends React.Component { render() { const { loading, fire, t } = this.props; + if (fire && fire.when) { + this.when = moment.tz(fire.when, moment.tz.guess()); + } return (
{t('Coordenadas:')} {fire.lat}, {fire.lon}
- {(fire.type === 'modis' || fire.type === 'virrs') && -{t('Fuego detectado por satélites de la NASA {{when}}', { when: moment(fire.when).fromNow() })}
+ {(fire.type === 'modis' || fire.type === 'viirs') && +{t('Fuego detectado por satélites de la NASA {{when}}', { when: this.when.fromNow() })}
} + {(fire.type === 'vecinal') && +{t('Fuego notificado por uno de nuestros usuarios/as {{when}}', { when: this.when.fromNow() })}
+ } {/* TODO: marcar tipo de fuego, industria, etc */}