<commit_before>'use strict';


// Controller for a single application
rcfApp.controller( 'appController', function( $scope, $rootScope, $route, $routeParams, Restangular ) {
	$scope.rootUrl = $rootScope.restUrl;
	$scope.noError = true;
	$scope.invoked = false;
	$scope.appName = $routeParams.appName;
	$scope.template = '';
	Restangular.setBaseUrl( $rootScope.restUrl );
	

	
	// Sort and format instances
	$scope.buildInstancesGraph = function( instances ) {
		var rootNodes = [];
		if( instances ) {
			
			var currentParentNode;
			var lastInstancePathLength = 1;
			
			for( var index = 0; index < instances.length; ++index ) {
				
				// Create the current node and see the instance path's length
				var currentNode = { instance: instances[ index ], children: []};
				instances[ index ].treeNode = currentNode;
				var pathLength = $scope.findPosition( currentNode.instance.path );
				
				// New root instance
				if( pathLength == 1 ) {
					currentParentNode = currentNode;
					rootNodes.push( currentNode );	
				}
				
				// The child is at the same level than the previous element.
				// Do we have to update the parent?
				else if( lastInstancePathLength === pathLength ) {
					if( $scope.findPosition( currentParentNode.instance.path ) === pathLength )
						currentParentNode = currentParentNode.parent;
					
					currentNode.parent = currentParentNode;
					currentParentNode.children.push( currentNode );
				}
				
				// If the child is at the same or the previous level, restore the parent.
				// The previous node does not have any child.
				else if( lastInstancePathLength > pathLength ) {
					currentParentNode = currentParentNode.parent;
					currentNode.parent = currentParentNode;
					currentParentNode.children.push( currentNode );
				}
				
				// If the child is a level after, change the parent
				else if( lastInstancePathLength < pathLength ) {
					currentNode.parent = currentParentNode;
					currentParentNode.children.push( currentNode );
					currentParentNode = currentNode;
				}
				
				// Remember the last path length
				lastInstancePathLength = pathLength;
			}
		}
		
		return rootNodes;
	};
	
	
	// Perform an action on a given instances
	$scope.changeState = function( newState ) {
		var path = 'app/' + $scope.appName + "/change-state?new-state=" + newState + "&instance-path=" + $scope.selectedInstance.path;
		Restangular.one( path ).post();
		$scope.selectedInstance.status = 'CUSTOM';
    };
    
    $scope.performAll = function( action, useInstance ) {
    	var path = 'app/' + $scope.appName + "/" + action;
    	if( useInstance )
    		path += "?instance-path=" + $scope.selectedInstance.path;
    	
    	Restangular.one( path ).post().then( function() {
    		$scope.loadInstances();
    	});
    	
    	if( useInstance )
    		$scope.selectedInstance.status = 'CUSTOM';
    };
    
    // Regularly poll the server
    // FIXME: shouldn't we stop the timeout at some time?
    $scope.updateFromServer = function() {<commit_msg>Fix a bug with instance hierarchy<commit_after>'use strict';


// Controller for a single application
rcfApp.controller( 'appController', function( $scope, $rootScope, $route, $routeParams, Restangular ) {
	$scope.rootUrl = $rootScope.restUrl;
	$scope.noError = true;
	$scope.invoked = false;
	$scope.appName = $routeParams.appName;
	$scope.template = '';
	Restangular.setBaseUrl( $rootScope.restUrl );
	

	
	// Sort and format instances
	$scope.buildInstancesGraph = function( instances ) {
		var rootNodes = [];
		if( instances ) {
			
			var lastProcessedInstance = null;
			for( var index = 0; index < instances.length; ++index ) {
				
				// Create the current node and see the instance path's length
				var currentNode = { instance: instances[ index ], children: []};
				instances[ index ].treeNode = currentNode;
				var pathLength = $scope.findPosition( currentNode.instance.path );
				
				// New root instance
				if( pathLength == 1 ) {
					rootNodes.push( currentNode );	
				}
				
				// Otherwise, count the segments.
				// Predicate: levels are incremented one by one, but can be decremented from several units at once.
				else {
					var sameSegmentsCount = 0;
					var old = lastProcessedInstance.instance.path.split( "/" );
					var curr = currentNode.instance.path.split( "/" );
					for( var i=0; i<old.length; i++ ) {
						if( old[ i ] === curr[ i ])
							sameSegmentsCount ++;
						else 
							break;
					}
					
					// Case: the current node is a (direct) child of the previous node.
					// Indirect child does not make sense in the Roboconf scope.
					if( sameSegmentsCount === old.length ) {
						currentNode.parent = lastProcessedInstance;
						lastProcessedInstance.children.push( currentNode );
					}
					
					// Case: sameSegmentsCount < old.length => there are common ancestors...
					else {
						var parentNode = lastProcessedInstance;
						for( var i=0; i < old.length - sameSegmentsCount; i++ )
							parentNode = parentNode.parent;
						
						currentNode.parent = parentNode;
						parentNode.children.push( currentNode );
					}
				}
				
				// Prepare the next iteration
				lastProcessedInstance = currentNode;
			}
		}
		
		return rootNodes;
	};
	
	
	// Perform an action on a given instances
	$scope.changeState = function( newState ) {
		var path = 'app/' + $scope.appName + "/change-state?new-state=" + newState + "&instance-path=" + $scope.selectedInstance.path;
		Restangular.one( path ).post();
		$scope.selectedInstance.status = 'CUSTOM';
    };
    
    $scope.performAll = function( action, useInstance ) {
    	var path = 'app/' + $scope.appName + "/" + action;
    	if( useInstance )
    		path += "?instance-path=" + $scope.selectedInstance.path;
    	
    	Restangular.one( path ).post().then( function() {
    		$scope.loadInstances();
    	});
    	
    	if( useInstance )
    		$scope.selectedInstance.status = 'CUSTOM';
    };
    
    // Regularly poll the server
    // FIXME: shouldn't we stop the timeout at some time?
    $scope.updateFromServer = function() {
{'fraction_non_alphanumeric': 0.11799702528507684, 'fraction_numerical': 0.0013220955214014213, 'mean_word_length': 2.832805573147562, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '4710981', 'n_tokens_mistral': 2157, 'n_tokens_neox': 1775, 'n_words': 486}