Skip to content

Commit

Permalink
[HHQ-3216] Split out metric data APIs into MetricDataApi.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Jul 21, 2009
1 parent c957a74 commit fca49cd
Show file tree
Hide file tree
Showing 17 changed files with 1,106 additions and 9 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
@@ -1,6 +1,14 @@

Changes in HQApi 3.0

*) [HHQ-3216] Revamp metricData CLI command to allow metrics to be pulled
for a single resource in addition to metrics. Allow different time
windows to be specified and format output in human readable form rather
than XML.

*) [HHQ-3216] Split out APIs for pulling or adding Metric data into a new
MetricDataApi. Old methods for pulling metric data have been deprecated.

*) [HHQ-3244] Add better error handling to Resource create and updates.

*) MetricApi.getMetrics(Resource r) and MetricApi.getEnabledMetrics(Resource r)
Expand Down
12 changes: 12 additions & 0 deletions hqu/hqapi1/app/MetricController.groovy
Expand Up @@ -42,6 +42,9 @@ class MetricController extends ApiController {
}
}

/**
* @deprecated
*/
private Closure getMetricDataXML(r) {
{ doc ->
MetricData(resourceId: r.resource.id,
Expand Down Expand Up @@ -334,6 +337,9 @@ class MetricController extends ApiController {
}
}

/**
* @deprecated
*/
def getData(params) {
def metricId = params.getOne("metricId")?.toInteger()
def start = params.getOne("start")?.toLong()
Expand Down Expand Up @@ -380,6 +386,9 @@ class MetricController extends ApiController {
}
}

/**
* @deprecated
*/
def getGroupData(params) {
def groupId = params.getOne("groupId")?.toInteger()
def templateId = params.getOne("templateId")?.toInteger()
Expand Down Expand Up @@ -459,6 +468,9 @@ class MetricController extends ApiController {
}
}

/**
* @deprecated
*/
def getResourceData(params) {

def ids = params["ids"]
Expand Down
222 changes: 222 additions & 0 deletions hqu/hqapi1/app/MetricdataController.groovy
@@ -0,0 +1,222 @@
import org.hyperic.hq.hqapi1.ErrorCode;

class MetricdataController extends ApiController {

private Closure getMetricDataXML(r) {
{ doc ->
MetricData(resourceId: r.resource.id,
resourceName: r.resource.name,
metricId: r.metric.id,
metricName: r.metric.template.name) {
// TODO: Backend does not always return data in asending order
for (dp in r.data.sort {a, b -> a.timestamp <=> b.timestamp}) {
DataPoint(timestamp : dp.timestamp,
value : dp.value)
}
}
}
}

private Closure getLastMetricDataXML(r) {
{ doc ->
LastMetricData(resourceId: r.resource.id,
resourceName: r.resource.name,
metricId: r.metric.id,
metricName: r.metric.template.name) {
if (!r.data) {
log.warn("No data found for metric id=" + r.metric.id)
} else {
DataPoint(timestamp : r.data.timestamp,
value : r.data.value)
}
}
}
}

/**
* Validate metric parameters, returning a Closure representing the error
* or null if the parameters are valid
*/
private Closure validateParameters(metricIds, start, end) {

if (!start) {
return getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Start time not given")
}
if (!end) {
return getFailureXML(ErrorCode.INVALID_PARAMETERS,
"End time not given")
}
if (end < start) {
return getFailureXML(ErrorCode.INVALID_PARAMETERS,
"End time cannot be < start time")
}
return validateParameters(metricIds)
}

/**
* Validate metric parameters, returning a Closure representing the error
* or null if the parameters are valid
*/
private Closure validateParameters(metricIds) {
if (metricIds == null || metricIds.size() == 0) {
return getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Metric id not given")
}

for (mid in metricIds) {
if (!mid) {
return getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Metric id not given")
}
def metric = metricHelper.findMeasurementById(mid)
if (!metric) {
return getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Metric id " + mid + " not found")
}
}
return null
}

def get(params) {
def metricId = params.getOne("id")?.toInteger()
def start = params.getOne("start")?.toLong()
def end = params.getOne("end")?.toLong()

def failureXml = validateParameters([metricId], start, end)
def metric = metricHelper.findMeasurementById(metricId)
def data
if (!failureXml) {
try {
data = metric.getData(start, end)
} catch (Exception e) {
log.error("UnexpectedError: " + e.getMessage(), e);
failureXml = getFailureXML(ErrorCode.UNEXPECTED_ERROR)
}
}

renderXml() {
MetricDataResponse() {
if (failureXml) {
out << failureXml
} else {
def result = [resource: metric.resource, metric: metric,
data: data]
out << getSuccessXML()
out << getMetricDataXML(result)
}
}
}
}

def getLast(params) {
def metricId = params.getOne("id")?.toInteger()

def failureXml = validateParameters([metricId])
def metric = metricHelper.findMeasurementById(metricId)
def data
if (!failureXml) {
try {
data = metric.getLastDataPoint()
} catch (Exception e) {
log.error("UnexpectedError: " + e.getMessage(), e);
failureXml = getFailureXML(ErrorCode.UNEXPECTED_ERROR)
}
}

renderXml() {
LastMetricDataResponse() {
if (failureXml) {
out << failureXml
} else {
def result = [resource: metric.resource, metric: metric,
data: data]
out << getSuccessXML()
out << getLastMetricDataXML(result)
}
}
}
}

def getMulti(params) {
def metricIds = params.get("id")*.toInteger()
def start = params.getOne("start")?.toLong()
def end = params.getOne("end")?.toLong()

def failureXml = validateParameters(metricIds, start, end)

def results = []

if (!failureXml) {
for (m in metricIds) {
try {
// TODO: Switch to collections based API
def metric = metricHelper.findMeasurementById(m)
def data = metric.getData(start, end)
results << [resource: metric.resource,
metric: metric, data: data]
} catch (Exception e) {
log.error("UnexpectedError: " + e.getMessage(), e);
failureXml = getFailureXML(ErrorCode.UNEXPECTED_ERROR)
}
}
}

renderXml() {
MetricsDataResponse() {
if (failureXml) {
out << failureXml
} else {
out << getSuccessXML()
for (result in results) {
out << getMetricDataXML(result)
}
}
}
}
}

def getMultiLast(params) {
def metricIds = params.get("id")*.toInteger()

def failureXml = validateParameters(metricIds)

def results = []

if (!failureXml) {
for (m in metricIds) {
try {
def metric = metricHelper.findMeasurementById(m)
def data = metric.getLastDataPoint()
results << [resource: metric.resource,
metric: metric, data: data]
} catch (Exception e) {
log.error("UnexpectedError: " + e.getMessage(), e);
failureXml = getFailureXML(ErrorCode.UNEXPECTED_ERROR)
}
}
}

renderXml() {
LastMetricsDataResponse() {
if (failureXml) {
out << failureXml
} else {
out << getSuccessXML()
for (result in results) {
out << getLastMetricDataXML(result)
}
}
}
}
}

def put(params) {

renderXml() {
StatusResponse() {
out << getFailureXML(ErrorCode.NOT_IMPLEMENTED);
}
}
}
}
12 changes: 12 additions & 0 deletions src/org/hyperic/hq/hqapi1/HQApi.java
Expand Up @@ -47,6 +47,7 @@ public class HQApi {
private final ResourceEdgeApi _resourceEdgeApi;
private final ServerConfigApi _serverConfigApi;
private final AlertApi _alertApi;
private final MetricDataApi _metricDataApi;

/**
* @param host The hostname of the HQ Server to connect to.
Expand All @@ -72,6 +73,7 @@ public HQApi(String host, int port, boolean isSecure, String user,
_resourceEdgeApi = new ResourceEdgeApi(connection);
_serverConfigApi = new ServerConfigApi(connection);
_alertApi = new AlertApi(connection);
_metricDataApi = new MetricDataApi(connection);
}

/**
Expand Down Expand Up @@ -119,6 +121,16 @@ public MetricApi getMetricApi() {
return _metricApi;
}

/**
* Import or Export Metric data
*
* @return The API for querying or reporting Metric information.
*/
public MetricDataApi getMetricDataApi() {
return _metricDataApi;
}


/**
* Add, remove and assign escalations.
*
Expand Down
3 changes: 3 additions & 0 deletions src/org/hyperic/hq/hqapi1/MetricApi.java
Expand Up @@ -248,6 +248,7 @@ public StatusResponse syncMetricTemplates(List<MetricTemplate> templates)
* via {@link org.hyperic.hq.hqapi1.types.MetricDataResponse#getMetricData()}.
*
* @throws IOException If a network error occurs while making the request.
* @deprecated See {@link org.hyperic.hq.hqapi1.MetricDataApi}.
*/
public MetricDataResponse getMetricData(int metricId, long start, long end)
throws IOException
Expand Down Expand Up @@ -277,6 +278,7 @@ public MetricDataResponse getMetricData(int metricId, long start, long end)
* via {@link org.hyperic.hq.hqapi1.types.MetricsDataResponse#getMetricData()}.
*
* @throws IOException If a network error occurs while making the request.
* @deprecated See {@link org.hyperic.hq.hqapi1.MetricDataApi}.
*/
public MetricsDataResponse getMetricData(int groupId, int templateId,
long start, long end)
Expand Down Expand Up @@ -311,6 +313,7 @@ public MetricsDataResponse getMetricData(int groupId, int templateId,
* via {@link org.hyperic.hq.hqapi1.types.MetricsDataResponse#getMetricData()}.
*
* @throws IOException If a network error occurs while making the request.
* @deprecated See {@link org.hyperic.hq.hqapi1.MetricDataApi}.
*/
public MetricsDataResponse getMetricData(int[] resourceIds, int templateId,
long start, long end)
Expand Down

0 comments on commit fca49cd

Please sign in to comment.