/*
 * Copyright 2014 University of Southern California
 *
 * 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 edu.usc.pgroup.floe.flake;

import com.codahale.metrics.Counter;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;

/**
 * @author kumbhare
 */
public class QueueLenMonitor extends Thread {

    /**
     * Metric Registry.
     */
    private final MetricRegistry metricReg;

    /**
     * Queue counter.
     */
    private final Counter qCounter;

    /**
     * @param registry Metric registry.
     * @param counter queue counter.
     */
    public QueueLenMonitor(final MetricRegistry registry,
                           final Counter counter) {
        this.metricReg = registry;
        this.qCounter = counter;
    }

    @Override
    public final void run() {

        Histogram qhist
                = metricReg.histogram(MetricRegistry.name(QueueLenMonitor.class,
                                                    "q.len.histo"));

        final int monitorint = 10;
        while (!Thread.interrupted()) {

            qhist.update(qCounter.getCount());

            try {
                Thread.sleep(monitorint);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

@@ -712,25 +712,30 @@
 metrics.
-Histogram
+MetricRegistry
 ;%0Aimport
@@ -756,30 +756,60 @@
 metrics.
-MetricRegistry
+Timer;%0A%0Aimport java.util.concurrent.TimeUnit
 ;%0A%0A/**%0A 
@@ -1369,16 +1369,18 @@
         
+/*
 Histogra
@@ -1536,16 +1536,149 @@
 isto%22));
+*/%0A%0A        Timer qhist%0A                = metricReg.timer(MetricRegistry.name(QueueLenMonitor.class,%0A                %22q.len.histo%22));
 %0A%0A      
@@ -1759,16 +1759,18 @@
         
+//
 qhist.up
@@ -1795,16 +1795,133 @@
 ount());
+%0A            //qhist.mark(qCounter.getCount());%0A            qhist.update(qCounter.getCount(), TimeUnit.MILLISECONDS);
 %0A%0A      
@@ -1961,26 +1961,17 @@
 d.sleep(
-monitorint
+1
 );%0A     

Update Qlen monitor.