Skip to content

Commit

Permalink
Change MetricDataApi's to take Metric objects rather than ids to avoi…
Browse files Browse the repository at this point in the history
…d needing to convert Lists to arrays of ids when querying multiple metrics.
  • Loading branch information
Ryan Morgan committed Jul 21, 2009
1 parent fca49cd commit f67fd3a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 78 deletions.
61 changes: 37 additions & 24 deletions src/org/hyperic/hq/hqapi1/MetricDataApi.java
Expand Up @@ -7,6 +7,7 @@
import org.hyperic.hq.hqapi1.types.LastMetricsDataResponse;
import org.hyperic.hq.hqapi1.types.LastMetricDataResponse;
import org.hyperic.hq.hqapi1.types.MetricsDataResponse;
import org.hyperic.hq.hqapi1.types.Metric;

import java.io.IOException;
import java.util.List;
Expand All @@ -21,9 +22,9 @@ public class MetricDataApi extends BaseApi {

/**
* Get the {@link org.hyperic.hq.hqapi1.types.MetricData} for the
* given {@link org.hyperic.hq.hqapi1.types.Metric} id.
* given {@link org.hyperic.hq.hqapi1.types.Metric}.
*
* @param metricId The id of the {@link org.hyperic.hq.hqapi1.types.Metric} to query.
* @param metric The {@link org.hyperic.hq.hqapi1.types.Metric} to query.
* @param start The start time to query, in epoch-millis.
* @param end The end time to query, in epoch-millis.
*
Expand All @@ -33,42 +34,42 @@ public class MetricDataApi extends BaseApi {
*
* @throws IOException If a network error occurs while making the request.
*/
public MetricDataResponse getData(int metricId, long start, long end)
public MetricDataResponse getData(Metric metric, long start, long end)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("id", new String[] { Integer.toString(metricId) });
params.put("id", new String[] { Integer.toString(metric.getId()) });
params.put("start", new String[] { Long.toString(start)});
params.put("end", new String[] { Long.toString(end)});
return doGet("metricData/get.hqu", params, MetricDataResponse.class);
}

/**
* Get the {@link org.hyperic.hq.hqapi1.types.LastMetricData} for the
* given {@link org.hyperic.hq.hqapi1.types.Metric} id. This object
* represents the last metric collection for the given metric id.
* given {@link org.hyperic.hq.hqapi1.types.Metric}. This object
* represents the last metric collection for the given Metric.
*
* @param metricId The id of the {@link org.hyperic.hq.hqapi1.types.Metric} to query.
* @param metric The {@link org.hyperic.hq.hqapi1.types.Metric} to query.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS}
* if the data was succesfully queried. The returned data can be retrieved
* via {@link org.hyperic.hq.hqapi1.types.LastMetricDataResponse#getLastMetricData()}.
*
* @throws IOException If a network error occurs while making the request.
*/
public LastMetricDataResponse getData(int metricId)
public LastMetricDataResponse getData(Metric metric)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("id", new String[] { Integer.toString(metricId) });
params.put("id", new String[] { Integer.toString(metric.getId()) });
return doGet("metricData/getLast.hqu", params, LastMetricDataResponse.class);
}

