<commit_before>/* Copyright 2015 IBM Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and
limitations under the License. */

var express = require('express');
var router = express.Router();
var config = require('../config/server');
var parseString = require('xml2js').parseString;
var request = require('request');
var Busboy = require('busboy');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.redirect('/');
});

//getfeed for home page content
router.get('/feed', function(req, res, next){
  if(!req.user)
    res.status(403).end();

  //config.server.domain is the domain name of the server (without the https or the directoy i.e example.com)

  var url = 'https://' + config.server.domain + '/files/oauth/api/documents/feed?visibility=public&includeTags=true';

  //if query parameters exist, append them onto the url
  if(!isEmpty(req.query.q)){
    url = url + '?tag=' + req.query.q;
  }

  var headers = {};

  // we must attach the key we got through passportto the header as
  // Authorization: Bearer + key. Passport gives us access to the user profile
  // we saved through the request user object
  headers['Authorization'] = 'Bearer ' + req.user.accessToken;

  var options = {
    url: url,
    headers: headers
  };

  request.get(options, function(error, response, body){

    // if there is an error lets log it to the console and inform the angular
    // of our app so it can be handled
    if(error){
      console.log('Error occured while getting feed from Connections Cloud: ' + error);
      res.status(500).end();
    } else {

      // otherwise, the api returns an xml which can be easily converted to a
      // JSON to make parsing easier using the xml2js module for nodejs
      parseString(body, function(err, result){

        // initialize the array of photos we will be sending back
        var photos = [];

        // get the actual entries object in the response
        var entries = result.feed.entry;

        // iterate over the entries to send back each photo that was returned
        for(var i = 0; i < entries.length; i++){
          var photo = {};
          var entry = entries[i];

          // the photo id which is used to reference the photo internally for
          // ibm connection cloud, this will need to be stored to quickly
          // get the photo and information on the photo later
          photo.id = entry['td:uuid'][0];

          // iterate through the tags in the entry and build an object that has
          // all the tags, starting at 1 because the first category describes
          // the document and is not a true 'tag'
          var tags = [];
          for(var j = 1; j < entry.category.length; j ++){
            var category = entry.category[j];
            var tag = category.$.label;
            tags.push(tag);
          }
          photo.tags = tags;

          // the author of the file, and in our case the photographer
          photo.photographer = entry.author[0].name[0];

          // the title of the document
          photo.title = entry.title[0]['_'];

          // the upload date of the document
          photo.published = entry.published[0];

          // the link object contains many links related to the document,
          // however we want the link to the actual image, therefore we will
          // look for the object with the type of image
          for(var j = 0; j < entry.link.length; j++){
            var link = entry.link[j];
            var type = link.$.type;
            if(!(type === undefined) && (type.indexOf('image') > -1)){
              photo.link = link.$.href;
              break;
            }
          }

          // in addition we need to pass the library id of the entry, for later
          // calls in which we will have to pass the library id to the api
          photo.libraryid = entry['td:libraryId'][0];

          // push the photo to our photos array
          console.log('pushing :' + JSON.stringify(photo));
          photos.push(photo);
        }

        //return our photos array
        console.log('sending response');
        res.send(photos);
      });
    }
  });
});

//get photo for retrieving a photo by id
router.get('/photo', function(req, res, next){

  if(!req.user)
    res.status(403).end();

  //if no id was passed, return an error code
  if(isEmpty(req.query.id)){
    console.log('query not found');
    res.status(412).end();
  } else {
      var url = 'https://' + config.server.domain + '/files/oauth/api/myuserlibrary/document/' + req.query.id + '/entry?includeTags=true';

      // we must attach the key we got through passportto the header as
      // Authorization: Bearer + key. Passport gives us access to the user profile
      // we saved through the request user object
      var headers = {'Authorization': 'Bearer ' + req.user.accessToken};

      var options = {
        url: url,
        headers: headers
      };

      request.get(options, function(error, response, body){
        if(error){
          console.log('photo error: ' + error);
          res.status(500).end();
        } else {
          //see get feed for more details
          parseString(body, function(err, result){
            var photo = {};
            var entry = result.entry;
            photo.id = entry['td:uuid'][0];
            var tags = [];
            for(var j = 1; j < entry.category.length; j ++){
              var category = entry.category[j];
              var tag = category.$.label;
              tags.push(tag);
            }
            photo.tags = tags;
            for(var j = 0; j < entry.link.length; j++){
              var link = entry.link[j];
              var type = link.$.type;
              if(!(type === undefined) && (type.indexOf('image') > -1)){
                photo.link = link.$.href;
                break;
              }
            }
            photo.photographer = entry.author[0].name[0];
            photo.title = entry.title[0]['_'];
            photo.published = entry.published[0];
            var socialx = entry['snx:rank'];
            for(var i = 0; i < socialx.length; i++){
              var x = socialx[i];
              if(x.$.scheme.indexOf('recommendations') > -1){
                photo.likes = parseInt(x['_']);
                break;
              }
            }
            photo.libraryid = entry['td:libraryId'][0];
            console.log('Sending response');
            res.send(photo);
          });
        }
      });
  }

});

router.post('/like', function(req, res, next){
  if(!req.user)
    res.status(403).end();

  if(isEmpty(req.query.id)){
    req.status(412).end();
  } else {

  }
})

router.get('/commments', function(req, res, next){
  if(!req.user)
    res.status(403).end();

  //if no id was passed
  if(isEmpty(req.query.id)){
    res.status(412).end();
  } else if(isEmpty(req.query.userid)){
    res.status(412).end();
  } else {
    var url = 'https://' + config.server.domain + 'files/oauth/api/userlibrary/' + req.query.userid + '/document/' + req.query.id + '/feed';

    var headers = {'Authorization': 'Bearer ' + req.user.accessToken};

    var options = {
      url: url,
      headers: headers
    };

    request.get(options, function(error, response, body){
      if(error){
        res.send(error);
      } else {
        res.send(body);
      }
    });
  }
});

//upload a file
router.post('/upload', function(req, res, next){
  // if the user does not exist, send back forbidden
  if(!req.user)
    res.status(403).end()

  // before uploading, we must obtain a nonce, which is a handshake between
  // the api and our server to allow us to post to the server
  var url = 'https://' + config.server.domain + '/files/oauth/api/nonce';

  var headers = {'Authorization': 'Bearer ' + req.user.accessToken};

  var options = {
    url: url,
    headers: headers
  };

  // making the nonce request
  request.get(options, function(error, response, body){

    // if there was an error log the error and send back an error
    if(error){
      console.log('nonce failed: ' + error);
      res.status(500).end()
    } else {

      // the nonce is returned in the body of the response
      var nonce = body;

      // to obtain the file from the client, we use a module called busboy
      // that allows us to obtain the file stream from the client
      var busboy = new Busboy({headers: req.headers});

      // when busboy encounters a file (which should be the only thing it
      // obtains from the page) it will then pip the file to a request to the
      // api server
      busboy.on('file', function(fieldname, file, filename, encoding, mimetype){
        var url = 'https://' + server.domain + '/files/oauth/api/myuserlibrary/feed';

        // the slug is used to tell the api what it should call the file
        var slug = filename;

        var headers = {
          'Authorization': 'Bearer ' + req.user.accessToken,
          'Slug': slug,
          //the nonce is the nonce we obtained before
          'X-Update-Nonce': nonce
        };

        var options = {
          url: url,
          headers: headers
        };

        // we then pipe the file to the request to the api server
        file.pipe(request.post(options, function(error, response, body){

          // if we recieve an error then log it and send and erro back to the
          // client
          if(error){
            console.log('upload failed: ' + error);
            res.status(500).end();
          }
        }));

      });

      // when the busboy finishes processing the file, send back an OK status
      // and close the connection
      busboy.on('finish', function(){
        res.status(200).end();
      });
    }
  });
});

var hasOwnProperty = Object.prototype.hasOwnProperty;

function isEmpty(obj) {

    // null and undefined are "empty"
    if (obj == null) return true;

    // Assume if it has a length property with a non-zero value
    // that that property is correct.
    if (obj.length > 0)    return false;
    if (obj.length === 0)  return true;

    // Otherwise, does it have any properties of its own?
    // Note that this doesn't handle
    // toString and valueOf enumeration bugs in IE < 9
    for (var key in obj) {
        if (hasOwnProperty.call(obj, key)) return false;
    }

    return true;
}

module.exports = router;
<commit_msg>fixed comment route and added user id to photo route return<commit_after>/* Copyright 2015 IBM Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and
limitations under the License. */

var express = require('express');
var router = express.Router();
var config = require('../config/server');
var parseString = require('xml2js').parseString;
var request = require('request');
var Busboy = require('busboy');
var fs = require('fs');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.redirect('/');
});

//getfeed for home page content
router.get('/feed', function(req, res, next){
  if(!req.user)
    res.status(403).end();

  //config.server.domain is the domain name of the server (without the https or the directoy i.e example.com)

  var url = 'https://' + config.server.domain + '/files/oauth/api/documents/feed?visibility=public&includeTags=true';

  //if query parameters exist, append them onto the url
  if(!isEmpty(req.query.q)){
    url = url + '?tag=' + req.query.q;
  }

  var headers = {};

  // we must attach the key we got through passportto the header as
  // Authorization: Bearer + key. Passport gives us access to the user profile
  // we saved through the request user object
  headers['Authorization'] = 'Bearer ' + req.user.accessToken;

  var options = {
    url: url,
    headers: headers
  };

  request.get(options, function(error, response, body){

    // if there is an error lets log it to the console and inform the angular
    // of our app so it can be handled
    if(error){
      console.log('Error occured while getting feed from Connections Cloud: ' + error);
      res.status(500).end();
    } else {

      // otherwise, the api returns an xml which can be easily converted to a
      // JSON to make parsing easier using the xml2js module for nodejs
      parseString(body, function(err, result){

        // initialize the array of photos we will be sending back
        var photos = [];

        // get the actual entries object in the response
        var entries = result.feed.entry;

        // iterate over the entries to send back each photo that was returned
        for(var i = 0; i < entries.length; i++){
          var photo = {};
          var entry = entries[i];

          // the photo id which is used to reference the photo internally for
          // ibm connection cloud, this will need to be stored to quickly
          // get the photo and information on the photo later
          photo.id = entry['td:uuid'][0];

          // iterate through the tags in the entry and build an object that has
          // all the tags, starting at 1 because the first category describes
          // the document and is not a true 'tag'
          var tags = [];
          for(var j = 1; j < entry.category.length; j ++){
            var category = entry.category[j];
            var tag = category.$.label;
            tags.push(tag);
          }
          photo.tags = tags;

          // the author of the file, and in our case the photographer
          photo.photographer = entry.author[0].name[0];

          // the title of the document
          photo.title = entry.title[0]['_'];

          // the upload date of the document
          photo.published = entry.published[0];

          // the link object contains many links related to the document,
          // however we want the link to the actual image, therefore we will
          // look for the object with the type of image
          for(var j = 0; j < entry.link.length; j++){
            var link = entry.link[j];
            var type = link.$.type;
            if(!(type === undefined) && (type.indexOf('image') > -1)){
              photo.link = link.$.href;
              break;
            }
          }

          // in addition we need to pass the library id of the entry, for later
          // calls in which we will have to pass the library id to the api
          photo.libraryid = entry['td:libraryId'][0];
          console.log(photo.id + ' : ' + photo.libraryid);

          // push the photo to our photos array
          console.log('pushing :' + JSON.stringify(photo));
          photos.push(photo);
        }

        //return our photos array
        console.log('sending response');
        res.send(photos);
      });
    }
  });
});

