<commit_before>'use strict';

/**
 * TasksController
 * controller for the view of tasks
 */
tasksModule.controller('TasksController',
    ['$scope', '$rootScope', '$state', '$stateParams', '$timeout', '$filter', '$http', 'fileSystemService', 'tasksService', '$cookies', '$interval',
        function ($scope, $rootScope, $state, $stateParams, $timeout, $filter, $http, fileSystemService, tasksService, $cookies, $interval) {

            console.log("tasks!");


            $scope.myMessages = [];
            $scope.myKeys = [];
            $scope.validTasks = {"no": "...not loaded..."};
            $scope.availableContainers = undefined;

            $scope.newTaskDefinition = {
                "type": $stateParams.task,
                "container": $stateParams.container,
                "tenant": $cookies.get('MCM-TENANT'),
                "token": $cookies.get('XSRF-TOKEN')
            };
            $scope.credentials = {
                "tenant": $cookies.get('MCM-TENANT'),
                "token": $cookies.get('XSRF-TOKEN')
            };

            /**
             *
             * Get the list of valid tasks
             *
             * */
            tasksService.getValidTasks()
                .then(function (response) {
                    $scope.validTasks = response.data;
                });


            /**
             *
             * Get the list of container from swift
             *
             * */

            fileSystemService.getContainers("", "", 10000)
                .then(function (response) {
                    $scope.availableContainers = response.containers;
                });




            /**
             *
             * send a new message
             *
             * */

            $scope.sendMessage = function () {
                tasksService.postMessage($scope.newTaskDefinition)
                    .then(function (response) {
                        $rootScope.$broadcast('FlashMessage', {
                            "type": "success",
                            "text": "message sent"
                        });
                        //console.log($scope.availableContainers);
                    })
            };
            /**
            helper function to add response data to messages
            */
            var addMsgs= function (resp) {
                var idx=$scope.myKeys.indexOf(resp.correlation);
                if(idx==-1)
                {
                    $scope.myKeys.unshift(resp.correlation);
                    $scope.myMessages.unshift([resp]);
                }
                else{
                    $scope.myMessages[idx].push(resp);
                }
		     };
            /**
             *
             * receive the messages
             *
             * */
            $scope.receive_from_beginning = function () {
                tasksService.retrieveMessages($scope.credentials, true)
                    .then(function (response) {
                        for (var i = 0; i < response.data.length; i++) {
                            addMsgs(response.data[i]);
                        }
                    })
            };

            var receive = function () {
                tasksService.retrieveMessages($scope.credentials, false)
                    .then(function (response) {
                        if (response.data) {
                            for (var i = 0; i < response.data.length; i++) {
                               addMsgs(response.data[i]);
                            }
                            $interval(function () {
                                receive();
                            }, 2000, 1);
                        }
                    })
            };
            receive();


            /**
             *
             * clear the existing messages
             *
             * */
            $scope.clear_all_messages = function () {
                $scope.myMessages = {};
            };


            /**
             *<commit_msg>fixed bug when clearing tasks list and loading new tasks<commit_after>'use strict';

/**
 * TasksController
 * controller for the view of tasks
 */
tasksModule.controller('TasksController',
    ['$scope', '$rootScope', '$state', '$stateParams', '$timeout', '$filter', '$http', 'fileSystemService', 'tasksService', '$cookies', '$interval',
        function ($scope, $rootScope, $state, $stateParams, $timeout, $filter, $http, fileSystemService, tasksService, $cookies, $interval) {

            console.log("tasks!");


            $scope.myMessages = [];
            $scope.myKeys = [];
            $scope.validTasks = {"no": "...not loaded..."};
            $scope.availableContainers = undefined;

            $scope.newTaskDefinition = {
                "type": $stateParams.task,
                "container": $stateParams.container,
                "tenant": $cookies.get('MCM-TENANT'),
                "token": $cookies.get('XSRF-TOKEN')
            };
            $scope.credentials = {
                "tenant": $cookies.get('MCM-TENANT'),
                "token": $cookies.get('XSRF-TOKEN')
            };

            /**
             *
             * Get the list of valid tasks
             *
             * */
            tasksService.getValidTasks()
                .then(function (response) {
                    $scope.validTasks = response.data;
                });


            /**
             *
             * Get the list of container from swift
             *
             * */

            fileSystemService.getContainers("", "", 10000)
                .then(function (response) {
                    $scope.availableContainers = response.containers;
                });




            /**
             *
             * send a new message
             *
             * */

            $scope.sendMessage = function () {
                tasksService.postMessage($scope.newTaskDefinition)
                    .then(function (response) {
                        $rootScope.$broadcast('FlashMessage', {
                            "type": "success",
                            "text": "message sent"
                        });
                        //console.log($scope.availableContainers);
                    })
            };
            /**
            helper function to add response data to messages
            */
            var addMsgs= function (resp) {
                var idx=$scope.myKeys.indexOf(resp.correlation);
                if(idx==-1)
                {
                    $scope.myKeys.unshift(resp.correlation);
                    $scope.myMessages.unshift([resp]);
                }
                else{
                    $scope.myMessages[idx].push(resp);
                }
		     };
            /**
             *
             * receive the messages
             *
             * */
            $scope.receive_from_beginning = function () {
                tasksService.retrieveMessages($scope.credentials, true)
                    .then(function (response) {
                        $scope.clear_all_messages();
                        for (var i = 0; i < response.data.length; i++) {
                            addMsgs(response.data[i]);
                        }
                    })
            };

            var receive = function () {
                tasksService.retrieveMessages($scope.credentials, false)
                    .then(function (response) {
                        if (response.data) {
                            for (var i = 0; i < response.data.length; i++) {
                               addMsgs(response.data[i]);
                            }
                            $interval(function () {
                                receive();
                            }, 2000, 1);
                        }
                    })
            };
            receive();


            /**
             *
             * clear the existing messages
             *
             * */
            $scope.clear_all_messages = function () {
                $scope.myMessages = [];
                $scope.myKeys = [];
            };


            /**
             *
{'fraction_non_alphanumeric': 0.1210448859455482, 'fraction_numerical': 0.003188619082658818, 'mean_word_length': 1.0245779543197617, 'pattern_counts': {'":': 18, '<': 7, '<?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': '525354', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1839, 'n_words': 333}