<commit_before>'use strict';

let angular = require('angular');

module.exports = angular
  .module('spinnaker.applications.read.service', [
    require('exports?"restangular"!imports?_=lodash!restangular'),
    require('../../../core/cluster/cluster.service.js'),
    require('../../../tasks/tasks.read.service.js'),
    require('../../../loadBalancers/loadBalancer.read.service.js'),
    require('../../../loadBalancers/loadBalancer.transformer.js'),
    require('../../../securityGroups/securityGroup.read.service.js'),
    require('../../cache/infrastructureCaches.js'),
    require('../../../scheduler/scheduler.service.js'),
    require('../../../core/delivery/execution.service.js'),
    require('../../../serverGroups/serverGroup.transformer.js'),
  ])
  .factory('applicationReader', function ($q, $log, $window,  $exceptionHandler, $rootScope, Restangular, _, clusterService, tasksReader,
                                          loadBalancerReader, loadBalancerTransformer, securityGroupReader, scheduler,
                                          infrastructureCaches, settings, executionService, serverGroupTransformer) {

    function listApplications(forceRemoteCall) {
      var endpoint = Restangular
        .all('applications')
        .withHttpConfig({cache: infrastructureCaches.applications});

      if (forceRemoteCall) {
        infrastructureCaches.applications.remove(endpoint.getRestangularUrl());
      }

      return endpoint.getList();
    }



    var gateEndpoint = Restangular.withConfig(function(RestangularConfigurer) {

      RestangularConfigurer.addElementTransformer('applications', false, function(application) {

        function refreshApplication() {
          application.refreshing = true;
          application.reloadTasks();
          application.reloadExecutions();
          return getApplication(application.name).then(function (newApplication) {
            deepCopyApplication(application, newApplication);
            application.autoRefreshHandlers.forEach(function (handler) {
              handler.call();
            });
            newApplication = null;
            application.refreshing = false;
          });
        }


        function registerAutoRefreshHandler(method, scope) {
          if (scope.$$destroyed) {
            return;
          }
          application.autoRefreshHandlers.push(method);
          scope.$on('$destroy', function () {
            application.autoRefreshHandlers = application.autoRefreshHandlers.filter((handler) => handler !== method);
          });
        }

        function enableAutoRefresh(scope) {
          let dataLoader = scheduler.subscribe(refreshApplication);
          scope.$on('$destroy', () => dataLoader.dispose());
        }

        function reloadTasks() {
          return tasksReader.listAllTasksForApplication(application.name).then(function(tasks) {
            addTasksToApplication(application, tasks);
            if (!application.tasksLoaded) {
              application.tasksLoaded = true;
              $rootScope.$broadcast('tasks-loaded', application);
            } else {
              $rootScope.$broadcast('tasks-reloaded', application);
            }
          });
        }

        function reloadExecutions() {
          return executionService.getAll(application).then(function(execution) {
            addExecutionsToApplication(application, execution);
            if (!application.executionsLoaded) {
              application.executionsLoaded = true;
              $rootScope.$broadcast('executions-loaded', application);
            } else {
              $rootScope.$broadcast('executions-reloaded', application);
            }
          });
        }


        application.registerAutoRefreshHandler = registerAutoRefreshHandler;
        application.autoRefreshHandlers = [];
        application.refreshImmediately = scheduler.scheduleImmediate;
        application.enableAutoRefresh = enableAutoRefresh;
        application.reloadTasks = reloadTasks;
        application.reloadExecutions = reloadExecutions;

        if (application.fromServer && application.clusters) {
          application.accounts = Object.keys(application.clusters);
        }

        return application;

      });
    });

    function getApplicationEndpoint(application) {
      return gateEndpoint.one('applications', application);
    }

    function addDefaultRegion(application) {
      var fromServerGroups = _.pluck(application.serverGroups, 'region'),
        fromLoadBalancers = _.pluck(application.loadBalancers, 'region'),
        fromSecurityGroups = _.pluck(application.securityGroups, 'region');

      var allRegions = _.union(fromServerGroups, fromLoadBalancers, fromSecurityGroups);

      if (allRegions.length === 1) {
        application.defaultRegion = allRegions[0];
      }
    }

    function addDefaultCredentials(application) {
      var fromServerGroups = _.pluck(application.serverGroups, 'account'),
        fromLoadBalancers = _.pluck(application.loadBalancers, 'account'),
        fromSecurityGroups = _.pluck(application.securityGroups, 'accountName');

      var allCredentials = _.union(fromServerGroups, fromLoadBalancers, fromSecurityGroups);

      if (allCredentials.length === 1) {
        application.defaultCredentials = allCredentials[0];
      }
    }

    function addTasksToApplication(application, tasks) {
      application.tasks = angular.isArray(tasks) ? tasks : [];
      clusterService.addTasksToServerGroups(application);
    }

    function addExecutionsToApplication(application, executions=[]) {
      if (application.executions) {
<commit_msg>distrust empty executions from orca<commit_after>'use strict';

let angular = require('angular');

module.exports = angular
  .module('spinnaker.applications.read.service', [
    require('exports?"restangular"!imports?_=lodash!restangular'),
    require('../../../core/cluster/cluster.service.js'),
    require('../../../tasks/tasks.read.service.js'),
    require('../../../loadBalancers/loadBalancer.read.service.js'),
    require('../../../loadBalancers/loadBalancer.transformer.js'),
    require('../../../securityGroups/securityGroup.read.service.js'),
    require('../../cache/infrastructureCaches.js'),
    require('../../../scheduler/scheduler.service.js'),
    require('../../../core/delivery/execution.service.js'),
    require('../../../serverGroups/serverGroup.transformer.js'),
  ])
  .factory('applicationReader', function ($q, $log, $window,  $exceptionHandler, $rootScope, Restangular, _, clusterService, tasksReader,
                                          loadBalancerReader, loadBalancerTransformer, securityGroupReader, scheduler,
                                          infrastructureCaches, settings, executionService, serverGroupTransformer) {

    function listApplications(forceRemoteCall) {
      var endpoint = Restangular
        .all('applications')
        .withHttpConfig({cache: infrastructureCaches.applications});

      if (forceRemoteCall) {
        infrastructureCaches.applications.remove(endpoint.getRestangularUrl());
      }

      return endpoint.getList();
    }



    var gateEndpoint = Restangular.withConfig(function(RestangularConfigurer) {

      RestangularConfigurer.addElementTransformer('applications', false, function(application) {

        function refreshApplication() {
          application.refreshing = true;
          application.reloadTasks();
          application.reloadExecutions();
          return getApplication(application.name).then(function (newApplication) {
            deepCopyApplication(application, newApplication);
            application.autoRefreshHandlers.forEach(function (handler) {
              handler.call();
            });
            newApplication = null;
            application.refreshing = false;
          });
        }


        function registerAutoRefreshHandler(method, scope) {
          if (scope.$$destroyed) {
            return;
          }
          application.autoRefreshHandlers.push(method);
          scope.$on('$destroy', function () {
            application.autoRefreshHandlers = application.autoRefreshHandlers.filter((handler) => handler !== method);
          });
        }

        function enableAutoRefresh(scope) {
          let dataLoader = scheduler.subscribe(refreshApplication);
          scope.$on('$destroy', () => dataLoader.dispose());
        }

        function reloadTasks() {
          return tasksReader.listAllTasksForApplication(application.name).then(function(tasks) {
            addTasksToApplication(application, tasks);
            if (!application.tasksLoaded) {
              application.tasksLoaded = true;
              $rootScope.$broadcast('tasks-loaded', application);
            } else {
              $rootScope.$broadcast('tasks-reloaded', application);
            }
          });
        }

        function reloadExecutions() {
          return executionService.getAll(application).then(function(executions) {
            addExecutionsToApplication(application, executions);
            if (!application.executionsLoaded) {
              application.executionsLoaded = true;
              $rootScope.$broadcast('executions-loaded', application);
            } else {
              $rootScope.$broadcast('executions-reloaded', application);
            }
          });
        }


        application.registerAutoRefreshHandler = registerAutoRefreshHandler;
        application.autoRefreshHandlers = [];
        application.refreshImmediately = scheduler.scheduleImmediate;
        application.enableAutoRefresh = enableAutoRefresh;
        application.reloadTasks = reloadTasks;
        application.reloadExecutions = reloadExecutions;

        if (application.fromServer && application.clusters) {
          application.accounts = Object.keys(application.clusters);
        }

        return application;

      });
    });

    function getApplicationEndpoint(application) {
      return gateEndpoint.one('applications', application);
    }

    function addDefaultRegion(application) {
      var fromServerGroups = _.pluck(application.serverGroups, 'region'),
        fromLoadBalancers = _.pluck(application.loadBalancers, 'region'),
        fromSecurityGroups = _.pluck(application.securityGroups, 'region');

      var allRegions = _.union(fromServerGroups, fromLoadBalancers, fromSecurityGroups);

      if (allRegions.length === 1) {
        application.defaultRegion = allRegions[0];
      }
    }

    function addDefaultCredentials(application) {
      var fromServerGroups = _.pluck(application.serverGroups, 'account'),
        fromLoadBalancers = _.pluck(application.loadBalancers, 'account'),
        fromSecurityGroups = _.pluck(application.securityGroups, 'accountName');

      var allCredentials = _.union(fromServerGroups, fromLoadBalancers, fromSecurityGroups);

      if (allCredentials.length === 1) {
        application.defaultCredentials = allCredentials[0];
      }
    }

    function addTasksToApplication(application, tasks) {
      application.tasks = angular.isArray(tasks) ? tasks : [];
      clusterService.addTasksToServerGroups(application);
    }

    function addExecutionsToApplication(application, executions=[]) {
      // only add executions if we actually got some executions back
      // this will fail if there was just one execution and someone just deleted it
      // but that is much less likely at this point than orca falling over under load,
      // resulting in an empty list of executions coming back
      if (application.executions && application.executions.length && executions.length) {

{'fraction_non_alphanumeric': 0.11732324101152589, 'fraction_numerical': 0.0006881128505074832, 'mean_word_length': 2.7848307291666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7251620', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2750, 'n_words': 458}