<commit_before>/*
 * Copyright 2016 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.clouddriver.titus.caching.providers

import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spinnaker.cats.cache.Cache
import com.netflix.spinnaker.cats.cache.CacheData
import com.netflix.spinnaker.clouddriver.core.provider.agent.ExternalHealthProvider
import com.netflix.spinnaker.clouddriver.model.InstanceProvider
import com.netflix.spinnaker.clouddriver.titus.TitusCloudProvider
import com.netflix.spinnaker.clouddriver.titus.caching.Keys
import com.netflix.spinnaker.clouddriver.titus.caching.utils.AwsLookupUtil
import com.netflix.spinnaker.clouddriver.titus.client.model.Job
import com.netflix.spinnaker.clouddriver.titus.model.TitusInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.HEALTH
import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.INSTANCES

@Component
class TitusInstanceProvider implements InstanceProvider<TitusInstance> {
  final String cloudProvider = TitusCloudProvider.ID
  private final Cache cacheView
  private final ObjectMapper objectMapper
  private final TitusCloudProvider titusCloudProvider

  @Autowired
  AwsLookupUtil awsLookupUtil

  @Autowired(required = false)
  List<ExternalHealthProvider> externalHealthProviders

  @Autowired
  TitusInstanceProvider(Cache cacheView, TitusCloudProvider titusCloudProvider, ObjectMapper objectMapper) {
    this.cacheView = cacheView
    this.titusCloudProvider = titusCloudProvider
    this.objectMapper = objectMapper
  }

  @Override
  TitusInstance getInstance(String account, String region, String id) {

    String awsAccount = awsLookupUtil.awsAccountId(account, region)

    if (!awsAccount) {
      return null
    }

    String stack = awsLookupUtil.stack(account)
    if(!stack){
      stack = 'mainvpc'
    }

    CacheData instanceEntry = cacheView.get(INSTANCES.ns, Keys.getInstanceKey(id, awsAccount, stack, region))
    if (!instanceEntry) {
      return null
    }
    Job.TaskSummary task = objectMapper.convertValue(instanceEntry.attributes.task, Job.TaskSummary)
    Job job = objectMapper.convertValue(instanceEntry.attributes.job, Job)
    TitusInstance instance = new TitusInstance(job, task)
    instance.health = instance.health ?: []
    if (instanceEntry.attributes[HEALTH.ns]) {
      instance.health.addAll(instanceEntry.attributes[HEALTH.ns])
    }
    externalHealthProviders.each { externalHealthProvider ->
      def healthKeys = []
      externalHealthProvider.agents.each { externalHealthAgent ->
        healthKeys << Keys.getInstanceHealthKey(instance.instanceId, externalHealthAgent.healthId)
      }
      healthKeys.unique().each { key ->
        def externalHealth = cacheView.getAll(HEALTH.ns, key)
        if (externalHealth) {
          def health = externalHealth*.attributes
          health.each { it.remove('lastUpdatedTimestamp') }
          instance.health.addAll(health)
        }
      }
    }
    awsLookupUtil.lookupTargetGroupHealth(job, [instance])
    instance
  }

  @Override<commit_msg>Titus: fix instance sidebar not showing for titus<commit_after>/*
 * Copyright 2016 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.clouddriver.titus.caching.providers

import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spinnaker.cats.cache.Cache
import com.netflix.spinnaker.cats.cache.CacheData
import com.netflix.spinnaker.clouddriver.core.provider.agent.ExternalHealthProvider
import com.netflix.spinnaker.clouddriver.model.InstanceProvider
import com.netflix.spinnaker.clouddriver.titus.TitusCloudProvider
import com.netflix.spinnaker.clouddriver.titus.caching.Keys
import com.netflix.spinnaker.clouddriver.titus.caching.utils.AwsLookupUtil
import com.netflix.spinnaker.clouddriver.titus.client.model.Job
import com.netflix.spinnaker.clouddriver.titus.model.TitusInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.HEALTH
import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.INSTANCES

@Component
class TitusInstanceProvider implements InstanceProvider<TitusInstance> {
  final String cloudProvider = TitusCloudProvider.ID
  private final Cache cacheView
  private final ObjectMapper objectMapper
  private final TitusCloudProvider titusCloudProvider

  @Autowired
  AwsLookupUtil awsLookupUtil

  @Autowired(required = false)
  List<ExternalHealthProvider> externalHealthProviders

  @Autowired
  TitusInstanceProvider(Cache cacheView, TitusCloudProvider titusCloudProvider, ObjectMapper objectMapper) {
    this.cacheView = cacheView
    this.titusCloudProvider = titusCloudProvider
    this.objectMapper = objectMapper
  }

  @Override
  TitusInstance getInstance(String account, String region, String id) {

    String awsAccount = awsLookupUtil.awsAccountId(account, region)

    if (!awsAccount) {
      return null
    }

    String stack = awsLookupUtil.stack(account)
    if(!stack){
      stack = 'mainvpc'
    }

    CacheData instanceEntry = cacheView.get(INSTANCES.ns, Keys.getInstanceKey(id, awsAccount, stack, region))
    if (!instanceEntry) {
      return null
    }
    Job.TaskSummary task = objectMapper.convertValue(instanceEntry.attributes.task, Job.TaskSummary)
    Job job = objectMapper.convertValue(instanceEntry.attributes.job, Job)
    TitusInstance instance = new TitusInstance(job, task)
    instance.health = instance.health ?: []
    if (instanceEntry.attributes[HEALTH.ns]) {
      instance.health.addAll(instanceEntry.attributes[HEALTH.ns])
    }
    externalHealthProviders.each { externalHealthProvider ->
      def healthKeys = []
      externalHealthProvider.agents.each { externalHealthAgent ->
        healthKeys << Keys.getInstanceHealthKey(instance.instanceId, externalHealthAgent.healthId)
      }
      healthKeys.unique().each { key ->
        def externalHealth = cacheView.getAll(HEALTH.ns, key)
        if (externalHealth) {
          def health = externalHealth*.attributes
          health.each { it.remove('lastUpdatedTimestamp') }
          instance.health.addAll(health)
        }
      }
    }
    awsLookupUtil.lookupTargetGroupHealth(job, [instance].toSet())
    instance
  }

  @Override
{'fraction_non_alphanumeric': 0.08264900662251656, 'fraction_numerical': 0.002119205298013245, 'mean_word_length': 4.343949044585988, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '5330957', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2115, 'n_words': 474}