<commit_before>var config = require('histograph-config');
var bcrypt = require('bcrypt');
var neo4j = require('neo4j');
var async = require('async');
var esIndex = require('./elasticsearch-index');

if (config.neo4j.user && config.neo4j.password) {
  var neo4jUrl = 'http://' + config.neo4j.user + ':' + config.neo4j.password + '@' +
    config.neo4j.host + ':' + config.neo4j.port;
} else {
  var neo4jUrl = 'http://' + config.neo4j.host + ':' + config.neo4j.port;
}

var graphDb = new neo4j.GraphDatabase(neo4jUrl);

function send500(res, data) {
  res.status(500).send(data);
}

function send404(res, type, id) {
  res.status(404).send({
    message: type + ' \'' + id + '\' not found'
  });
}

function formatNeo4jError(err, results) {
  if (err) {
    return {
      source: 'Neo4j',
      message: err.neo4j
    };
  } else {
    return;
  }
}

module.exports.initAdmin = function() {
  // Create admin owner (if not exists)
  var name = config.api.admin.name;
  var password = config.api.admin.password;

  graphDb.cypher({
    query: 'MERGE (admin:Owner { name:{name} }) ON CREATE SET admin.password = {password}',
    params: {
      name: name,
      password: <PASSWORD>.hashSync(password, 8)
    }
  }, function(err) {
    if (err) {
      console.error('Error creating admin owner');
    }
  });
};

function datasetExists(req, res, next) {
  graphDb.cypher({
    query: 'MATCH (dataset:Dataset {dataset: {id}}) RETURN dataset',
    params: {
      id: req.params.dataset
    }
  }, function(err, results) {
    if (err) {
      send500(res, err.neo4j);
    } else if (results.length == 1) {
      next();
    } else {
      send404(res, 'Dataset', req.params.dataset);
    }
  });
}

function createDataset(res, dataset, ownerName, callback) {
  async.series([
    function(callback) {
      esIndex.create(dataset.id, function(err) {
        callback();
      });
    },
    function(callback) {

      var params = {
        ownerName: ownerName
      };

      var queryDataset = Object.keys(dataset).map(function(key) {
        var newKey = key;
        if (key === 'id') {
          newKey = 'dataset';
        }

        params[newKey] = dataset[key];
        return newKey + ': {' + newKey + '}';
      }).join(', ');

      graphDb.cypher({
        query: 'MATCH (owner:Owner {name: {ownerName}}) MERGE (owner)-[:OWNS]->(:Dataset {' + queryDataset + '})',
        params: params
      }, function(err, results) {
        callback(formatNeo4jError(err, results));
      });
    },
  ],
  function(err) {
    if (err) {
      send500(res, err.neo4j || err);<commit_msg>don't eat!<commit_after>var config = require('histograph-config');
var bcrypt = require('bcrypt');
var neo4j = require('neo4j');
var async = require('async');
var esIndex = require('./elasticsearch-index');

if (config.neo4j.user && config.neo4j.password) {
  var neo4jUrl = 'http://' + config.neo4j.user + ':' + config.neo4j.password + '@' +
    config.neo4j.host + ':' + config.neo4j.port;
} else {
  var neo4jUrl = 'http://' + config.neo4j.host + ':' + config.neo4j.port;
}

var graphDb = new neo4j.GraphDatabase(neo4jUrl);

function send500(res, data) {
  res.status(500).send(data);
}

function send404(res, type, id) {
  res.status(404).send({
    message: type + ' \'' + id + '\' not found'
  });
}

function formatNeo4jError(err, results) {
  if (err) {
    return {
      source: 'Neo4j',
      message: err.neo4j
    };
  } else {
    return;
  }
}

module.exports.initAdmin = function() {
  // Create admin owner (if not exists)
  var name = config.api.admin.name;
  var password = <PASSWORD>;

  graphDb.cypher({
    query: 'MERGE (admin:Owner { name:{name} }) ON CREATE SET admin.password = {password}',
    params: {
      name: name,
      password: bcrypt.hashSync(password, 8)
    }
  }, function(err) {
    if (err) {
      console.error('Error creating admin owner');
    }
  });
};

function datasetExists(req, res, next) {
  graphDb.cypher({
    query: 'MATCH (dataset:Dataset {dataset: {id}}) RETURN dataset',
    params: {
      id: req.params.dataset
    }
  }, function(err, results) {
    if (err) {
      send500(res, err.neo4j);
    } else if (results.length == 1) {
      next();
    } else {
      send404(res, 'Dataset', req.params.dataset);
    }
  });
}

function createDataset(res, dataset, ownerName, callback) {
  async.series([
    function(callback) {
      esIndex.create(dataset.id, function(err) {
        // TODO: DON'T EAT ERROR!
        callback();
      });
    },
    function(callback) {

      var params = {
        ownerName: ownerName
      };

      var queryDataset = Object.keys(dataset).map(function(key) {
        var newKey = key;
        if (key === 'id') {
          newKey = 'dataset';
        }

        params[newKey] = dataset[key];
        return newKey + ': {' + newKey + '}';
      }).join(', ');

      graphDb.cypher({
        query: 'MATCH (owner:Owner {name: {ownerName}}) MERGE (owner)-[:OWNS]->(:Dataset {' + queryDataset + '})',
        params: params
      }, function(err, results) {
        callback(formatNeo4jError(err, results));
      });
    },
  ],
  function(err) {
    if (err) {
      send500(res, err.neo4j || err);
{'fraction_non_alphanumeric': 0.17379576107899808, 'fraction_numerical': 0.016570327552986513, 'mean_word_length': 2.671145685997171, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '3561942', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1786, 'n_words': 366}