//get photo for retrieving a photo by id
router.get('/photo', function(req, res, next){

  if(!req.user)
    res.status(403).end();

  //if no id was passed, return an error code
  if(isEmpty(req.query.id) || isEmpty(req.query.lid)){
    console.log('query not found');
    res.status(412).end();
  } else {
      var url = 'https://' + config.server.domain + '/files/oauth/api/library/' + req.query.lid + '/document/' + req.query.id + '/entry?includeTags=true';

      // we must attach the key we got through passportto the header as
      // Authorization: Bearer + key. Passport gives us access to the user profile
      // we saved through the request user object
      var headers = {'Authorization': 'Bearer ' + req.user.accessToken};

      var options = {
        url: url,
        headers: headers
      };

      request.get(options, function(error, response, body){
        if(error){
          console.log('photo error: ' + error);
          res.status(500).end();
        } else {
          //see get feed for more details
          parseString(body, function(err, result){
            fs.writeFile("response.txt", JSON.stringify(result));
            var photo = {};
            var entry = result.entry;
            photo.id = entry['td:uuid'][0];
            var tags = [];
            for(var j = 1; j < entry.category.length; j ++){
              var category = entry.category[j];
              var tag = category.$.label;
              tags.push(tag);
            }
            photo.tags = tags;
            for(var j = 0; j < entry.link.length; j++){
              var link = entry.link[j];
              var type = link.$.type;
              if(!(type === undefined) && (type.indexOf('image') > -1)){
                photo.link = link.$.href;
                break;
              }
            }
            photo.photographer = entry.author[0].name[0];
            photo.userid = entry.author[0]['snx:userid'][0];
            photo.title = entry.title[0]['_'];
            photo.published = entry.published[0];
            var socialx = entry['snx:rank'];
            for(var i = 0; i < socialx.length; i++){
              var x = socialx[i];
              if(x.$.scheme.indexOf('recommendations') > -1){
                photo.likes = parseInt(x['_']);
                break;
              }
            }
            photo.libraryid = entry['td:libraryId'][0];
            console.log('Sending response');
            res.send(photo);
          });
        }
      });
  }
});

router.post('/like', function(req, res, next){
  if(!req.user)
    res.status(403).end();

  if(isEmpty(req.query.id)){
    req.status(412).end();
  } else {

  }
})

router.get('/comments', function(req, res, next){
  if(!req.user)
    res.status(403).end();

  //if no id was passed
  if(isEmpty(req.query.id)){
    console.log("query not found");
    res.status(412).end();
  } else if(isEmpty(req.query.userid)){
    res.status(412).end();
  } else {
    var url = 'https://' + config.server.domain + '/files/oauth/api/userlibrary/' + req.query.userid + '/document/' + req.query.id + '/feed?category=comment';

    var headers = {'Authorization': 'Bearer ' + req.user.accessToken};

    var options = {
      url: url,
      headers: headers
    };

    console.log(JSON.stringify(options));

    console.log('Making request');

    request.get(options, function(error, response, body){
      if(error){
        console.log('error in get comment: ' + error);
        res.status(500).end();
      } else {
        parseString(body, function(err, result){
          // fs.writeFile("response.txt", JSON.stringify(result));
          var comments = [];
          var entries = result.feed.entry;
          for(var i = 0; i < entries.length; i++){
            var entry = entries[i];
            var comment = {};
            comment.author = entry.author[0].name[0];
            comment.date = entry.published[0];
            comment.content = entry.content[0]['_'];
            comments.push(comment);
          }
          res.send(comments);
        });
      }
    });
  }
});

//upload a file
router.post('/upload', function(req, res, next){
  // if the user does not exist, send back forbidden
  if(!req.user)
    res.status(403).end()

  // before uploading, we must obtain a nonce, which is a handshake between
  // the api and our server to allow us to post to the server
  var url = 'https://' + config.server.domain + '/files/oauth/api/nonce';

  var headers = {'Authorization': 'Bearer ' + req.user.accessToken};

  var options = {
    url: url,
    headers: headers
  };

  // making the nonce request
  request.get(options, function(error, response, body){

    // if there was an error log the error and send back an error
    if(error){
      console.log('nonce failed: ' + error);
      res.status(500).end()
    } else {

      // the nonce is returned in the body of the response
      var nonce = body;

      // to obtain the file from the client, we use a module called busboy
      // that allows us to obtain the file stream from the client
      var busboy = new Busboy({headers: req.headers});

      // when busboy encounters a file (which should be the only thing it
      // obtains from the page) it will then pip the file to a request to the
      // api server
      busboy.on('file', function(fieldname, file, filename, encoding, mimetype){
        var url = 'https://' + server.domain + '/files/oauth/api/myuserlibrary/feed';

        // the slug is used to tell the api what it should call the file
        var slug = filename;

        var headers = {
          'Authorization': 'Bearer ' + req.user.accessToken,
          'Slug': slug,
          //the nonce is the nonce we obtained before
          'X-Update-Nonce': nonce
        };

        var options = {
          url: url,
          headers: headers
        };

        // we then pipe the file to the request to the api server
        file.pipe(request.post(options, function(error, response, body){

          // if we recieve an error then log it and send and erro back to the
          // client
          if(error){
            console.log('upload failed: ' + error);
            res.status(500).end();
          }
        }));

      });

      // when the busboy finishes processing the file, send back an OK status
      // and close the connection
      busboy.on('finish', function(){
        res.status(200).end();
      });
    }
  });
});

router.get('/profile', function(req, res, next){
  if(!req.user){
    res.status(403).end();
  }

  if(isEmpty(req.query.displayName)){
    console.log('query not found');
    req.status(412).end()
  } else {

    var url = '/profiles/atom/search.do?displayName=' + req.query.displayName;

    var headers = {'Authorization' : 'Bearer ' + req.user.accessToken};

    var options = {
      url : url,
      headers : headers
    };

    request.get(options, function(error, response, body){
      if(error){
        console.log('error: ' + error);
        res.status(500).end();
      }

      parseString(body, function(err, result){
        fs.writeFile("response.txt", JSON.stringify(result));
      });
    });
  }
});

var hasOwnProperty = Object.prototype.hasOwnProperty;

function isEmpty(obj) {

    // null and undefined are "empty"
    if (obj == null) return true;

    // Assume if it has a length property with a non-zero value
    // that that property is correct.
    if (obj.length > 0)    return false;
    if (obj.length === 0)  return true;

    // Otherwise, does it have any properties of its own?
    // Note that this doesn't handle
    // toString and valueOf enumeration bugs in IE < 9
    for (var key in obj) {
        if (hasOwnProperty.call(obj, key)) return false;
    }

    return true;
}

module.exports = router;
<|endoftext|>


<commit_before>const express = require('express');
const playerFields = require('./playerFields');
const filterDeps = require('../util/filterDeps');
const spec = require('./spec');
const cors = require('cors');

const api = new express.Router();
const subkeys = playerFields.subkeys;

// CORS headers
api.use(cors());
api.use('/metadata', cors({
  origin: true,
  credentials: true,
}));

// Player endpoints middleware
api.use('/players/:account_id/:info?', (req, res, cb) => {
  if (isNaN(Number(req.params.account_id))) {
    return cb('invalid account_id');
  }
  if (req.params.info !== 'matches') {
    // We want to show insignificant/unbalanced matches in match view
    // Set default significant to true in all other views
    req.query.significant = [1];
  }
  let filterCols = [];
  Object.keys(req.query).forEach((key) => {
    // numberify and arrayify everything in query
    req.query[key] = [].concat(req.query[key]).map(e =>
      (isNaN(Number(e)) ? e : Number(e))
    );
    // build array of required projections due to filters
    filterCols = filterCols.concat(filterDeps[key] || []);
  });
  req.queryObj = {
    project: ['match_id', 'player_slot', 'radiant_win'].concat(filterCols).concat((req.query.sort || []).filter(f => subkeys[f])),
    filter: req.query || {},
    sort: req.query.sort,
    limit: Number(req.query.limit),
    offset: Number(req.query.offset),
  };
  return cb();
});

// API spec
api.get('/', (req, res) => {
  res.json(spec);
});

// API endpoints
Object.keys(spec.paths).forEach((path) => {
  Object.keys(spec.paths[path]).forEach((verb) => {
    const {
      route,
      func,
    } = spec.paths[path][verb];
    api[verb](route(), func);
  });
});

module.exports = api;
<commit_msg>use origin/credentials for all endpoints<commit_after>const express = require('express');
const playerFields = require('./playerFields');
const filterDeps = require('../util/filterDeps');
const spec = require('./spec');
const cors = require('cors');

const api = new express.Router();
const subkeys = playerFields.subkeys;

// CORS headers
api.use(cors({
  origin: true,
  credentials: true,
}));

// Player endpoints middleware
api.use('/players/:account_id/:info?', (req, res, cb) => {
  if (isNaN(Number(req.params.account_id))) {
    return cb('invalid account_id');
  }
  if (req.params.info !== 'matches') {
    // We want to show insignificant/unbalanced matches in match view
    // Set default significant to true in all other views
    req.query.significant = [1];
  }
  let filterCols = [];
  Object.keys(req.query).forEach((key) => {
    // numberify and arrayify everything in query
    req.query[key] = [].concat(req.query[key]).map(e =>
      (isNaN(Number(e)) ? e : Number(e))
    );
    // build array of required projections due to filters
    filterCols = filterCols.concat(filterDeps[key] || []);
  });
  req.queryObj = {
    project: ['match_id', 'player_slot', 'radiant_win'].concat(filterCols).concat((req.query.sort || []).filter(f => subkeys[f])),
    filter: req.query || {},
    sort: req.query.sort,
    limit: Number(req.query.limit),
    offset: Number(req.query.offset),
  };
  return cb();
});

