<commit_before>'use strict';

angular.module('deckApp')
  .factory('serverGroupConfigurationService', function(imageService, accountService, securityGroupService,
                                                       instanceTypeService,
                                                       $q, subnetReader, keyPairsReader, loadBalancerReader) {


    function configureCommand(application, command) {
      var imageLoader;
      if (command.viewState.disableImageSelection) {
        imageLoader = $q.when(null);
      } else {
        imageLoader = command.viewState.imageId ? loadImagesFromAmi(command) : loadImagesFromApplicationName(application, command.selectedProvider);
      }

      return $q.all({
        regionsKeyedByAccount: accountService.getRegionsKeyedByAccount(),
        securityGroups: securityGroupService.getAllSecurityGroups(),
        loadBalancers: loadBalancerReader.listAWSLoadBalancers(),
        subnets: subnetReader.listSubnets(),
        preferredZones: accountService.getPreferredZonesByAccount(),
        keyPairs: keyPairsReader.listKeyPairs(),
        packageImages: imageLoader,
        instanceTypes: instanceTypeService.getAllTypesByRegion(),
      }).then(function(loader) {
        loader.accounts = _.keys(loader.regionsKeyedByAccount);
        loader.filtered = {};
        command.backingData = loader;
        attachEventHandlers(command);
      });
    }

    function loadImagesFromApplicationName(application, provider) {
      return imageService.findImages({
        provider: provider,
        q: application.name
      });
    }

    function loadImagesFromAmi(command) {
      return imageService.getAmi(command.selectedProvider, command.viewState.imageId, command.region, command.credentials).then(
        function (namedImage) {
          command.amiName = namedImage.imageName;

          var packageRegex = /((nf(lx)?-)?\w+)-?\w+/;
          var match = packageRegex.exec(namedImage.imageName);
          var packageBase = match[1];
          if (!packageBase || packageBase.length < 3) {
            return [namedImage];
          }

          return imageService.findImages({
            provider: command.selectedProvider,
            q: packageBase
          });
        },
        function() {
          return [];
        }
      );
    }

    function configureKeyPairs(command) {
      command.backingData.filtered.keyPairs = _(command.backingData.keyPairs)
        .filter({account: command.credentials, region: command.region})
        .pluck('keyName')
        .valueOf();
    }
<commit_msg>avoid NPE when ami lookup fails<commit_after>'use strict';

angular.module('deckApp')
  .factory('serverGroupConfigurationService', function(imageService, accountService, securityGroupService,
                                                       instanceTypeService,
                                                       $q, subnetReader, keyPairsReader, loadBalancerReader) {


    function configureCommand(application, command) {
      var imageLoader;
      if (command.viewState.disableImageSelection) {
        imageLoader = $q.when(null);
      } else {
        imageLoader = command.viewState.imageId ? loadImagesFromAmi(command) : loadImagesFromApplicationName(application, command.selectedProvider);
      }

      return $q.all({
        regionsKeyedByAccount: accountService.getRegionsKeyedByAccount(),
        securityGroups: securityGroupService.getAllSecurityGroups(),
        loadBalancers: loadBalancerReader.listAWSLoadBalancers(),
        subnets: subnetReader.listSubnets(),
        preferredZones: accountService.getPreferredZonesByAccount(),
        keyPairs: keyPairsReader.listKeyPairs(),
        packageImages: imageLoader,
        instanceTypes: instanceTypeService.getAllTypesByRegion(),
      }).then(function(loader) {
        loader.accounts = _.keys(loader.regionsKeyedByAccount);
        loader.filtered = {};
        command.backingData = loader;
        attachEventHandlers(command);
      });
    }

    function loadImagesFromApplicationName(application, provider) {
      return imageService.findImages({
        provider: provider,
        q: application.name
      });
    }

    function loadImagesFromAmi(command) {
      return imageService.getAmi(command.selectedProvider, command.viewState.imageId, command.region, command.credentials).then(
        function (namedImage) {
          if (!namedImage) {
            return [];
          }
          command.amiName = namedImage.imageName;

          var packageRegex = /((nf(lx)?-)?\w+)-?\w+/;
          var match = packageRegex.exec(namedImage.imageName);
          var packageBase = match[1];
          if (!packageBase || packageBase.length < 3) {
            return [namedImage];
          }

          return imageService.findImages({
            provider: command.selectedProvider,
            q: packageBase
          });
        },
        function() {
          return [];
        }
      );
    }

    function configureKeyPairs(command) {
      command.backingData.filtered.keyPairs = _(command.backingData.keyPairs)
        .filter({account: command.credentials, region: command.region})
        .pluck('keyName')
        .valueOf();
    }

{'fraction_non_alphanumeric': 0.10126582278481013, 'fraction_numerical': 0.0007671653241273494, 'mean_word_length': 2.449074074074074, 'pattern_counts': {'":': 0, '<': 5, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1559661', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1310, 'n_words': 218}