Skip to content

Commit

Permalink
Split availability and counter end-points to separate handler files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Negrea committed Apr 20, 2015
1 parent 02e16e2 commit 3469aed
Show file tree
Hide file tree
Showing 7 changed files with 509 additions and 436 deletions.
12 changes: 6 additions & 6 deletions api/metrics-api-jaxrs/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ meta data and data retention.

*Request URI*

POST /hawkular-metrics/{tenantId}/metrics/availability
POST /hawkular-metrics/{tenantId}/availability

*Example Request*

----
curl http://localhost:8080/hawkular-metrics/{tenantId}/metrics/availability -d@request.json -HContent-Type:application/json
curl http://localhost:8080/hawkular-metrics/{tenantId}/availability -d@request.json -HContent-Type:application/json
----

*Description*
Expand Down Expand Up @@ -258,7 +258,7 @@ points where each one consists of `timestamp` and `value` properties.

*Request URI*

POST /hawkular-metrics/{tenantId}/metrics/availability/{id}/data
POST /hawkular-metrics/{tenantId}/availability/{id}/data

*Request Body*

Expand All @@ -273,7 +273,7 @@ POST /hawkular-metrics/{tenantId}/metrics/availability/{id}/data
*Example Request*

----
curl -X POST http://localhost:8080/hawkular-metrics/{tenantId}/metrics/availability/{id}/data -d@request.json -HContent-Type:application/json
curl -X POST http://localhost:8080/hawkular-metrics/{tenantId}/availability/{id}/data -d@request.json -HContent-Type:application/json
----

*Description*
Expand Down Expand Up @@ -323,7 +323,7 @@ Insert data for multiple numeric metrics.

*Request URI*

POST /hawkular-metrics/{tenantId}/metrics/availability/data
POST /hawkular-metrics/{tenantId}/availability/data

*Request Body*

Expand All @@ -350,7 +350,7 @@ POST /hawkular-metrics/{tenantId}/metrics/availability/data
*Example Request*

----
curl -X POST http://localhost:8080/hawkular-metrics/{tenantId}/metrics/availability/data -d@request.json -HContent-Type:application/json
curl -X POST http://localhost:8080/hawkular-metrics/{tenantId}/availability/data -d@request.json -HContent-Type:application/json
----

*Description*
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright 2014-2015 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");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.api.jaxrs;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.hawkular.metrics.core.api.MetricsService.DEFAULT_TENANT_ID;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Response;

import org.hawkular.metrics.api.jaxrs.callback.NoDataCallback;
import org.hawkular.metrics.api.jaxrs.callback.SimpleDataCallback;
import org.hawkular.metrics.core.api.Counter;
import org.hawkular.metrics.core.api.MetricsService;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

/**
* @author Stefan Negrea
*
*/
@Path("/")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@Api(value = "", description = "Availability metrics interface")
public class CounterHandler {
@Inject
private MetricsService metricsService;

@POST
@Path("/{tenantId}/counters")
@ApiOperation(value = "List of counter definitions", hidden = true)
public void updateCountersForGroups(@Suspended final AsyncResponse asyncResponse, Collection<Counter> counters) {
ListenableFuture<Void> future = metricsService.updateCounters(counters);
Futures.addCallback(future, new NoDataCallback<>(asyncResponse));
}

@POST
@Path("/{tenantId}/counters/{group}")
@ApiOperation(value = "Update multiple counters in a single counter group", hidden = true)
public void updateCounterForGroup(@Suspended final AsyncResponse asyncResponse, @PathParam("group") String group,
Collection<Counter> counters) {
for (Counter counter : counters) {
counter.setGroup(group);
}
ListenableFuture<Void> future = metricsService.updateCounters(counters);
Futures.addCallback(future, new NoDataCallback<>(asyncResponse));
}

@POST
@Path("/{tenantId}/counters/{group}/{counter}")
@ApiOperation(value = "Increase value of a counter", hidden = true)
public void updateCounter(@Suspended final AsyncResponse asyncResponse, @PathParam("group") String group,
@PathParam("counter") String counter) {
ListenableFuture<Void> future = metricsService
.updateCounter(new Counter(DEFAULT_TENANT_ID, group, counter, 1L));
Futures.addCallback(future, new NoDataCallback<>(asyncResponse));
}

@POST
@Path("/{tenantId}/counters/{group}/{counter}/{value}")
@ApiOperation(value = "Update value of a counter", hidden = true)
public void updateCounter(@Suspended final AsyncResponse asyncResponse, @PathParam("group") String group,
@PathParam("counter") String counter, @PathParam("value") Long value) {
ListenableFuture<Void> future = metricsService.updateCounter(new Counter(DEFAULT_TENANT_ID, group, counter,
value));
Futures.addCallback(future, new NoDataCallback<>(asyncResponse));
}

@GET
@Path("/{tenantId}/counters/{group}")
@ApiOperation(value = "Retrieve a list of counter values in this group", hidden = true,
response = Counter.class, responseContainer = "List")
@Produces({ APPLICATION_JSON })
public void getCountersForGroup(@Suspended final AsyncResponse asyncResponse, @PathParam("group") String group) {
ListenableFuture<List<Counter>> future = metricsService.findCounters(group);
Futures.addCallback(future, new SimpleDataCallback<>(asyncResponse));
}

@GET
@Path("/{tenantId}/counters/{group}/{counter}")
@ApiOperation(value = "Retrieve value of a counter", hidden = true,
response = Counter.class, responseContainer = "List")
public void getCounter(@Suspended final AsyncResponse asyncResponse, @PathParam("group") final String group,
@PathParam("counter") final String counter) {
ListenableFuture<List<Counter>> future = metricsService.findCounters(group, Collections.singletonList(counter));
Futures.addCallback(future, new FutureCallback<List<Counter>>() {
@Override
public void onSuccess(List<Counter> counters) {
if (counters.isEmpty()) {
asyncResponse.resume(Response.status(404)
.entity("Counter[group: " + group + ", name: " + counter + "] not found").build());
} else {
Response jaxrs = Response.ok(counters.get(0)).build();
asyncResponse.resume(jaxrs);
}
}

@Override
public void onFailure(Throwable t) {
asyncResponse.resume(t);
}
});
}
}

0 comments on commit 3469aed

Please sign in to comment.