<commit_before>// The chatlogs retrieval service

var dbname = 'MdbChatLogBot';
var mdbServer = '127.0.0.1:27017';

function error(res, msg, callback) {
  res.setHeader('Content-Type', 'application/json');
  json = JSON.stringify({"error": msg});
  if (callback) {
    json = callback + '(' + json + ')';
  }
  res.end(json);
}

function search(res, qstr) {
  var MongoClient = require('mongodb').MongoClient;
  var format = require('util').format;

  var murl = 'mongodb://' + mdbServer + '/' + dbname;
  console.dir('Connecting: ' + murl);
  MongoClient.connect(murl, function(err, db) {
    var callback = qstr['callback'];

    try {
      if (err) throw err;

      if (!qstr['room']) {
        throw 'No room specified';
      }
      var collname = qstr['room'];

      var query = {};
      if (qstr['regexp']) {
        query['message'] = new RegExp(
          qstr['regexp'], qstr['regexpopts']);
      }
      if (qstr['startdt'] && qstr['enddt']) {
        startdt = new Date(Date.parse(qstr['startdt']))
        enddt = new Date(Date.parse(qstr['enddt']));
        query['timestamp'] = { $gte: startdt, $lt: enddt };
      }
      else if (qstr['startdt']) {
        startdt = new Date(Date.parse(qstr['startdt']));
        query['timestamp'] = { $gte: startdt };
      }
      else if (qstr['enddt']) {
        enddt = new Date(Date.parse(qstr['enddt']));
        query['timestamp'] = { $lt: enddt };
      }
      var pretty = qstr['pretty'] && qstr['pretty'] == 1;
      var projection = { timestamp: 1, nick: 1, message: 1, _id: 0 };

      console.dir(query);
      var coll = db.collection(collname);
      coll.find(query, projection).sort({ timestamp: 1 }).toArray(
        function(err, results) {
          if (err) {
            throw err;
          }
          console.dir(results);
          res.setHeader('Content-Type', 'application/json');
          if (pretty) {
            json = JSON.stringify(results, null, 3);
          }
          else {
            json = JSON.stringify(results);
          }
          if (callback) {
            json = callback + '(' + json + ')';
          }
          res.end(json);
          db.close();
        });
    }
    catch (err) {
      console.log('Caught exception: ' + err);
      error(res, String(err), callback);
    }
  });
};


var Search = function() {
  var handleRequest = function(req, res) {
    var url = require('url');
    qstr = url.parse(req.url, true).query;
    try {
      search(res, qstr);<commit_msg>search is now a subpath uner api (/api/search)<commit_after>// The chatlogs retrieval service

var dbname = 'MdbChatLogBot';
var mdbServer = '127.0.0.1:27017';

function error(res, msg, callback) {
  res.setHeader('Content-Type', 'application/json');
  json = JSON.stringify({"error": msg});
  if (callback) {
    json = callback + '(' + json + ')';
  }
  res.end(json);
}

function search(res, qstr) {
  var MongoClient = require('mongodb').MongoClient;
  var format = require('util').format;

  var murl = 'mongodb://' + mdbServer + '/' + dbname;
  console.dir('Connecting: ' + murl);
  MongoClient.connect(murl, function(err, db) {
    var callback = qstr['callback'];

    try {
      if (err) throw err;

      if (!qstr['room']) {
        throw 'No room specified';
      }
      var collname = qstr['room'];

      var query = {};
      if (qstr['regexp']) {
        query['message'] = new RegExp(
          qstr['regexp'], qstr['regexpopts']);
      }
      if (qstr['startdt'] && qstr['enddt']) {
        startdt = new Date(Date.parse(qstr['startdt']))
        enddt = new Date(Date.parse(qstr['enddt']));
        query['timestamp'] = { $gte: startdt, $lt: enddt };
      }
      else if (qstr['startdt']) {
        startdt = new Date(Date.parse(qstr['startdt']));
        query['timestamp'] = { $gte: startdt };
      }
      else if (qstr['enddt']) {
        enddt = new Date(Date.parse(qstr['enddt']));
        query['timestamp'] = { $lt: enddt };
      }
      var pretty = qstr['pretty'] && qstr['pretty'] == 1;
      var projection = { timestamp: 1, nick: 1, message: 1, _id: 0 };

      console.dir(query);
      var coll = db.collection(collname);
      coll.find(query, projection).sort({ timestamp: 1 }).toArray(
        function(err, results) {
          if (err) {
            throw err;
          }
          console.dir(results);
          res.setHeader('Content-Type', 'application/json');
          if (pretty) {
            json = JSON.stringify(results, null, 3);
          }
          else {
            json = JSON.stringify(results);
          }
          if (callback) {
            json = callback + '(' + json + ')';
          }
          res.end(json);
          db.close();
        });
    }
    catch (err) {
      console.log('Caught exception: ' + err);
      error(res, String(err), callback);
    }
  });
};


var Search = function() {
  var handleRequest = function(req, res) {
    var url = require('url');
    var parts = url.parse(req.url, true);
    // Split into components, remove empty elements
    var comps = parts.pathname.split('/').filter(function(x) { return x; });
    var qstr = parts.query;
    try {
      if (comps.length == 2 && comps[0] == 'api' && comps[1] == 'search') {
        search(res, qstr);
      }
      else {
        throw 'Invalid API call'
      }
{'fraction_non_alphanumeric': 0.17516462841015992, 'fraction_numerical': 0.007337723424270932, 'mean_word_length': 2.2414634146341466, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1407783', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1706, 'n_words': 385}