<?php

// Construct the Request
$request = request();

// Instantiate Tests
$tests = new Tests($request);

// Gather results
$result = [
	'Matches Waiting for Export' => $tests->getMatchesWaitingForExport(),
	'Processed Files' => $tests->getProcessedFiles(),
	'Stuck Files in Customer Folder' => $tests->getStuckFiles(),
	'JAR Status: AlertsParser' => $tests->getstatusAlertsParser(),
	'JAR Status: Exporter' => $tests->getstatusExporter(),
	'JAR Status: Parser' => $tests->getstatusParser(),
	'Incoming Issue: Alerts' => $tests->getstatusIncomingAlerts(),
	'Incoming Issue: Skipped Alerts' => $tests->getstatusSkippedAlerts(),
	'Incoming Issue: Feed' => $tests->getstatusIncomingFeed(),
	'Incoming Issue: Skipped Feed' => $tests->getstatusSkippedFeed(),
	'Outgoing Issue: Processing' => $tests->getstatusOutgoingProcess(),
	'Outgoing Issue: Failed' => $tests->getstatusOutgoingFailed(),
	'Outgoing Issue: Last Processed File' => $tests->getstatusOutgoingLastFile(),
	'Exporter Issue: Stuck Files - export' => $tests->getstatusExporterExport(),
	'Exporter Issue: Stuck Files - oddsUpload' => $tests->getstatusExporteroddsUpload()
];

// Return response
returnResponse($result);



class Tests
{
	protected $MatchesWaitingForExport;
	protected $customer;
	protected $timeFrom;
	protected $timeTo;
	protected $processedFiles;
	protected $time;
	protected $inTime;
	protected $stuckDir;
	protected $stuckFiles;
	protected $statusAlertsParser;
	protected $statusExporter;
	protected $statusParser;
	Protected $timeNow;
    	protected $recentFile;
    	protected $recentTimeStamp;
	Protected $statusIncomingAlerts;
	Protected $statusIncomingFeed;
	Protected $statusSkippedAlerts;
	Protected $statusSkippedFeed;
	Protected $statusOutgoingProcess;
	Protected $statusOutgoingFailed;
	Protected $statusOutgoingLastFile;
	Protected $recentFileExporter;
	Protected $recentTimeStampExporter;
	Protected $statusExporterExport;
	Protected $statusExporteroddsUpload;

    public function __construct($request)
    {
	$this->calculateMatchesWaitingForExport();
	$this->customer = $request['customer'];
	$this->stuckDir = $request['stuckDir'];
	$this->timeFrom = $request['timeFrom'];
	$this->timeTo = $request['timeTo'];
	$this->time = date('H:i');
	$this->setInTime();
	$this->calculateProcessedFiles();
	$this->calculateStuckFiles();
	$this->calculateAlertsParser();
	$this->calculateExporter();
	$this->calculateParser();
	$this->timeNow = time();                                                                                                                                                 
  	$this->recentFile = exec('ls -tr /odds/ToCheck/' . $this->customer . '/processed|tail -n1');                                                                             
  	$this->recentTimeStamp = exec('date -r /odds/ToCheck/' . $this->customer . '/processed/' . strval($this->recentFile) . ' +%s');
  	$this->recentFileExporter = exec('ls -tr /odds/betradar/export/ | tail -n1');                                                                             
  	$this->recentTimeStampExporter = exec('date -r /odds/betradar/export/' . strval($this->recentFileExporter) . ' +%s');
   	$this->recentFileoddsUpload = exec('ls -tr /odds/betradar/oddsUpload/ | tail -n1');     
   	$this->recentTimeStampoddsUpload = exec('date -r /odds/betradar/oddsUpload/' . strval($this->recentFileoddsUpload) . ' +%s');
    	$this->recentCheckExport = exec('find /odds/betradar/export/ -maxdepth 1 -mindepth 1 -type f | wc -l');
	$this->recentCheckoddsUpload = exec('find /odds/betradar/oddsUpload/ -maxdepth 1 -mindepth 1 -type f | wc -l');
	$this->calculateIncomingAlerts();
	$this->calculateIncomingFeed();
	$this->calculateSkippedAlerts();
	$this->calculateSkippedFeed();
	$this->calculateOutgoingProcess();
	$this->calculateOutgoingFailed();
	$this->calculateOutgoingLastFile();
	$this->calculateExporterLastFile();
	$this->calculateoddsUploadLastFile();
    }
	public function getProcessedFiles()
    {
		return $this->processedFiles;
    }
	private function calculateProcessedFiles()
    {
		if($this->inTime) {
		$this->processedFiles = 1;
        } else {
		$this->processedFiles = exec("find /odds/$this->customer/processed/ -mmin -6 -type f | wc -l");        
        }
    }
	public function getStuckFiles()                                                                                                                               		{                                                                                                                                                                   
		return $this->stuckFiles;                                                                                                                                   
    }

