<commit_before><?php namespace App\Http\Controllers;

use DB;
use Input;
use App\Variable;
use App\TimeType;
use App\Datasource;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class DataController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index() {
		return "Controller for data";
	}

	public function dimensions( Request $request ) {

		$data = array();
		$dataByVariable = array();
		$dataByEntity = array();
		$dataByEntityTime = array();

		//extra array for storing values for export
		$times = array();
		$entities = array();
		$datasourcesIdsArr = array();

		if( !Input::has( 'dimensions' ) ) {
			return false;
		}

		$dimensionsInput = Input::get( 'dimensions' );
		$dimensions = json_decode( $dimensionsInput );

		$chartType = Input::get( 'chartType' );
		//there's special setting for linechart
		$isLineChart = ( $chartType == "1" )? true: false;

		//find out how many variables we have 
		$groupByEntity = true;
		//$groupByEntity = ( count( $dimensions ) > 1 )? false: true;

		$timeType = '';

		foreach( $dimensions as $dimension ) {
			
			$id = $dimension->variableId;
			$property = $dimension->property;

			//use query builder instead of eloquent
			$variableData = DB::table( 'data_values' )
				->join( 'entities', 'data_values.fk_ent_id', '=', 'entities.id' )
				->join( 'times', 'data_values.fk_time_id', '=', 'times.id' )
				//->join( 'variables', 'data_values.fk_var_id', '=', 'variables.id' )
				->where( 'data_values.fk_var_id', $id )
				->get();

			if( $groupByEntity ) {
			
				//group variable data by entities
				$i = 0;
				$oldEntityId = -1;
				foreach( $variableData as $datum ) {

					$entityId = $datum->fk_ent_id;
					//check if new entity and we need to reset cycle
					if( $oldEntityId != $entityId ) {
						$i = 0;
					}
					$oldEntityId = $entityId;
					
					//do we have already object for that entity
					if( !array_key_exists($entityId, $dataByEntity) ) {
						$dataByEntity[ $entityId ] = array( 
							"id" => intval($entityId),
							"key" => $datum->name,
							"values" => []
						);
					}

					//$dataByEntity[ $entityId ][ "values" ][] = array( "x" => floatval($datum->date), "y" => floatval($datum->value) );
					
					//is it first property being saved for given property
					if( !array_key_exists( $i, $dataByEntity[ $entityId ][ "values" ] ) ) {
						$dataByEntity[ $entityId ][ "values" ][ $i ] = [];// "x" => floatval($datum->label), "y" => floatval($datum->value) ];
					}
					//store value
					$dataByEntity[ $entityId ][ "values" ][ $i ][ $property ] = floatval( $datum->value );
					//if is linechart, store 
					if( $isLineChart ) {
						$dataByEntity[ $entityId ][ "values" ][ $i ][ "x" ] = floatval( $datum->label );
					}
					$i++;

					//store time type if not stored
					if( empty( $timeType ) ) {
						$timeType = TimeType::find( $datum->fk_ttype_id )->name; 
					}

					//store for the need of export 
					if( !array_key_exists($entityId, $dataByEntityTime) ) {
						$dataByEntityTime[ $entityId ] = [];
						$entities[ $entityId ] = $datum->name; 
					}
					$dataByEntityTime[ $entityId ][ $datum->label ] = $datum->value;
					$times[ $datum->label ] = true;
					$datasourcesIdsArr[ $datum->fk_dsr_id ] = true;

					//more complicated case for scatter plot and else
					//do we have already array for that value
					/*if( !array_key_exists( $i, $dataByEntity[ $entityId ][ "values" ] ) ) {
						$dataByEntity[ $entityId ][ "values" ][ $i ] = [ "x" => floatval($datum->label), "y" => floatval($datum->value) ];
					}
					$i++;*/
					/*$values = $data[ $entityId ][ "values" ][ $i ];
					$values[ $property ] = $datum->value;
					$data[ $entityId ][ "values" ][ $i ] = $values;*/

				}

			} else {

				//multivariables
				$dataByVariable[ "id-".$id ] = array( 
					"id" => $id,
					"key" => $dimension->variableId,
					"values" => []
				);

				foreach( $variableData as $datum ) {
					$dataByVariable[ "id-".$id ][ "values" ][] = array( "x" => floatval($datum->date), "y" => floatval($datum->value) );
					$times[$datum->label] = true;
				}

			}

		}
		
		if( $groupByEntity ) {
			//convert to array
			foreach( $dataByEntity as $entityData ) {
				$data[] = $entityData;
			}
		} else {
			//convert to array
			foreach( $dataByVariable as $varData ) {
				$data[] = $varData;
			}
		}

		//get all necessary info for datasources
		$datasourcesIds = array_keys( $datasourcesIdsArr );
		$datasources = Datasource::findMany( $datasourcesIds );

		if( $request->ajax() ) {

			return [ 'success' => true, 'data' => $data, 'datasources' => $datasources, 'timeType' => $timeType ];
<commit_msg>display source information properly for multivariant<commit_after><?php namespace App\Http\Controllers;

use DB;
use Input;
use App\Variable;
use App\TimeType;
use App\Datasource;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class DataController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index() {
		return "Controller for data";
	}

	public function dimensions( Request $request ) {

		$data = array();
		$dataByVariable = array();
		$dataByEntity = array();
		$dataByEntityTime = array();

		//extra array for storing values for export
		$times = array();
		$entities = array();
		$datasourcesIdsArr = array();

		if( !Input::has( 'dimensions' ) ) {
			return false;
		}

		$dimensionsInput = Input::get( 'dimensions' );
		$dimensions = json_decode( $dimensionsInput );

		$chartType = Input::get( 'chartType' );
		//there's special setting for linechart
		$isLineChart = ( $chartType == "1" )? true: false;

		//find out how many variables we have 
		$groupByEntity = (!$isLineChart || count( $dimensions ) == 1)? true: false;

		$timeType = '';

		foreach( $dimensions as $dimension ) {
			
			$id = $dimension->variableId;
			$property = $dimension->property;

			//use query builder instead of eloquent
			$variableData = DB::table( 'data_values' )
				->join( 'entities', 'data_values.fk_ent_id', '=', 'entities.id' )
				->join( 'times', 'data_values.fk_time_id', '=', 'times.id' )
				//->join( 'variables', 'data_values.fk_var_id', '=', 'variables.id' )
				->where( 'data_values.fk_var_id', $id )
				->get();

			if( $groupByEntity ) {
			
				//group variable data by entities
				$i = 0;
				$oldEntityId = -1;
				foreach( $variableData as $datum ) {

					$entityId = $datum->fk_ent_id;
					//check if new entity and we need to reset cycle
					if( $oldEntityId != $entityId ) {
						$i = 0;
					}
					$oldEntityId = $entityId;
					
					//do we have already object for that entity
					if( !array_key_exists($entityId, $dataByEntity) ) {
						$dataByEntity[ $entityId ] = array( 
							"id" => intval($entityId),
							"key" => $datum->name,
							"values" => []
						);
					}

					//$dataByEntity[ $entityId ][ "values" ][] = array( "x" => floatval($datum->date), "y" => floatval($datum->value) );
					
					//is it first property being saved for given property
					if( !array_key_exists( $i, $dataByEntity[ $entityId ][ "values" ] ) ) {
						$dataByEntity[ $entityId ][ "values" ][ $i ] = [];// "x" => floatval($datum->label), "y" => floatval($datum->value) ];
					}
					//store value
					$dataByEntity[ $entityId ][ "values" ][ $i ][ $property ] = floatval( $datum->value );
					//if is linechart, store 
					if( $isLineChart ) {
						$dataByEntity[ $entityId ][ "values" ][ $i ][ "x" ] = floatval( $datum->date );
					}
					$i++;

					//store time type if not stored
					if( empty( $timeType ) ) {
						$timeType = TimeType::find( $datum->fk_ttype_id )->name; 
					}

					//store for the need of export 
					if( !array_key_exists($entityId, $dataByEntityTime) ) {
						$dataByEntityTime[ $entityId ] = [];
						$entities[ $entityId ] = $datum->name; 
					}
					$dataByEntityTime[ $entityId ][ $datum->label ] = $datum->value;
					$times[ $datum->label ] = true;
					$datasourcesIdsArr[ $datum->fk_dsr_id ] = true;

					//more complicated case for scatter plot and else
					//do we have already array for that value
					/*if( !array_key_exists( $i, $dataByEntity[ $entityId ][ "values" ] ) ) {
						$dataByEntity[ $entityId ][ "values" ][ $i ] = [ "x" => floatval($datum->label), "y" => floatval($datum->value) ];
					}
					$i++;*/
					/*$values = $data[ $entityId ][ "values" ][ $i ];
					$values[ $property ] = $datum->value;
					$data[ $entityId ][ "values" ][ $i ] = $values;*/

				}

			} else {

				//multivariables
				$dataByVariable[ "id-".$id ] = array( 
					"id" => $id,
					"key" => $dimension->variableId,
					"values" => []
				);

				foreach( $variableData as $datum ) {
					$dataByVariable[ "id-".$id ][ "values" ][] = array( "x" => floatval($datum->date), "y" => floatval($datum->value) );
					$times[$datum->label] = true;
					$datasourcesIdsArr[ $datum->fk_dsr_id ] = true;
				}

			}

		}
		
		if( $groupByEntity ) {
			//convert to array
			foreach( $dataByEntity as $entityData ) {
				$data[] = $entityData;
			}
		} else {
			//convert to array
			foreach( $dataByVariable as $varData ) {
				$data[] = $varData;
			}
		}

		//get all necessary info for datasources
		$datasourcesIds = array_keys( $datasourcesIdsArr );
		$datasources = Datasource::findMany( $datasourcesIds );

		if( $request->ajax() ) {

			return [ 'success' => true, 'data' => $data, 'datasources' => $datasources, 'timeType' => $timeType ];

{'fraction_non_alphanumeric': 0.1774900814366256, 'fraction_numerical': 0.0010440593025683859, 'mean_word_length': 2.919394435351882, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 101, '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': '7234790', 'n_tokens_mistral': 4117, 'n_tokens_neox': 3326, 'n_words': 793}