/**
 * Copyright (C) 2011-2019 Red Hat, Inc. (https://github.com/Commonjava/indy)
 *
 * 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 org.commonjava.indy.core.bind.jaxrs.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.StreamingOutput;

import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.CountingOutputStream;
import org.commonjava.indy.measure.annotation.Measure;
import org.commonjava.indy.measure.annotation.MetricNamed;
import org.commonjava.indy.metrics.IndyMetricsManager;
import org.commonjava.indy.metrics.conf.IndyMetricsConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.commonjava.indy.measure.annotation.MetricNamed.DEFAULT;
import static org.commonjava.indy.metrics.IndyMetricsConstants.METER;
import static org.commonjava.indy.metrics.IndyMetricsConstants.getName;
import static org.commonjava.indy.metrics.IndyMetricsConstants.getDefaultName;

public class TransferStreamingOutput
    implements StreamingOutput
{

    private static final String TRANSFER_METRIC_NAME = "indy.transferred.content";

    private static final double NANOS_PER_SEC = 1000000000.0;

    private InputStream stream;

    private IndyMetricsManager metricsManager;

    private IndyMetricsConfig metricsConfig;

    public TransferStreamingOutput( final InputStream stream, final IndyMetricsManager metricsManager,
                                    final IndyMetricsConfig metricsConfig )
    {
        this.stream = stream;
        this.metricsManager = metricsManager;
        this.metricsConfig = metricsConfig;
    }

    @Override
    @Measure( timers = @MetricNamed( DEFAULT ) )
    public void write( final OutputStream out )
        throws IOException, WebApplicationException
    {
        long start = System.nanoTime();
        try
        {
            CountingOutputStream cout = new CountingOutputStream( out );
            IOUtils.copy( stream, cout );

            Logger logger = LoggerFactory.getLogger( getClass() );
            logger.trace( "Wrote: {} bytes", cout.getByteCount() );

            String name = getName( metricsConfig.getNodePrefix(), TRANSFER_METRIC_NAME,
                                   getDefaultName( TransferStreamingOutput.class, "write" ), METER );

            long end = System.nanoTime();
            double elapsed = (end-start)/NANOS_PER_SEC;

            Meter meter = metricsManager.getMeter( name );
            meter.mark( Math.round( cout.getByteCount() / elapsed ) );
        }
        finally
        {
            IOUtils.closeQuietly( stream );
        }
    }

}

@@ -703,320 +703,34 @@
 ort 
-java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.stream.Stream;%0A%0Aimport javax.ws.rs.WebApplicationException;%0Aimport javax.ws.rs.core.StreamingOutput;%0A%0Aimport com.codahale.metrics.Meter;%0Aimport com.codahale.metrics.MetricRegistry
+com.codahale.metrics.Meter
 ;%0Aim
@@ -882,67 +882,8 @@
 re;%0A
-import org.commonjava.indy.measure.annotation.MetricNamed;%0A
 impo
@@ -1061,144 +1061,171 @@
 ort 
-static org.commonjava.indy.measure.annotation.MetricNamed.DEFAULT;%0Aimport static org.commonjava.indy.metrics.IndyMetricsConstants.METER;
+javax.ws.rs.WebApplicationException;%0Aimport javax.ws.rs.core.StreamingOutput;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;%0A
 %0Aimp
@@ -1284,23 +1284,21 @@
 nstants.
-getName
+METER
 ;%0Aimport
@@ -1369,16 +1369,88 @@
 ultName;
+%0Aimport static org.commonjava.indy.metrics.IndyMetricsConstants.getName;
 %0A%0Apublic
@@ -1651,19 +1651,10 @@
  = 1
-000000000.0
+E9
 ;%0A%0A 
@@ -2121,44 +2121,8 @@
 sure
-( timers = @MetricNamed( DEFAULT ) )
 %0A   

use a cleaner notation for nanos-per-second divisor.