<commit_before>/*
 * Serve JSON to our AngularJS client
 */



/*
 * GET users listing.
 */
"use strict";
/* jshint -W098 */
/* jshint -W003 */
/* jshint -W009 */
var q = require('q');
var db = require('../db');
var async = require('async');
var config = require('../config');
//jobs rest api

//parameter all defined the recursive level of query:0 no recursive level. 1: one depth level query indefinite: all avaialable depth query
module.exports.list = function(req, res)
	{
	console.log("param all "+req.params.all);
	console.log("param id "+req.params.id);
	query_post(req.params.id,req.params.all).then(function(val) 
		{
			res.send(val);
		});  
	};
//quality control rest api
module.exports.qc_list = function(req, res){
	console.log("qc api");
	query_qc_post(req.params.id).then (function(val) 
		{
		res.send(val);
		});
	};

function query_qc_post(id)
	{  
		var deferred = q.defer();
		db.client_pau("quality_control").select().where('job_id',id).andWhere('ref', 'not like', 'general').then  
		(
			function(resp) 
			{
				console.log(resp);
				deferred.resolve(resp);
			}, 
			function(err) 
			{
				console.log(err.message);
			}
		); 
	return deferred.promise;
	}  
//select p.id , (select count(a.id) from job a where a.super_id = p.id) from job p
function query_post(id,all)
{  
	var deferred = q.defer();
	console.log("entra");
	if (id === undefined) 
	{
		db.client_job('job as p').select(db.client_job.raw('*, (select count(*)  from job a where a.super_id = p.id) as nbr')).whereNull('super_id').then
		(
			function(resp) 
			{	
				//console.log(resp);
				var job_ids = new Array();
				var ids = {};
				for (var i in resp) {
					
					job_ids.push(resp[i].id);				
					ids[resp[i].id] = i;}
					
				db.client_pau("quality_control").select().whereIn('job_id',job_ids).andWhere('ref','general').then
								(
					function(resp_qc) 
					{
						
						var jobs = new Array();
						for (var i in resp_qc) {
							resp[ids[resp_qc[i].job_id]].qc = resp_qc[i].qc_pass;
							}	
					
						deferred.resolve(resp);
					}, 
					function(err) 
					{
						console.log(err.message);
					}
				);  
				//deferred.resolve(resp);
			}, 
			function(err) 
			{
				console.log(err.message);
			}
		); 
	}
	else 
	{
		if (all === '1') 
		{  
			if (config.job.client === "pg")
			{
				console.log(recursive_query(id,1));
				db.client_job.raw(recursive_query(id,1)).then
				( 
					function(resp) 
					{
						
						deferred.resolve(resp.rows);
					}, 
					function(err) 
					{
						console.log(err.message);
					}
				);
			}
			else
			{
				flat_tree_dict(id, 0,1, function (treeSet) 
				{
					console.log("lunghezza",treeSet.length);
					console.log("lunghezza",treeSet[0].id);
					deferred.resolve(treeSet);
				});
			}

		}
		if (all === '0') 
		{
			db.client_job('job').where("id",id).select().then
			( 
				function(resp) 
				{
					
					deferred.resolve(resp);
				}, 
				function(err) 
				{
					console.log(err.message);
				}
			);

		}
		if (all === 'All') 
		{  
			if (config.job.client === "pg")
			{
				console.log(recursive_query(id,100));
				db.client_job.raw(recursive_query(id,100)).then
				( 
					function(resp) 
					{
						
						deferred.resolve(resp.rows);
					}, 
					function(err) 
					{
						console.log(err.message);
					}
				);
			}
			else
			{
				flat_tree_dict(id, 0,100, function (treeSet) 
				{
					console.log("lunghezza",treeSet.length);
					console.log("lunghezza",treeSet[0].id);
					deferred.resolve(treeSet);
				});
			}
		}

	}
return deferred.promise;
}<commit_msg>simplify<commit_after>/*
 * Serve JSON to our AngularJS client
 */



/*
 * GET users listing.
 */
"use strict";
/* jshint -W098 */
/* jshint -W003 */
/* jshint -W009 */
var q = require('q');
var db = require('../db');
var async = require('async');
var config = require('../config');
//jobs rest api

//parameter all defined the recursive level of query:0 no recursive level. 1: one depth level query indefinite: all avaialable depth query
module.exports.list = function(req, res)
	{
	console.log("param all "+req.params.all);
	console.log("param id "+req.params.id);
	query_post(req.params.id,req.params.all).then(function(val) 
		{
			res.send(val);
		});  
	};
//quality control rest api
module.exports.qc_list = function(req, res){
	console.log("qc api");
	query_qc_post(req.params.id).then (function(val) 
		{
		res.send(val);
		});
	};
// select operative quality_controls as a funciton of job id
function query_qc_post(id)
	{  
		var deferred = q.defer();
		db.client_pau("quality_control").select().where('job_id',id).andWhere('ref', 'not like', 'general').then  
		(
			function(resp) 
			{
				console.log(resp);
				deferred.resolve(resp);
			}, 
			function(err) 
			{
				console.log(err.message);
			}
		); 
	return deferred.promise;
	}  

function query_post(id,all)
{  
	var deferred = q.defer();
	console.log("entra");
	if (id === undefined) 
	{	//select all job with super_id null
		db.client_job('job as p').select(db.client_job.raw('*, (select count(*)  from job a where a.super_id = p.id) as nbr')).whereNull('super_id').then
		(
			function(resp) 
			{	
				//console.log(resp);
				var job_ids = new Array();
				var ids = {};
				for (var i in resp) {
					
					job_ids.push(resp[i].id);				
					ids[resp[i].id] = i;}
					//select informative quality control and attac h them to job informations
				db.client_pau("quality_control").select().whereIn('job_id',job_ids).andWhere('ref','general').then
								(
					function(resp_qc) 
					{
						
						var jobs = new Array();
						for (var i in resp_qc) {
							resp[ids[resp_qc[i].job_id]].qc = resp_qc[i].qc_pass;
							}	
					
						deferred.resolve(resp);
					}, 
					function(err) 
					{
						console.log(err.message);
					}
				);  
				
			}, 
			function(err) 
			{
				console.log(err.message);
			}
		); 
	}
	else 
	{
		if (all === '1') 
		{  	//select all job with one level depth for the given job id
			//if db backend is postgres query will be sql recursive enabled if sqlite the recursion is given at server level (it takes much more time)
			if (config.job.client === "pg")
			{
				console.log(recursive_query(id,1));
				db.client_job.raw(recursive_query(id,1)).then
				( 
					function(resp) 
					{
						
						deferred.resolve(resp.rows);
					}, 
					function(err) 
					{
						console.log(err.message);
					}
				);
			}
			else
			{
				flat_tree_dict(id, 0,1, function (treeSet) 
				{
					console.log("lunghezza",treeSet.length);
					console.log("lunghezza",treeSet[0].id);
					deferred.resolve(treeSet);
				});
			}

		}
		if (all === '0') 
		{   //select all job with zero level depth for the given job id
			db.client_job('job').where("id",id).select().then
			( 
				function(resp) 
				{
					
					deferred.resolve(resp);
				}, 
				function(err) 
				{
					console.log(err.message);
				}
			);

		}
		if (all === 'All') 
		{  //select all job with infinite level depth for the given job id
			if (config.job.client === "pg")
			{
				console.log(recursive_query(id,100));
				db.client_job.raw(recursive_query(id,100)).then
				( 
					function(resp) 
					{
						
						deferred.resolve(resp.rows);
					}, 
					function(err) 
					{
						console.log(err.message);
					}
				);
			}
			else
			{
				flat_tree_dict(id, 0,100, function (treeSet) 
				{
					console.log("lunghezza",treeSet.length);
					console.log("lunghezza",treeSet[0].id);
					deferred.resolve(treeSet);
				});
			}
		}

	}
return deferred.promise;
}
{'fraction_non_alphanumeric': 0.1661579296615793, 'fraction_numerical': 0.0076974120769741205, 'mean_word_length': 2.4696132596685083, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6907757', 'n_tokens_mistral': 3613, 'n_tokens_neox': 2731, 'n_words': 501}