// API spec
api.get('/', (req, res) => {
  res.json(spec);
});

// API endpoints
Object.keys(spec.paths).forEach((path) => {
  Object.keys(spec.paths[path]).forEach((verb) => {
    const {
      route,
      func,
    } = spec.paths[path][verb];
    api[verb](route(), func);
  });
});

module.exports = api;
<|endoftext|>


<commit_before>const express = require("express");
const connect = require("../utils/database.js");

let app = express.Router();

app.get("/", function(req, res){
	res.send("
Welcome to the API Endpoint. Please refer to the documentation for its usage.
");
});

app.get("*", function(req, res, next){
	res.header("Access-Control-Allow-Origin", "*");
	next();
});

app.get("/:collection", function(req, res, next){
	if(req.params.collection.substr(0, 1) == "_"){
		res.status(403).send({error: "Collection name may not start with _"});
	}else{
		next();
	}
});

app.get("/:collection", function (req, res){
	var collection = req.params.collection;

	connect.then(function(db){
		db.collection(collection).find().toArray(function (err, result) {
			if (err) throw err;

			_.each(result, function(el){
				delete el._metadata;
			});
			res.json(result);
		});
	});
});

app.get("/:collection/:name/:value", function(req, res){
	var collection = req.params.collection;
	var obj = {};
	obj[req.params.name] = req.params.value;

	connect.then(function(db){
		db.collection(collection).find(obj).toArray(function (err, result) {
			if (err) throw err;

			_.each(result, function(el){
				delete el._metadata;
			});
			res.json(result);
		});
	});
});

app.get("/:collection/filter\\[:name\]=:value", function(req, res){
	var collection = req.params.collection;
	var obj = {};
	obj[req.params.name] = req.params.value;

	connect.then(function(db){
		db.collection(collection).find(obj).toArray(function (err, result) {
			if (err) throw err;

			_.each(result, function(el){
				delete el._metadata;
			});
			res.json(result);
		});
	});
});

module.exports = app;<commit_msg>Bug fixes<commit_after>const express = require("express");
const connect = require("../utils/database.js");
const _ = require("lodash");

let app = express.Router();

app.get("/", function(req, res){
	res.send("
Welcome to the API Endpoint. Please refer to the documentation for its usage.
");
});

app.get("*", function(req, res, next){
	res.header("Access-Control-Allow-Origin", "*");
	next();
});

app.get("/:collection*", function(req, res, next){
	if(req.params.collection.substr(0, 1) == "_"){
		res.status(403).send({error: "Collection name may not start with _"});
	}else{
		next();
	}
});

app.get("/:collection", function (req, res){
	var collection = req.params.collection;

	connect.then(function(db){
		db.collection(collection).find().toArray(function (err, result) {
			if (err) throw err;

			_.each(result, function(el){
				delete el._metadata;
			});
			res.json(result);
		});
	});
});

app.get("/:collection/:name/:value", function(req, res){
	var collection = req.params.collection;
	var obj = {};
	obj[req.params.name] = req.params.value;

	connect.then(function(db){
		db.collection(collection).find(obj).toArray(function (err, result) {
			if (err) throw err;

			_.each(result, function(el){
				delete el._metadata;
			});
			res.json(result);
		});
	});
});

app.get("/:collection/filter\\[:name\]=:value", function(req, res){
	var collection = req.params.collection;
	var obj = {};
	obj[req.params.name] = req.params.value;

	connect.then(function(db){
		db.collection(collection).find(obj).toArray(function (err, result) {
			if (err) throw err;

			_.each(result, function(el){
				delete el._metadata;
			});
			res.json(result);
		});
	});
});

module.exports = app;<|endoftext|>


<commit_before>var express = require('express');
var hipchat = require('../lib/hipchat');
var router = express.Router();
var fs = require('fs');

router.get('/', function(req, res, next) {
    res.render('json', {layout: false });
});

router.get("/image/:name", function(req, res) {

    var name = 'uploads/'+req.params.name;
    console.log( name );

    fs.readFile(name, function (err, data) {
        if (err) throw err;
        var img = new Buffer(data, 'base64');

        res.writeHead(200, {
            'Content-Type': 'image/jpg',
            'Content-Length': img.length
        });
        res.end(img);
    });

});

/* POST TEST */
router.post('/save', function(req, res, next) {

    var msg = req.body.message + ' ('+req.body.Filename+')';

    console.log( req.body );
    var link = 'http://lmodev.com:8888/api/' + req.files[0].path;

    msg += "\n";
    msg += link;

    hipchat.success(msg);

    if (false) {
        res.render('xmlError', { layout: false, message: "Something broke!"});
    } else {
        res.render('xmlSuccess', { layout: false });
    }

});

module.exports = router;
<commit_msg>fix link path<commit_after>var express = require('express');
var hipchat = require('../lib/hipchat');
var router = express.Router();
var fs = require('fs');

router.get('/', function(req, res, next) {
    res.render('json', {layout: false });
});

router.get("/image/:name", function(req, res) {

    var name = 'uploads/'+req.params.name;
    console.log( name );

    fs.readFile(name, function (err, data) {
        if (err) throw err;
        var img = new Buffer(data, 'base64');

        res.writeHead(200, {
            'Content-Type': 'image/jpg',
            'Content-Length': img.length
        });
        res.end(img);
    });

});

/* POST TEST */
router.post('/save', function(req, res, next) {

    var msg = req.body.message + ' ('+req.body.Filename+')';

    console.log( req.body );
    var link = 'http://lmodev.com:8888/api/image/' + req.files[0].filename;

    msg += "\n";
    msg += link;

    hipchat.success(msg);

    if (false) {
        res.render('xmlError', { layout: false, message: "Something broke!"});
    } else {
        res.render('xmlSuccess', { layout: false });
    }

});

module.exports = router;
<|endoftext|>


<commit_before>var express = require('express');
_ = require('underscore')._
var request = require('request');
var router = express.Router({mergeParams: true});
var exec = require('child_process').exec;
var path = require('path');
var parentDir = path.resolve(process.cwd());

router.get('/', function(req, res, next) {
    res.json({'info' : 'This is the ControCurator API v0.1'});
});

router.post('/',function(req,res) {
    try {
        var input = JSON.parse(req);
    } catch (e) {
        res.json({'error':'Input was not valid JSON.'});
    }

    var amountofitems = input.length;
    var gooditems = 0;
    var baditems = 0;

    foreach (var currentitem in input)
    {
        if (!currentitem.hasOwnProperty('id') || !currentitem.hasOwnProperty('text'))
        {
            baditems++;
            continue;
        }
        gooditems++;
    }
    var controscore = Math.random();

    res.json({'controversy':controscore,'totalItems':amountofitems,'goodItems':gooditems,'badItems':baditems});

});
module.exports = router;
<commit_msg>Fix in loop<commit_after>var express = require('express');
_ = require('underscore')._
var request = require('request');
var router = express.Router({mergeParams: true});
var exec = require('child_process').exec;
var path = require('path');
var parentDir = path.resolve(process.cwd());

router.get('/', function(req, res, next) {
    res.json({'info' : 'This is the ControCurator API v0.1'});
});

router.post('/',function(req,res) {
    try {
        var input = JSON.parse(req);
    } catch (e) {
        res.json({'error':'Input was not valid JSON.'});
    }

    var amountofitems = input.length;
    var gooditems = 0;
    var baditems = 0;

    for (var i=0; i < amountofitems; i++)
    {
        var currentitem = input[i];
        if (!currentitem.hasOwnProperty('id') || !currentitem.hasOwnProperty('text'))
        {
            baditems++;
            continue;
        }
        gooditems++;
    }
    var controscore = Math.random();

    res.json({'controversy':controscore,'totalItems':amountofitems,'goodItems':gooditems,'badItems':baditems});

});
module.exports = router;
<|endoftext|>


<commit_before>var express = require('express');
var router = express.Router();

var cassandra = require('cassandra-driver');
var client = new cassandra.Client({ contactPoints: ['127.0.0.1']});

/**
 * List all keyspaces for this node
 */
router.get('/', function(req, res, next) {
    client.execute('select keyspace_name from system.schema_keyspaces', function (err, result) {
        if(err !== null) {
            var error = new Error('Bad Request');
            error.status = 400;
            error.message = err.message;
            next(error);
            return
        }
        var keyspaces = result.rows;
        res.json(keyspaces);
    });
});

/**
 * List all column_families for given key_space
 */
router.get('/:keyspace/', function(req, res, next) {
    var query = 'select keyspace_name, columnfamily_name from system.schema_columnfamilies where keyspace_name = ?';
    var params = [req.params.keyspace];

    client.execute(query, params, {prepare: true}, function (err, result) {
        if(err !== null) {
            var error = new Error('Bad Request');
            error.status = 400;
            error.message = err.message;
            next(error);
            return
        }
        var columnfamilies = result.rows;
        res.json(columnfamilies);
    });
});

/**
 * List all columns for given column family
 */
router.get('/:keyspace/:columnfamily/', function(req, res, next) {
    var query = 'select * from system.schema_columns where keyspace_name = ? and columnfamily_name= ?';
    var params = [req.params.keyspace, req.params.columnfamily];

    client.execute(query, params, {prepare: true}, function (err, result) {
        if(err !== null) {
            var error = new Error('Bad Request');
            error.status = 400;
            error.message = err.message;
            next(error);
            return
        }
        var columns = result.rows;
        res.json(columns);
    });
});

/**
 * Fetch data rows from given column_family
 */
router.get('/:keyspace/:columnfamily/select', function(req, res, next) {
    var query = 'select * from ' + req.params.keyspace + '.' + req.params.columnfamily;

    client.execute(query,  function (err, result) {
        if(err !== null) {
            var error = new Error('Bad Request');
            error.status = 400;
            error.message = err.message;
            next(error);
            return;
        }
        var columns = result.rows;
        res.json(columns);
    });
});

/**
 * Execute a query
 */
router.put('/', function(req, res, next) {
    client.execute(req.body.query, function (err, result) {
        if(err !== null) {
            var error = new Error('Bad Request');
            error.status = 400;
            error.message = err.message;
            error.name = err.name;
            next(error);
            return
        }
        res.json(result);
    });
});

/**
 * Execute a query for given keyspace
 */
router.put('/:keyspace/', function(req, res, next) {
    client.keyspace = req.params.keyspace;
    client.execute(req.body.query, function (err, result) {
    if(err !== null) {
        var error = new Error('Bad Request');
        error.status = 400;
        error.message = err.message;
        error.name = err.name;
        next(error);
        return
    }
    res.json(result);
    });
});

router.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

router.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.json({
        message: err.message,
        error: err.name
    });
});