	private function calculateStuckFiles()
    {
		if($this->inTime) {
			$this->stuckFiles = 1;
        } else {
		$this->stuckFiles = exec("find /odds/$this->stuckDir/ -maxdepth 1 -type f | wc -l");
        }
    }

	public function getstatusAlertsParser()
    {                                                                                                                                                                 
	return $this->statusAlertsParser;
    }

	private function calculateAlertsParser()
    	{
		if($this->inTime) {
			$this->statusAlertsParser = 0;
        	} else {
			$this->statusAlertsParser = exec("cat /var/www/html/monitoring/result_alertparser.txt");
        	}
    	}
	public function getstatusExporter()
    {
        return $this->statusExporter;
    }
	private function calculateExporter()
        {
                if($this->inTime) {
                        $this->statusExporter = 0;
                } else {                                                                                                                                                                                                                       
			$this->statusExporter = exec("cat /var/www/html/monitoring/result_exporter.txt");                                                                                                                              
                }
        }


        public function getMatchesWaitingForExport()
    {
        return $this->MatchesWaitingForExport;
    }
        private function calculateMatchesWaitingForExport()
        {
                if($this->inTime) {
                        $this->MatchesWaitingForExport = 0;
                } else {
                        $this->MatchesWaitingForExport = exec("cat /var/www/html/search/js/value.txt");
                }
        }

	public function getstatusParser()
    {
        return $this->statusParser;
    }
	private function calculateParser()
        {
                if($this->inTime) {
                        $this->statusParser = 0;
                } else {                                                                                                                                                                                                                       
                        $this->statusParser = exec("cat /var/www/html/monitoring/result_parser.txt");                                                                                                                              
                }
        }
    private function setInTime()                                                                                                                                                 
    {                                                                                                                                                                            
        $this->inTime = TRUE;                                                                                                                                                    
                                                                                                                                                                                 
//        if ($this->timeFrom >= $this->time && $this->timeFrom != NULL) {                                                                                                       
//            $this->inTime = FALSE;                                                                                                                                             
//        }                                                                                                                                                                      
                                                                                                                                                                                 
        if($this->timeFrom < $this->time && $this->timeTo > $this->time) {                                                                                                       
//            if ($this->timeTo <= $this->time && $this->timeTo != NULL) {                                                                                                       
                $this->inTime = TRUE;                                                                                                                                            
//            }                                                                                                                                                                  
        } else {                                                                                                                                                                 
//            if ($this->timeTo >= $this->time && $this->timeTo != NULL) {                                                                                                       
                $this->inTime = FALSE;                                                                                                                                           
//            }                                                                                                                                                                  
        }                                                                                                                                                                        
                                                                                                                                                                                 
                                                                                                                                                                                 
    }                                                                                                                                                                            
                                                                                                                                                                                 
public function getstatusIncomingAlerts()                                                                                                                                        
    {                                                                                                                                                                            
        return $this->statusIncomingAlerts;                                                                                                                                              
    }                                                                                                                                                                            
        private function calculateIncomingAlerts()                                                                                                                                       
        {                                                                                                                                                                        
                if($this->inTime) {                                                                                                                                              
                        $this->statusIncomingAlerts = 1;                                                                                                                                 
                } else {                                                                                                                                                         
                                                                                                                                                                                 
                        $this->statusIncomingAlerts = exec("ls /odds/betradar/alerts|wc -l");                                                                            
                                                                                                                                                                                 
                }                                                                                                                                                                
        }

        public function getstatusIncomingFeed()                                                                                                                                        
    {                                                                                                                                                                            
        return $this->statusIncomingFeed;                                                                                                                                              
    }                                                                                                                                                                            
        private function calculateIncomingFeed()                                                                                                                                       
        {                                                                                                                                                                        
                if($this->inTime) {                                                                                                                                              
                        $this->statusIncomingFeed = 1;                                                                                                                                 
                } else {                                                                                                                                                         
                                                                                                                                                                                 
                        $this->statusIncomingFeed = exec("ls /odds/betradar/feed|wc -l");                                                                            
                                                                                                                                                                                 
                }                                                                                                                                                                
        }



	public function getstatusSkippedAlerts()                                                                                                                                        
    {                                                                                                                                                                            
        return $this->statusSkippedAlerts;                                                                                                                                              
    }                                                                                                                                                                            
        private function calculateSkippedAlerts()                                                                                                                                       
        {                                                                                                                                                                        
                if($this->inTime) {                                                                                                                                              
                        $this->statusSkippedAlerts = 1;                                                                                                                                 
                } else {                                                                                                                                                         
                                                                                                                                                                                 
                        $this->statusSkippedAlerts = exec("find /odds/betradar/skippedAlerts/ -type f -cmin -30|wc -l");
                }                                                                                                                                                                
        }