/**
* Get the {@link org.hyperic.hq.hqapi1.types.MetricData} for the
* given list of {@link org.hyperic.hq.hqapi1.types.Metric} ids.
* given List of {@link org.hyperic.hq.hqapi1.types.Metric}s.
*
* @param metricIds The ids of the {@link org.hyperic.hq.hqapi1.types.Metric}s to query.
* @param metrics The List of {@link org.hyperic.hq.hqapi1.types.Metric}s to query.
* @param start The start time to query, in epoch-millis.
* @param end The end time to query, in epoch-millis.
*
Expand All @@ -78,13 +79,13 @@ public LastMetricDataResponse getData(int metricId)
*
* @throws IOException If a network error occurs while making the request.
*/
public MetricsDataResponse getData(int[] metricIds, long start, long end)
public MetricsDataResponse getData(List<Metric> metrics, long start, long end)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
String[] ids = new String[metricIds.length];
for (int i = 0; i < metricIds.length; i++) {
ids[i] = Integer.toString(metricIds[i]);
String[] ids = new String[metrics.size()];
for (int i = 0; i < metrics.size(); i++) {
ids[i] = Integer.toString(metrics.get(i).getId());

}
params.put("id", ids);
Expand All @@ -95,35 +96,47 @@ public MetricsDataResponse getData(int[] metricIds, long start, long end)

/**
* Get the {@link org.hyperic.hq.hqapi1.types.LastMetricData} for the
* given list of {@link org.hyperic.hq.hqapi1.types.Metric} ids. This object
* represents the last metric collection for the given metric id.
* given List of {@link org.hyperic.hq.hqapi1.types.Metric}s. This object
* represents the last metric collection for the given Metric.
*
* @param metricIds The ids of the {@link org.hyperic.hq.hqapi1.types.Metric}s to query.
* @param metrics The List of {@link org.hyperic.hq.hqapi1.types.Metric}s to query.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS}
* if the data was succesfully queried. The returned data can be retrieved
* via {@link org.hyperic.hq.hqapi1.types.LastMetricsDataResponse#getLastMetricData()}.
*
* @throws IOException If a network error occurs while making the request.
*/
public LastMetricsDataResponse getData(int[] metricIds)
public LastMetricsDataResponse getData(List<Metric> metrics)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
String[] ids = new String[metricIds.length];
for (int i = 0; i < metricIds.length; i++) {
ids[i] = Integer.toString(metricIds[i]);
String[] ids = new String[metrics.size()];
for (int i = 0; i < metrics.size(); i++) {
ids[i] = Integer.toString(metrics.get(i).getId());

}
params.put("id", ids);
return doGet("metricData/getMultiLast.hqu", params, LastMetricsDataResponse.class);
}

public StatusResponse addData(int metricId, List<DataPoint> data)
/**
* Insert {@link org.hyperic.hq.hqapi1.types.DataPoint}s for the specified
* Metric.
*
* @param metric The Metric to insert data for.
* @param data A List of {@link org.hyperic.hq.hqapi1.types.DataPoint}s to insert.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if the
* data was sucessfully inserted.
*
* @throws IOException If a network error occurs while making the request.
*/
public StatusResponse addData(Metric metric, List<DataPoint> data)
throws IOException
{
DataPointsRequest request = new DataPointsRequest();
request.setMetricId(metricId);
request.setMetricId(metric.getId());
request.getDataPoint().addAll(data);

return doPost("metricData/put.hqu", request, StatusResponse.class);
Expand Down
2 changes: 1 addition & 1 deletion src/org/hyperic/hq/hqapi1/test/MetricDataAddData_test.java
Expand Up @@ -40,7 +40,7 @@ public void testAddData() throws Exception {
dps.add(dp);
}

StatusResponse response = dataApi.addData(m.getId(), dps);
StatusResponse response = dataApi.addData(m, dps);
hqAssertFailureNotImplemented(response);
}
}
38 changes: 17 additions & 21 deletions src/org/hyperic/hq/hqapi1/test/MetricDataGetLastMulti_test.java
Expand Up @@ -13,6 +13,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;