module.exports = router;<commit_msg>error detection added for missing parameter<commit_after>var express = require('express');
var router = express.Router();

var cassandra = require('cassandra-driver');
var client = new cassandra.Client({ contactPoints: ['127.0.0.1']});

/**
 * List all keyspaces for this node
 */
router.get('/', function(req, res, next) {
    client.execute('select keyspace_name from system.schema_keyspaces', function (err, result) {
        if(err !== null) {
            var error = new Error('Bad Request');
            error.status = 400;
            error.message = err.message;
            next(error);
            return
        }
        var keyspaces = result.rows;
        res.json(keyspaces);
    });
});

/**
 * List all column_families for given key_space
 */
router.get('/:keyspace/', function(req, res, next) {
    var query = 'select keyspace_name, columnfamily_name from system.schema_columnfamilies where keyspace_name = ?';
    var params = [req.params.keyspace];

    client.execute(query, params, {prepare: true}, function (err, result) {
        if(err !== null) {
            var error = new Error('Bad Request');
            error.status = 400;
            error.message = err.message;
            next(error);
            return
        }
        var columnfamilies = result.rows;
        res.json(columnfamilies);
    });
});

/**
 * List all columns for given column family
 */
router.get('/:keyspace/:columnfamily/', function(req, res, next) {
    var query = 'select * from system.schema_columns where keyspace_name = ? and columnfamily_name= ?';
    var params = [req.params.keyspace, req.params.columnfamily];

    client.execute(query, params, {prepare: true}, function (err, result) {
        if(err !== null) {
            var error = new Error('Bad Request');
            error.status = 400;
            error.message = err.message;
            next(error);
            return
        }
        var columns = result.rows;
        res.json(columns);
    });
});

/**
 * Fetch data rows from given column_family
 */
router.get('/:keyspace/:columnfamily/select', function(req, res, next) {
    var query = 'select * from ' + req.params.keyspace + '.' + req.params.columnfamily;

    client.execute(query,  function (err, result) {
        if(err !== null) {
            var error = new Error('Bad Request');
            error.status = 400;
            error.message = err.message;
            next(error);
            return;
        }
        var columns = result.rows;
        res.json(columns);
    });
});

/**
 * Execute a query
 */
router.put('/', function(req, res, next) {
    if(req.body.query === undefined) {
        var error = new Error('Bad Request');
        error.status = 400;
        error.message = "query parameter is missing";
        error.name = "missing parameter";
        next(error);
        return
    }

    client.execute(req.body.query, function (err, result) {
        if(err !== null) {
            var error = new Error('Bad Request');
            error.status = 400;
            error.message = err.message;
            error.name = err.name;
            next(error);
            return
        }
        res.json(result);
    });
});

/**
 * Execute a query for given keyspace
 */
router.put('/:keyspace/', function(req, res, next) {
    client.keyspace = req.params.keyspace;
    client.execute(req.body.query, function (err, result) {
    if(err !== null) {
        var error = new Error('Bad Request');
        error.status = 400;
        error.message = err.message;
        error.name = err.name;
        next(error);
        return
    }
    res.json(result);
    });
});

router.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

router.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.json({
        message: err.message,
        error: err.name
    });
});

module.exports = router;<|endoftext|>


<commit_before>'use strict';

var express = require('express');
var router = express.Router();
var extend = require('node.extend');
var request = require('request');
var lxc = require('../lxc');
var doapi = require('../doapi')();

var label2container = {};
var workers = [];

var checkBalance = function(){

};

var getFreeMem = function(ip, callback){

	return lxc.exec(
		"python3 -c \"a=`head /proc/meminfo|grep MemAvail|grep -Po '\\d+'`;t=`head /proc/meminfo|grep MemTotal|grep -Po '\\d+'`;print(round(((t-a)/t)*100, 2))\"",
		ip,
		callback
	);
};

var containerFree = function(container){
	lxc.stop(name, container);
	container.worker.usedContainer--;
	delete label2container[container.label];
};

var lxcTimeout = function(container, time){
	time = time || 900000; // 15 minutes

	if(container.hasOwnProperty('timeout')){
		clearTimeout(container.timeout);
	}

	return container.timeout = setTimeout(function(){
		containerFree(container)
		return startAll(container.worker);
	}, time);
};

var runner = function(req, res, container){
	console.log('calling runner:', container);

	var httpOptions = {
		url: 'http://' + container.worker.ip,
		headers: {
			Host: container.name
		},
		body: JSON.stringify({
			code: req.body.code
		})
	};

	return request.post(httpOptions, function(error, response, body){
		console.log('runner response:', arguments)
		body = JSON.parse(body);
		var i = -1;
		while(workers[++i].availContainers.length && workers[i].index < container.worker.index){
			containerFree(container);

			container = workers[i].availContainers.pop();
			container.usedContainer++;
		}
		body['ip'] = container.label;
		lxcTimeout(container);
		return res.json(body);

	});
};

var makeWokerObj = function(worker){
	worker.networks.v4.forEach(function(value){
		worker[value.type+'IP'] = value.ip_address;
	});
	worker.availContainers = [];
	worker.ip = worker.privateIP;
	worker.usedContainer = 0;
	return worker;
};

var getWorkers = function(){
	doapi.dropletsByTag('clworker', function(data){
		data = JSON.parse(data);
		data['droplets'].forEach(function(value){
			var workerIDX = workers.push(makeWokerObj(value)) - 1;
			workers[workerIDX].index = workerIDX;

			startWorker(workers[workerIDX]);
		});
	});
};

var getAvailContainer = function(){
	var i = -1;
	while(workers[++i].availContainers.length){
		var container = wrokers[i].availContainers.pop();
		label2container[container.label] = container;
		container.worker.usedContainer++;
		return container;
	}
};

var startWorker = function(worker, stopPercent){
	stopPercent = stopPercent || 30;
	getFreeMem(worker.ip, function(usedMemPercent){
		if(usedMemPercent < stopPercent ){
			var name = 'crunner-'+(Math.random()*100).toString().replace('.','');
			return lxc.startEphemeral(name, 'crunner0', worker.ip, function(data){
				if( !data.ip ) return setTimeout(startWorker(worker),0);
				console.log('started container')

				worker.availContainers.push({
					ip: data.ip,
					name: name,
					worker: worker,
					label: worker.name+':'+name
				});
				return setTimeout(startWorker(worker, stopPercent), 0);
			});
		}else{
			console.log('using', usedMemPercent, 'percent memory, stopping container creation!', worker.availContainers.length, 'created');
		}
	});
};

getWorkers();

// router.get('/start/:name', function(req, res, next){
// 	return lxc.start(req.params.name, function(data){
// 		if(!data){
// 			return res.json({
// 				status: 500,
// 				name: req.params.name,
// 				message: data
// 			});
// 		}else{
// 			res.json({});
// 		}
// 	});
// });

// router.get('/live/:template/:name', function(req, res, next){
// 	return lxc.startEphemeral(req.params.name, req.params.template, function (data) {
// 		console.log('live', arguments);
// 		return res.json(data);
// 	});
// });

// router.get('/clone/:template/:name', function(req, res, next){
// 	return lxc.clone(req.params.name, req.params.template, function(data){
// 		console.log('clone', arguments);
// 		if( data.match(/Created container/) ){
// 			return res.json({status: 200});
// 		}else{
// 			return res.json({status: 500, message: data});
// 		}
// 	});
// });

// router.get('/destroy/:name', function(req, res, next){
// 	return lxc.destroy(req.params.name, function(data){
// 		console.log('destroy', arguments);
// 		if(data){
// 			return res.json({status: 500, message: data});
// 		}else{
// 			return res.json({status: 200});
// 		}
// 	});
// });

// router.get('/info/:name', function(req, res, next){
// 	return lxc.info(req.params.name, function(data){
// 		return res.json(data);
// 	});
// });

// router.get('/list', function(req, res, next) {
// 	return lxc.list(workers.clworker0.ip, function(data){
// 		return res.json(data);
// 	});
// });

router.get('/stop/:name', function(req, res, next){
	return lxc.stop(req.params.name, function(data){
		console.log('stop', arguments);
		if(data){
			return res.json({status: 500, name: req.params.name, message: data});
		}else{
			return res.json({status: 200});
		}
	});
});

router.get('/liststuff', function(req, res, next){
	res.json({'workers': workers, 'availContainers': availContainers})
});

router.post('/run/:ip?', function doRun(req, res, next){
	// check if server is
	console.log('hit runner!')

	var container = label2container[req.params.ip] || getAvailContainer();

	return runner(req, res, container);
});

module.exports = router;
<commit_msg>stuff<commit_after>'use strict';

var express = require('express');
var router = express.Router();
var extend = require('node.extend');
var request = require('request');
var lxc = require('../lxc');
var doapi = require('../doapi')();

var label2container = {};
var workers = [];

var checkBalance = function(){

};

var getFreeMem = function(ip, callback){

	return lxc.exec(
		"python3 -c \"a=`head /proc/meminfo|grep MemAvail|grep -Po '\\d+'`;t=`head /proc/meminfo|grep MemTotal|grep -Po '\\d+'`;print(round(((t-a)/t)*100, 2))\"",
		ip,
		callback
	);
};

var containerFree = function(container){
	lxc.stop(name, container);
	container.worker.usedContainer--;
	delete label2container[container.label];
};

var lxcTimeout = function(container, time){
	time = time || 900000; // 15 minutes

	if(container.hasOwnProperty('timeout')){
		clearTimeout(container.timeout);
	}

	return container.timeout = setTimeout(function(){
		containerFree(container)
		return startAll(container.worker);
	}, time);
};

var runner = function(req, res, container){
	console.log('calling runner:', container);

	var httpOptions = {
		url: 'http://' + container.worker.ip,
		headers: {
			Host: container.name
		},
		body: JSON.stringify({
			code: req.body.code
		})
	};

	return request.post(httpOptions, function(error, response, body){
		console.log('runner response:', arguments)
		body = JSON.parse(body);
		var i = -1;
		while(workers[++i].availContainers.length && workers[i].index < container.worker.index){
			containerFree(container);

			container = workers[i].availContainers.pop();
			container.usedContainer++;
		}
		body['ip'] = container.label;
		lxcTimeout(container);
		return res.json(body);

	});
};

var makeWokerObj = function(worker){
	worker.networks.v4.forEach(function(value){
		worker[value.type+'IP'] = value.ip_address;
	});
	worker.availContainers = [];
	worker.ip = worker.privateIP;
	worker.usedContainer = 0;
	return worker;
};

