diff --git a/.gitignore b/.gitignore index 37d7e73..a9d3ef2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules .env +.dotenv diff --git a/.jscsrc b/.jscsrc new file mode 100644 index 0000000..034c8a7 --- /dev/null +++ b/.jscsrc @@ -0,0 +1,8 @@ +{ + "preset": "node-style-guide", + "requireTrailingComma": { + "ignoreSingleValue": true, + "ignoreSingleLine": true + }, + "excludeFiles": ["node_modules/**"] +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..b0fc325 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +Locations +========= + +Location generator in node + +Usage +----- + +ACTION=find LATITUDE_RANGE=- LONGITUDE_RANGE=- TIMESTAMP_RANGE=- npm start + +Example: +``` +$ ACTION=find LATITUDE_RANGE=20-30 npm start +``` + +If it is needed to search for the exact value rather than a range just specify only the value in front of the relevant tag. + +ACTION=find LATITUDE_RANGE= LONGITUDE_RANGE= TIMESTAMP_RANGE= npm start + +Example: +``` +ACTION=find LONGITUDE_RANGE=75 npm start +``` + +Author +------ + +* Akila Nonis - +* Kasun Samarasinghe - +* Pierre Repetto-Andipatin - + +License +------- + +MIT diff --git a/index.js b/index.js index c020d6f..155adaf 100644 --- a/index.js +++ b/index.js @@ -1,25 +1,193 @@ 'use strict'; +var async = require('async'); + require('dotenv').config({ silent: true }); var mongoose = require('mongoose'); mongoose.Promise = require('bluebird'); var Location = require('./models/Location.js'); +var packageVersion = require('./package.json'); +console.log('version: ' + packageVersion.version); + mongoose.connect(process.env.MONGODB_URI); -mongoose.connection.on('error', function () { - console.log( - 'MongoDB Connection Error. Please make sure that MongoDB is running.' - ); - process.exit(1); -}); -var location = new Location({ - timestamp: new Date().getTime(), - latitude: '12.12', - longitude: '5.14', +var db = mongoose.connection; +db.on('error', console.error.bind(console, + 'MongoDB Connection Error. ' + + 'Please make sure that MongoDB is running.\n')); + +db.once('open', () => { + console.log('Connection opened.'); + + switch (process.env.ACTION) { + case 'flush': { + flush(); + break; + } + case 'find': + case 'delete': { + findOrDeleteLocation(); + break; + } + case 'getAll': { + getAllLocations(); + break; + } + default: { + saveLocation(); + } + } }); -location.save(function (err, location) { - console.log(location); +function getAllLocations() { + console.log("all the data "); + Location.find(function(err, searchedLocations) { + if (err) { + console.log(err); + throw err; + } + console.log(searchedLocations); + console.log('total content count : ' +searchedLocations.length); process.exit(0); }); + + +} +function flush() { + Location.remove({}).exec() + .then(() => { + console.log('Database has been flushed.'); + process.exit(0); + }) + .catch(err => { + console.error(err); + process.exit(1); + }); +} + +function saveLocation() { + if (process.env.BULK_SAVE > 0) { + saveMultiple(process.env.BULK_SAVE); + } else { + saveSimple(); + } +} + +function saveSimple() { + var location = new Location({ + timestamp: new Date().getTime(), + latitude: '12.12', + longitude: '5.14', + }); + location.save() + .then(location => { + console.log(location); + process.exit(0); + }) + .catch(err => { + console.error(err); + process.exit(1); + }); +} + +function saveMultiple(count) { + + var locations = []; + + // Creating locations with random coordinates and adding them to an array + for (var i = 0; i < count; i++) { + var location = new Location({ + timestamp: new Date().getTime(), + latitude: Math.round((Math.random() * 90) * 100) / 100 , + longitude: Math.round((Math.random() * 180) * 100) / 100, + }); + locations[i] = location; + } + + // Bulk inserting the array of locations into the DB + Location.insertMany(locations) + .then(locations => { + console.log(locations); + process.exit(0); + }) + .catch(err => { + console.error(err); + process.exit(1); + }); +} + +function findOrDeleteLocation() { + + var latitudeRange, longitudeRange, timestampRange; + + var query = Location.find({}); + + // Latitude Range + if(process.env.LATITUDE_RANGE){ + latitudeRange = process.env.LATITUDE_RANGE.split('-'); + if(latitudeRange.length == 1){ + query.where('latitude').equals(latitudeRange[0]); + }else{ + if(latitudeRange[0]){ + query.where('latitude').gte(latitudeRange[0]); + } + if(latitudeRange[1]){ + query.where('latitude').lte(latitudeRange[1]); + } + } + } + + // Longitude Range + if(process.env.LONGITUDE_RANGE){ + longitudeRange = process.env.LONGITUDE_RANGE.split('-'); + if(longitudeRange.length == 1){ + query.where('longitude').equals(longitudeRange[0]); + }else{ + if(longitudeRange[0]){ + query.where('longitude').gte(longitudeRange[0]); + } + if(longitudeRange[1]){ + query.where('longitude').lte(longitudeRange[1]); + } + } + } + + // Timestamp range + if(process.env.TIMESTAMP_RANGE){ + timestampRange = process.env.TIMESTAMP_RANGE.split('-'); + if(timestampRange.length == 1){ + query.where('timestamp').equals(timestampRange[0]); + }else{ + if(timestampRange[0]){ + query.where('timestamp').gte(timestampRange[0]); + } + if(timestampRange[1]){ + query.where('timestamp').lte(timestampRange[1]); + } + } + } + //after setting the find query in above, execute to get the findings or delete + if (process.env.ACTION == 'delete') { + query.remove({}).exec(function(err) { + console.log('deleted the found'); + process.exit(0); + }).catch(err => { + + console.error(err + 'the errror'); + process.exit(1); + }); + } + else { + console.log('in querying '); + query.exec(function(err, foundLocations) { + console.log(foundLocations); + console.log('Results :' + foundLocations.length); + process.exit(0); + }) + .catch(err => { + console.error(err); + process.exit(1); + }); + } +} diff --git a/models/Location.js b/models/Location.js index 92eff03..57c61f7 100644 --- a/models/Location.js +++ b/models/Location.js @@ -9,6 +9,6 @@ var locationsSchema = new mongoose.Schema({ longitude: String, }); -var Stocks = mongoose.model('Stocks', locationsSchema); +var Locations = mongoose.model('Locations', locationsSchema); -module.exports = Stocks; +module.exports = Locations; diff --git a/package.json b/package.json index e783cde..e7f410b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "locations", - "version": "0.0.1", + "version": "0.0.2", "description": "", "main": "index.js", "scripts": { @@ -11,7 +11,7 @@ "license": "MIT", "dependencies": { "bluebird": "^3.4.6", - "dotenv": "^2.0.0", - "mongoose": "^4.6.6" + "dotenv": "^4.0.0", + "mongoose": "^4.7.6" } }