        public function getstatusSkippedFeed()                                                                                                                                        
    {                                                                                                                                                                            
        return $this->statusSkippedFeed;                                                                                                                                              
    }                                                                                                                                                                            
        private function calculateSkippedFeed()                                                                                                                                       
        {                                                                                                                                                                        
                if($this->inTime) {                                                                                                                                              
                        $this->statusSkippedFeed = 1;                                                                                                                                 
                } else {                                                                                                                                                         
                                                                                                                                                                                 
                        $this->statusSkippedFeed = exec("find /odds/betradar/skippedFeed/ -type f -cmin -30|wc -l");
                }                                                                                                                                                                
        }


	public function getstatusOutgoingProcess()                                                                                                                                        
    {                                                                                                                                                                            
        return $this->statusOutgoingProcess;                                                                                                                                              
    }                                                                                                                                                                            
        private function calculateOutgoingProcess()                                                                                                                                       
        {                                                                                                                                                                        
                if($this->inTime) {                                                                                                                                              
                        $this->statusOutgoingProcess = 1;                                                                                                                                 
                } else {                                                                                                                                                         
                                                                                                                                                                                 
                        $this->statusOutgoingProcess = exec("find /odds/ToCheck/$this->customer/ -maxdepth 1 -mindepth 1 -type f|wc -l");
                }                                                                                                                                                                
        }



	public function getstatusOutgoingFailed()                                                                                                                                        
    {                                                                                                                                                                            
        return $this->statusOutgoingFailed;                                                                                                                                              
    }                                                                                                                                                                            
        private function calculateOutgoingFailed()                                                                                                                                       
        {                                                                                                                                                                        
                if($this->inTime) {                                                                                                                                              
                        $this->statusOutgoingFailed = 1;                                                                                                                                 
                } else {                                                                                                                                                         
                                                                                                                                                                                 
                        $this->statusOutgoingFailed = exec("find /odds/ToCheck/$this->customer/failed/ -type f -cmin -30|wc -l");                                                                                                                   
                }                                                                                                                                                                
        }


	public function getstatusOutgoingLastFile()                                                                                                                                                 
    {                                                                                                                                                                            
        return $this->statusOutgoingLastFile;                                                                                                                                                                     
    }                                                                                                                                                                            
        private function calculateOutgoingLastFile()                                                                                                                                                      
        {                                                                                                                                                                        
                if($this->inTime) {                                                                                                                                              
                        $this->statusOutgoingLastFile = 1;                                                                                                                                                             
                } else {                                                                                                                                                         
                        $timeDelta = intval($this->timeNow) - intval($this->recentTimeStamp);                                                                                                                                                
                        $this->statusOutgoingLastFile = exec("echo " . $timeDelta);                                                                                                                                                             
                }                                                                                                                                                                
        }


	public function getstatusExporterExport()                                                                                                                                                 
    {
        return $this->statusExporterExport;                           
    }                                                                                                                                                                            
        private function calculateExporterLastFile()                      
        { 
                if($this->inTime || $this->recentCheckExport == 0) {                                                                                                                                              
                        $this->statusExporterExport = 1;        
                } else {                                                                                                                                                         
                        $timeDelta1 = intval($this->timeNow) - intval($this->recentTimeStampExporter);          
                        $this->statusExporterExport = exec("echo " . $timeDelta1);                                                               
                }
        }

	public function getstatusExporteroddsUpload()              
    {
        return $this->statusExporteroddsUpload;                            
    }
        private function calculateoddsUploadLastFile()                                                                                                                                                      
        { 
                if($this->inTime || $this->recentCheckoddsUpload == 0) {                                                                                       
                        $this->statusExporteroddsUpload = 1;                                                                                                                                                             
                } else {                                                                                                                                                         
                        $timeDelta2 = intval($this->timeNow) - intval($this->recentTimeStampoddsUpload);      
                        $this->statusExporteroddsUpload = exec("echo " . $timeDelta2);                                                                 
                } 
        }





}


function request()
{
	if(!empty($_GET["customer"])) {
		$request['customer'] = $_GET["customer"];
    	} else {
        	$request['customer'] = NULL;
    	}

	if(!empty($_GET["stuckDir"])) {
        	$request['stuckDir'] = $_GET["stuckDir"];
    	} else {
        	$request['stuckDir'] = NULL;
    	}
	if(!empty($_GET["time_from"])) {
        	$request['timeFrom'] = $_GET["time_from"];
    	} else {
        	$request['timeFrom'] = NULL;
    	}
	if(!empty($_GET["time_to"])) {
        	$request['timeTo'] = $_GET["time_to"];
    	} else {
        	$request['timeTo'] = NULL;
    	}

	return $request;
}

function returnResponse($result)
{
    	header('Content-type: application/json');
    	echo json_encode($result);
}
