Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #496 from jsanda/hwkmetrics-384
[HWKMETRICS-384] Add string metric type
- Loading branch information
Showing
14 changed files
with
738 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
...metrics-api-jaxrs/src/main/java/org/hawkular/metrics/api/jaxrs/handler/StringHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* Copyright 2014-2016 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.handler; | ||
|
||
import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
|
||
import static org.hawkular.metrics.api.jaxrs.filter.TenantFilter.TENANT_HEADER_NAME; | ||
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.badRequest; | ||
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.serverError; | ||
import static org.hawkular.metrics.model.MetricType.STRING; | ||
|
||
import java.util.List; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.DefaultValue; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.HeaderParam; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.container.AsyncResponse; | ||
import javax.ws.rs.container.Suspended; | ||
|
||
import org.hawkular.metrics.api.jaxrs.handler.observer.ResultSetObserver; | ||
import org.hawkular.metrics.api.jaxrs.util.ApiUtils; | ||
import org.hawkular.metrics.core.service.Functions; | ||
import org.hawkular.metrics.core.service.MetricsService; | ||
import org.hawkular.metrics.core.service.Order; | ||
import org.hawkular.metrics.model.ApiError; | ||
import org.hawkular.metrics.model.DataPoint; | ||
import org.hawkular.metrics.model.Metric; | ||
import org.hawkular.metrics.model.MetricId; | ||
import org.hawkular.metrics.model.param.TimeRange; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
|
||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import io.swagger.annotations.ApiParam; | ||
import io.swagger.annotations.ApiResponse; | ||
import io.swagger.annotations.ApiResponses; | ||
import rx.Observable; | ||
|
||
/** | ||
* @author jsanda | ||
*/ | ||
@Path("/strings") | ||
@Consumes(APPLICATION_JSON) | ||
@Produces(APPLICATION_JSON) | ||
@Api(tags = "String", description = "This resource is experimental and changes may be made to it in subsequent " + | ||
"releases that are not backwards compatible.") | ||
public class StringHandler { | ||
|
||
@Inject | ||
private MetricsService metricsService; | ||
|
||
@HeaderParam(TENANT_HEADER_NAME) | ||
private String tenantId; | ||
|
||
@POST | ||
@Path("/{id}/raw") | ||
@ApiOperation(value = "Add data for a single string metric.") | ||
@ApiResponses(value = { | ||
@ApiResponse(code = 200, message = "Adding data succeeded."), | ||
@ApiResponse(code = 400, message = "Missing or invalid payload", response = ApiError.class), | ||
@ApiResponse(code = 500, message = "Unexpected error happened while storing the data", | ||
response = ApiError.class) | ||
}) | ||
public void addStringForMetric( | ||
@Suspended final AsyncResponse asyncResponse, @PathParam("id") String id, | ||
@ApiParam(value = "List of string datapoints", required = true) List<DataPoint<String>> data | ||
) { | ||
Observable<Metric<String>> metrics = Functions.dataPointToObservable(tenantId, id, data, | ||
STRING); | ||
Observable<Void> observable = metricsService.addDataPoints(STRING, metrics); | ||
observable.subscribe(new ResultSetObserver(asyncResponse)); | ||
} | ||
|
||
@POST | ||
@Path("/raw") | ||
@ApiOperation(value = "Add metric data for multiple string metrics in a single call.") | ||
@ApiResponses(value = { | ||
@ApiResponse(code = 200, message = "Adding data succeeded."), | ||
@ApiResponse(code = 400, message = "Missing or invalid payload", response = ApiError.class), | ||
@ApiResponse(code = 500, message = "Unexpected error happened while storing the data", | ||
response = ApiError.class) | ||
}) | ||
public void addStringData( | ||
@Suspended final AsyncResponse asyncResponse, | ||
@ApiParam(value = "List of string metrics", required = true) | ||
@JsonDeserialize() | ||
List<Metric<String>> availabilities | ||
) { | ||
Observable<Metric<String>> metrics = Functions.metricToObservable(tenantId, availabilities, | ||
STRING); | ||
Observable<Void> observable = metricsService.addDataPoints(STRING, metrics); | ||
observable.subscribe(new ResultSetObserver(asyncResponse)); | ||
} | ||
|
||
@GET | ||
@Path("/{id}/raw") | ||
@ApiOperation(value = "Retrieve string data.", response = DataPoint.class, responseContainer = "List") | ||
@ApiResponses(value = { | ||
@ApiResponse(code = 200, message = "Successfully fetched string data."), | ||
@ApiResponse(code = 204, message = "No string data was found."), | ||
@ApiResponse(code = 500, message = "Unexpected error occurred while fetching string data.", | ||
response = ApiError.class) | ||
}) | ||
public void findRawStringData( | ||
@Suspended AsyncResponse asyncResponse, | ||
@PathParam("id") String id, | ||
@ApiParam(value = "Defaults to now - 8 hours") @QueryParam("start") Long start, | ||
@ApiParam(value = "Defaults to now") @QueryParam("end") Long end, | ||
@ApiParam(value = "Set to true to return only distinct, contiguous values") | ||
@QueryParam("distinct") @DefaultValue("false") Boolean distinct, | ||
@ApiParam(value = "Limit the number of data points returned") @QueryParam("limit") Integer limit, | ||
@ApiParam(value = "Data point sort order, based on timestamp") @QueryParam("order") Order order | ||
) { | ||
|
||
TimeRange timeRange = new TimeRange(start, end); | ||
if (!timeRange.isValid()) { | ||
asyncResponse.resume(badRequest(new ApiError(timeRange.getProblem()))); | ||
return; | ||
} | ||
|
||
MetricId<String> metricId = new MetricId<>(tenantId, STRING, id); | ||
if (limit != null) { | ||
if (order == null) { | ||
if (start == null && end != null) { | ||
order = Order.DESC; | ||
} else if (start != null && end == null) { | ||
order = Order.ASC; | ||
} else { | ||
order = Order.DESC; | ||
} | ||
} | ||
} else { | ||
limit = 0; | ||
} | ||
|
||
if (order == null) { | ||
order = Order.DESC; | ||
} | ||
|
||
metricsService | ||
.findStringData(metricId, timeRange.getStart(), timeRange.getEnd(), distinct, limit, order) | ||
.toList() | ||
.map(ApiUtils::collectionToResponse) | ||
.subscribe(asyncResponse::resume, t -> asyncResponse.resume(serverError(t))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.