Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.env
.dotenv
8 changes: 8 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"preset": "node-style-guide",
"requireTrailingComma": {
"ignoreSingleValue": true,
"ignoreSingleLine": true
},
"excludeFiles": ["node_modules/**"]
}
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Locations
=========

Location generator in node

Usage
-----

ACTION=find LATITUDE_RANGE=<lower bound>-<upper bound> LONGITUDE_RANGE=<lower bound>-<upper bound> TIMESTAMP_RANGE=<lower bound>-<upper bound> 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=<value> LONGITUDE_RANGE=<value> TIMESTAMP_RANGE=<value> npm start

Example:
```
ACTION=find LONGITUDE_RANGE=75 npm start
```

Author
------

* Akila Nonis - <akila@anthillsolutions.ch>
* Kasun Samarasinghe - <kasun@anthillsolutions.ch>
* Pierre Repetto-Andipatin - <pierre@anthillsolutions.ch>

License
-------

MIT
192 changes: 180 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
4 changes: 2 additions & 2 deletions models/Location.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "locations",
"version": "0.0.1",
"version": "0.0.2",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -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"
}
}