<commit_before>/*
 * Copyright 2014 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.spinnaker.oort.aws.model

import com.fasterxml.jackson.annotation.JsonAnyGetter
import com.fasterxml.jackson.annotation.JsonAnySetter
import com.netflix.spinnaker.oort.model.HealthState
import com.netflix.spinnaker.oort.model.Instance
import com.netflix.spinnaker.oort.model.ServerGroup
import groovy.transform.CompileStatic

@CompileStatic
class AmazonServerGroup implements ServerGroup, Serializable {

  String name
  String region
  Set<String> zones
  Set<Instance> instances
  Set health
  Map<String, Object> image
  Map<String, Object> launchConfig
  Map<String, Object> asg
  List<Map> scalingPolicies
  List<Map> scheduledActions
  Map buildInfo
  String vpcId

  private Map<String, Object> dynamicProperties = new HashMap<String, Object>()

  @JsonAnyGetter
  public Map<String,Object> any() {
    return dynamicProperties;
  }

  @JsonAnySetter
  public void set(String name, Object value) {
    dynamicProperties.put(name, value);
  }

  @Override
  String getType() {
    return "aws"
  }

  @Override
  Boolean isDisabled() {
    if (asg) {
      List<Map> suspendedProcesses = (List<Map>) asg.suspendedProcesses
      List<String> processNames = (List<String>) suspendedProcesses.collect { it.processName }
      return processNames.contains('AddToLoadBalancer')
    }
    return false
  }

  @Override
  Long getCreatedTime() {
    if (!asg) {
      return null
    }
    (Long) asg.createdTime
  }

  @Override
  Set<String> getLoadBalancers() {
    Set<String> loadBalancerNames = []
    def asg = getAsg()
    if (asg && asg.containsKey("loadBalancerNames")) {
      loadBalancerNames = (Set<String>) asg.loadBalancerNames
    }
    return loadBalancerNames
  }

  @Override
  Set<String> getSecurityGroups() {
    Set<String> securityGroups = []
    if (launchConfig && launchConfig.containsKey("securityGroups")) {
      securityGroups = (Set<String>) launchConfig.securityGroups
    }
    securityGroups
  }

  @Override
  ServerGroup.InstanceCounts getInstanceCounts() {
    Collection<Instance> instances = getInstances()
    new ServerGroup.InstanceCounts(
      total: instances.size(),
      up: filterInstancesByHealthState(instances, HealthState.Up)?.size() ?: 0,
      down: filterInstancesByHealthState(instances, HealthState.Down)?.size() ?: 0,
      unknown: filterInstancesByHealthState(instances, HealthState.Unknown)?.size() ?: 0,
      starting: filterInstancesByHealthState(instances, HealthState.Starting)?.size() ?: 0,
      outOfService: filterInstancesByHealthState(instances, HealthState.OutOfService)?.size() ?: 0)
  }

  @Override
  ServerGroup.Capacity getCapacity() {
    if (asg) {
      return new ServerGroup.Capacity(
        min: asg.minSize ? asg.minSize as Integer : 0,
        max: asg.maxSize ? asg.maxSize as Integer : 0,
        desired: asg.desiredCapacity ? asg.desiredCapacity as Integer : 0
      )
    }
    return null
  }

  @Override
  ServerGroup.ImageSummary getImageSummary() {
    def i = image
    def bi = buildInfo
    return new ServerGroup.ImageSummary() {
      String serverGroupName = name
      String imageName = i.name
      String imageId = i.imageId

      @Override
      Map<String, Object> getBuildInfo() {
        return bi
      }

      @Override
      Map<String, Object> getImage() {
        return i
      }
    }
  }

  static Collection<Instance> filterInstancesByHealthState(Set<Instance> instances, HealthState healthState) {
    instances.findAll { Instance it -> it.getHealthState() == healthState }
  }

}
<commit_msg>Avoid an NPE when constructing ImageSummary if LaunchConfig/Image is not available<commit_after>/*
 * Copyright 2014 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.spinnaker.oort.aws.model

import com.fasterxml.jackson.annotation.JsonAnyGetter
import com.fasterxml.jackson.annotation.JsonAnySetter
import com.netflix.spinnaker.oort.model.HealthState
import com.netflix.spinnaker.oort.model.Instance
import com.netflix.spinnaker.oort.model.ServerGroup
import groovy.transform.CompileStatic

@CompileStatic
class AmazonServerGroup implements ServerGroup, Serializable {

  String name
  String region
  Set<String> zones
  Set<Instance> instances
  Set health
  Map<String, Object> image
  Map<String, Object> launchConfig
  Map<String, Object> asg
  List<Map> scalingPolicies
  List<Map> scheduledActions
  Map buildInfo
  String vpcId

  private Map<String, Object> dynamicProperties = new HashMap<String, Object>()

  @JsonAnyGetter
  public Map<String,Object> any() {
    return dynamicProperties;
  }

  @JsonAnySetter
  public void set(String name, Object value) {
    dynamicProperties.put(name, value);
  }

  @Override
  String getType() {
    return "aws"
  }

  @Override
  Boolean isDisabled() {
    if (asg) {
      List<Map> suspendedProcesses = (List<Map>) asg.suspendedProcesses
      List<String> processNames = (List<String>) suspendedProcesses.collect { it.processName }
      return processNames.contains('AddToLoadBalancer')
    }
    return false
  }

  @Override
  Long getCreatedTime() {
    if (!asg) {
      return null
    }
    (Long) asg.createdTime
  }

  @Override
  Set<String> getLoadBalancers() {
    Set<String> loadBalancerNames = []
    def asg = getAsg()
    if (asg && asg.containsKey("loadBalancerNames")) {
      loadBalancerNames = (Set<String>) asg.loadBalancerNames
    }
    return loadBalancerNames
  }

  @Override
  Set<String> getSecurityGroups() {
    Set<String> securityGroups = []
    if (launchConfig && launchConfig.containsKey("securityGroups")) {
      securityGroups = (Set<String>) launchConfig.securityGroups
    }
    securityGroups
  }

  @Override
  ServerGroup.InstanceCounts getInstanceCounts() {
    Collection<Instance> instances = getInstances()
    new ServerGroup.InstanceCounts(
      total: instances.size(),
      up: filterInstancesByHealthState(instances, HealthState.Up)?.size() ?: 0,
      down: filterInstancesByHealthState(instances, HealthState.Down)?.size() ?: 0,
      unknown: filterInstancesByHealthState(instances, HealthState.Unknown)?.size() ?: 0,
      starting: filterInstancesByHealthState(instances, HealthState.Starting)?.size() ?: 0,
      outOfService: filterInstancesByHealthState(instances, HealthState.OutOfService)?.size() ?: 0)
  }

  @Override
  ServerGroup.Capacity getCapacity() {
    if (asg) {
      return new ServerGroup.Capacity(
        min: asg.minSize ? asg.minSize as Integer : 0,
        max: asg.maxSize ? asg.maxSize as Integer : 0,
        desired: asg.desiredCapacity ? asg.desiredCapacity as Integer : 0
      )
    }
    return null
  }

  @Override
  ServerGroup.ImageSummary getImageSummary() {
    def i = image
    def bi = buildInfo
    return new ServerGroup.ImageSummary() {
      String serverGroupName = name
      String imageName = i?.name
      String imageId = i?.imageId

      @Override
      Map<String, Object> getBuildInfo() {
        return bi
      }

      @Override
      Map<String, Object> getImage() {
        return i
      }
    }
  }

  static Collection<Instance> filterInstancesByHealthState(Set<Instance> instances, HealthState healthState) {
    instances.findAll { Instance it -> it.getHealthState() == healthState }
  }

}

{'fraction_non_alphanumeric': 0.0961423623551893, 'fraction_numerical': 0.0038218081930013136, 'mean_word_length': 3.606160616061606, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '1121132', 'n_tokens_mistral': 2509, 'n_tokens_neox': 2436, 'n_words': 638}