<commit_before>app.directive('command', function() { 
	return { 
    	restrict: 'E',  
	    templateUrl:'./directives/command/command.html',
	    controller: 'CommandCtrl',
  	}; 
})

app.controller('CommandCtrl', 
	function($scope, userService, commandService, dashboardService, $interval, $window){

	$scope.email = userService.getUserEmail();
	$scope.mission = dashboardService.getCurrentMission();

    $scope.isLoaded = false;
	$scope.sent = false;

	$scope.initialise = function(){
		$scope.cmd = "";
		$scope.arguments = "";
		$scope.entered =  false;
		$scope.locked = false;
		$scope.disableEnter = false;
		$scope.disableInput = false;
		$scope.disableLock = true;
        $scope.lockModel = "LOCK";

        $scope.command = {
            name : "",
            arguments : "",
            sent_timestamp : "",
            time : ""
        };
	}

    $scope.enter = function(){
    	if($scope.cmd && $scope.arguments) {
			$scope.command.name = $scope.cmd;
		    $scope.command.arguments = $scope.arguments;

            commandService.saveCommand($scope.email, $scope.command, $scope.mission.missionName)
            .then(function(response) {
                if(response.status == 200){
                    $scope.entered = true;
                    $scope.disableEnter = true;
                }
            });
	    } 
    }

    $scope.lockCommand = function(){
    	if($scope.command.name && $scope.entered) {
            if($scope.lockModel == "LOCK"){
                commandService.lockCommand($scope.mission.missionName)
                .then(function(response) {
                    if(response.status == 200){
                        $scope.locked = true;
                        $scope.disableInput = true;
                        $scope.disableLock = true;
                        $scope.lockModel = "UNLOCK";
                    }
                });
            } else {
                commandService.unlockCommand($scope.mission.missionName)
                .then(function(response) {
                    if(response.status == 200){
                        $scope.locked = false;
                        $scope.disableInput = false;
                        $scope.disableLock = false;
                        $scope.lockModel = "LOCK";
                    }
                });
            }
	    } else {
	    	$window.alert("Please enter the commands before locking");
	    }
    }

    $scope.changeInput = function(){
    	if($scope.entered) {
    		$scope.entered = false;
    		$scope.disableEnter = false;
            commandService.removeCommand($scope.mission.missionName)
            .then(function(response) {
                if(response.status == 200){
                    console.log("removed");
                }
            });
    	} else {
    		$scope.disableEnter = false;
    		$scope.disableLock = false;
    	}
    }

    $scope.sendCommand = function(){
    	var time = dashboardService.getTime('UTC');
    	var systemTime = new Date();
    	var cmdId = systemTime.getTime();
        $scope.timestamp = {
            "sent_timestamp" : cmdId,
            "time" : time.utc
        }

        commandService.sendCommand($scope.mission.missionName, $scope.timestamp)
    	.then(function(response) {
	        if(response.status == 200){
	        	$scope.initialise();
                $scope.commandForm.$setPristine();
                $scope.commandForm.$setUntouched();
	        }
	    });
    }

	$scope.updateCommandlog = function(){
        commandService.getCommandLog($scope.mission.missionName)
        .then(function(response) {
            if(response.status == 200) {
                $scope.commandLog = response.data;

                //get all the responses of a command
                //find the response with the max timestamp
                //assign the status and data to a the commandLog variable
                var commandLen = $scope.commandLog.length;

                for(var i=0;i<commandLen;i++){
                    //call function to get the max date;
                    //loop through the responses and find the response which matches that response
                    //assign to a variable used to display in the command log
                    var maxTime = getMaxTime($scope.commandLog[i].response,"gwp_timestamp");
                    var responsesLen = $scope.commandLog[i].response.length;
                    for(var j=0;j<responsesLen;j++){
                        var dateformat = moment($scope.commandLog[i].response[j].gwp_timestamp).tz('UTC');
                        if(dateformat.valueOf() === maxTime){
                            $scope.commandLog[i].responseStatus = $scope.commandLog[i].response[j].status;
                            $scope.commandLog[i].responseData = $scope.commandLog[i].response[j].metadata_data;
                        }
                    }
                }
            }
        });

        commandService.getCommand($scope.mission.missionName)
        .then(function success(response) {
            var command = response.data;
            if(command != ""){
                if(command.entered && !command.sent) {
                    $scope.cmd = command.name;
                    $scope.arguments = command.arguments;
                    $scope.command = {
                        name : command.name,
                        arguments : command.arguments,
                        sent_timestamp : "",
                        time : ""
                    };
                    $scope.entered = true;
                    $scope.disableEnter = true;
                    if(command.locked) {
                        $scope.locked = true;
                        $scope.disableInput = true;
                        $scope.disableLock = true;
                        $scope.lockModel = "UNLOCK"
                    }
                }
            } else {
                if($scope.command.name != "" && $scope.command.arguments != "" && $scope.entered){
                    $scope.initialise();
                    $scope.commandForm.$setPristine();
                    $scope.commandForm.$setUntouched();
                }
            }
        });
    }

    function getMaxTime(responses,filter){
        var timestampArray = [];
        var maxTime;
        var numOfResponses = responses.length;
        for(var j=0;j<numOfResponses;j++){
            var dateformat = moment(responses[j][filter]).tz('UTC');
            timestampArray.push(dateformat.valueOf());
        }
        maxTime = Math.max.apply(null,timestampArray);<commit_msg>update status of buttons in command qwidget<commit_after>app.directive('command', function() { 
	return { 
    	restrict: 'E',  
	    templateUrl:'./directives/command/command.html',
	    controller: 'CommandCtrl',
  	}; 
})

app.controller('CommandCtrl', 
	function($scope, userService, commandService, dashboardService, $interval, $window){

	$scope.email = userService.getUserEmail();
	$scope.mission = dashboardService.getCurrentMission();

    $scope.isLoaded = false;
	$scope.sent = false;

	$scope.initialise = function(){
		$scope.cmd = "";
		$scope.arguments = "";
		$scope.entered =  false;
		$scope.locked = false;
		$scope.disableEnter = false;
		$scope.disableInput = false;
		$scope.disableLock = true;
        $scope.lockModel = "LOCK";

        $scope.command = {
            name : "",
            arguments : "",
            sent_timestamp : "",
            time : ""
        };
	}

    $scope.enter = function(){
    	if($scope.cmd && $scope.arguments) {
			$scope.command.name = $scope.cmd;
		    $scope.command.arguments = $scope.arguments;

            commandService.saveCommand($scope.email, $scope.command, $scope.mission.missionName)
            .then(function(response) {
                if(response.status == 200){
                    $scope.entered = true;
                    $scope.disableEnter = true;
                }
            });
	    } 
    }

    $scope.lockCommand = function(){
    	if($scope.command.name && $scope.entered) {
            if($scope.lockModel == "LOCK"){
                commandService.lockCommand($scope.mission.missionName)
                .then(function(response) {
                    if(response.status == 200){
                        $scope.locked = true;
                        $scope.disableInput = true;
                        $scope.disableLock = true;
                        $scope.lockModel = "UNLOCK";
                    }
                });
            } else {
                commandService.unlockCommand($scope.mission.missionName)
                .then(function(response) {
                    if(response.status == 200){
                        $scope.locked = false;
                        $scope.disableInput = false;
                        $scope.disableLock = false;
                        $scope.lockModel = "LOCK";
                    }
                });
            }
	    } else {
	    	$window.alert("Please enter the commands before locking");
	    }
    }

    $scope.changeInput = function(){
    	if($scope.entered) {
    		$scope.entered = false;
    		$scope.disableEnter = false;
            commandService.removeCommand($scope.mission.missionName)
            .then(function(response) {
                if(response.status == 200){
                    console.log("removed");
                }
            });
    	} else {
    		$scope.disableEnter = false;
    		$scope.disableLock = false;
    	}
    }

    $scope.sendCommand = function(){
    	var time = dashboardService.getTime('UTC');
    	var systemTime = new Date();
    	var cmdId = systemTime.getTime();
        $scope.timestamp = {
            "sent_timestamp" : cmdId,
            "time" : time.utc
        }

        commandService.sendCommand($scope.mission.missionName, $scope.timestamp)
    	.then(function(response) {
	        if(response.status == 200){
	        	$scope.initialise();
                $scope.commandForm.$setPristine();
                $scope.commandForm.$setUntouched();
	        }
	    });
    }

	$scope.updateCommandlog = function(){
        commandService.getCommandLog($scope.mission.missionName)
        .then(function(response) {
            if(response.status == 200) {
                $scope.commandLog = response.data;

                //get all the responses of a command
                //find the response with the max timestamp
                //assign the status and data to a the commandLog variable
                var commandLen = $scope.commandLog.length;

                for(var i=0;i<commandLen;i++){
                    //call function to get the max date;
                    //loop through the responses and find the response which matches that response
                    //assign to a variable used to display in the command log
                    var maxTime = getMaxTime($scope.commandLog[i].response,"gwp_timestamp");
                    var responsesLen = $scope.commandLog[i].response.length;
                    for(var j=0;j<responsesLen;j++){
                        var dateformat = moment($scope.commandLog[i].response[j].gwp_timestamp).tz('UTC');
                        if(dateformat.valueOf() === maxTime){
                            $scope.commandLog[i].responseStatus = $scope.commandLog[i].response[j].status;
                            $scope.commandLog[i].responseData = $scope.commandLog[i].response[j].metadata_data;
                        }
                    }
                }
            }
        });

        commandService.getCommand($scope.mission.missionName)
        .then(function success(response) {
            var command = response.data;
            if(command != ""){
                if(command.entered && !command.sent) {
                    $scope.cmd = command.name;
                    $scope.arguments = command.arguments;
                    $scope.command = {
                        name : command.name,
                        arguments : command.arguments,
                        sent_timestamp : "",
                        time : ""
                    };
                    $scope.entered = true;
                    $scope.disableEnter = true;

                    if(command.locked) {
                        $scope.locked = true;
                        $scope.disableInput = true;
                        $scope.disableLock = true;
                        $scope.lockModel = "UNLOCK"
                    } else {
                        $scope.lockModel = "LOCK";
                        $scope.locked = false;
                    }
                }
            } else {
                if($scope.command.name != "" && $scope.command.arguments != "" && $scope.entered){
                    $scope.initialise();
                    $scope.commandForm.$setPristine();
                    $scope.commandForm.$setUntouched();
                }
            }
        });
    }

    function getMaxTime(responses,filter){
        var timestampArray = [];
        var maxTime;
        var numOfResponses = responses.length;
        for(var j=0;j<numOfResponses;j++){
            var dateformat = moment(responses[j][filter]).tz('UTC');
            timestampArray.push(dateformat.valueOf());
        }
        maxTime = Math.max.apply(null,timestampArray);
{'fraction_non_alphanumeric': 0.12, 'fraction_numerical': 0.003157894736842105, 'mean_word_length': 1.465430954587581, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '1343591', 'n_tokens_mistral': 3581, 'n_tokens_neox': 3302, 'n_words': 576}