<commit_before>import { IPromise, module } from 'angular';
import { chain, filter, flatten, map } from 'lodash';

import { IHealth, IServerGroup, IInstance, IVpc, Application } from '@spinnaker/core';

import { AWSProviderSettings } from 'amazon/aws.settings';
import { IAmazonApplicationLoadBalancer,
  IALBListenerCertificate,
  IAmazonClassicLoadBalancer,
  IAmazonLoadBalancer,
  IApplicationLoadBalancerSourceData,
  IClassicListenerCommand,
  IClassicLoadBalancerSourceData,
  IUpsertAmazonApplicationLoadBalancerCommand,
  IUpsertAmazonClassicLoadBalancerCommand,
  ITargetGroup
} from 'amazon/domain';
import { VPC_READ_SERVICE, VpcReader } from 'amazon/vpc/vpc.read.service';

export class AwsLoadBalancerTransformer {
  public constructor(private vpcReader: VpcReader) { 'ngInject'; }

  private updateHealthCounts(serverGroup: IServerGroup | ITargetGroup | IAmazonLoadBalancer): void {
    const instances = serverGroup.instances;
    let serverGroups: IServerGroup[]  = [serverGroup] as IServerGroup[];
    if ((serverGroup as ITargetGroup | IAmazonLoadBalancer).serverGroups) {
      const container = serverGroup as ITargetGroup;
      serverGroups = container.serverGroups;
    }
    serverGroup.instanceCounts = {
      up: instances.filter((instance) => instance.health[0].state === 'InService').length,
      down: instances.filter((instance) => instance.health[0].state === 'OutOfService').length,
      outOfService: serverGroups.reduce((acc, sg): number => {
        return sg.instances.filter((instance): boolean => {
          return instance.healthState === 'OutOfService';
        }).length + acc;
      }, 0),
      starting: undefined,
      succeeded: undefined,
      failed: undefined,
      unknown: undefined,
    };
  }

  private transformInstance(instance: IInstance, provider: string, account: string, region: string): void {
    const health: IHealth = (instance.health && instance.health.length > 0) ? instance.health[0] : {} as IHealth;
    if (health.state === 'healthy') {
      // Target groups use 'healthy' instead of 'InService' and a lot of deck expects InService
      // to surface health in the UI; just set it as InService since we don't really care the
      // specific state name... yet
      health.state = 'InService';
    }
    instance.provider = provider;
    instance.account = account;
    instance.region = region;
    instance.healthState = health.state ? health.state === 'InService' ? 'Up' : 'Down' : 'OutOfService';
    instance.health = [health];
  }

  private addVpcNameToContainer(container: IAmazonLoadBalancer | ITargetGroup): (vpcs: IVpc[]) => IAmazonLoadBalancer | ITargetGroup {
    return (vpcs: IVpc[]) => {
      const match = vpcs.find((test) => test.id === container.vpcId);
      container.vpcName = match ? match.name : '';
      return container;
    };
  }

  private normalizeServerGroups(serverGroups: IServerGroup[], container: IAmazonLoadBalancer | ITargetGroup, containerType: string, healthType: string): void {
    serverGroups.forEach((serverGroup) => {
      serverGroup.account = container.account;
      serverGroup.region = container.region;
      if (serverGroup.detachedInstances) {
        serverGroup.detachedInstances = (serverGroup.detachedInstances as any).map((instanceId: string) => {
          return { id: instanceId } as IInstance;
        });
        serverGroup.instances = serverGroup.instances.concat(serverGroup.detachedInstances);<commit_msg>fix(provider/amazon): Fix instance health setting in target groups (#3813)<commit_after>import { IPromise, module } from 'angular';
import { chain, filter, flatten, map } from 'lodash';

import { IHealth, IServerGroup, IInstance, IVpc, Application } from '@spinnaker/core';

import { AWSProviderSettings } from 'amazon/aws.settings';
import { IAmazonApplicationLoadBalancer,
  IALBListenerCertificate,
  IAmazonClassicLoadBalancer,
  IAmazonLoadBalancer,
  IApplicationLoadBalancerSourceData,
  IClassicListenerCommand,
  IClassicLoadBalancerSourceData,
  IUpsertAmazonApplicationLoadBalancerCommand,
  IUpsertAmazonClassicLoadBalancerCommand,
  ITargetGroup
} from 'amazon/domain';
import { VPC_READ_SERVICE, VpcReader } from 'amazon/vpc/vpc.read.service';

export class AwsLoadBalancerTransformer {
  public constructor(private vpcReader: VpcReader) { 'ngInject'; }

  private updateHealthCounts(serverGroup: IServerGroup | ITargetGroup | IAmazonLoadBalancer): void {
    const instances = serverGroup.instances;
    let serverGroups: IServerGroup[]  = [serverGroup] as IServerGroup[];
    if ((serverGroup as ITargetGroup | IAmazonLoadBalancer).serverGroups) {
      const container = serverGroup as ITargetGroup;
      serverGroups = container.serverGroups;
    }
    serverGroup.instanceCounts = {
      up: instances.filter((instance) => instance.health[0].state === 'InService').length,
      down: instances.filter((instance) => instance.health[0].state === 'OutOfService').length,
      outOfService: serverGroups.reduce((acc, sg): number => {
        return sg.instances.filter((instance): boolean => {
          return instance.healthState === 'OutOfService';
        }).length + acc;
      }, 0),
      starting: undefined,
      succeeded: undefined,
      failed: undefined,
      unknown: undefined,
    };
  }

  private transformInstance(instance: IInstance, provider: string, account: string, region: string): void {
    // instance in this case should be some form if instance source data, but force to 'any' type to fix
    // instnace health in load balancers until we can actually shape this bit properly
    const health: IHealth = (instance.health as any) || {} as IHealth;
    if (health.state === 'healthy') {
      // Target groups use 'healthy' instead of 'InService' and a lot of deck expects InService
      // to surface health in the UI; just set it as InService since we don't really care the
      // specific state name... yet
      health.state = 'InService';
    }
    instance.provider = provider;
    instance.account = account;
    instance.region = region;
    instance.healthState = health.state ? health.state === 'InService' ? 'Up' : 'Down' : 'OutOfService';
    instance.health = [health];
  }

  private addVpcNameToContainer(container: IAmazonLoadBalancer | ITargetGroup): (vpcs: IVpc[]) => IAmazonLoadBalancer | ITargetGroup {
    return (vpcs: IVpc[]) => {
      const match = vpcs.find((test) => test.id === container.vpcId);
      container.vpcName = match ? match.name : '';
      return container;
    };
  }

  private normalizeServerGroups(serverGroups: IServerGroup[], container: IAmazonLoadBalancer | ITargetGroup, containerType: string, healthType: string): void {
    serverGroups.forEach((serverGroup) => {
      serverGroup.account = container.account;
      serverGroup.region = container.region;
      if (serverGroup.detachedInstances) {
        serverGroup.detachedInstances = (serverGroup.detachedInstances as any).map((instanceId: string) => {
          return { id: instanceId } as IInstance;
        });
        serverGroup.instances = serverGroup.instances.concat(serverGroup.detachedInstances);
{'fraction_non_alphanumeric': 0.11067415730337078, 'fraction_numerical': 0.0016853932584269663, 'mean_word_length': 4.053938963804116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '6441401', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1984, 'n_words': 502}