<commit_before><?php

require("rb.php");
require("papercut.php");
R::setup("sqlite:db/database.db");
class DB
{
	public static function AddJob($filename)
	{
		$w = R::dispense( 'job' );
		$w->name = $filename;
		$t = time();
		$w->timestamp = $t;
		$w->status = R::enum('status:Queued');
		$w->progress = 0;
		$id = R::store( $w );
		$bean = R::load('job', $id);
		$bean->filename =  $filename;
		http_response_code(200);
		print $bean;
	}
	
	public static function ListJobs($app)
	{
		$jobs = R::find( 'job' );
		if ( !count( $jobs ) )
		{			
			//print ( "The job table is empty!\n" );
                        // return FALSE;
			$app->response()->status(400);
			$error = "No jobs in your table";
			print $error;
			return;
		}

		$beans = R::exportAll( $jobs );
		foreach ($beans as $bean) 
		{
			$percentage = pc::getTime($bean["timestamp"] . '/' . substr($bean["name"], 0, strrpos($bean["name"], '.')) . ".out", $bean["id"]);
			if($percentage != null)
			{
				DB::UpdateStatus($bean["id"], round($percentage, 2));
			}
			else
			{
				DB::UpdateStatus($bean["id"], 0);
			}
		}
		$app->response()->status(200);
		print json_encode($beans);
	}

	public static function FindJob($id)
	{
		$job = R::load('job',$id);
		print_r(json_decode($job));
                return $job;    // Added by <NAME>. 
	}

	public static function DeleteJob($id)
	{
		$job = R::load('job',$id);
		$filename = $job['name'];
		$timestamp = $job['timestamp'];
		//$bool = DB::deleteDir("uploads\\" . $timestamp);
		if(!$bool)
		{
			$app->response()->status(400);
			$error = "File {$filename} could not be deleted or was open.";
			return json_encode($error);
		}
		$result = R::trash('job', $id);
		if($result)
		{
			$app->response()->status(200);
			$response = "Job {$id} has been deleted.";
			return json_encode($response);
		}
		else
		{
			$app->response()->status(400);
			$error = "Job {$id} could not be deleted or doesn't exist.";
			return json_encode($error);
		}	
	}

	/*
	public static function deleteDir($dirPath) 
	{
	    if (!is_dir($dirPath)) 
	    {
	        //throw new InvalidArgumentException("$dirPath must be a directory");
	        return false;
	    }
	    if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') 
	    {
	        $dirPath .= '/';
	    }
	    $files = glob($dirPath . '*', GLOB_MARK);
	    foreach ($files as $file) 
	    {
	        if (is_dir($file)) 
	        {
	            self::deleteDir($file);
	        } 
	        else 
	        {
	            unlink($file);
	        }
	    }
	    rmdir($dirPath);
	    return true;
	}*/
	
	public static function UpdateStatus($id,$percentage)
	{
		$job = R::load('job', $id);
		if($percentage >= 100)
		{
			$percentage = 100;
			$job->status = R::enum('status:Completed');
		}
		else if ($percentage < 100 && $percentage > 0)
		{
			$job->status = R::enum('status:In Progress');
		}
		$job->progress = $percentage;
		R::store($job);
	}

	//debug function
	public static function MakeJob($filename)
	{
		$w = R::dispense( 'job' );
		$w->name = $filename;
		$t = time();
		$w->timestamp = $t;
		$w->progress = $percentage;
		$w->status = R::enum('status:Queued');
		$id = R::store( $w );
	}

}
?><commit_msg>Changed how the database was refreshed.<commit_after><?php

require("rb.php");
require("papercut.php");
R::setup("sqlite:db/database.db");
class DB
{
	public static function AddJob($filename)
	{
		$w = R::dispense( 'job' );
		$w->name = $filename;
		$t = time();
		$w->timestamp = $t;
		$w->status = R::enum('status:Queued');
		$w->progress = 0;
		$id = R::store( $w );
		$bean = R::load('job', $id);
		$bean->filename =  $filename;
		http_response_code(200);
		print $bean;
	}
	
