<commit_before><?php

 # This parses $_GET parameters to obtain timerange to plot
include('plot_common.php');
include('evalmath.class.php');

 #=====================================
 # DataBase Initialisation
 # connect to SQLite database
 $dbh = new PDO("sqlite:HappyFace.db");

 // Quick'n'dirty check to avoid SQL injection attacks on column names.
 // Unfortunately preparated statements do not work on table or column names
 // with SQLite.
 function verify_column_name($name)
 {
   for($i = 0; $i < count($name); ++$i)
     if(!ctype_alnum($name[$i]) && $name[$i] != '_')
       { echo "Invalid column name: $name"; die; }
   return $name;
 }

 // from http://www.actionscript.org/forums/archive/index.php3/t-50746.html
 function HSV_TO_RGB($H, $S, $V) // HSV Values: Number 0-1
 {                               // RGB Results: Number 0-255
   $RGB = array();

   if($S == 0)
   {
     $R = $G = $B = $V * 255;
   }
   else
   {
     $var_H = $H * 6;
     $var_i = floor( $var_H );
     $var_1 = $V * ( 1 - $S );
     $var_2 = $V * ( 1 - $S * ( $var_H - $var_i ) );
     $var_3 = $V * ( 1 - $S * (1 - ( $var_H - $var_i ) ) );

     if ($var_i == 0) { $var_R = $V ; $var_G = $var_3 ; $var_B = $var_1 ; }
     else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $V ; $var_B = $var_1 ; }
     else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $V ; $var_B = $var_3 ; }
     else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $V ; }
     else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $V ; }
     else { $var_R = $V ; $var_G = $var_1 ; $var_B = $var_2 ; }

     $R = $var_R * 255;
     $G = $var_G * 255;
     $B = $var_B * 255;
   }

   $RGB['R'] = $R;
   $RGB['G'] = $G;
   $RGB['B'] = $B;

   return $RGB;
 }

 $renormalize = isset($_GET['renormalize']) && intval($_GET['renormalize']) != 0;

 # Get timestamp variable to show
 $timestamp_var = 'timestamp';
 if(isset($_GET['timestamp_var']) && $_GET['timestamp_var'] != '')
   $timestamp_var = verify_column_name($_GET['timestamp_var']);
 $module_table = verify_column_Name($_GET['module'] . '_table');
 if(isset($_GET['subtable']) && $_GET['subtable'] != '')
   $module_table = verify_column_name($_GET['subtable']);

 # Apply additional constraint
 $where_clause = "";
 $constraint_vars = array();
 $constraint_values = array();
 if(isset($_GET['constraint']) && $_GET['constraint'] != '')
 {
   $comp = explode('=', $_GET['constraint'], 2);
   if(count($comp) == 2 && $comp[1] != '')
   {
     $constraint_var = verify_column_name($comp[0]);
     $comp = explode(',', $comp[1]);

     // If constraint is on one column only then just select it via a WHERE clause
     // Otherwise exclude manually later.
     if(count($comp) == 1)
     {
       $constraint_value = $comp[0];
       $where_clause = "AND $constraint_var = :constraint_value";
     }
     else
     {
       $constraint_vars[] = $constraint_var;
       $constraint_values[$constraint_var] = $comp; // constrain this var to the given values
     }
   }
   else
   {
     // make a separate plot for each value of this var but don't constrain
     if(count($comp) == 2)
       $constraint_vars[] = verify_column_name($comp[0]);
     else
       $constraint_vars[] = verify_column_name($_GET['constraint']);
   }
 }

 # Get variables to plot
 if(isset($_GET['variable']))
   $variables = array($_GET['variable']);
 if(isset($_GET['variables']))
 {
   $variables = explode(',', $_GET['variables']);
 }

 // Get legend
 $legend = 'bottom';
 if(isset($_GET['legend']) && $_GET['legend'] != '')
   $legend = $_GET['legend'];

 $stmt = $dbh->prepare("SELECT * FROM $module_table WHERE $timestamp_var >= :timestamp_begin AND $timestamp_var <= :timestamp_end $where_clause ORDER BY $timestamp_var");
 $stmt->bindParam(':timestamp_begin', $timestamp0);
 $stmt->bindParam(':timestamp_end', $timestamp1);
 if(isset($constraint_value))
   $stmt->bindParam(':constraint_value', $constraint_value);

 # create empty arrays 
 $array["values"] = array();
 $array["timestamps"] = array();
 foreach($variables as $variable)
   $array["values"][$variable] = array();
 
 # fill arrays with data if available
 $stmt->execute();
 $avail_columns = array();
 for($i = 0; $i < $stmt->columnCount(); ++$i)
 {
   $meta = $stmt->getColumnMeta($i);
   if($meta['native_type'] == 'integer' || $meta['native_type'] == 'float' || $meta['native_type'] == 'double')
     $avail_columns[] = $meta['name'];
 }

 $meval = new EvalMath;
 $meval->silent_errors = true;
 while($data = $stmt->fetch())
 {
   // Check constraints
   $c_array = array();
   $constrained = false;
   foreach($constraint_vars as $constraint_var)
   {
     if(isset($constraint_values[$constraint_var]))
       if(array_search($data[$constraint_var], $constraint_values[$constraint_var]) === false)
         { $constrained = true; break; }
     $c_array[] = $data[$constraint_var];
   }

   if($constrained) continue;
   $c_string = implode(',', $c_array);

   foreach($avail_columns as $column)
     $meval->Evaluate($column . '=' . $data[$column]);

   // Add data for each variable
   foreach($variables as $variable)
   {
     if(!isset($array["values"][$variable][$c_string]))
       $array["values"][$variable][$c_string] = array();
     $value = $meval->Evaluate($variable);
     if($value !== false)
       $array["values"][$variable][$c_string][] = $value;
     else
       $array["values"][$variable][$c_string][] = -1; // value not set...
   }

   $date = date( "d. M, H:i",$data[$timestamp_var]);
   if(count($array['timestamps']) == 0 || $array['timestamps'][count($array['timestamps'])-1] != $date)
     $array["timestamps"][] = $date;<commit_msg>Plotter: Don't die when values are not available in database<commit_after><?php

 # This parses $_GET parameters to obtain timerange to plot
include('plot_common.php');
include('evalmath.class.php');

 #=====================================
 # DataBase Initialisation
 # connect to SQLite database
 $dbh = new PDO("sqlite:HappyFace.db");

 // Quick'n'dirty check to avoid SQL injection attacks on column names.
 // Unfortunately preparated statements do not work on table or column names
 // with SQLite.
 function verify_column_name($name)
 {
   for($i = 0; $i < count($name); ++$i)
     if(!ctype_alnum($name[$i]) && $name[$i] != '_')
       { echo "Invalid column name: $name"; die; }
   return $name;
 }

 // from http://www.actionscript.org/forums/archive/index.php3/t-50746.html
 function HSV_TO_RGB($H, $S, $V) // HSV Values: Number 0-1
 {                               // RGB Results: Number 0-255
   $RGB = array();

   if($S == 0)
   {
     $R = $G = $B = $V * 255;
   }
   else
   {
     $var_H = $H * 6;
     $var_i = floor( $var_H );
     $var_1 = $V * ( 1 - $S );
     $var_2 = $V * ( 1 - $S * ( $var_H - $var_i ) );
     $var_3 = $V * ( 1 - $S * (1 - ( $var_H - $var_i ) ) );

     if ($var_i == 0) { $var_R = $V ; $var_G = $var_3 ; $var_B = $var_1 ; }
     else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $V ; $var_B = $var_1 ; }
     else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $V ; $var_B = $var_3 ; }
     else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $V ; }
     else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $V ; }
     else { $var_R = $V ; $var_G = $var_1 ; $var_B = $var_2 ; }

     $R = $var_R * 255;
     $G = $var_G * 255;
     $B = $var_B * 255;
   }

   $RGB['R'] = $R;
   $RGB['G'] = $G;
   $RGB['B'] = $B;

   return $RGB;
 }

 $renormalize = isset($_GET['renormalize']) && intval($_GET['renormalize']) != 0;

 # Get timestamp variable to show
 $timestamp_var = 'timestamp';
 if(isset($_GET['timestamp_var']) && $_GET['timestamp_var'] != '')
   $timestamp_var = verify_column_name($_GET['timestamp_var']);
 $module_table = verify_column_Name($_GET['module'] . '_table');
 if(isset($_GET['subtable']) && $_GET['subtable'] != '')
   $module_table = verify_column_name($_GET['subtable']);

 # Apply additional constraint
 $where_clause = "";
 $constraint_vars = array();
 $constraint_values = array();
 if(isset($_GET['constraint']) && $_GET['constraint'] != '')
 {
   $comp = explode('=', $_GET['constraint'], 2);
   if(count($comp) == 2 && $comp[1] != '')
   {
     $constraint_var = verify_column_name($comp[0]);
     $comp = explode(',', $comp[1]);

     // If constraint is on one column only then just select it via a WHERE clause
     // Otherwise exclude manually later.
     if(count($comp) == 1)
     {
       $constraint_value = $comp[0];
       $where_clause = "AND $constraint_var = :constraint_value";
     }
     else
     {
       $constraint_vars[] = $constraint_var;
       $constraint_values[$constraint_var] = $comp; // constrain this var to the given values
     }
   }
   else
   {
     // make a separate plot for each value of this var but don't constrain
     if(count($comp) == 2)
       $constraint_vars[] = verify_column_name($comp[0]);
     else
       $constraint_vars[] = verify_column_name($_GET['constraint']);
   }
 }

 # Get variables to plot
 if(isset($_GET['variable']))
   $variables = array($_GET['variable']);
 if(isset($_GET['variables']))
 {
   $variables = explode(',', $_GET['variables']);
 }

 // Get legend
 $legend = 'bottom';
 if(isset($_GET['legend']) && $_GET['legend'] != '')
   $legend = $_GET['legend'];

 $stmt = $dbh->prepare("SELECT * FROM $module_table WHERE $timestamp_var >= :timestamp_begin AND $timestamp_var <= :timestamp_end $where_clause ORDER BY $timestamp_var");
 $stmt->bindParam(':timestamp_begin', $timestamp0);
 $stmt->bindParam(':timestamp_end', $timestamp1);
 if(isset($constraint_value))
   $stmt->bindParam(':constraint_value', $constraint_value);

 # create empty arrays 
 $array["values"] = array();
 $array["timestamps"] = array();
 foreach($variables as $variable)
   $array["values"][$variable] = array();
 
 # fill arrays with data if available
 $stmt->execute();
 $avail_columns = array();
 for($i = 0; $i < $stmt->columnCount(); ++$i)
 {
   $meta = $stmt->getColumnMeta($i);
   if($meta['native_type'] == 'integer' || $meta['native_type'] == 'float' || $meta['native_type'] == 'double')
     $avail_columns[] = $meta['name'];
 }

 $meval = new EvalMath;
 $meval->suppress_errors = true;
 while($data = $stmt->fetch())
 {
   // Check constraints
   $c_array = array();
   $constrained = false;
   foreach($constraint_vars as $constraint_var)
   {
     if(isset($constraint_values[$constraint_var]))
       if(array_search($data[$constraint_var], $constraint_values[$constraint_var]) === false)
         { $constrained = true; break; }
     $c_array[] = $data[$constraint_var];
   }

   if($constrained) continue;
   $c_string = implode(',', $c_array);

   foreach($avail_columns as $column)
   {
     if(!isset($data[$column]) || $data[$column] == '')
       $meval->Evaluate($column . '= -1'); // value not available in DB
     else
       $meval->Evaluate($column . '=' . floatval($data[$column]));
   }

   // Add data for each variable
   foreach($variables as $variable)
   {
     if(!isset($array["values"][$variable][$c_string]))
       $array["values"][$variable][$c_string] = array();
     $value = $meval->Evaluate($variable);
     if($value !== false)
       $array["values"][$variable][$c_string][] = $value;
     else
       $array["values"][$variable][$c_string][] = -2; // value not computable (division by zero, ...)
   }

   $date = date( "d. M, H:i",$data[$timestamp_var]);
   if(count($array['timestamps']) == 0 || $array['timestamps'][count($array['timestamps'])-1] != $date)
     $array["timestamps"][] = $date;
{'fraction_non_alphanumeric': 0.2052790999567287, 'fraction_numerical': 0.011683254002596278, 'mean_word_length': 3.4756003098373354, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2092949', 'n_tokens_mistral': 4100, 'n_tokens_neox': 3935, 'n_words': 881}