// Path: robusto-core/src/main/java/com/homeadvisor/robusto/health/HealthCheckResult.java
// public class HealthCheckResult
// {
//    /**
//     * State of the health check.
//     */
//    public Status status = Status.HEALTHY;
// 
//    /**
//     * Optional description to elaborate on the status enum.
//     */
//    public String description;
// 
//    /**
//     * Convenience constructor to set all required fields.
//     * @param status Status of the check.
//     */
//    public HealthCheckResult(Status status)
//    {
//       this.status = status;
//    }
// 
//    /**
//     * Convenience constructor to set all fields.
//     * @param status Status of the check.
//     * @param description Optional descriptipn to elaborate on the status.
//     */
//    public HealthCheckResult(Status status, String description)
//    {
//       this.status = status;
//       this.description = description;
//    }
// }
// 
// Path: robusto-core/src/main/java/com/homeadvisor/robusto/health/Status.java
// public enum Status
// {
//    HEALTHY,
//    IMPAIRED,
//    UNHEALTHY,
//    UNKNOWN
// }
// 
// Path: robusto-core/src/main/java/com/homeadvisor/robusto/health/HealthCheck.java
// public interface HealthCheck
// {
//    /**
//     * Run the health check and return the result.
//     * @return Result of the health check.
//     */
//    HealthCheckResult doCheck();
// }


import com.homeadvisor.robusto.health.HealthCheckResult;
import com.homeadvisor.robusto.health.Status;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandMetrics;
import com.netflix.hystrix.util.HystrixRollingNumberEvent;
import com.homeadvisor.robusto.health.HealthCheck;
/*
 * Copyright 2016 HomeAdvisor, 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.homeadvisor.robusto.health.hystrix;



/**
 * Implemenation of {@link HealthCheck} that bases health off of
 * a hystrix command pool.
 */
public class HystrixHealthCheck implements HealthCheck
{
   /**
    * The minimum number of failures in a bucket required to report unhealthy.
    */
   private int minNumFailures;

   /**
    * Command group key uses to lookup Hystrix metrics. Should likely be the
    * same as service name.
    */
   private String key;

   public HystrixHealthCheck(String key, int minNumFailures)
   {
      this.key            = key;
      this.minNumFailures = minNumFailures;
   }

   @Override
   public HealthCheckResult doCheck()
   {
      //
      // Lookup the command group using the provided key. These may be null if
      // no ApiCommands have been created and executed so no need to panic.
      //

      try
      {
         HystrixCommandKey cmdKey = HystrixCommandKey.Factory.asKey(key);

         if (cmdKey == null)
         {
// Path: robusto-core/src/main/java/com/homeadvisor/robusto/health/HealthCheckResult.java
// public class HealthCheckResult
// {
//    /**
//     * State of the health check.
//     */
//    public Status status = Status.HEALTHY;
// 
//    /**
//     * Optional description to elaborate on the status enum.
//     */
//    public String description;
// 
//    /**
//     * Convenience constructor to set all required fields.
//     * @param status Status of the check.
//     */
//    public HealthCheckResult(Status status)
//    {
//       this.status = status;
//    }
// 
//    /**
//     * Convenience constructor to set all fields.
//     * @param status Status of the check.
//     * @param description Optional descriptipn to elaborate on the status.
//     */
//    public HealthCheckResult(Status status, String description)
//    {
//       this.status = status;
//       this.description = description;
//    }
// }
// 
// Path: robusto-core/src/main/java/com/homeadvisor/robusto/health/Status.java
// public enum Status
// {
//    HEALTHY,
//    IMPAIRED,
//    UNHEALTHY,
//    UNKNOWN
// }
// 
// Path: robusto-core/src/main/java/com/homeadvisor/robusto/health/HealthCheck.java
// public interface HealthCheck
// {
//    /**
//     * Run the health check and return the result.
//     * @return Result of the health check.
//     */
//    HealthCheckResult doCheck();
// }


// Path: robusto-core/src/main/java/com/homeadvisor/robusto/health/hystrix/HystrixHealthCheck.java
import com.homeadvisor.robusto.health.HealthCheckResult;
import com.homeadvisor.robusto.health.Status;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandMetrics;
import com.netflix.hystrix.util.HystrixRollingNumberEvent;
import com.homeadvisor.robusto.health.HealthCheck;

/*
 * Copyright 2016 HomeAdvisor, 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.homeadvisor.robusto.health.hystrix;



/**
 * Implemenation of {@link HealthCheck} that bases health off of
 * a hystrix command pool.
 */
public class HystrixHealthCheck implements HealthCheck
{
   /**
    * The minimum number of failures in a bucket required to report unhealthy.
    */
   private int minNumFailures;

   /**
    * Command group key uses to lookup Hystrix metrics. Should likely be the
    * same as service name.
    */
   private String key;

   public HystrixHealthCheck(String key, int minNumFailures)
   {
      this.key            = key;
      this.minNumFailures = minNumFailures;
   }

   @Override
   public HealthCheckResult doCheck()
   {
      //
      // Lookup the command group using the provided key. These may be null if
      // no ApiCommands have been created and executed so no need to panic.
      //

      try
      {
         HystrixCommandKey cmdKey = HystrixCommandKey.Factory.asKey(key);

         if (cmdKey == null)
         {
            return new HealthCheckResult(Status.HEALTHY);