var getWorkers = function(){
	doapi.dropletsByTag('clworker', function(data){
		data = JSON.parse(data);
		data['droplets'].forEach(function(value){
			var workerIDX = workers.push(makeWokerObj(value)) - 1;
			workers[workerIDX].index = workerIDX;

			startWorker(workers[workerIDX]);
		});
	});
};

var getAvailContainer = function(){
	var i = -1;
	while(workers[++i].availContainers.length){
		console.log('found avail');
		var container = wrokers[i].availContainers.pop();
		label2container[container.label] = container;
		container.worker.usedContainer++;
		return container;
	}
};

var startWorker = function(worker, stopPercent){
	stopPercent = stopPercent || 30;
	getFreeMem(worker.ip, function(usedMemPercent){
		if(usedMemPercent < stopPercent ){
			var name = 'crunner-'+(Math.random()*100).toString().replace('.','');
			return lxc.startEphemeral(name, 'crunner0', worker.ip, function(data){
				if( !data.ip ) return setTimeout(startWorker(worker),0);
				console.log('started container')

				worker.availContainers.push({
					ip: data.ip,
					name: name,
					worker: worker,
					label: worker.name + ':' + name
				});
				return setTimeout(startWorker(worker, stopPercent), 0);
			});
		}else{
			console.log('using', usedMemPercent, 'percent memory, stopping container creation!', worker.availContainers.length, 'created');
		}
	});
};

getWorkers();

// router.get('/start/:name', function(req, res, next){
// 	return lxc.start(req.params.name, function(data){
// 		if(!data){
// 			return res.json({
// 				status: 500,
// 				name: req.params.name,
// 				message: data
// 			});
// 		}else{
// 			res.json({});
// 		}
// 	});
// });

// router.get('/live/:template/:name', function(req, res, next){
// 	return lxc.startEphemeral(req.params.name, req.params.template, function (data) {
// 		console.log('live', arguments);
// 		return res.json(data);
// 	});
// });

// router.get('/clone/:template/:name', function(req, res, next){
// 	return lxc.clone(req.params.name, req.params.template, function(data){
// 		console.log('clone', arguments);
// 		if( data.match(/Created container/) ){
// 			return res.json({status: 200});
// 		}else{
// 			return res.json({status: 500, message: data});
// 		}
// 	});
// });

// router.get('/destroy/:name', function(req, res, next){
// 	return lxc.destroy(req.params.name, function(data){
// 		console.log('destroy', arguments);
// 		if(data){
// 			return res.json({status: 500, message: data});
// 		}else{
// 			return res.json({status: 200});
// 		}
// 	});
// });

// router.get('/info/:name', function(req, res, next){
// 	return lxc.info(req.params.name, function(data){
// 		return res.json(data);
// 	});
// });

// router.get('/list', function(req, res, next) {
// 	return lxc.list(workers.clworker0.ip, function(data){
// 		return res.json(data);
// 	});
// });

router.get('/stop/:name', function(req, res, next){
	return lxc.stop(req.params.name, function(data){
		console.log('stop', arguments);
		if(data){
			return res.json({status: 500, name: req.params.name, message: data});
		}else{
			return res.json({status: 200});
		}
	});
});

router.get('/liststuff', function(req, res, next){
	res.json({'workers': workers})
});

router.post('/run/:ip?', function doRun(req, res, next){
	// check if server is
	console.log('hit runner!')

	var container = label2container[req.params.ip] || getAvailContainer();

	return runner(req, res, container);
});

module.exports = router;
<|endoftext|>


<commit_before>module.exports = function (app, db) {

    // get the weather collection
    var weather = db.collection("weather");

    app.get('/api', function(req, res) {
        var body = '/api/weather GET, POST\n';
        body += '/api/weather/date/range/:start/:end GET\n';
        res.send(body);
    });

    app.get('/api/weather', function (req, res) {
        // return most recent weather sorted by date
        // var weather = db.collection("weather");
        weather.find({}).sort({date:-1}).toArray(function(err, documents) {
            if (err) res.jsonp({err:err});
            else res.jsonp({result: documents});
        });
    });

    app.post('/api/weather', function (req, res) {
        // add/overwrite date here since Tessel dates cannot be trusted
        req.body.date = new Date();

        console.log(req.body.humid.toFixed(1) + '%RH', req.body.temp.f.toFixed(1) + 'F', req.body.temp.c.toFixed(1) + 'C', req.body.date.toLocaleString());

        // insert req.body into weather collection
        weather.insert(req.body, function (err, result) {
            // handle error results
            if (err) return res.jsonp({err:err});
            if (!result) return res.jsonp({err:'No data inserted into database.'});

            // return results
            res.jsonp({result:result[0]});
        });
    });

    app.get('/api/weather/date/range/:start/:end', function (req, res) {
        // 2015-01-12T18:41
        // console.log(req.params.start, req.params.end);
        // console.log(new Date(req.params.start));
        var start = new Date(req.params.start); //new Date('2015-01-11T00:00:00');
        var end = req.params.end ? new Date(req.params.end) : new Date(); //('2015-01-12T00:00:00');
        var query = {'date' : {'$gte' : start, '$lte' : end}};
        console.log(start, end);
        weather.find(query).toArray(function(err, docs) {
            console.log(docs.length)
            if(err) res.jsonp({err:err});
            else res.jsonp({results: docs});
        });
        // return res.jsonp({start:req.params.start, end:req.params.end});
    });

    app.get('/api/weather/testing', function (req, res) {
        // return some recent weather sorted by date
        weather.find({}).sort({date:-1}).limit(2000).toArray(function(err, documents) {
            if (err) res.jsonp({err:err});
            else res.jsonp({result: documents});
        });
    });

};
<commit_msg>updated /api/weather/testing to retrieve hourly results by day.<commit_after>var moment = require('moment');

module.exports = function (app, db) {

    // get the weather collection
    var weather = db.collection("weather");

    app.get('/api', function(req, res) {
        var body = '/api/weather GET, POST\n';
        body += '/api/weather/date/range/:start/:end GET\n';
        res.send(body);
    });

    app.get('/api/weather', function (req, res) {
        // return most recent weather sorted by date
        // var weather = db.collection("weather");
        weather.find({}).sort({date:-1}).toArray(function(err, documents) {
            if (err) res.jsonp({err:err});
            else res.jsonp({result: documents});
        });
    });

    app.post('/api/weather', function (req, res) {
        // add/overwrite date here since Tessel dates cannot be trusted
        req.body.date = new Date();

        console.log(req.body.humid.toFixed(1) + '%RH', req.body.temp.f.toFixed(1) + 'F', req.body.temp.c.toFixed(1) + 'C', req.body.date.toLocaleString());

        // insert req.body into weather collection
        weather.insert(req.body, function (err, result) {
            // handle error results
            if (err) return res.jsonp({err:err});
            if (!result) return res.jsonp({err:'No data inserted into database.'});

            // return results
            res.jsonp({result:result[0]});
        });
    });

    app.get('/api/weather/date/range/:start/:end', function (req, res) {
        // 2015-01-12T18:41
        // console.log(req.params.start, req.params.end);
        // console.log(new Date(req.params.start));
        var start = new Date(req.params.start); //new Date('2015-01-11T00:00:00');
        var end = req.params.end ? new Date(req.params.end) : new Date(); //('2015-01-12T00:00:00');
        var query = {'date' : {'$gte' : start, '$lte' : end}};
        console.log(start, end);
        weather.find(query).toArray(function(err, docs) {
            console.log(docs.length)
            if(err) res.jsonp({err:err});
            else res.jsonp({results: docs});
        });
        // return res.jsonp({start:req.params.start, end:req.params.end});
    });

    app.get('/api/weather/testing/:date/:count', function (req, res) {
        var datestring = req.params.date; // || '2015-02-04T00:00:00.000Z';
        var count = req.params.count;

        var endDate = moment(datestring);
        var startDate = moment(datestring).subtract(count, 'days');

        var query = {
            date: {
                $gte: new Date(startDate.format()),
                $lt: new Date(endDate.format())
            }
        }

        var data = [];
        var lastInsert;

        var cursor = weather.find(query);

        cursor.each(function (err, doc) {
            if (err) return;

            if (doc === null) {
                // done
                console.log('filtered result count:', data.length);
                return res.jsonp({results: data});
            }

            // first item
            if (!lastInsert) {
                data.push(doc);
                lastInsert = doc;

            } else {
                // if doc.date is in the next hour after the lastInsert.date
                //    push to data; update lastInsert
                if (docDateIsNextHourPlus(doc.date, lastInsert.date)) {
                    data.push(doc);
                    lastInsert = doc;
                }
            }
        });

    });

};

// return current.date is in the hour following previous.date (or later)
function docDateIsNextHourPlus (current, previous) {
    var mCurrent = moment(current);
    var mPrevious = moment(previous);

    var startHour = mPrevious.clone().startOf('hour');
    startHour.add(1, 'hours');
    var endHour = startHour.clone().add(1, 'hours');

    var isBetween = mCurrent.isAfter(startHour);

    return isBetween;
}
<|endoftext|>