public class MetricDataGetLastMulti_test extends MetricDataTestBase {

Expand All @@ -31,12 +32,8 @@ public void testValidGet() throws Exception {
assertTrue("No metrics found for " + platform.getName(),
metricsResponse.getMetric().size() > 0);

int[] mids = new int[metricsResponse.getMetric().size()];
for (int i = 0; i < metricsResponse.getMetric().size(); i++) {
mids[i] = metricsResponse.getMetric().get(i).getId();
}

LastMetricsDataResponse dataResponse = dataApi.getData(mids);
LastMetricsDataResponse dataResponse =
dataApi.getData(metricsResponse.getMetric());
hqAssertSuccess(dataResponse);

for (LastMetricData metricData : dataResponse.getLastMetricData()) {
Expand All @@ -48,19 +45,22 @@ public void testGetInvalidMetricId() throws Exception {

MetricDataApi dataApi = getApi().getMetricDataApi();

int[] mids = { Integer.MAX_VALUE };
List<Metric> metrics = new ArrayList<Metric>();
Metric m = new Metric();
m.setId(Integer.MAX_VALUE);
metrics.add(m);

LastMetricsDataResponse dataResponse = dataApi.getData(mids);
LastMetricsDataResponse dataResponse = dataApi.getData(metrics);
hqAssertFailureObjectNotFound(dataResponse);
}

public void testGetEmptyMetricArray() throws Exception {
public void testGetEmptyMetricList() throws Exception {

MetricDataApi dataApi = getApi().getMetricDataApi();

int[] mids = {};
List<Metric> metrics = new ArrayList<Metric>();

LastMetricsDataResponse dataResponse = dataApi.getData(mids);
LastMetricsDataResponse dataResponse = dataApi.getData(metrics);
hqAssertFailureInvalidParameters(dataResponse);
}

Expand All @@ -75,21 +75,17 @@ public void testGetLastNoData() throws Exception {
assertTrue("No metrics found for " + platform.getName(),
metricsResponse.getMetric().size() > 0);

List<Metric> disabledMetrics = new ArrayList<Metric>();
for (Metric m : metricsResponse.getMetric()) {
if (!m.isEnabled()) {
disabledMetrics.add(m);
List<Metric> disabledMetrics = metricsResponse.getMetric();
for (Iterator<Metric> i = disabledMetrics.iterator(); i.hasNext();) {
Metric m = i.next();
if (m.isEnabled()) {
i.remove();
}
}

assertTrue("No disabled metrics could be found", disabledMetrics.size() > 0);

int[] mids = new int[disabledMetrics.size()];
for (int i = 0; i < disabledMetrics.size(); i++) {
mids[i] = disabledMetrics.get(i).getId();
}

LastMetricsDataResponse dataResponse = dataApi.getData(mids);
LastMetricsDataResponse dataResponse = dataApi.getData(disabledMetrics);
hqAssertSuccess(dataResponse);
// TODO: What is the correct behavior of the API if the last data point
// could not be found? Return an error for simply null?
Expand Down
11 changes: 8 additions & 3 deletions src/org/hyperic/hq/hqapi1/test/MetricDataGetLast_test.java
Expand Up @@ -7,6 +7,9 @@
import org.hyperic.hq.hqapi1.types.Metric;
import org.hyperic.hq.hqapi1.types.LastMetricDataResponse;

import java.util.List;
import java.util.Iterator;

public class MetricDataGetLast_test extends MetricDataTestBase {

public MetricDataGetLast_test(String name) {
Expand All @@ -25,15 +28,17 @@ public void testValidGet() throws Exception {

Metric m = metricsResponse.getMetric().get(0);

LastMetricDataResponse dataResponse = dataApi.getData(m.getId());
LastMetricDataResponse dataResponse = dataApi.getData(m);
hqAssertSuccess(dataResponse);

validateLastMetricData(dataResponse.getLastMetricData());
}

public void testGetInvalidMetricId() throws Exception {
MetricDataApi dataApi = getApi().getMetricDataApi();
LastMetricDataResponse dataResponse = dataApi.getData(Integer.MAX_VALUE);
Metric m = new Metric();
m.setId(Integer.MAX_VALUE);
LastMetricDataResponse dataResponse = dataApi.getData(m);
hqAssertFailureObjectNotFound(dataResponse);
}

Expand All @@ -57,7 +62,7 @@ public void testGetLastNoData() throws Exception {

assertNotNull("No disabled metric could be found", metric);

LastMetricDataResponse dataResponse = dataApi.getData(metric.getId());
LastMetricDataResponse dataResponse = dataApi.getData(metric);
hqAssertSuccess(dataResponse);
// TODO: What is the correct behavior of the API if the last data point
// could not be found? Return an error for simply null?
Expand Down
33 changes: 16 additions & 17 deletions src/org/hyperic/hq/hqapi1/test/MetricDataGetMulti_test.java
Expand Up @@ -6,6 +6,10 @@
import org.hyperic.hq.hqapi1.types.MetricsResponse;
import org.hyperic.hq.hqapi1.types.MetricsDataResponse;
import org.hyperic.hq.hqapi1.types.MetricData;
import org.hyperic.hq.hqapi1.types.Metric;

import java.util.ArrayList;
import java.util.List;

public class MetricDataGetMulti_test extends MetricDataTestBase {

Expand All @@ -24,14 +28,10 @@ public void testValidGet() throws Exception {
assertTrue("No metrics found for " + platform.getName(),
metricsResponse.getMetric().size() > 0);

int[] mids = new int[metricsResponse.getMetric().size()];
for (int i = 0; i < metricsResponse.getMetric().size(); i++) {
mids[i] = metricsResponse.getMetric().get(i).getId();
}

long end = System.currentTimeMillis();
long start = end - (8 * 60 * 60 * 1000);
MetricsDataResponse dataResponse = dataApi.getData(mids, start, end);
MetricsDataResponse dataResponse = dataApi.getData(metricsResponse.getMetric(),
start, end);
hqAssertSuccess(dataResponse);

for (MetricData metricData : dataResponse.getMetricData()) {
Expand All @@ -46,22 +46,25 @@ public void testGetInvalidMetricId() throws Exception {
long end = System.currentTimeMillis();
long start = end - (8 * 60 * 60 * 1000);

int[] mids = { Integer.MAX_VALUE };
List<Metric> metrics = new ArrayList<Metric>();
Metric m = new Metric();
m.setId(Integer.MAX_VALUE);
metrics.add(m);

MetricsDataResponse dataResponse = dataApi.getData(mids, start, end);
MetricsDataResponse dataResponse = dataApi.getData(metrics, start, end);
hqAssertFailureObjectNotFound(dataResponse);
}

public void testGetEmptyMetricArray() throws Exception {
public void testGetEmptyMetricList() throws Exception {

MetricDataApi dataApi = getApi().getMetricDataApi();

long end = System.currentTimeMillis();
long start = end - (8 * 60 * 60 * 1000);

int[] mids = {};
List<Metric> metrics = new ArrayList<Metric>();

MetricsDataResponse dataResponse = dataApi.getData(mids, start, end);
MetricsDataResponse dataResponse = dataApi.getData(metrics, start, end);
hqAssertFailureInvalidParameters(dataResponse);
}

Expand All @@ -76,14 +79,10 @@ public void testGetInvalidRange() throws Exception {
assertTrue("No metrics found for " + platform.getName(),
metricsResponse.getMetric().size() > 0);

int[] mids = new int[metricsResponse.getMetric().size()];
for (int i = 0; i < metricsResponse.getMetric().size(); i++) {
mids[i] = metricsResponse.getMetric().get(i).getId();
}

long end = System.currentTimeMillis();
long start = end - (8 * 60 * 60 * 1000);
MetricsDataResponse dataResponse = dataApi.getData(mids, end, start);
MetricsDataResponse dataResponse = dataApi.getData(metricsResponse.getMetric(),
end, start);
hqAssertFailureInvalidParameters(dataResponse);
}
}
10 changes: 6 additions & 4 deletions src/org/hyperic/hq/hqapi1/test/MetricDataGet_test.java
Expand Up @@ -28,7 +28,7 @@ public void testValidGet() throws Exception {

long end = System.currentTimeMillis();
long start = end - (8 * 60 * 60 * 1000);
MetricDataResponse dataResponse = dataApi.getData(m.getId(), start, end);
MetricDataResponse dataResponse = dataApi.getData(m, start, end);
hqAssertSuccess(dataResponse);

validateMetricData(dataResponse.getMetricData());
Expand All @@ -38,10 +38,12 @@ public void testGetInvalidMetricId() throws Exception {

MetricDataApi dataApi = getApi().getMetricDataApi();

Metric m = new Metric();
m.setId(Integer.MAX_VALUE);

long end = System.currentTimeMillis();
long start = end - (8 * 60 * 60 * 1000);
MetricDataResponse dataResponse = dataApi.getData(Integer.MAX_VALUE,
start, end);
MetricDataResponse dataResponse = dataApi.getData(m, start, end);
hqAssertFailureObjectNotFound(dataResponse);
}

Expand All @@ -60,7 +62,7 @@ public void testGetInvalidRange() throws Exception {

long end = System.currentTimeMillis();
long start = end - (8 * 60 * 60 * 1000);
MetricDataResponse dataResponse = dataApi.getData(m.getId(), start, end);
MetricDataResponse dataResponse = dataApi.getData(m, start, end);
hqAssertSuccess(dataResponse);
}
}

0 comments on commit f67fd3a

Please sign in to comment.