	public static function ListJobs($app)
	{
		$bool = DB::refreshJobs();
		if($bool[0])
		{
			$app->response()->status(200);
			print json_encode($bool[1]);
		}
		else 
		{
			$app->response()->status(400);
			$error = "No jobs in your table";
			print $error;
		}
	}

	public static function FindJob($id)
	{
		$job = R::load('job',$id);
		print_r(json_decode($job));
                return $job;    // Added by <NAME>. 
	}

	public static function DeleteJob($id)
	{
		$job = R::load('job',$id);
		$filename = $job['name'];
		$timestamp = $job['timestamp'];
		//$bool = DB::deleteDir("uploads\\" . $timestamp);
		if(!$bool)
		{
			$app->response()->status(400);
			$error = "File {$filename} could not be deleted or was open.";
			return json_encode($error);
		}
		$result = R::trash('job', $id);
		if($result)
		{
			$app->response()->status(200);
			$response = "Job {$id} has been deleted.";
			return json_encode($response);
		}
		else
		{
			$app->response()->status(400);
			$error = "Job {$id} could not be deleted or doesn't exist.";
			return json_encode($error);
		}	
	}

	/*
	public static function deleteDir($dirPath) 
	{
	    if (!is_dir($dirPath)) 
	    {
	        //throw new InvalidArgumentException("$dirPath must be a directory");
	        return false;
	    }
	    if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') 
	    {
	        $dirPath .= '/';
	    }
	    $files = glob($dirPath . '*', GLOB_MARK);
	    foreach ($files as $file) 
	    {
	        if (is_dir($file)) 
	        {
	            self::deleteDir($file);
	        } 
	        else 
	        {
	            unlink($file);
	        }
	    }
	    rmdir($dirPath);
	    return true;
	}*/
	
	public static function UpdateStatus($id,$percentage)
	{
		$job = R::load('job', $id);
		if($percentage >= 100)
		{
			$percentage = 100;
			$job->status = R::enum('status:Completed');
		}
		else if ($percentage < 100 && $percentage > 0)
		{
			$job->status = R::enum('status:In Progress');
		}
		$job->progress = $percentage;
		R::store($job);
	}

	//debug function
	public static function MakeJob($filename)
	{
		$w = R::dispense( 'job' );
		$w->name = $filename;
		$t = time();
		$w->timestamp = $t;
		$w->progress = $percentage;
		$w->status = R::enum('status:Queued');
		$id = R::store( $w );
	}

	public static function refreshJobs()
	{
		$jobs = R::find( 'job' );
		if ( !count( $jobs ) )
		{			
			return array(false);
		}

		$beans = R::exportAll($jobs);
		foreach ($beans as $bean) 
		{
			$percentage = pc::getTime($bean["timestamp"] . '/' . substr($bean["name"], 0, strrpos($bean["name"], '.')) . ".out", $bean["id"]);
			if($percentage != null)
			{
				DB::UpdateStatus($bean["id"], round($percentage, 2));
			}
			else
			{
				DB::UpdateStatus($bean["id"], 0);
			}
		}
		return array(true,$beans);
	}

	public static function queryFiles()
	{
		$toRunJobs = array();
		//refresh jobs
		$result = DB::refreshJobs();
		//check result for true return or false return

		//find out how many are running currently
		$count = R::exec('SELECT count(id), status FROM job WHERE job.status = 2');
		print var_dump($count);

		//if($count < 3)
		//{
			//query for next jobs
			$jobs = R::exec('SELECT * FROM job WHERE job.status = 1');
			print var_dump($jobs);
			//forech for each job queued and add them to an array of jobs and add 1 to count
			foreach ($jobs as $job)
			{
				if($count < 3)
				{
					array_push($toRunJobs, $job);
					$count++;
				}
				else
				{
					break;
				}
			}
			return $toRunJobs;
		//}
		//else
		//{
		//	return null;
		//}
	}

}
?>
{'fraction_non_alphanumeric': 0.20258082419869572, 'fraction_numerical': 0.010406549188289163, 'mean_word_length': 2.728918779099845, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 55, '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': '560055', 'n_tokens_mistral': 3027, 'n_tokens_neox': 2572, 'n_words': 564}