Skip to content

Commit

Permalink
Some cleanup..
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Burman authored and John Sanda committed May 26, 2015
1 parent 7f9bad0 commit cfd8093
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public void getAvailabilityMetricTags(
metricsService.getMetricTags(tenantId, MetricType.AVAILABILITY, new MetricId(id)).subscribe(
optional -> asyncResponse.resume(ApiUtils.valueToResponse(optional)),
t -> asyncResponse.resume(ApiUtils.serverError(t)));
// @TODO Above is repeated code, refactor (GaugeHandler has it also)
}

@PUT
Expand Down Expand Up @@ -279,14 +278,8 @@ public void findAvailabilityData(
Availability metric = new Availability(tenantId, new MetricId(id));
if (bucketsCount == null && bucketDuration == null) {
metricsService.findAvailabilityData(tenantId, metric.getId(), startTime, endTime, distinct).toList()
// @TODO Refactor this subscriber.. repeated code
.subscribe(l -> {
if (l.isEmpty()) {
asyncResponse.resume(Response.noContent().build());
} else {
asyncResponse.resume(Response.ok(l).build());
}
}, t -> asyncResponse.resume(Response.serverError().entity(new ApiError(t.getMessage())).build()));
.map(ApiUtils::collectionToResponse)
.subscribe(r -> asyncResponse.resume(r), t -> ApiUtils.serverError(t));
} else if (bucketsCount != null && bucketDuration != null) {
asyncResponse.resume(badRequest(new ApiError("Both buckets and bucketDuration parameters are used")));
} else {
Expand All @@ -303,13 +296,9 @@ public void findAvailabilityData(
}

metricsService.findAvailabilityStats(metric, startTime, endTime, buckets).map(
BucketedOutput::getData).subscribe(m -> {
if (m.isEmpty()) {
asyncResponse.resume(Response.noContent().build());
} else {
asyncResponse.resume(Response.ok(m).build());
}
}, t -> asyncResponse.resume(Response.serverError().entity(new ApiError(t.getMessage())).build()));
BucketedOutput::getData)
.map(ApiUtils::collectionToResponse)
.subscribe(r -> asyncResponse.resume(r), t -> ApiUtils.serverError(t));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.executeAsync;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

import javax.inject.Inject;
Expand Down Expand Up @@ -289,7 +286,7 @@ public void findGaugeData(
long endTime = end == null ? now : end;

metricsService.findGaugeData(tenantId, new MetricId(id), startTime, endTime)
.reduce(new ArrayList<>(), ApiUtils::addToCollection)
.toList()
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> asyncResponse.resume(ApiUtils.serverError(t)));
} else {
Expand Down Expand Up @@ -417,23 +414,15 @@ public void findTaggedGaugeData(
@Suspended final AsyncResponse asyncResponse,
@ApiParam("Tag list") @PathParam("tags") Tags tags
) {
Observable<Map<MetricId, Set<GaugeData>>> gaugeDataByTags = metricsService.findGaugeDataByTags(tenantId,
tags.getTags());

gaugeDataByTags.map(input -> {
Map<String, Set<GaugeData>> result = new HashMap<>(input.size());
for (Map.Entry<MetricId, Set<GaugeData>> entry : input.entrySet()) {
result.put(entry.getKey().getName(), entry.getValue());
}
return result;
}).subscribe(m -> { // @TODO Repeated code, refactor and use Optional?
if (m.isEmpty()) {
asyncResponse.resume(Response.noContent().build());
} else {
asyncResponse.resume(Response.ok(m).build());
}
}, t -> asyncResponse.resume(Response.serverError().entity(new ApiError(t.getMessage())).build()));

metricsService.findGaugeDataByTags(tenantId, tags.getTags())
.flatMap(input -> Observable.from(input.entrySet())).toMap(e -> e.getKey().getName(), e2 -> e2.getValue())
.subscribe(m -> { // @TODO Repeated code
if (m.isEmpty()) {
asyncResponse.resume(Response.noContent().build());
} else {
asyncResponse.resume(Response.ok(m).build());
}
}, t -> asyncResponse.resume(ApiUtils.serverError(t)));
}

@POST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void findMetrics(

try {
metricsService.findMetrics(tenantId, MetricType.fromTextCode(type))
.reduce(new ArrayList<>(), ApiUtils::addToCollection)
.toList()
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> asyncResponse.resume(ApiUtils.serverError(t)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
package org.hawkular.metrics.api.jaxrs.handler.observer;

import org.hawkular.metrics.api.jaxrs.ApiError;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.core.Response;

import com.datastax.driver.core.ResultSet;

import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.core.Response;
import org.hawkular.metrics.api.jaxrs.util.ApiUtils;

import rx.Observer;

Expand All @@ -45,7 +45,7 @@ public void onCompleted() {

@Override
public void onError(Throwable t) {
asyncResponse.resume(Response.serverError().entity(new ApiError(t.getMessage())).build());
asyncResponse.resume(ApiUtils.serverError(t));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,24 @@
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.core.Response;

import org.hawkular.metrics.api.jaxrs.ApiError;

import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

import org.hawkular.metrics.api.jaxrs.ApiError;

/**
* @author jsanda
*/
public class ApiUtils {

public static <E, C extends Collection<E>> C addToCollection(C collection, E element) {
collection.add(element);
return collection;
public static Response collectionToResponse(Collection<?> collection) {
return collection.isEmpty() ? noContent() : Response.ok(collection).build();
}

public static Response collectionToResponse(Collection<?> collection) {
public static Response mapToResponse(Map<?, ?> collection) {
return collection.isEmpty() ? noContent() : Response.ok(collection).build();
}

Expand All @@ -59,19 +58,13 @@ public static Response valueToResponse(Optional<?> optional) {
return optional.map(value -> Response.ok(value).build()).orElse(noContent());
}

public static final Function<Void, Response> MAP_VOID = v -> Response.ok().build();

@Deprecated
public static final Function<List<Void>, Response> MAP_LIST_VOID = v -> Response.ok().build();

public static final Function<Optional<?>, Response> MAP_VALUE = optional ->
optional.map(value -> Response.ok(value).build()).orElse(noContent());

@Deprecated
public static final Function<Collection<?>, Response> MAP_COLLECTION = collection ->
collection.isEmpty() ? noContent() : Response.ok(collection).build();

public static final Function<Map<?, ?>, Response> MAP_MAP = map ->
map.isEmpty() ? noContent() : Response.ok(map).build();

public static Response noContent() {
return Response.noContent().build();
}
Expand Down

0 comments on commit cfd8093

Please sign in to comment.