<commit_before>var fs = require('fs');
var path = require('path');
var config = require('histograph-config');
var request = require('request');
var neo4j = require('neo4j');

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);

var shortestPathQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'shortest-path.cypher'), {encoding: 'utf8'});
var relatedQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'related.cypher'), {encoding: 'utf8'});

var peopleFromOrgsFromPersonQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'peopleFromOrgsFromPerson.cypher'), {encoding: 'utf8'});
var peopleFromOrgsFromPersonQueryCC = fs.readFileSync(path.join(__dirname, '..', 'queries', 'peopleFromOrgsFromPerson.concurrent.cypher'), {encoding: 'utf8'});
var orgsFromPersonQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'orgsFromPerson.cypher'), {encoding: 'utf8'});
var peopleFromOrgQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'peopleFromOrg.cypher'), {encoding: 'utf8'});
var equivalentIDsQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'equivalentIDs.cypher'), {encoding: 'utf8'});
var equivalentNodesQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'equivalentNodes.cypher'), {encoding: 'utf8'});

function replaceRelations(query, relations) {
  return query.replace(/«relations»/g, relations.map(function(relation) {
    return '`' + relation + '`';
  }).join('|'));
}


function replaceOrganizations(query, organizations) {
  return query.replace(/«organizations»/g, organizations.map(function(org) {
    return '"' + org + '"';
  }).join(','));
}



exports.shortestPath = function(idsFrom, idsTo, relations, callback) {
  var query = replaceRelations(shortestPathQuery, relations);
  graphDb.cypher({
    query: query,
    params: {
      idsFrom: idsFrom,
      idsTo: idsTo
    }
  }, function(err, results) {
    var ids;
    if (results) {
      ids = results.map(function(result) {
        return result.id;
      });
    }
    callback(err, ids);
  });
};


// http://localhost:3001/equivalence?id=urn:hgid:overheidsorganisaties/2517
exports.equivalentIDs = function(id, relations, callback) {
  var query = replaceRelations(equivalentIDsQuery, relations);
  graphDb.cypher({
    query: query,
    params: {
      id: id
    }
  }, function(err, results) {
    var ids;
    if (results) {
        ids = results.map(function(result) {
          return result.ids;
        });
    }
    callback(err, ids);
  });
};

exports.equivalentNodes = function(id, relations, callback) {
  var query = replaceRelations(equivalentNodesQuery, relations);
  graphDb.cypher({
    query: query,
    params: {
      id: id
    }
  }, function(err, results) {
    var ids;
    if (results) {
        ids = results.map(function(result) {
          return result.found.properties.id;
        });
    }
    callback(err, ids);
  });
};


// http://localhost:3001/peopleFromOrg?id=nevenfuncties/Ny0H0O-BhGe
exports.peopleFromOrg = function(id, relations, callback) {
  var query = replaceRelations(peopleFromOrgQuery, relations);
  graphDb.cypher({
    query: query,
    params: {
      id: id
    }
  }, function(err, results) {
    var ids;
    if (results) {
      ids = results.map(function(result) {
		    return result.people.properties.id;
      });
      types = results.map(function(result) {
		    return result['r.type'];
      });
    }
    callback(err, { "ids": ids, "types": types } );
  });
};


// http://localhost:3001/orgsFromPerson?id=nevenfuncties/NyXL7RdbH2ze
exports.orgsFromPerson = function(id, relations, organizations, callback) {
  var query = replaceOrganizations(replaceRelations(orgsFromPersonQuery, relations), organizations);
  graphDb.cypher({
    query: query,
    params: {
      id: id
    }
  }, function(err, results) {
    var ids;
    if (results) {
      ids = results.map(function(result) {
		    return result.orgs.properties.id;
      });
      types = results.map(function(result) {
		    return result['r.type'];
      });
    }
    // return both ids and the type of relations
    callback(err, { "ids": ids, "types": types } );
  });
};


exports.peopleFromOrgsFromPerson = function(id, relations, organizations, callback) {
  var query = replaceOrganizations(replaceRelations(peopleFromOrgsFromPersonQuery, relations), organizations);

  graphDb.cypher({
    query: query,
    params: {
      id: id
    }
  }, function(err, results) {
    var ids;
    if (results) {
            
      ids = results.map(function(result) {
		    return result.people.properties.id;
      });
      orgs = results.map(function(result) {
		    return {"id": result.orgs.properties.id, "name": result.orgs.properties.name } ;
      });
      types = results.map(function(result) {
		    return result['r.type'];
      });
    }
    callback(err,{"ids": ids, "orgs": orgs, "types": types});
  });
};



exports.related = function(ids, relations, direction, callback) {
  var query = replaceRelations(relatedQuery, relations);

  if (direction === 'from') {
    query = query.replace('«directionFrom»', '-').replace('«directionTo»', '->');
  } else if (direction === 'to') {
    query = query.replace('«directionFrom»', '<-').replace('«directionTo»', '-');
  }
  
  graphDb.cypher({<commit_msg>define vars in case of error<commit_after>var fs = require('fs');
var path = require('path');
var config = require('histograph-config');
var request = require('request');
var neo4j = require('neo4j');

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);

var shortestPathQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'shortest-path.cypher'), {encoding: 'utf8'});
var relatedQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'related.cypher'), {encoding: 'utf8'});

var peopleFromOrgsFromPersonQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'peopleFromOrgsFromPerson.cypher'), {encoding: 'utf8'});
var peopleFromOrgsFromPersonQueryCC = fs.readFileSync(path.join(__dirname, '..', 'queries', 'peopleFromOrgsFromPerson.concurrent.cypher'), {encoding: 'utf8'});
var orgsFromPersonQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'orgsFromPerson.cypher'), {encoding: 'utf8'});
var peopleFromOrgQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'peopleFromOrg.cypher'), {encoding: 'utf8'});
var equivalentIDsQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'equivalentIDs.cypher'), {encoding: 'utf8'});
var equivalentNodesQuery = fs.readFileSync(path.join(__dirname, '..', 'queries', 'equivalentNodes.cypher'), {encoding: 'utf8'});

function replaceRelations(query, relations) {
  return query.replace(/«relations»/g, relations.map(function(relation) {
    return '`' + relation + '`';
  }).join('|'));
}


function replaceOrganizations(query, organizations) {
  return query.replace(/«organizations»/g, organizations.map(function(org) {
    return '"' + org + '"';
  }).join(','));
}



exports.shortestPath = function(idsFrom, idsTo, relations, callback) {
  var query = replaceRelations(shortestPathQuery, relations);
  graphDb.cypher({
    query: query,
    params: {
      idsFrom: idsFrom,
      idsTo: idsTo
    }
  }, function(err, results) {
    var ids;
    if (results) {
      ids = results.map(function(result) {
        return result.id;
      });
    }
    callback(err, ids);
  });
};


// http://localhost:3001/equivalence?id=urn:hgid:overheidsorganisaties/2517
exports.equivalentIDs = function(id, relations, callback) {
  var query = replaceRelations(equivalentIDsQuery, relations);
  graphDb.cypher({
    query: query,
    params: {
      id: id
    }
  }, function(err, results) {
    var ids;
    if (results) {
        ids = results.map(function(result) {
          return result.ids;
        });
    }
    callback(err, ids);
  });
};

exports.equivalentNodes = function(id, relations, callback) {
  var query = replaceRelations(equivalentNodesQuery, relations);
  graphDb.cypher({
    query: query,
    params: {
      id: id
    }
  }, function(err, results) {
    var ids;
    if (results) {
        ids = results.map(function(result) {
          return result.found.properties.id;
        });
    }
    callback(err, ids);
  });
};


// http://localhost:3001/peopleFromOrg?id=nevenfuncties/Ny0H0O-BhGe
exports.peopleFromOrg = function(id, relations, callback) {
  var query = replaceRelations(peopleFromOrgQuery, relations);
  graphDb.cypher({
    query: query,
    params: {
      id: id
    }
  }, function(err, results) {
    var ids;
    var types;
    if (results) {
      ids = results.map(function(result) {
		    return result.people.properties.id;
      });
      types = results.map(function(result) {
		    return result['r.type'];
      });
    }
    callback(err, { "ids": ids, "types": types } );
  });
};


// http://localhost:3001/orgsFromPerson?id=nevenfuncties/NyXL7RdbH2ze
exports.orgsFromPerson = function(id, relations, organizations, callback) {
  var query = replaceOrganizations(replaceRelations(orgsFromPersonQuery, relations), organizations);
  graphDb.cypher({
    query: query,
    params: {
      id: id
    }
  }, function(err, results) {
    var ids, types;
    if (results) {
      ids = results.map(function(result) {
		    return result.orgs.properties.id;
      });
      types = results.map(function(result) {
		    return result['r.type'];
      });
    }
    // return both ids and the type of relations
    callback(err, { "ids": ids, "types": types } );
  });
};


exports.peopleFromOrgsFromPerson = function(id, relations, organizations, callback) {
  var query = replaceOrganizations(replaceRelations(peopleFromOrgsFromPersonQueryCC, relations), organizations);

  graphDb.cypher({
    query: query,
    params: {
      id: id
    }
  }, function(err, results) {
    var ids, orgs, types;
    if (results) {
      ids = results.map(function(result) {
		    return result.people.properties.id;
      });
      orgs = results.map(function(result) {
		    return {"id": result.orgs.properties.id, "name": result.orgs.properties.name } ;
      });
      types = results.map(function(result) {
		    return result['r.type'];
      });
    }
    callback(err,{"ids": ids, "orgs": orgs, "types": types});
  });
};



exports.related = function(ids, relations, direction, callback) {
  var query = replaceRelations(relatedQuery, relations);

  if (direction === 'from') {
    query = query.replace('«directionFrom»', '-').replace('«directionTo»', '->');
  } else if (direction === 'to') {
    query = query.replace('«directionFrom»', '<-').replace('«directionTo»', '-');
  }
  
  graphDb.cypher({
{'fraction_non_alphanumeric': 0.1673985124115064, 'fraction_numerical': 0.007527556232637333, 'mean_word_length': 3.6307053941908713, 'pattern_counts': {'":': 18, '<': 5, '<?xml version=': 0, '>': 5, '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': '4063504', 'n_tokens_mistral': 3745, 'n_tokens_neox': 3592, 'n_words': 719}