<commit_before>module.exports = function (app) {

    var Util = require("../util");
    var skinChanger = require("../generator/skinChanger");
    var auth = require("../generator/authentication");
    var dataFetcher = require("../generator/dataFetcher")
    var config = require("../config");

    // Schemas
    var Account = require("../db/schemas/account").Account;
    var Skin = require("../db/schemas/skin").Skin;
    var Traffic = require("../db/schemas/traffic").Traffic;
    var Stat = require("../db/schemas/stat").Stat;

    app.get("/get/delay", function (req, res) {
        var ip = req.realAddress;
        Util.getGeneratorDelay().then(function (delay) {
            Traffic.findOne({ip: ip}).lean().exec(function (err, traffic) {
                if (err) return console.log(err);
                if (traffic) {
                    res.json({delay: delay, next: (traffic.lastRequest.getTime() / 1000) + delay, nextRelative: ((traffic.lastRequest.getTime() / 1000) + delay) - (Date.now() / 1000)});
                } else {
                    res.json({delay: delay, next: Date.now() / 1000, nextRelative: 0});
                }
            })
        })
    })

    app.get("/get/stats/:details?", function (req, res) {
        var stats = {};

        stats.server = config.server;

        Util.getGeneratorDelay().then(function (delay) {
            stats.delay = delay;

            stats.queues = {
                auth:{
                    delay: config.requestQueue.auth,
                    size: auth.requestQueue.length
                },
                skinChanger: {
                    delay: config.requestQueue.skinChanger,
                    size: skinChanger.requestQueue.length
                }
            };
            stats.cache = {
               dataFetcher: Object.keys(dataFetcher.cache).length
            };

            // Skin.find({}, "duplicate views visibility time name type via generateDuration", function (err, skins) {
            //     if (err) return console.log(err);
            //     var skinStats = buildSkinStats(skins);
            //     Object.assign(stats, skinStats);
            //
            //     Account.count({enabled: true}, function (err, count) {
            //         if (err) return console.log(err);
            //         stats.accounts = count;
            //
            //         Stat.find({}, function (err, s) {
            //             var generateSuccess = 0;
            //             var generateFail = 0;
            //             s.forEach(function (stat) {
            //                 if (stat.key === "generate.success") generateSuccess = stat.value;
            //                 if (stat.key === "generate.fail") generateFail = stat.value;
            //             })
            //
            //             var generateTotal = generateSuccess + generateFail;
            //             stats.successRate = Number((generateSuccess / generateTotal).toFixed(3));
            //
            //             res.json(stats);
            //         })
            //     })
            // })

            Account.count({enabled: true}, function (err, count) {
                if (err) return console.log(err);
                stats.accounts = count;
                Account.count({enabled: true, errorCounter: {$lt: (config.errorThreshold || 10)}}, function (err, healthyCount) {
                    if (err) return console.log(err);
                    stats.healthyAccounts = healthyCount;
                    var time = Date.now()/1000;
                    Account.count({enabled: true, requestServer: {$in: [null, "default", config.server]}, lastUsed: {'$lt': (time - 100)}, forcedTimeoutAt: {'$lt': (time - 500)}, errorCounter: {'$lt': (config.errorThreshold||10)}},function(err,useableCount){
                        if (err) return console.log(err);
                        stats.useableAccounts = useableCount;
                        Stat.find({}).lean().exec(function (err, s) {
                            if (err) return console.log(err);
                            var generateSuccess = 0;
                            var generateFail = 0;
                            var testerSuccess = 0;
                            var testerFail = 0;
                            s.forEach(function (stat) {
                                if (stat.key === "generate.success") generateSuccess = stat.value;
                                if (stat.key === "generate.fail") generateFail = stat.value;
                                if (stat.key === "mineskintester.success") testerSuccess = stat.value;
                                if (stat.key === "mineskintester.fail") testerFail = stat.value;
                            });
                            var generateTotal = generateSuccess + generateFail;
                            stats.successRate = Number((generateSuccess / generateTotal).toFixed(3));
                            var testerTotal = testerSuccess + testerFail;
                            stats.mineskinTesterSuccessRate = Number((testerSuccess / testerTotal).toFixed(3));

                            Skin.aggregate([
                                {"$sort": {time:-1}},
                                {"$limit":1000},
                                {"$group": {
                                        "_id": null,
                                        "avgGenTime": { "$avg": "$generateDuration" }
                                    }
                                }
                            ], function (err, agg0) {
                                if (err) return console.log(err);

                                stats.avgGenerateDuration = agg0[0].avgGenTime;

                                Skin.aggregate([
                                    {
                                        "$group":
                                            {
                                                _id: "$type",
                                                duplicate: {$sum: "$duplicate"},
                                                views: {$sum: "$views"},
                                                count: {$sum: 1}
                                            }
                                    }
                                ], function (err, agg) {
                                    if (err) return console.log(err);
                                    var user = agg[0];
                                    var url = agg[1];
                                    var upload = agg[2];

                                    stats.genUpload = upload.count;
                                    stats.genUrl = url.count;
                                    stats.genUser = user.count;

                                    stats.unique = user.count + url.count + upload.count;

                                    stats.duplicate = user.duplicate + url.duplicate + upload.duplicate;
                                    stats.views = user.views + url.views + upload.views;
                                    stats.private = 0;
                                    stats.withNames = 0;

                                    stats.lastHour = 0;
                                    stats.lastDay = 0;
                                    stats.lastMonth = 0;
                                    stats.lastYear = 0;

                                    stats.viaApi = 0;
                                    stats.viaWebsite = 0;

                                    stats.total = stats.unique + stats.duplicate;

                                    if (req.params.details) {
                                        var lastHour = new Date(new Date() - 3.6e+6) / 1000;
                                        var lastDay = new Date(new Date() - 8.64e+7) / 1000;
                                        var lastMonth = new Date(new Date() - 2.628e+9) / 1000;
                                        var lastYear = new Date(new Date() - 3.154e+10) / 1000;

                                        Skin.aggregate([
                                            {
                                                $group: {
                                                    _id: null,
                                                    lastYear: {$sum: {$cond: [{$gte: ["$time", lastYear]}, 1, 0]}},
                                                    lastMonth: {$sum: {$cond: [{$gte: ["$time", lastMonth]}, 1, 0]}},
                                                    lastDay: {$sum: {$cond: [{$gte: ["$time", lastDay]}, 1, 0]}},
                                                    lastHour: {$sum: {$cond: [{$gte: ["$time", lastHour]}, 1, 0]}}
                                                }
                                            }
                                        ], function (err, agg1) {
                                            if (err) return console.log(err);

                                            stats.lastYear = agg1[0].lastYear;
                                            stats.lastMonth = agg1[0].lastMonth;
                                            stats.lastDay = agg1[0].lastDay;
                                            stats.lastHour = agg1[0].lastHour;

                                            res.json(stats);
                                        })

                                    } else {
                                        res.json(stats);
                                    }
                                })
                            })
                        })
                    })
                })
            })

        })
    });

    function buildSkinStats(skins) {
        var lastHour = new Date(new Date() - 3.6e+6) / 1000;
        var lastDay = new Date(new Date() - 8.64e+7) / 1000;
        var lastMonth = new Date(new Date() - 2.628e+9) / 1000;
        var lastYear = new Date(new Date() - 3.154e+10) / 1000;

        var stats = {};

        stats.unique = skins.length;

        stats.duplicate = 0;
        stats.views = 0;
        stats.private = 0;
        stats.withNames = 0;

        stats.lastHour = 0;
        stats.lastDay = 0;
        stats.lastMonth = 0;
        stats.lastYear = 0;

        stats.genUpload = 0;
        stats.genUrl = 0;
        stats.genUser = 0;

        stats.viaApi = 0;
        stats.viaWebsite = 0;

        var totalDuration = 0;
        skins.forEach(function (skin) {
            stats.duplicate += skin.duplicate;
            stats.views += skin.views;
            if (skin.visibility !== 0) stats.private++;
            if (skin.name && skin.name.length > 0) stats.withNames++;

            if (skin.time > lastHour) stats.lastHour++;
            if (skin.time > lastDay) stats.lastDay++;
            if (skin.time > lastMonth) stats.lastMonth++;
            if (skin.time > lastYear) stats.lastYear++;

            if (skin.type === "upload") stats.genUpload++;
            if (skin.type === "url") stats.genUrl++;
            if (skin.type === "user") stats.genUser++;

            if (skin.via === "api") stats.viaApi++;
            if (skin.via === "website") stats.viaWebsite++;

            if (skin.generateDuration)
                totalDuration += skin.generateDuration;
        });
        stats.total = stats.unique + stats.duplicate;

        stats.avgDuration = Number((totalDuration / skins.length).toFixed(4));

        return stats;
    }

    app.get("/get/id/:id", function (req, res) {
        Skin.findOne({id: req.params.id}).exec(function (err, skin) {
            if (err) return console.log(err);
            if (skin) {
                skin.views += 1;
                if (skin.model === "alex") {
                    skin.model = "slim";
                }
                skin.save(function (err, skin) {
                    if (err) return console.log(err);
                    res.json(Util.skinToJson(skin, 0, req));
                });
            } else {
                res.status(404).json({error: "Skin not found"});
            }
        })
    });

    app.get("/get/forTexture/:value/:signature?", function (req, res) {
        var search = {value: req.params.value};
        if (req.params.signature) {
            search.signature = req.params.signature;
        }
        Skin.findOne(search, function (err, skin) {
            if (err) return console.log(err);
            if (skin) {
                res.json(Util.skinToJson(skin, 0, req));
            } else {
                res.status(404).json({error: "Skin not found"});
            }
        });
    });

    app.get("/get/list/:page?", function (req, res) {
        var page = Math.max(req.params.page || 1, 1);
        var size = Math.max(req.query.size || 16, 1);
        size = Math.min(64, size);
        var sort = req.query.sort || -1;

        var query = {visibility: 0};
        if (req.query.filter && req.query.filter.length > 0) {
            query.name = {'$regex': ".*" + req.query.filter + ".*"};
        }
        if (req.query.cape) {
            if (req.query.cape === "true") {
                query.capeUrl = {'$ne': null};
            } else if (req.query.cape === "false") {
                query.capeUrl = {'$eq': null};
            }
        }
        Skin.count(query, function (err, count) {
            if (err) return console.log(err);
            Skin
                .find(query)
                .skip(size * (page - 1))
                .limit(size)
                .select({'_id': 0, id: 1, name: 1, url: 1, time: 1})
                .sort({time: sort})
                .lean()
                .exec(function (err, skins) {
                    if (err) return console.log(err)

                    res.json({
                        skins: skins,
                        page: {
                            index: page,
                            amount: Math.round(count / size),
                            total: count
                        },
                        filter: req.query.filter
                    })
                })
        });

    })

};
<commit_msg>load stats separately<commit_after>module.exports = function (app) {

    var Util = require("../util");
    var skinChanger = require("../generator/skinChanger");
    var auth = require("../generator/authentication");
    var dataFetcher = require("../generator/dataFetcher")
    var config = require("../config");

    // Schemas
    var Account = require("../db/schemas/account").Account;
    var Skin = require("../db/schemas/skin").Skin;
    var Traffic = require("../db/schemas/traffic").Traffic;
    var Stat = require("../db/schemas/stat").Stat;

    app.get("/get/delay", function (req, res) {
        var ip = req.realAddress;
        Util.getGeneratorDelay().then(function (delay) {
            Traffic.findOne({ip: ip}).lean().exec(function (err, traffic) {
                if (err) return console.log(err);
                if (traffic) {
                    res.json({delay: delay, next: (traffic.lastRequest.getTime() / 1000) + delay, nextRelative: ((traffic.lastRequest.getTime() / 1000) + delay) - (Date.now() / 1000)});
                } else {
                    res.json({delay: delay, next: Date.now() / 1000, nextRelative: 0});
                }
            })
        })
    });

    let stats = {
        server: config.server
    };

    function updateStats() {
        stats.queues = {
            auth:{
                delay: config.requestQueue.auth,
                size: auth.requestQueue.length
            },
            skinChanger: {
                delay: config.requestQueue.skinChanger,
                size: skinChanger.requestQueue.length
            }
        };
        stats.cache = {
            dataFetcher: Object.keys(dataFetcher.cache).length
        };

        Account.count({enabled: true}, function (err, count) {
            if (err) return console.log(err);
            stats.accounts = count;
            Account.count({enabled: true, errorCounter: {$lt: (config.errorThreshold || 10)}}, function (err, healthyCount) {
                if (err) return console.log(err);
                stats.healthyAccounts = healthyCount;
                var time = Date.now()/1000;
                Account.count({enabled: true, requestServer: {$in: [null, "default", config.server]}, lastUsed: {'$lt': (time - 100)}, forcedTimeoutAt: {'$lt': (time - 500)}, errorCounter: {'$lt': (config.errorThreshold||10)}},function(err,useableCount){
                    if (err) return console.log(err);
                    stats.useableAccounts = useableCount;
                    Stat.find({}).lean().exec(function (err, s) {
                        if (err) return console.log(err);
                        var generateSuccess = 0;
                        var generateFail = 0;
                        var testerSuccess = 0;
                        var testerFail = 0;
                        s.forEach(function (stat) {
                            if (stat.key === "generate.success") generateSuccess = stat.value;
                            if (stat.key === "generate.fail") generateFail = stat.value;
                            if (stat.key === "mineskintester.success") testerSuccess = stat.value;
                            if (stat.key === "mineskintester.fail") testerFail = stat.value;
                        });
                        var generateTotal = generateSuccess + generateFail;
                        stats.successRate = Number((generateSuccess / generateTotal).toFixed(3));
                        var testerTotal = testerSuccess + testerFail;
                        stats.mineskinTesterSuccessRate = Number((testerSuccess / testerTotal).toFixed(3));

                        Skin.aggregate([
                            {"$sort": {time:-1}},
                            {"$limit":1000},
                            {"$group": {
                                    "_id": null,
                                    "avgGenTime": { "$avg": "$generateDuration" }
                                }
                            }
                        ], function (err, agg0) {
                            if (err) return console.log(err);

                            stats.avgGenerateDuration = agg0[0].avgGenTime;

                            Skin.aggregate([
                                {
                                    "$group":
                                        {
                                            _id: "$type",
                                            duplicate: {$sum: "$duplicate"},
                                            views: {$sum: "$views"},
                                            count: {$sum: 1}
                                        }
                                }
                            ], function (err, agg) {
                                if (err) return console.log(err);
                                var user = agg[0];
                                var url = agg[1];
                                var upload = agg[2];

                                stats.genUpload = upload.count;
                                stats.genUrl = url.count;
                                stats.genUser = user.count;

                                stats.unique = user.count + url.count + upload.count;

                                stats.duplicate = user.duplicate + url.duplicate + upload.duplicate;
                                stats.views = user.views + url.views + upload.views;
                                stats.private = 0;
                                stats.withNames = 0;

                                stats.lastHour = 0;
                                stats.lastDay = 0;
                                stats.lastMonth = 0;
                                stats.lastYear = 0;

                                stats.viaApi = 0;
                                stats.viaWebsite = 0;

                                stats.total = stats.unique + stats.duplicate;

                                    var lastHour = new Date(new Date() - 3.6e+6) / 1000;
                                    var lastDay = new Date(new Date() - 8.64e+7) / 1000;
                                    var lastMonth = new Date(new Date() - 2.628e+9) / 1000;
                                    var lastYear = new Date(new Date() - 3.154e+10) / 1000;

                                    Skin.aggregate([
                                        {
                                            $group: {
                                                _id: null,
                                                lastYear: {$sum: {$cond: [{$gte: ["$time", lastYear]}, 1, 0]}},
                                                lastMonth: {$sum: {$cond: [{$gte: ["$time", lastMonth]}, 1, 0]}},
                                                lastDay: {$sum: {$cond: [{$gte: ["$time", lastDay]}, 1, 0]}},
                                                lastHour: {$sum: {$cond: [{$gte: ["$time", lastHour]}, 1, 0]}}
                                            }
                                        }
                                    ], function (err, agg1) {
                                        if (err) return console.log(err);

                                        stats.lastYear = agg1[0].lastYear;
                                        stats.lastMonth = agg1[0].lastMonth;
                                        stats.lastDay = agg1[0].lastDay;
                                        stats.lastHour = agg1[0].lastHour;

                                    })

                            })
                        })
                    })
                })
            })
        })
    }

    setInterval(() => updateStats(), 60000);

    app.get("/get/stats/:details?", function (req, res) {
        Util.getGeneratorDelay().then(function (delay) {
            stats.delay = delay;

            res.json(stats);
        })
    });

    function buildSkinStats(skins) {
        var lastHour = new Date(new Date() - 3.6e+6) / 1000;
        var lastDay = new Date(new Date() - 8.64e+7) / 1000;
        var lastMonth = new Date(new Date() - 2.628e+9) / 1000;
        var lastYear = new Date(new Date() - 3.154e+10) / 1000;

        var stats = {};

        stats.unique = skins.length;

        stats.duplicate = 0;
        stats.views = 0;
        stats.private = 0;
        stats.withNames = 0;

        stats.lastHour = 0;
        stats.lastDay = 0;
        stats.lastMonth = 0;
        stats.lastYear = 0;

        stats.genUpload = 0;
        stats.genUrl = 0;
        stats.genUser = 0;

        stats.viaApi = 0;
        stats.viaWebsite = 0;

        var totalDuration = 0;
        skins.forEach(function (skin) {
            stats.duplicate += skin.duplicate;
            stats.views += skin.views;
            if (skin.visibility !== 0) stats.private++;
            if (skin.name && skin.name.length > 0) stats.withNames++;

            if (skin.time > lastHour) stats.lastHour++;
            if (skin.time > lastDay) stats.lastDay++;
            if (skin.time > lastMonth) stats.lastMonth++;
            if (skin.time > lastYear) stats.lastYear++;

            if (skin.type === "upload") stats.genUpload++;
            if (skin.type === "url") stats.genUrl++;
            if (skin.type === "user") stats.genUser++;

            if (skin.via === "api") stats.viaApi++;
            if (skin.via === "website") stats.viaWebsite++;

            if (skin.generateDuration)
                totalDuration += skin.generateDuration;
        });
        stats.total = stats.unique + stats.duplicate;

        stats.avgDuration = Number((totalDuration / skins.length).toFixed(4));

        return stats;
    }

    app.get("/get/id/:id", function (req, res) {
        Skin.findOne({id: req.params.id}).exec(function (err, skin) {
            if (err) return console.log(err);
            if (skin) {
                skin.views += 1;
                if (skin.model === "alex") {
                    skin.model = "slim";
                }
                skin.save(function (err, skin) {
                    if (err) return console.log(err);
                    res.json(Util.skinToJson(skin, 0, req));
                });
            } else {
                res.status(404).json({error: "Skin not found"});
            }
        })
    });

    app.get("/get/forTexture/:value/:signature?", function (req, res) {
        var search = {value: req.params.value};
        if (req.params.signature) {
            search.signature = req.params.signature;
        }
        Skin.findOne(search, function (err, skin) {
            if (err) return console.log(err);
            if (skin) {
                res.json(Util.skinToJson(skin, 0, req));
            } else {
                res.status(404).json({error: "Skin not found"});
            }
        });
    });

    app.get("/get/list/:page?", function (req, res) {
        var page = Math.max(req.params.page || 1, 1);
        var size = Math.max(req.query.size || 16, 1);
        size = Math.min(64, size);
        var sort = req.query.sort || -1;

        var query = {visibility: 0};
        if (req.query.filter && req.query.filter.length > 0) {
            query.name = {'$regex': ".*" + req.query.filter + ".*"};
        }
        if (req.query.cape) {
            if (req.query.cape === "true") {
                query.capeUrl = {'$ne': null};
            } else if (req.query.cape === "false") {
                query.capeUrl = {'$eq': null};
            }
        }
        Skin.count(query, function (err, count) {
            if (err) return console.log(err);
            Skin
                .find(query)
                .skip(size * (page - 1))
                .limit(size)
                .select({'_id': 0, id: 1, name: 1, url: 1, time: 1})
                .sort({time: sort})
                .lean()
                .exec(function (err, skins) {
                    if (err) return console.log(err)

                    res.json({
                        skins: skins,
                        page: {
                            index: page,
                            amount: Math.round(count / size),
                            total: count
                        },
                        filter: req.query.filter
                    })
                })
        });

    })

};
<|endoftext|>


