<commit_before>/*
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

'use strict';

var _ = require('lodash');


/*
 * post process to hook through SGs not direct to images if sgs exist
 * currenly assumes just 1 or 0 security groups
 */
module.exports = function postProcessing (system) {

  function handleAutoScalingGroups (result) {
    var containers = result.topology.containers;
    var isAutoscaling = function(container) { return container.type === 'aws-autoscaling'; };
    var autoScalingGroups = _.filter(containers,  isAutoscaling);

    _.forEach(autoScalingGroups, function (cont) {
      var newContains = [];
      var sysContainers = system.topology.containers;
      var sysAwsAutoScalings = _.filter(sysContainers, isAutoscaling);

      _.forEach(sysAwsAutoScalings, function (sysAwsAutoScaling) {
        var sysAwsAutoScalingParent = sysContainers[sysAwsAutoScaling.containedBy];

        // if there is a security group container in the system we should
        // add it also in the analyzer result
        // ie: system contains   elb -> sg -> autoscaling
        //     analyzer returns  elb -> autoscaling
        //
        if (sysAwsAutoScalingParent.type === 'aws-sg') {
          var awsAutoScaling = containers[sysAwsAutoScaling.id];
          if (awsAutoScaling) {
            var awsAutoScalingParent = containers[awsAutoScaling.containedBy];

            var awsSg = containers[sysAwsAutoScalingParent.id];

            // Remove AutoScaling child
            _.remove(awsAutoScalingParent.contains, function (ele) {
              return ele === awsAutoScaling.id;
            });

            awsAutoScalingParent.contains.push(sysAwsAutoScalingParent.id);
            awsAutoScaling.containedBy = sysAwsAutoScalingParent.id;
            awsSg.containedBy = awsAutoScalingParent.id;
            awsSg.contains = _(awsSg.contains).push(awsAutoScaling.id).uniq().value();
          }
        }
      });

      // The autoscaling is supposed to have a single AMachine child
      var sysAwsAutoScalingChild = sysAwsAutoScalings[0].contains[0];
      _.forEach(cont.contains, function (instanceId) {

        var container = containers[instanceId];
        container.id = container.id.replace('i-', sysAwsAutoScalingChild + '-');<commit_msg>Do not crash if the AG contains an unknown instance.<commit_after>/*
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

'use strict';

var _ = require('lodash');


/*
 * post process to hook through SGs not direct to images if sgs exist
 * currenly assumes just 1 or 0 security groups
 */
module.exports = function postProcessing (system) {

  function handleAutoScalingGroups (result) {
    var containers = result.topology.containers;
    var isAutoscaling = function(container) { return container.type === 'aws-autoscaling'; };
    var autoScalingGroups = _.filter(containers,  isAutoscaling);

    _.forEach(autoScalingGroups, function (cont) {
      var newContains = [];
      var sysContainers = system.topology.containers;
      var sysAwsAutoScalings = _.filter(sysContainers, isAutoscaling);

      _.forEach(sysAwsAutoScalings, function (sysAwsAutoScaling) {
        var sysAwsAutoScalingParent = sysContainers[sysAwsAutoScaling.containedBy];

        // if there is a security group container in the system we should
        // add it also in the analyzer result
        // ie: system contains   elb -> sg -> autoscaling
        //     analyzer returns  elb -> autoscaling
        //
        if (sysAwsAutoScalingParent.type === 'aws-sg') {
          var awsAutoScaling = containers[sysAwsAutoScaling.id];
          if (awsAutoScaling) {
            var awsAutoScalingParent = containers[awsAutoScaling.containedBy];

            var awsSg = containers[sysAwsAutoScalingParent.id];

            // Remove AutoScaling child
            _.remove(awsAutoScalingParent.contains, function (ele) {
              return ele === awsAutoScaling.id;
            });

            awsAutoScalingParent.contains.push(sysAwsAutoScalingParent.id);
            awsAutoScaling.containedBy = sysAwsAutoScalingParent.id;
            awsSg.containedBy = awsAutoScalingParent.id;
            awsSg.contains = _(awsSg.contains).push(awsAutoScaling.id).uniq().value();
          }
        }
      });

      // The autoscaling is supposed to have a single AMachine child
      var sysAwsAutoScalingChild = sysAwsAutoScalings[0].contains[0];
      _.forEach(cont.contains, function (instanceId) {

        var container = containers[instanceId];

        if (!container) {
          // we are on a race condition between nscale and AWS autoscaling
          // let's just forget about this container
          return;
        }

        container.id = container.id.replace('i-', sysAwsAutoScalingChild + '-');
{'fraction_non_alphanumeric': 0.08545335942596216, 'fraction_numerical': 0.001304631441617743, 'mean_word_length': 3.042847725774555, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1164632', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1644, 'n_words': 533}