All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.emc.vipr.client.core.Metering Maven / Gradle / Ivy

There is a newer version: 3.5.0.0
Show newest version
/*
 * Copyright (c) 2015 EMC Corporation
 * All Rights Reserved
 */
package com.emc.vipr.client.core;

import java.io.InputStream;
import java.util.Date;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.emc.vipr.client.core.impl.PathConstants;
import com.emc.vipr.client.core.util.TimeBucketUtils;
import com.emc.vipr.client.impl.RestClient;
import com.sun.jersey.api.client.ClientResponse;

public class Metering {
    protected final RestClient client;

    public Metering(RestClient client) {
        this.client = client;
    }

    /**
     * Gets the statistics for the hour of the given date. This is a convenience method for:
     * getStatisticsForTimeBucketAsStream(TimeBucketUtils.forHour(date))
     * 
     * @param date
     *            the date to format as an hour time bucket.
     * @return the stream containing the statistics as XML.
     * 
     * @see #getStatisticsForTimeBucketAsStream(String)
     * @see TimeBucketUtils#forHour(Date)
     */
    public InputStream getStatisticsForHourAsStream(Date date) {
        String timeBucket = TimeBucketUtils.forHour(date);
        return getStatisticsForTimeBucketAsStream(timeBucket);
    }

    /**
     * Gets the statistics for the minute of the given date. This is a convenience method for:
     * getStatisticsForTimeBucketAsStream(TimeBucketUtils.forMinute(date))
     * 
     * @param date
     *            the date to format as a minute time bucket.
     * @return the stream containing the statistics as XML.
     * @see #getStatisticsForTimeBucketAsStream(String)
     * @see TimeBucketUtils#forMinute(Date)
     */
    public InputStream getStatisticsForMinuteAsStream(Date date) {
        String timeBucket = TimeBucketUtils.forMinute(date);
        return getStatisticsForTimeBucketAsStream(timeBucket);
    }

    /**
     * Gets the statistics for the given time bucket, as a stream. A time bucket can be either an hour or a minute of
     * any day, in the form of yyyy-MM-dd'T'HH for hour or yy-MM-dd'T'HH:mm for minute.
     * API Call: GET /metering/stats?time_bucket={timeBucket}
     * 
     * @param timeBucket
     *            the time bucket for which to retrieve the stats.
     * @return the stream containing the statistics as XML. This must be closed by the client in order to release the
     *         connection.
     */
    public InputStream getStatisticsForTimeBucketAsStream(String timeBucket) {
        ClientResponse response = getStatistics(ClientResponse.class, timeBucket);
        return response.getEntityInputStream();
    }

    protected  T getStatistics(Class responseType, String timeBucket) {
        UriBuilder builder = client.uriBuilder(PathConstants.METERING_STATS_URL).queryParam("time_bucket", timeBucket);
        return client.resource(builder.build()).type(MediaType.APPLICATION_XML).get(responseType);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy