/*
 * 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.addthis.hydra.job.spawn;

import com.yammer.metrics.core.Gauge;

class DownMinionGauge extends Gauge<Integer> {
    private Spawn spawn;

    public DownMinionGauge(Spawn spawn) {
        this.spawn = spawn;
    }

    @Override public Integer value() {
        int total = 0;
        if (spawn.monitored != null) {
            synchronized (spawn.monitored) {
                total = spawn.monitored.size();
            }
        }
        int up;
        if (spawn.minionMembers == null) {
            up = 0;
        } else {
            up = spawn.minionMembers.getMemberSetSize();
        }
        return total - up;
    }
}

@@ -589,16 +589,60 @@
 spawn;%0A%0A
+import com.addthis.hydra.job.mq.HostState;%0A%0A
 import c
@@ -873,21 +873,20 @@
     int 
-total
+down
  = 0;%0A  
@@ -895,308 +895,163 @@
     
-if (spawn.monitored != null) %7B%0A            synchronized (spawn.monitored) %7B%0A                total = spawn.monitored.size();%0A            %7D%0A        %7D%0A        int up;%0A        if (spawn.minionMembers == null) %7B%0A            up = 0;%0A        %7D else %7B%0A            up = spawn.minionMembers.getMemberSetSize();
+for (HostState host : spawn.listHostStatus(null)) %7B%0A            if (host != null && !host.isDead() && !host.isUp()) %7B%0A                down++;%0A            %7D
 %0A   
@@ -1076,18 +1076,12 @@
 urn 
-total - up
+down
 ;%0A  

Fix Spawn minionDown metric to correctly ignore failed hosts.