Skip to content

Commit

Permalink
Merge pull request #778 from burmanm/hwkmetrics-625
Browse files Browse the repository at this point in the history
[HWKMETRICS-625] Change timestamp fetching to be an optional parameter
  • Loading branch information
jsanda committed Mar 17, 2017
2 parents 9cfca2b + 2664dea commit 3418410
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@
import org.hawkular.metrics.api.jaxrs.handler.observer.MetricCreatedObserver;
import org.hawkular.metrics.api.jaxrs.handler.observer.ResultSetObserver;
import org.hawkular.metrics.api.jaxrs.handler.template.IMetricsHandler;
import org.hawkular.metrics.api.jaxrs.handler.transformer.MinMaxTimestampTransformer;
import org.hawkular.metrics.api.jaxrs.param.TimeAndBucketParams;
import org.hawkular.metrics.api.jaxrs.param.TimeAndSortParams;
import org.hawkular.metrics.api.jaxrs.util.ApiUtils;
import org.hawkular.metrics.api.jaxrs.util.Logged;
import org.hawkular.metrics.core.service.Functions;
import org.hawkular.metrics.core.service.Order;
import org.hawkular.metrics.core.service.transformers.MinMaxTimestampTransformer;
import org.hawkular.metrics.model.ApiError;
import org.hawkular.metrics.model.AvailabilityBucketPoint;
import org.hawkular.metrics.model.AvailabilityType;
Expand Down Expand Up @@ -140,7 +140,9 @@ public void createMetric(
})
public void getMetrics(
@Suspended AsyncResponse asyncResponse,
@ApiParam(value = "List of tags filters", required = false) @QueryParam("tags") String tags) {
@ApiParam(value = "List of tags filters", required = false) @QueryParam("tags") String tags,
@ApiParam(value = "Fetch min and max timestamps of available datapoints") @DefaultValue("false")
@QueryParam("timestamps") Boolean fetchTimestamps) {

Observable<Metric<AvailabilityType>> metricObservable = null;
if (tags != null) {
Expand All @@ -149,8 +151,12 @@ public void getMetrics(
metricObservable = metricsService.findMetrics(getTenant(), AVAILABILITY);
}

if(fetchTimestamps) {
metricObservable = metricObservable
.compose(new MinMaxTimestampTransformer<>(metricsService));
}

metricObservable
.compose(new MinMaxTimestampTransformer<>(metricsService))
.toList()
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@
import org.hawkular.metrics.api.jaxrs.handler.observer.MetricCreatedObserver;
import org.hawkular.metrics.api.jaxrs.handler.observer.ResultSetObserver;
import org.hawkular.metrics.api.jaxrs.handler.template.IMetricsHandler;
import org.hawkular.metrics.api.jaxrs.handler.transformer.MinMaxTimestampTransformer;
import org.hawkular.metrics.api.jaxrs.param.TimeAndBucketParams;
import org.hawkular.metrics.api.jaxrs.param.TimeAndSortParams;
import org.hawkular.metrics.api.jaxrs.util.ApiUtils;
import org.hawkular.metrics.api.jaxrs.util.Logged;
import org.hawkular.metrics.core.service.Functions;
import org.hawkular.metrics.core.service.Order;
import org.hawkular.metrics.core.service.transformers.MinMaxTimestampTransformer;
import org.hawkular.metrics.model.ApiError;
import org.hawkular.metrics.model.Buckets;
import org.hawkular.metrics.model.DataPoint;
Expand Down Expand Up @@ -146,7 +146,9 @@ public void createMetric(
})
public void getMetrics(
@Suspended AsyncResponse asyncResponse,
@ApiParam(value = "List of tags filters", required = false) @QueryParam("tags") String tags) {
@ApiParam(value = "List of tags filters", required = false) @QueryParam("tags") String tags,
@ApiParam(value = "Fetch min and max timestamps of available datapoints") @DefaultValue("false")
@QueryParam("timestamps") Boolean fetchTimestamps) {

Observable<Metric<Long>> metricObservable = null;
if (tags != null) {
Expand All @@ -155,8 +157,12 @@ public void getMetrics(
metricObservable = metricsService.findMetrics(getTenant(), COUNTER);
}

if(fetchTimestamps) {
metricObservable = metricObservable
.compose(new MinMaxTimestampTransformer<>(metricsService));
}

metricObservable
.compose(new MinMaxTimestampTransformer<>(metricsService))
.toList()
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
import org.hawkular.metrics.api.jaxrs.handler.observer.MetricCreatedObserver;
import org.hawkular.metrics.api.jaxrs.handler.observer.ResultSetObserver;
import org.hawkular.metrics.api.jaxrs.handler.template.IMetricsHandler;
import org.hawkular.metrics.api.jaxrs.handler.transformer.MinMaxTimestampTransformer;
import org.hawkular.metrics.api.jaxrs.param.TimeAndBucketParams;
import org.hawkular.metrics.api.jaxrs.param.TimeAndSortParams;
import org.hawkular.metrics.api.jaxrs.util.ApiUtils;
import org.hawkular.metrics.api.jaxrs.util.Logged;
import org.hawkular.metrics.core.service.Functions;
import org.hawkular.metrics.core.service.Order;
import org.hawkular.metrics.core.service.transformers.MinMaxTimestampTransformer;
import org.hawkular.metrics.model.ApiError;
import org.hawkular.metrics.model.DataPoint;
import org.hawkular.metrics.model.Metric;
Expand Down Expand Up @@ -146,7 +146,9 @@ public void createMetric(
})
public void getMetrics(
@Suspended AsyncResponse asyncResponse,
@ApiParam(value = "List of tags filters") @QueryParam("tags") String tags) {
@ApiParam(value = "List of tags filters") @QueryParam("tags") String tags,
@ApiParam(value = "Fetch min and max timestamps of available datapoints") @DefaultValue("false")
@QueryParam("timestamps") Boolean fetchTimestamps) {

Observable<Metric<Double>> metricObservable = null;
if (tags != null) {
Expand All @@ -155,8 +157,12 @@ public void getMetrics(
metricObservable = metricsService.findMetrics(getTenant(), GAUGE);
}

if(fetchTimestamps) {
metricObservable = metricObservable
.compose(new MinMaxTimestampTransformer<>(metricsService));
}

metricObservable
.compose(new MinMaxTimestampTransformer<>(metricsService))
.toList()
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@

import org.hawkular.metrics.api.jaxrs.StatsQueryRequest;
import org.hawkular.metrics.api.jaxrs.handler.observer.MetricCreatedObserver;
import org.hawkular.metrics.api.jaxrs.handler.transformer.MinMaxTimestampTransformer;
import org.hawkular.metrics.api.jaxrs.param.DurationConverter;
import org.hawkular.metrics.api.jaxrs.param.PercentilesConverter;
import org.hawkular.metrics.api.jaxrs.util.ApiUtils;
import org.hawkular.metrics.api.jaxrs.util.Logged;
import org.hawkular.metrics.api.jaxrs.util.MetricTypeTextConverter;
import org.hawkular.metrics.core.service.Functions;
import org.hawkular.metrics.core.service.MetricsService;
import org.hawkular.metrics.core.service.transformers.MinMaxTimestampTransformer;
import org.hawkular.metrics.model.ApiError;
import org.hawkular.metrics.model.AvailabilityType;
import org.hawkular.metrics.model.BucketPoint;
Expand Down Expand Up @@ -198,6 +198,8 @@ public <T> void findMetrics(
@ApiParam(value = "Queried metric type", required = false, allowableValues = "gauge, availability, " +
"counter, string")
@QueryParam("type") MetricType<T> metricType,
@ApiParam(value = "Fetch min and max timestamps of available datapoints") @DefaultValue("false")
@QueryParam("timestamps") Boolean fetchTimestamps,
@ApiParam(value = "List of tags filters", required = false) @QueryParam("tags") String tags,
@ApiParam(value = "Regexp to match metricId if tags filtering is used, otherwise exact matching",
required = false) @QueryParam("id") String id) {
Expand Down Expand Up @@ -229,8 +231,12 @@ public <T> void findMetrics(
}
}

if(fetchTimestamps) {
metricObservable = metricObservable
.compose(new MinMaxTimestampTransformer<>(metricsService));
}

metricObservable
.compose(new MinMaxTimestampTransformer<>(metricsService))
.toList()
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@
import org.hawkular.metrics.api.jaxrs.handler.observer.MetricCreatedObserver;
import org.hawkular.metrics.api.jaxrs.handler.observer.ResultSetObserver;
import org.hawkular.metrics.api.jaxrs.handler.template.IMetricsHandler;
import org.hawkular.metrics.api.jaxrs.handler.transformer.MinMaxTimestampTransformer;
import org.hawkular.metrics.api.jaxrs.param.TimeAndSortParams;
import org.hawkular.metrics.api.jaxrs.util.ApiUtils;
import org.hawkular.metrics.api.jaxrs.util.Logged;
import org.hawkular.metrics.core.service.Functions;
import org.hawkular.metrics.core.service.Order;
import org.hawkular.metrics.core.service.transformers.MinMaxTimestampTransformer;
import org.hawkular.metrics.model.ApiError;
import org.hawkular.metrics.model.DataPoint;
import org.hawkular.metrics.model.Metric;
Expand Down Expand Up @@ -129,7 +129,9 @@ public void createMetric(
})
public void getMetrics(
@Suspended AsyncResponse asyncResponse,
@ApiParam(value = "List of tags filters") @QueryParam("tags") String tags) {
@ApiParam(value = "List of tags filters") @QueryParam("tags") String tags,
@ApiParam(value = "Fetch min and max timestamps of available datapoints") @DefaultValue("false")
@QueryParam("timestamps") Boolean fetchTimestamps) {

Observable<Metric<String>> metricObservable = null;
if (tags != null) {
Expand All @@ -138,8 +140,12 @@ public void getMetrics(
metricObservable = metricsService.findMetrics(getTenant(), STRING);
}

if(fetchTimestamps) {
metricObservable = metricObservable
.compose(new MinMaxTimestampTransformer<>(metricsService));
}

metricObservable
.compose(new MinMaxTimestampTransformer<>(metricsService))
.toList()
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public interface IMetricsHandler<T> {

//Metric
void getMetrics(AsyncResponse asyncResponse, String tags);
void getMetrics(AsyncResponse asyncResponse, String tags, Boolean fetchTimestamps);

void createMetric(AsyncResponse asyncResponse, Metric<T> metric, Boolean overwrite, UriInfo uriInfo);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 Red Hat, Inc. and/or its affiliates
* Copyright 2014-2017 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,8 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hawkular.metrics.api.jaxrs.handler.transformer;
package org.hawkular.metrics.core.service.transformers;

import org.hawkular.metrics.core.service.MetricsService;
import org.hawkular.metrics.core.service.Order;
Expand Down Expand Up @@ -43,9 +42,8 @@ public Observable<Metric<T>> call(Observable<Metric<T>> metricObservable) {
long now = System.currentTimeMillis();
MetricId<T> metricId = metric.getMetricId();
return metricsService.findDataPoints(metricId, 0, now, 1, Order.ASC)
.zipWith(metricsService.findDataPoints(metricId, 0, now, 1, Order.DESC), (p1, p2) -> {
return new Metric<>(metric, p1.getTimestamp(), p2.getTimestamp());
})
.zipWith(metricsService.findDataPoints(metricId, 0, now, 1, Order.DESC), (p1, p2)
-> new Metric<>(metric, p1.getTimestamp(), p2.getTimestamp()))
.switchIfEmpty(Observable.just(metric));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,16 @@ class AvailabilityITest extends RESTTest {
assertEquals(1, response.data.minTimestamp)
assertEquals(4, response.data.maxTimestamp)

response = hawkularMetrics.get(path: "availability", headers: [(tenantHeaderName): tenantId])
response = hawkularMetrics.get(path: "availability", query: [timestamps: 'true'], headers: [(tenantHeaderName):
tenantId])
assertEquals(200, response.status)
assertContentEncoding(response)
def metric = (response.data as List).find { it.id.equals(metricId) }
assertEquals(1, metric.minTimestamp)
assertEquals(4, metric.maxTimestamp)

response = hawkularMetrics.get(path: "metrics", headers: [(tenantHeaderName): tenantId])
response = hawkularMetrics.get(path: "metrics", headers: [(tenantHeaderName): tenantId], query:
['timestamps': 'true'])
assertEquals(200, response.status)
assertContentEncoding(response)
metric = (response.data as List).find { it.id.equals(metricId) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 Red Hat, Inc. and/or its affiliates
* Copyright 2014-2017 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -132,7 +132,8 @@ class CORSITest extends RESTTest {
assertEquals(200, response.status)

// Now query for the gauge metrics
response = hawkularMetrics.get(path: "metrics", query: [type: "gauge"], headers: [(tenantHeaderName): tenantId])
response = hawkularMetrics.get(path: "metrics", query: [type: "gauge", timestamps: "true"], headers: [
(tenantHeaderName): tenantId])

assertEquals(200, response.status)
assertEquals([
Expand Down Expand Up @@ -162,7 +163,7 @@ class CORSITest extends RESTTest {
assertEquals(responseHeaders, (72 * 60 * 60) + "", response.headers[ACCESS_CONTROL_MAX_AGE].value)

//Requery "metrics" endpoint to make sure data gets returned and check headers
response = hawkularMetrics.get(path: "metrics", query: [type: "gauge"],
response = hawkularMetrics.get(path: "metrics", query: [type: "gauge", timestamps: "true"],
headers: [
(tenantHeaderName): tenantId,
(ORIGIN): testOrigin
Expand Down Expand Up @@ -209,7 +210,7 @@ class CORSITest extends RESTTest {
assertEquals(200, response.status)

// Now query for the gauge metrics
response = hawkularMetrics.get(path: "metrics", query: [type: "gauge"], headers: [(tenantHeaderName): tenantId])
response = hawkularMetrics.get(path: "metrics", query: [type: "gauge", timestamps: "true"], headers: [(tenantHeaderName): tenantId])

assertEquals(200, response.status)
assertEquals([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 Red Hat, Inc. and/or its affiliates
* Copyright 2014-2017 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -487,7 +487,8 @@ class CassandraBackendITest extends RESTTest {
assertEquals(201, response.status)

// Now query for the gauge metrics
response = hawkularMetrics.get(path: "metrics", query: [type: 'gauge'], headers: [(tenantHeaderName): tenantId])
response = hawkularMetrics.get(path: "metrics", query: [type: 'gauge', timestamps: 'true'], headers: [
(tenantHeaderName): tenantId])

assertEquals(200, response.status)
assertEquals([
Expand Down Expand Up @@ -524,7 +525,8 @@ class CassandraBackendITest extends RESTTest {
assertEquals(201, response.status)

// Query for the availability metrics
response = hawkularMetrics.get(path: "metrics", query: [type: 'availability'], headers: [(tenantHeaderName): tenantId])
response = hawkularMetrics.get(path: "metrics", query: [type: 'availability', timestamps: 'true'], headers: [
(tenantHeaderName): tenantId])

assertEquals(200, response.status)
assertEquals([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,8 @@ Actual: ${response.data}
])
assertEquals(201, response.status)

response = hawkularMetrics.get(path: "counters/${metricId}", headers: [(tenantHeaderName): tenantId])
response = hawkularMetrics.get(path: "counters/${metricId}", query: [timestamps: "true"], headers: [
(tenantHeaderName): tenantId])
assertEquals(200, response.status)
assertFalse("Metric should not have the minTimestamp attribute: ${response.data}", response.data.containsKey('minTimestamp'))
assertFalse("Metric should not have the maxTimestamp attribute: ${response.data}", response.data.containsKey('maxTimestamp'))
Expand Down Expand Up @@ -1757,14 +1758,16 @@ Actual: ${response.data}
assertEquals(1, response.data.minTimestamp)
assertEquals(4, response.data.maxTimestamp)

response = hawkularMetrics.get(path: "counters", headers: [(tenantHeaderName): tenantId])
response = hawkularMetrics.get(path: "counters", query: [timestamps: "true"], headers: [(tenantHeaderName):
tenantId])
assertEquals(200, response.status)
assertContentEncoding(response)
def metric = (response.data as List).find { it.id.equals(metricId) }
assertEquals(1, metric.minTimestamp)
assertEquals(4, metric.maxTimestamp)

response = hawkularMetrics.get(path: "metrics", headers: [(tenantHeaderName): tenantId])
response = hawkularMetrics.get(path: "metrics", query: [timestamps: "true"], headers: [(tenantHeaderName):
tenantId])
assertEquals(200, response.status)
assertContentEncoding(response)
metric = (response.data as List).find { it.id.equals(metricId) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,15 @@ class GaugesITest extends RESTTest {
assertEquals(1, response.data.minTimestamp)
assertEquals(4, response.data.maxTimestamp)

response = hawkularMetrics.get(path: "gauges", headers: [(tenantHeaderName): tenantId])
response = hawkularMetrics.get(path: "gauges", query: [timestamps: "true"], headers: [(tenantHeaderName): tenantId])
assertEquals(200, response.status)
assertContentEncoding(response)
def metric = (response.data as List).find { it.id.equals(metricId) }
assertEquals(1, metric.minTimestamp)
assertEquals(4, metric.maxTimestamp)

response = hawkularMetrics.get(path: "metrics", headers: [(tenantHeaderName): tenantId])
response = hawkularMetrics.get(path: "metrics", query: [timestamps: "true"], headers: [(tenantHeaderName):
tenantId])
assertEquals(200, response.status)
assertContentEncoding(response)
metric = (response.data as List).find { it.id.equals(metricId) }
Expand Down

0 comments on commit 3418410

Please sign in to comment.