<commit_before>/**
 * Querystring parameters:
 *
 * event - The type of event to log (views, calls or emails). Other parameters differ by event type:
 *
 * 'views'
 * 	- can be passed parameterless to register a pageview
 *  - can come with a 'legislators' comma-separated string of legislator IDs
 *
 * 'calls'
 * 	- must come with a 'legislators' parameter containing only a single ID
 *
 * 'emails'
 * 	- must come with a 'legislators' parameter containing only a single ID
 *
 * 'tweets'
 * 	- must come with a 'legislators' parameter containing only a single ID
 *
 * 'facebooks'
 * 	- must come with a 'legislators' parameter containing only a single ID
 */

var async = require('async');

var mongo = require(__dirname + '/../lib/database');
var config = require(__dirname + '/../_config_');

var EVENT_TYPES = ['views', 'calls', 'emails', 'tweets', 'facebooks'];
var COLLECTION_NAME = 'log_totals';
var OVERALL_TOTALS_ID = 'overall_totals';

var TIMES_COLLECTION_NAME = 'log_event_times';

module.exports = function() {

    function sendToMongo(query, event_type, db) {

        var inc_update = {$inc: {} };
        inc_update['$inc'][event_type] = 1;

        var eventLog = {
        	type : event_type,
        	time : new Date()
        };

        db.collection(COLLECTION_NAME).update(
            query,
            inc_update,
            function(err, result) {
                if (err) {
                    console.warn("Mongo aggregate write failed:", query, inc_update);
                    return;
                }

                console.log("Added event to MongoDB");
            }
        );

        // :TODO: these logs need to be differentiatable per legislator they target
        db.collection(TIMES_COLLECTION_NAME).insert(eventLog, function(err, res) {
            if (err) {
                console.warn("Mongo log write failed:", eventLog);
                return;
            }
        });
    }

    function success(req) {
        req.io.respond({
            message: 'Event logged'
        });
    }

    function fail(req) {
        req.io.respond({
            error: 'Failed to log'
        });
    }

    function invalidRequest(req) {
        req.io.respond({
            error: 'Invalid request'
        });
    }

    function main(req) {

        var event_type = req.data.event;
        var legislators = req.data.legislators || null;
        var query;

        if (EVENT_TYPES.indexOf(event_type) == -1) {
        	console.warn("HACK ATTEMPT:", req.data);	// let Forever catch these
	        invalidRequest(req);
        	return;
        }

        try {
	        switch (event_type) {
	        	case 'views':
		        	if (legislators) {
		        		query = { _id : { $in : legislators.split(',') } };
		        	} else {
		        		query = { _id : OVERALL_TOTALS_ID };
		        	}
	       			break;
	        	default:
	        		query = { _id : legislators };
	       			break;
	        }
		} catch (e) {
			// validation error.
        	console.warn("HACK ATTEMPT:", req.data);	// let Forever catch these
	        invalidRequest(req);
	        return;
		}

        console.log("LOG A "+event_type+" EVENT", query);

        // send application event
        this.io.broadcast(event_type, legislators || []);

        mongo.get().then(
            function onSuccess(db) {
                sendToMongo(query, event_type, db);
                success(req);	// do not wait for write acknowledgement
            },
            function onErr(err) {
                fail(req);
            }
        );
    };

    return main;

}();
<commit_msg>call log<commit_after>/**
 * Querystring parameters:
 *
 * event - The type of event to log (views, calls or emails). Other parameters differ by event type:
 *
 * 'views'
 * 	- can be passed parameterless to register a pageview
 *  - can come with a 'legislators' comma-separated string of legislator IDs
 *
 * 'calls'
 * 	- must come with a 'legislators' parameter containing only a single ID
 *
 * 'emails'
 * 	- must come with a 'legislators' parameter containing only a single ID
 *
 * 'tweets'
 * 	- must come with a 'legislators' parameter containing only a single ID
 *
 * 'facebooks'
 * 	- must come with a 'legislators' parameter containing only a single ID
 */

