<commit_before>/*
 * Copyright 2014 Google, 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.gce.model.callbacks

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
import com.google.api.client.googleapis.batch.BatchRequest
import com.google.api.client.googleapis.batch.json.JsonBatchCallback
import com.google.api.client.googleapis.json.GoogleJsonError
import com.google.api.client.http.HttpHeaders
import com.google.api.services.compute.Compute
import com.google.api.services.replicapool.model.InstanceGroupManagerList
import com.netflix.frigga.Names
import com.netflix.spinnaker.oort.gce.model.GoogleApplication
import com.netflix.spinnaker.oort.gce.model.GoogleCluster
import com.netflix.spinnaker.oort.gce.model.GoogleServerGroup
import com.netflix.spinnaker.oort.gce.model.ResourceViewsBuilder
import org.apache.log4j.Logger

class MIGSCallback<InstanceGroupManagerList> extends JsonBatchCallback<InstanceGroupManagerList> {
  protected static final Logger log = Logger.getLogger(this)

  private static final String GOOGLE_SERVER_GROUP_TYPE = "gce"

  private HashMap<String, GoogleApplication> tempAppMap
  private String region
  private String localZoneName
  private String accountName
  private String project
  private Compute compute
  private GoogleCredential.Builder credentialBuilder
  private BatchRequest resourceViewsBatch
  private BatchRequest instancesBatch

  public MIGSCallback(HashMap<String, GoogleApplication> tempAppMap,
                      String region,
                      String localZoneName,
                      String accountName,
                      String project,
                      Compute compute,
                      GoogleCredential.Builder credentialBuilder,
                      BatchRequest resourceViewsBatch,
                      BatchRequest instancesBatch) {
    this.tempAppMap = tempAppMap
    this.region = region
    this.localZoneName = localZoneName
    this.accountName = accountName
    this.project = project
    this.compute = compute
    this.credentialBuilder = credentialBuilder
    this.resourceViewsBatch = resourceViewsBatch
    this.instancesBatch = instancesBatch
  }

  @Override
  void onSuccess(InstanceGroupManagerList instanceGroupManagerList, HttpHeaders responseHeaders) throws IOException {
    for (def instanceGroupManager : instanceGroupManagerList.getItems()) {
      def names = Names.parseName(instanceGroupManager.name)
      def appName = names.app.toLowerCase()

      if (appName) {
        if (!tempAppMap[appName]) {
          tempAppMap[appName] = new GoogleApplication(name: appName)
          tempAppMap[appName].clusterNames[accountName] = new HashSet<String>()
          tempAppMap[appName].clusters[accountName] = new HashMap<String, GoogleCluster>()
        }

        if (!tempAppMap[appName].clusters[accountName][names.cluster]) {
          tempAppMap[appName].clusters[accountName][names.cluster] = new GoogleCluster(name: names.cluster,
                                                                                       accountName: accountName)
        }<commit_msg>Minor tweak to avoid potential NPE when querying multiple GCE accounts.<commit_after>/*
 * Copyright 2014 Google, 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.gce.model.callbacks

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
import com.google.api.client.googleapis.batch.BatchRequest
import com.google.api.client.googleapis.batch.json.JsonBatchCallback
import com.google.api.client.googleapis.json.GoogleJsonError
import com.google.api.client.http.HttpHeaders
import com.google.api.services.compute.Compute
import com.google.api.services.replicapool.model.InstanceGroupManagerList
import com.netflix.frigga.Names
import com.netflix.spinnaker.oort.gce.model.GoogleApplication
import com.netflix.spinnaker.oort.gce.model.GoogleCluster
import com.netflix.spinnaker.oort.gce.model.GoogleServerGroup
import com.netflix.spinnaker.oort.gce.model.ResourceViewsBuilder
import org.apache.log4j.Logger

class MIGSCallback<InstanceGroupManagerList> extends JsonBatchCallback<InstanceGroupManagerList> {
  protected static final Logger log = Logger.getLogger(this)

  private static final String GOOGLE_SERVER_GROUP_TYPE = "gce"

  private HashMap<String, GoogleApplication> tempAppMap
  private String region
  private String localZoneName
  private String accountName
  private String project
  private Compute compute
  private GoogleCredential.Builder credentialBuilder
  private BatchRequest resourceViewsBatch
  private BatchRequest instancesBatch

  public MIGSCallback(HashMap<String, GoogleApplication> tempAppMap,
                      String region,
                      String localZoneName,
                      String accountName,
                      String project,
                      Compute compute,
                      GoogleCredential.Builder credentialBuilder,
                      BatchRequest resourceViewsBatch,
                      BatchRequest instancesBatch) {
    this.tempAppMap = tempAppMap
    this.region = region
    this.localZoneName = localZoneName
    this.accountName = accountName
    this.project = project
    this.compute = compute
    this.credentialBuilder = credentialBuilder
    this.resourceViewsBatch = resourceViewsBatch
    this.instancesBatch = instancesBatch
  }

  @Override
  void onSuccess(InstanceGroupManagerList instanceGroupManagerList, HttpHeaders responseHeaders) throws IOException {
    for (def instanceGroupManager : instanceGroupManagerList.getItems()) {
      def names = Names.parseName(instanceGroupManager.name)
      def appName = names.app.toLowerCase()

      if (appName) {
        if (!tempAppMap[appName]) {
          tempAppMap[appName] = new GoogleApplication(name: appName)
        }

        if (!tempAppMap[appName].clusterNames[accountName]) {
          tempAppMap[appName].clusterNames[accountName] = new HashSet<String>()
          tempAppMap[appName].clusters[accountName] = new HashMap<String, GoogleCluster>()
        }

        if (!tempAppMap[appName].clusters[accountName][names.cluster]) {
          tempAppMap[appName].clusters[accountName][names.cluster] = new GoogleCluster(name: names.cluster,
                                                                                       accountName: accountName)
        }
{'fraction_non_alphanumeric': 0.07157244527997851, 'fraction_numerical': 0.0026856452262656104, 'mean_word_length': 3.34032634032634, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7254580', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1874, 'n_words': 480}