const express = require('express');
const router = express.Router();
const qp = require('./query_parser');
const agg = require('./aggregation');
// --------------------------------------------------------------------------------------
// get data
// --------------------------------------------------------------------------------------
router.get('/', function(req, res, next) {
  var query = qp.parse_query_string(req.url,
    ['timestamp' ],
    qp.validate_days);
  search(req, res, next, query);     // perform search with constructed mongo query
});
// --------------------------------------------------------------------------------------
// search database for specified data
// timestamp matches specific day or range of days
// --------------------------------------------------------------------------------------
function search(req, res, next, query) {
  // query for username with records for multiple days
  // gets results separated for each day !
  // this is defined by the query -> needs to be computed on the fly

  // ===================================================
  // construct base query
  var aggregate_query = [
    { $match : query.filter },      // filter by query
    { $project : { visinst_1 : 1, visinst_2 : 1  } },     // limit to visited instituons
    { $group : { _id : { visinst_1 : "$visinst_1", visinst_2 : "$visinst_2" }, count : { $sum : 1 } } },
    { $sort : { count : -1 } },
    { $project : { visinst_1 : "$_id.visinst_1", visinst_2 : "$_id.visinst_2", count : 1, _id : 0 } },
  ];

  // ===================================================
  // search

  req.db.concurrent_users.aggregate(aggregate_query,
  function(err1, items) {
    if(err1) {
      var err2 = new Error();      // just to detect where the original error happened
      console.error(err2);
      console.error(err1);
      next([err2, err1]);
      return;
    }

    respond(filter_data(req, items), res);
  });
}
// --------------------------------------------------------------------------------------
// filter results so each user can search only relevant records
// realm admin: only records for all administered realms
// --------------------------------------------------------------------------------------
function filter_data(req, data)
{
  var ret = [];

  if(req.session.user.role == "user")
    return ret;

  return data;  // no filtration othewrise
}
// --------------------------------------------------------------------------------------
// send data to user
// --------------------------------------------------------------------------------------
function respond(items, res) {
  res.json(items);
}
// --------------------------------------------------------------------------------------
module.exports = router;

@@ -2290,100 +2290,1251 @@
  if(
-req.session.user.role == %22user%22)%0A    return ret;%0A%0A  return data;  // no filtration othewrise
+!req.headers%5B%22remote_user%22%5D) %7B     // remote user not set for machine processing%0A    var ip = req.headers%5B'x-forwarded-for'%5D %7C%7C%0A      req.connection.remoteAddress %7C%7C%0A      req.socket.remoteAddress %7C%7C%0A      req.connection.socket.remoteAddress;%0A%0A    check_privileged_ips(req, ip, function(found) %7B%0A      if(found)%0A        return data;      // no filtering for machine processing%0A      else%0A        return ret;      // not found in privileged ips%0A    %7D);%0A  %7D%0A%0A  else if(req.session.user.role == %22user%22)%0A    return ret;%0A%0A  return data;  // no filtration othewrise%0A%7D%0A// --------------------------------------------------------------------------------------%0A// check database for specific privileged ip%0A// --------------------------------------------------------------------------------------%0Afunction check_privileged_ips(req, ip, callback)%0A%7B%0A  // search for privileged ip%0A  req.db.privileged_ips.find(%7B %22ip%22 : ip %7D, %7B ip : 1, _id : 0 %7D function(err1, items) %7B%0A    if(err1) %7B%0A      var err2 = new Error();      // just to detect where the original error happened%0A      console.error(err2);%0A      console.error(err1);%0A      next(%5Berr2, err1%5D);%0A      return;%0A    %7D%0A%0A    if(items.length %3E 0)%0A      callback(true);%0A    else%0A      callback(false);%0A  %7D);
 %0A%7D%0A/

enable machine processing