Skip to content

Commit

Permalink
Merge pull request #179 from tsegismont/jira/HWKMETRICS-35
Browse files Browse the repository at this point in the history
HWKMETRICS-35 Availability REST API Calculations Required for MVP2 given a date range
  • Loading branch information
jsanda committed Mar 20, 2015
2 parents 9aa52a7 + bc3f1e0 commit 587fe39
Show file tree
Hide file tree
Showing 20 changed files with 1,130 additions and 301 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@
import org.hawkular.metrics.api.jaxrs.callback.SimpleDataCallback;
import org.hawkular.metrics.api.jaxrs.param.Duration;
import org.hawkular.metrics.core.api.Availability;
import org.hawkular.metrics.core.api.AvailabilityBucketDataPoint;
import org.hawkular.metrics.core.api.AvailabilityMetric;
import org.hawkular.metrics.core.api.BucketDataPoint;
import org.hawkular.metrics.core.api.BucketedOutput;
import org.hawkular.metrics.core.api.Buckets;
import org.hawkular.metrics.core.api.Counter;
import org.hawkular.metrics.core.api.Metric;
import org.hawkular.metrics.core.api.MetricId;
import org.hawkular.metrics.core.api.MetricType;
import org.hawkular.metrics.core.api.MetricsService;
import org.hawkular.metrics.core.api.NumericBucketDataPoint;
import org.hawkular.metrics.core.api.NumericData;
import org.hawkular.metrics.core.api.NumericMetric;
import org.hawkular.metrics.core.impl.cassandra.MetricUtils;
Expand Down Expand Up @@ -443,11 +444,13 @@ public List<NumericData> apply(NumericMetric input) {
asyncResponse.resume(response);
return;
}
ListenableFuture<BucketedOutput> dataFuture = metricsService.findNumericStats(metric, start, end, buckets);
ListenableFuture<List<BucketDataPoint>> outputFuture = Futures.transform(
dataFuture, new Function<BucketedOutput, List<BucketDataPoint>>() {
ListenableFuture<BucketedOutput<NumericBucketDataPoint>> dataFuture = metricsService.findNumericStats(
metric, start, end, buckets
);
ListenableFuture<List<NumericBucketDataPoint>> outputFuture = Futures.transform(
dataFuture, new Function<BucketedOutput<NumericBucketDataPoint>, List<NumericBucketDataPoint>>() {
@Override
public List<BucketDataPoint> apply(BucketedOutput input) {
public List<NumericBucketDataPoint> apply(BucketedOutput<NumericBucketDataPoint> input) {
if (input == null) {
return null;
}
Expand Down Expand Up @@ -523,37 +526,86 @@ public void findPeriods(

@GET
@Path("/{tenantId}/metrics/availability/{id}/data")
@ApiOperation(value = "Retrieve availability data.", response = Availability.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully fetched availability data."),
@ApiResponse(code = 204, message = "No availability data was found.")})
public void findAvailabilityData(@Suspended final AsyncResponse asyncResponse,
@PathParam("tenantId") String tenantId, @PathParam("id") final String id,
@ApiParam(value = "Defaults to now - 8 hours", required = false) @QueryParam("start") Long start,
@ApiParam(value = "Defaults to now", required = false) @QueryParam("end") Long end) {

@ApiOperation(
value = "Retrieve availability data. When buckets or bucketDuration query parameter is used, the time "
+ "range between start and end will be divided in buckets of equal duration, and availability "
+ "statistics will be computed for each bucket.", response = List.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully fetched availability data."),
@ApiResponse(code = 204, message = "No availability data was found."),
@ApiResponse(code = 400, message = "buckets or bucketDuration parameter is invalid, or both are used.",
response = ApiError.class),
@ApiResponse(code = 500, message = "Unexpected error occurred while fetching availability data.",
response = ApiError.class),
})
public void findAvailabilityData(
@Suspended AsyncResponse asyncResponse,
@PathParam("tenantId") String tenantId,
@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 = "Total number of buckets") @QueryParam("buckets") Integer bucketsCount,
@ApiParam(value = "Bucket duration") @QueryParam("bucketDuration") Duration bucketDuration
) {
long now = System.currentTimeMillis();
if (start == null) {
start = now - EIGHT_HOURS;
}
if (end == null) {
end = now;
}
start = start == null ? now - EIGHT_HOURS : start;
end = end == null ? now : end;

AvailabilityMetric metric = new AvailabilityMetric(tenantId, new MetricId(id));
ListenableFuture<AvailabilityMetric> future = metricsService.findAvailabilityData(metric, start, end);

ListenableFuture<List<Availability>> outputfuture = Futures.transform(future,
new Function<AvailabilityMetric, List<Availability>>() {
if (bucketsCount == null && bucketDuration == null) {
ListenableFuture<AvailabilityMetric> dataFuture = metricsService.findAvailabilityData(metric, start, end);
ListenableFuture<List<Availability>> outputFuture = Futures.transform(
dataFuture, new Function<AvailabilityMetric, List<Availability>>() {
@Override
public List<Availability> apply(AvailabilityMetric input) {
if (input == null) {
return null;
}
return input.getData();
}
}
);
Futures.addCallback(outputFuture, new SimpleDataCallback<Object>(asyncResponse));
return;
}

if (bucketsCount != null && bucketDuration != null) {
ApiError apiError = new ApiError("Both buckets and bucketDuration parameters are used");
Response response = Response.status(Status.BAD_REQUEST).entity(apiError).build();
asyncResponse.resume(response);
return;
}

Buckets buckets;
try {
if (bucketsCount != null) {
buckets = Buckets.fromCount(start, end, bucketsCount);
} else {
buckets = Buckets.fromStep(start, end, bucketDuration.toMillis());
}
} catch (IllegalArgumentException e) {
ApiError apiError = new ApiError("Bucket: " + e.getMessage());
Response response = Response.status(Status.BAD_REQUEST).entity(apiError).build();
asyncResponse.resume(response);
return;
}
ListenableFuture<BucketedOutput<AvailabilityBucketDataPoint>> dataFuture = metricsService.findAvailabilityStats(
metric, start, end, buckets
);
ListenableFuture<List<AvailabilityBucketDataPoint>> outputFuture = Futures.transform(
dataFuture,
new Function<BucketedOutput<AvailabilityBucketDataPoint>, List<AvailabilityBucketDataPoint>>() {
@Override
public List<Availability> apply(AvailabilityMetric input) {
public List<AvailabilityBucketDataPoint> apply(BucketedOutput<AvailabilityBucketDataPoint> input) {
if (input == null) {
return null;
}
return input.getData();
}
});

Futures.addCallback(outputfuture, new SimpleDataCallback<List<Availability>>(asyncResponse));
}
);
Futures.addCallback(outputFuture, new SimpleDataCallback<Object>(asyncResponse));
}

@POST
Expand Down
38 changes: 0 additions & 38 deletions core/metrics-core-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,42 +107,4 @@

</dependencies>

<profiles>
<profile>
<id>testOnly</id>
<activation>
<property>
<name>skipTests</name>
<value>!true</value>
</property>
</activation>

<build>
<plugins>

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<property>
<name>keyspace</name>
<value>${test.keyspace}</value>
</property>
<property>
<name>nodes</name>
<value>${nodes}</value>
</property>
</systemProperties>
</configuration>
</plugin>

<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
</plugin>
</plugins>
</build>

</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* 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.core.api;

import static java.lang.Double.NaN;
import static java.lang.Double.isNaN;

import com.wordnik.swagger.annotations.ApiModel;
import com.wordnik.swagger.annotations.ApiModelProperty;

/**
* Statistics for availability data in a time range.
*
* @author Thomas Segismont
*/
@ApiModel(value = "Statistics for availability data in a time range.")
public class AvailabilityBucketDataPoint {
private long start;
private long end;
private long downtimeDuration;
private long lastDowntime;
private double uptimeRatio;
private long downtimeCount;

public AvailabilityBucketDataPoint(
long start,
long end,
long downtimeDuration,
long lastDowntime,
double uptimeRatio,
long downtimeCount
) {
this.start = start;
this.end = end;
this.downtimeDuration = downtimeDuration;
this.lastDowntime = lastDowntime;
this.uptimeRatio = uptimeRatio;
this.downtimeCount = downtimeCount;
}

@ApiModelProperty(value = "Start timestamp of this bucket in milliseconds since epoch")
public long getStart() {
return start;
}

public void setStart(long start) {
this.start = start;
}

@ApiModelProperty(value = "End timestamp of this bucket in milliseconds since epoch")
public long getEnd() {
return end;
}

public void setEnd(long end) {
this.end = end;
}

@ApiModelProperty(value = "Total downtime duration in milliseconds")
public long getDowntimeDuration() {
return downtimeDuration;
}

public void setDowntimeDuration(long downtimeDuration) {
this.downtimeDuration = downtimeDuration;
}

@ApiModelProperty(value = "Time of the last downtime in milliseconds since epoch")
public long getLastDowntime() {
return lastDowntime;
}

public void setLastDowntime(long lastDowntime) {
this.lastDowntime = lastDowntime;
}

@ApiModelProperty(value = "Ratio of uptime to the length of the bucket")
public double getUptimeRatio() {
return uptimeRatio;
}

public void setUptimeRatio(double uptimeRatio) {
this.uptimeRatio = uptimeRatio;
}

@ApiModelProperty(value = "Number of downtime periods in the bucket")
public long getDowntimeCount() {
return downtimeCount;
}

public void setDowntimeCount(long downtimeCount) {
this.downtimeCount = downtimeCount;
}

public boolean isEmpty() {
return isNaN(uptimeRatio);
}

@Override
public String toString() {
return "AvailabilityBucketDataPoint[" +
"start=" + start +
", end=" + end +
", downtimeDuration=" + downtimeDuration +
", lastDowntime=" + lastDowntime +
", uptimeRatio=" + uptimeRatio +
", downtimeCount=" + downtimeCount +
']';
}

/**
* Create {@link AvailabilityBucketDataPoint} instances following the builder pattern.
*/
public static class Builder {
private final long start;
private final long end;
private long downtimeDuration = 0;
private long lastDowntime = 0;
private double uptimeRatio = NaN;
private long downtimeCount = 0;

/**
* Creates a builder for an initially empty instance, configurable with the builder setters.
*
* @param start the start timestamp of this bucket data point
* @param end the end timestamp of this bucket data point
*/
public Builder(long start, long end) {
this.start = start;
this.end = end;
}

public Builder setDowntimeDuration(long downtimeDuration) {
this.downtimeDuration = downtimeDuration;
return this;
}

public Builder setLastDowntime(long lastDowntime) {
this.lastDowntime = lastDowntime;
return this;
}

public Builder setUptimeRatio(double uptimeRatio) {
this.uptimeRatio = uptimeRatio;
return this;
}

public Builder setDowntimeCount(long downtimeCount) {
this.downtimeCount = downtimeCount;
return this;
}

public AvailabilityBucketDataPoint build() {
return new AvailabilityBucketDataPoint(
start,
end,
downtimeDuration,
lastDowntime,
uptimeRatio,
downtimeCount
);
}
}
}

0 comments on commit 587fe39

Please sign in to comment.