var async = require('async');

var mongo = require(__dirname + '/../lib/database');
var config = require(__dirname + '/../_config_');

var EVENT_TYPES = ['views', 'calls', 'emails', 'tweets', 'facebooks'];
var COLLECTION_NAME = 'log_totals';
var OVERALL_TOTALS_ID = 'overall_totals';

var TIMES_COLLECTION_NAME = 'log_event_times';

module.exports = function() {

    function sendToMongo(query, event_type, db) {

        var inc_update = {$inc: {} };
        inc_update['$inc'][event_type] = 1;

        var eventLog = {
        	type : event_type,
        	time : new Date()
        };

        db.collection(COLLECTION_NAME).update(
            query,
            inc_update,
            function(err, result) {
                if (err) {
                    console.warn("Mongo aggregate write failed:", query, inc_update);
                    return;
                }

                console.log("Added event to MongoDB");
            }
        );

        // :TODO: these logs need to be differentiatable per legislator they target
        db.collection(TIMES_COLLECTION_NAME).insert(eventLog, function(err, res) {
            if (err) {
                console.warn("Mongo log write failed:", eventLog);
                return;
            }
        });
    }

    function success(req) {
        req.io.respond({
            message: 'Event logged'
        });
    }

    function fail(req) {
        req.io.respond({
            error: 'Failed to log'
        });
    }

    function invalidRequest(req) {
        req.io.respond({
            error: 'Invalid request'
        });
    }

    function main(req) {

        var event_type = req.data.event;
        var legislators = req.data.legislators || null;
        var query;

        if (EVENT_TYPES.indexOf(event_type) == -1) {
        	console.warn("HACK ATTEMPT:", req.data);	// let Forever catch these
	        invalidRequest(req);
        	return;
        }

        try {
	        switch (event_type) {
	        	case 'views':
		        	if (legislators) {
		        		query = { _id : { $in : legislators.split(',') } };
		        	} else {
		        		query = { _id : OVERALL_TOTALS_ID };
		        	}
	       			break;
                case 'emails':
                    if (legislators) {
                        query = { _id : { $in : legislators.split(',') } };
                    } else {
                        query = { _id : OVERALL_TOTALS_ID };
                    }
                    break;
                case 'calls':
                    if (legislators) {
                        query = { _id : { $in : legislators.split(',') } };
                    } else {
                        query = { _id : OVERALL_TOTALS_ID };
                    }
                    break;
	        	default:
	        		query = { _id : legislators };
	       			break;
	        }
		} catch (e) {
			// validation error.
        	console.warn("HACK ATTEMPT:", req.data);	// let Forever catch these
	        invalidRequest(req);
	        return;
		}

        console.log("LOG A "+event_type+" EVENT", query);

        // send application event
        this.io.broadcast(event_type, legislators || []);

        mongo.get().then(
            function onSuccess(db) {
                sendToMongo(query, event_type, db);
                success(req);	// do not wait for write acknowledgement
            },
            function onErr(err) {
                fail(req);
            }
        );
    };

    return main;

}();
<|endoftext|>


<commit_before>/*

*/

var moment = require('moment');
var Parser = require('../parser').Parser;
var ParsedResult = require('../../result').ParsedResult;

var PATTERN = /(\W|^)(?:within\s*)?([0-9]+|an?)\s*(minutes?|hours?|days?)\s*ago(?=(?:\W|$))/i;

exports.Parser = function ENTimeAgoFormatParser(){
    Parser.call(this);

    this.pattern = function() { return PATTERN; }

    this.extract = function(text, ref, match, opt){

        if (match.index > 0 && text[match.index-1].match(/\w/)) return null;

        var text = match[0];
        text  = match[0].substr(match[1].length, match[0].length - match[1].length);
        index = match.index + match[1].length;

        var result = new ParsedResult({
            index: index,
            text: text,
            ref: ref,
        });

        var num = match[2];
        if(num === 'a' || num === 'an'){
            num = 1;
        } else {
            num = parseInt(num);
        }

        var date = moment(ref);
        if (match[3].match(/day/)) {

            impliedComponents = []
            date.add(-num, 'd');

            result.start.assign('day', date.date());
            result.start.assign('month', date.month() + 1);
            result.start.assign('year', date.year());
            return result;
        }

        if (match[3].match(/hour/)) {

            date.add(-num, 'hour');

        } else if (match[3].match(/minute/)) {

            date.add(-num, 'minute');
        }

        result.start.imply('day', date.date());
        result.start.imply('month', date.month() + 1);
        result.start.imply('year', date.year());
        result.start.assign('hour', date.hour());
        result.start.assign('minute', date.minute());
        result.tags['ENTimeAgoFormatParser'] = true;
        return result;
    };
}
<commit_msg>Now listens for `in`, not just `within`<commit_after>/*

*/

var moment = require('moment');
var Parser = require('../parser').Parser;
var ParsedResult = require('../../result').ParsedResult;

var PATTERN = /(\W|^)(?:within|in\s*)?([0-9]+|an?)\s*(minutes?|hours?|days?)\s*ago(?=(?:\W|$))/i;

exports.Parser = function ENTimeAgoFormatParser(){
    Parser.call(this);

    this.pattern = function() { return PATTERN; }

    this.extract = function(text, ref, match, opt){

        if (match.index > 0 && text[match.index-1].match(/\w/)) return null;

        var text = match[0];
        text  = match[0].substr(match[1].length, match[0].length - match[1].length);
        index = match.index + match[1].length;

        var result = new ParsedResult({
            index: index,
            text: text,
            ref: ref,
        });

        var num = match[2];
        if(num === 'a' || num === 'an'){
            num = 1;
        } else {
            num = parseInt(num);
        }

        var date = moment(ref);
        if (match[3].match(/day/)) {

            impliedComponents = []
            date.add(-num, 'd');

            result.start.assign('day', date.date());
            result.start.assign('month', date.month() + 1);
            result.start.assign('year', date.year());
            return result;
        }

        if (match[3].match(/hour/)) {

            date.add(-num, 'hour');

        } else if (match[3].match(/minute/)) {

            date.add(-num, 'minute');
        }

        result.start.imply('day', date.date());
        result.start.imply('month', date.month() + 1);
        result.start.imply('year', date.year());
        result.start.assign('hour', date.hour());
        result.start.assign('minute', date.minute());
        result.tags['ENTimeAgoFormatParser'] = true;
        return result;
    };
}
<|endoftext|>


<commit_before>/* global localStorage */
import axios from 'axios';

export const BASE_URL = process.env.REACT_APP_API_URL;

const request = (endpoint, { headers = {}, body, ...otherOptions }, method) => {
  return axios(`${BASE_URL}${endpoint}`, {
    ...otherOptions,
    headers: {
      ...headers,
      Authorization: `Bearer ${localStorage.getItem('jwt.token')}}`,
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    data: body ? JSON.stringify(body) : undefined,
    method,
  });
};

export const api = {
  get(endpoint, options = {}) {
    return request(endpoint, options, 'get');
  },
  post(endpoint, options = {}) {
    return request(endpoint, options, 'post');
  },
};

export default api;
<commit_msg>Refresh JWT token if expires<commit_after>/* global window */
import axios from 'axios';

export const BASE_URL = process.env.REACT_APP_API_URL;

const getToken = () => {
  return window.localStorage.getItem('jwt.token');
};

const setToken = ({ newToken }) => {
  window.localStorage.setItem('jwt.token', newToken);
};

const abstractRequest = (endpoint, { headers = {}, body, ...otherOptions }, method) => {
  return axios(`${BASE_URL}${endpoint}`, {
    ...otherOptions,
    headers: {
      ...headers,
      Authorization: `Bearer ${getToken()}`,
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    data: body ? JSON.stringify(body) : undefined,
    method,
  });
};

const checkForRefreshToken = (endpoint, content, method) => (error) => {
  if (error.response && error.response.status === 401) {
    return abstractRequest('/auth/refresh', {}, 'post').then(({ data }) => {
      setToken(data);

      return abstractRequest(endpoint, content, method);
    });
  }
  return Promise.reject(error);
};

const checkForRelogin = error => {
  if (!error.response || !error.response.data || window.location.pathname === '/login') {
    return Promise.reject(error);
  }

  const message = error.response.data.error;

  if (['token_expired', 'token_invalid', 'token_not_provided'].includes(message)) {
    window.location = '/login';
  }

  return Promise.reject(error);
};

const request = (endpoint, content, method) => {
  return abstractRequest(endpoint, content, method)
    .catch(checkForRefreshToken(endpoint, content, method))
    .catch(checkForRelogin);
};

export const api = {
  get(endpoint, options = {}) {
    return request(endpoint, options, 'get');
  },
  post(endpoint, options = {}) {
    return request(endpoint, options, 'post');
  },
};

export default api;
<|endoftext|>