From f67fd3adf2d5853b431f4cc7dbceecf8f784a9bf Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Tue, 21 Jul 2009 16:54:17 -0700 Subject: [PATCH] Change MetricDataApi's to take Metric objects rather than ids to avoid needing to convert Lists to arrays of ids when querying multiple metrics. --- src/org/hyperic/hq/hqapi1/MetricDataApi.java | 61 +++++++++++-------- .../hqapi1/test/MetricDataAddData_test.java | 2 +- .../test/MetricDataGetLastMulti_test.java | 38 ++++++------ .../hqapi1/test/MetricDataGetLast_test.java | 11 +++- .../hqapi1/test/MetricDataGetMulti_test.java | 33 +++++----- .../hq/hqapi1/test/MetricDataGet_test.java | 10 +-- .../hq/hqapi1/tools/MetricDataCommand.java | 15 +++-- 7 files changed, 92 insertions(+), 78 deletions(-) diff --git a/src/org/hyperic/hq/hqapi1/MetricDataApi.java b/src/org/hyperic/hq/hqapi1/MetricDataApi.java index 224f09ab..b02198ff 100644 --- a/src/org/hyperic/hq/hqapi1/MetricDataApi.java +++ b/src/org/hyperic/hq/hqapi1/MetricDataApi.java @@ -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; @@ -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. * @@ -33,11 +34,11 @@ 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 params = new HashMap(); - 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); @@ -45,10 +46,10 @@ public MetricDataResponse getData(int metricId, long start, long end) /** * 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 @@ -56,19 +57,19 @@ public MetricDataResponse getData(int metricId, long start, long end) * * @throws IOException If a network error occurs while making the request. */ - public LastMetricDataResponse getData(int metricId) + public LastMetricDataResponse getData(Metric metric) throws IOException { Map params = new HashMap(); - 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. * @@ -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 metrics, long start, long end) throws IOException { Map params = new HashMap(); - 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); @@ -95,10 +96,10 @@ 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 @@ -106,24 +107,36 @@ public MetricsDataResponse getData(int[] metricIds, long start, long end) * * @throws IOException If a network error occurs while making the request. */ - public LastMetricsDataResponse getData(int[] metricIds) + public LastMetricsDataResponse getData(List metrics) throws IOException { Map params = new HashMap(); - 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 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 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); diff --git a/src/org/hyperic/hq/hqapi1/test/MetricDataAddData_test.java b/src/org/hyperic/hq/hqapi1/test/MetricDataAddData_test.java index 2f4132b8..cce9c218 100644 --- a/src/org/hyperic/hq/hqapi1/test/MetricDataAddData_test.java +++ b/src/org/hyperic/hq/hqapi1/test/MetricDataAddData_test.java @@ -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); } } diff --git a/src/org/hyperic/hq/hqapi1/test/MetricDataGetLastMulti_test.java b/src/org/hyperic/hq/hqapi1/test/MetricDataGetLastMulti_test.java index 1f22cb6c..510b1c1f 100644 --- a/src/org/hyperic/hq/hqapi1/test/MetricDataGetLastMulti_test.java +++ b/src/org/hyperic/hq/hqapi1/test/MetricDataGetLastMulti_test.java @@ -13,6 +13,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Iterator; public class MetricDataGetLastMulti_test extends MetricDataTestBase { @@ -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()) { @@ -48,19 +45,22 @@ public void testGetInvalidMetricId() throws Exception { MetricDataApi dataApi = getApi().getMetricDataApi(); - int[] mids = { Integer.MAX_VALUE }; + List metrics = new ArrayList(); + 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 metrics = new ArrayList(); - LastMetricsDataResponse dataResponse = dataApi.getData(mids); + LastMetricsDataResponse dataResponse = dataApi.getData(metrics); hqAssertFailureInvalidParameters(dataResponse); } @@ -75,21 +75,17 @@ public void testGetLastNoData() throws Exception { assertTrue("No metrics found for " + platform.getName(), metricsResponse.getMetric().size() > 0); - List disabledMetrics = new ArrayList(); - for (Metric m : metricsResponse.getMetric()) { - if (!m.isEnabled()) { - disabledMetrics.add(m); + List disabledMetrics = metricsResponse.getMetric(); + for (Iterator 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? diff --git a/src/org/hyperic/hq/hqapi1/test/MetricDataGetLast_test.java b/src/org/hyperic/hq/hqapi1/test/MetricDataGetLast_test.java index 8cf9b537..ae8d8a0a 100644 --- a/src/org/hyperic/hq/hqapi1/test/MetricDataGetLast_test.java +++ b/src/org/hyperic/hq/hqapi1/test/MetricDataGetLast_test.java @@ -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) { @@ -25,7 +28,7 @@ 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()); @@ -33,7 +36,9 @@ public void testValidGet() throws Exception { 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); } @@ -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? diff --git a/src/org/hyperic/hq/hqapi1/test/MetricDataGetMulti_test.java b/src/org/hyperic/hq/hqapi1/test/MetricDataGetMulti_test.java index dc4b54ce..2ba9dfa7 100644 --- a/src/org/hyperic/hq/hqapi1/test/MetricDataGetMulti_test.java +++ b/src/org/hyperic/hq/hqapi1/test/MetricDataGetMulti_test.java @@ -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 { @@ -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()) { @@ -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 metrics = new ArrayList(); + 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 metrics = new ArrayList(); - MetricsDataResponse dataResponse = dataApi.getData(mids, start, end); + MetricsDataResponse dataResponse = dataApi.getData(metrics, start, end); hqAssertFailureInvalidParameters(dataResponse); } @@ -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); } } diff --git a/src/org/hyperic/hq/hqapi1/test/MetricDataGet_test.java b/src/org/hyperic/hq/hqapi1/test/MetricDataGet_test.java index 87efebc1..18250239 100644 --- a/src/org/hyperic/hq/hqapi1/test/MetricDataGet_test.java +++ b/src/org/hyperic/hq/hqapi1/test/MetricDataGet_test.java @@ -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()); @@ -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); } @@ -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); } } diff --git a/src/org/hyperic/hq/hqapi1/tools/MetricDataCommand.java b/src/org/hyperic/hq/hqapi1/tools/MetricDataCommand.java index 9e9561bd..25993901 100644 --- a/src/org/hyperic/hq/hqapi1/tools/MetricDataCommand.java +++ b/src/org/hyperic/hq/hqapi1/tools/MetricDataCommand.java @@ -39,6 +39,7 @@ import org.hyperic.hq.hqapi1.types.MetricsResponse; import org.hyperic.hq.hqapi1.types.ResourceResponse; import org.hyperic.hq.hqapi1.types.LastMetricData; +import org.hyperic.hq.hqapi1.types.MetricResponse; import java.util.Arrays; import java.util.Date; @@ -100,10 +101,13 @@ private void list(String[] args) throws Exception { } if (options.has(OPT_METRIC_ID)) { + MetricResponse metric = + metricApi.getMetric((Integer)getRequired(options, + OPT_METRIC_ID)); + checkSuccess(metric); MetricDataResponse data = - dataApi.getData((Integer)getRequired(options, OPT_METRIC_ID), - start, end); + dataApi.getData(metric.getMetric(), start, end); checkSuccess(data); System.out.println("Values for " + data.getMetricData().getMetricName() + @@ -122,12 +126,7 @@ private void list(String[] args) throws Exception { MetricsResponse metrics = metricApi.getMetrics(resource.getResource(), true); checkSuccess(metrics); - int[] ids = new int[metrics.getMetric().size()]; - for (int i = 0; i < metrics.getMetric().size(); i++) { - ids[i] = metrics.getMetric().get(i).getId(); - } - - LastMetricsDataResponse data = dataApi.getData(ids); + LastMetricsDataResponse data = dataApi.getData(metrics.getMetric()); checkSuccess(data); System.out.println("Last metric values for " + resource.getResource().getName());