<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

include './assets/functions.php';
include './settings/settings.php';
include './settings/config.php';


$tes= '40';

$test_r = (isset($test))? $test : (isset($test2)? $test2 : 'none');

echo $test_r;
//Connect to DB
$pdo = dbConnect($dbname);


//GET 
$sql = 'SELECT rowID, description, equipmentid FROM history where type="Maintenance_Test" and description like "%doubletestvalues%"';
$stmt = $pdo->prepare($sql);
$stmt->execute();
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);

$watchlist_byproduct = [];
$watchlist_bytest = [];
$watchlist_totals = [];

//train the model
$total_measurement = traintotalMeasurement($messages);

//get statics on results
$total_results = statisticalAnalyses($total_measurement);

//COMPARISON -- CHECK DEVIATIONS FROM STANDARD
foreach ($total_measurement as $measurement => $values){
    foreach($total_results as $total_result => $measured_values){
        if ($measurement == $total_result){
            foreach ($values as $id => $measured_value){
                if (($measured_value <= $total_results[$total_result]['stdev-3']) && ($measured_value >= $total_results[$total_result]['stdev+3'])){
                    $watchlist_byproduct[$id][] = array(
                        "measurement" => $measurement,
                        "value" => $measured_value,
                        "deviation" => 3
                    );
                    $watchlist_bytest[$measurement][] = array(
                        "equipmentid" => $id,
                        "value" => $measured_value,
                        "deviation" => 3
                    );
                }
                elseif ((($measured_value <= $total_results[$total_result]['stdev-2']) && ($measured_value >= $total_results[$total_result]['stdev-3'])) || (($measured_value >= $total_results[$total_result]['stdev+2']) && ($measured_value <= $total_results[$total_result]['stdev+3']))){
                    $watchlist_byproduct[$id][] = array(
                        "measurement" => $measurement,
                        "value" => $measured_value,
                        "deviation" => 2
                    );
                    $watchlist_bytest[$measurement][] = array(
                        "equipmentid" => $id,
                        "value" => $measured_value,
                        "deviation" => 2
                    );
                }
                elseif ((($measured_value <= $total_results[$total_result]['stdev-1']) && ($measured_value >= $total_results[$total_result]['stdev-2'])) || (($measured_value >= $total_results[$total_result]['stdev+1']) && ($measured_value <= $total_results[$total_result]['stdev+2']))){
                    /*$watchlist_byproduct[$id][] = array(
                        "measurement" => $measurement,
                        "value" => $measured_value,
                        "deviation" => 1
                    );
                    $watchlist_bytest[$measurement][] = array(
                        "equipmentid" => $id,
                        "value" => $measured_value,
                        "deviation" => 1
                    );
                    */
                }           
            }
        }
    }
}

//GET WATCHLIST SUMMARY
foreach ($watchlist_bytest as $test => $value){
    $watchlist_totals[$test]['n_deviation'] = count($value);
    //calculate percentage
    //$watchlist_totals[$test]['percentage'] = ($watchlist_totals[$test]['n_deviation'] / $watchlist_totals[$test]['n_total'])*100;
}

print "<pre>";
print_r($total_results);
print "</pre>";


/*MAIN STATISCAL FUNCTION
function standDeviation($arr)
{
    $num_of_elements = count($arr);
    $variance = 0.0;
    // Calculate mean using array_sum() method
    $average = array_sum($arr) / $num_of_elements;
    foreach($arr as $i)
    {
        // Sum of squares of differences between all numbers and means.
        $variance += pow(($i - $average), 2);
    }
    return (float)sqrt($variance / $num_of_elements);
}
function average($arr)
{
    $num_of_elements = count($arr);
    $average = array_sum($arr) / $num_of_elements;   
    return $average;
}
function calculateMedian($array) {
    if (empty($array)) {
        return null;
    } else {
        sort($array);
        $lowMiddle = $array[floor((count($array) - 1) / 2)];
        $highMiddle = $array[ceil((count($array) - 1) / 2)];
        return ($lowMiddle + $highMiddle) / 2;
    }
}
*/

?>