Skip to content

Commit

Permalink
[HWKMETRICS-185] Replace first and last calls with a method that retu…
Browse files Browse the repository at this point in the history
…rns a null default for cases where nothing gets emitted.
  • Loading branch information
Stefan Negrea committed Aug 13, 2015
1 parent dc25488 commit 48d6d9d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ public Response getAvailabilityMetric(@HeaderParam("tenantId") String tenantId,
.map(MetricDefinition::new)
.map(metricDef -> Response.ok(metricDef).build())
.switchIfEmpty(Observable.just(noContent()))
.toBlocking().first();
.toBlocking()
.firstOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -159,7 +160,10 @@ public Response getAvailabilityMetricTags(
Observable<Optional<Map<String, String>>> something = metricsService
.getMetricTags(new MetricId(tenantId, AVAILABILITY, id));
try {
return something.map(optional -> valueToResponse(optional)).toBlocking().last();
return something
.map(optional -> valueToResponse(optional))
.toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -179,7 +183,11 @@ public Response updateAvailabilityMetricTags(
) {
Metric<AvailabilityType> metric = new Metric<>(new MetricId(tenantId, AVAILABILITY, id));
try {
return metricsService.addTags(metric, tags).map(ApiUtils::simpleOKResponse).toBlocking().last();
return metricsService
.addTags(metric, tags)
.map(ApiUtils::simpleOKResponse)
.toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -200,8 +208,11 @@ public Response deleteAvailabilityMetricTags(
Metric<AvailabilityType> metric = new Metric<>(new MetricId(tenantId, AVAILABILITY, id));

try {
return metricsService.deleteTags(metric, tags.getTags()).map(ApiUtils::simpleOKResponse).toBlocking()
.last();
return metricsService
.deleteTags(metric, tags.getTags())
.map(ApiUtils::simpleOKResponse)
.toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -224,8 +235,11 @@ public Response addAvailabilityForMetric(
requestToAvailabilityDataPoints(data));

try {
return metricsService.addAvailabilityData(Observable.just(metric)).map(ApiUtils::simpleOKResponse)
.toBlocking().last();
return metricsService
.addAvailabilityData(Observable.just(metric))
.map(ApiUtils::simpleOKResponse)
.toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -245,8 +259,11 @@ public Response addAvailabilityData(
List<Availability> availabilities
) {
try {
return metricsService.addAvailabilityData(requestToAvailabilities(tenantId, availabilities))
.map(ApiUtils::simpleOKResponse).toBlocking().last();
return metricsService
.addAvailabilityData(requestToAvailabilities(tenantId, availabilities))
.map(ApiUtils::simpleOKResponse)
.toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -273,7 +290,9 @@ public Response findAvailabilityDataByTags(
} else {
return Response.ok(m).build();
}
}).toBlocking().last();
})
.toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
return Response.serverError().entity(new ApiError(e.getMessage())).build();
}
Expand Down Expand Up @@ -311,7 +330,9 @@ public Response findAvailabilityData(
return metricsService.findAvailabilityData(metric.getId(), startTime, endTime, distinct)
.map(AvailabilityDataPoint::new)
.toList()
.map(ApiUtils::collectionToResponse).toBlocking().last();
.map(ApiUtils::collectionToResponse)
.toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
logger.warn("Failed to fetch availability data", e);
return ApiUtils.serverError(e);
Expand All @@ -331,7 +352,8 @@ public Response findAvailabilityData(
}
try {
return metricsService.findAvailabilityStats(metric, startTime, endTime, buckets)
.map(BucketedOutput::getData).map(ApiUtils::collectionToResponse).toBlocking().last();
.map(BucketedOutput::getData).map(ApiUtils::collectionToResponse).toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand Down Expand Up @@ -359,7 +381,10 @@ public Response tagAvailabilityData(
}

try {
return resultSetObservable.map(ApiUtils::simpleOKResponse).toBlocking().last();
return resultSetObservable
.map(ApiUtils::simpleOKResponse)
.toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -383,7 +408,7 @@ public Response findTaggedAvailabilityData(
} else {
return Response.ok(m).build();
}
}).toBlocking().last();
}).toBlocking().lastOrDefault(null);
} catch (Exception e) {
return Response.serverError().entity(new ApiError(e.getMessage())).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Response createCounter(
EntityCreatedObserver<?> observer = new MetricCreatedObserver(location);
Observable<Void> observable = metricsService.createMetric(metric);
observable.subscribe(observer);
observable.toBlocking().last();
observable.toBlocking().lastOrDefault(null);
return observer.getResponse();
} catch (Exception e) {
return ApiUtils.serverError(e);
Expand All @@ -131,7 +131,7 @@ public Response getCounter(@PathParam("id") String id) {
.map(MetricDefinition::new)
.map(metricDef -> Response.ok(metricDef).build())
.switchIfEmpty(Observable.just(ApiUtils.noContent()))
.toBlocking().last();
.toBlocking().lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -150,7 +150,11 @@ public Response addData(@ApiParam(value = "List of metrics", required = true) Li
) {
Observable<Metric<Long>> metrics = requestToCounters(tenantId, counters);
try {
return metricsService.addCounterData(metrics).map(ApiUtils::simpleOKResponse).toBlocking().last();
return metricsService
.addCounterData(metrics)
.map(ApiUtils::simpleOKResponse)
.toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -172,8 +176,11 @@ public Response addData(
) {
Metric<Long> metric = new Metric<>(new MetricId(tenantId, COUNTER, id), requestToCounterDataPoints(data));
try {
return metricsService.addCounterData(Observable.just(metric)).map(ApiUtils::simpleOKResponse).toBlocking()
.last();
return metricsService
.addCounterData(Observable.just(metric))
.map(ApiUtils::simpleOKResponse)
.toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand Down Expand Up @@ -202,7 +209,9 @@ public Response findCounterData(
return metricsService.findCounterData(new MetricId(tenantId, COUNTER, id), startTime, endTime)
.map(CounterDataPoint::new)
.toList()
.map(ApiUtils::collectionToResponse).toBlocking().last();
.map(ApiUtils::collectionToResponse)
.toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
logger.warn("Failed to fetch counter data", e);
return ApiUtils.serverError(e);
Expand Down Expand Up @@ -236,7 +245,8 @@ public Response findRate(
.map(GaugeDataPoint::new)
.toList()
.map(ApiUtils::collectionToResponse)
.toBlocking().last();
.toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
logger.warn("Failed to fetch counter rate data", e);
return ApiUtils.serverError(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ public Response getGaugeMetric(@PathParam("id") String id) {
return metricsService.findMetric(new MetricId(tenantId, GAUGE, id))
.map(MetricDefinition::new)
.map(metricDef -> Response.ok(metricDef).build())
.switchIfEmpty(Observable.just(ApiUtils.noContent()))
.toBlocking().last();
.switchIfEmpty(Observable.just(ApiUtils.noContent())).toBlocking().lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -153,7 +152,8 @@ public Response getGaugeMetric(@PathParam("id") String id) {
public Response getGaugeMetricTags(@PathParam("id") String id) {
try {
return metricsService.getMetricTags(new MetricId(tenantId, GAUGE, id))
.map(ApiUtils::valueToResponse).toBlocking().last();
.map(ApiUtils::valueToResponse)
.toBlocking().lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -173,7 +173,8 @@ public Response updateGaugeMetricTags(
) {
Metric<Double> metric = new Metric<>(new MetricId(tenantId, GAUGE, id));
try {
return metricsService.addTags(metric, tags).map(ApiUtils::simpleOKResponse).toBlocking().last();
return metricsService.addTags(metric, tags).map(ApiUtils::simpleOKResponse).toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -195,7 +196,7 @@ public Response deleteGaugeMetricTags(
try {
Metric<Double> metric = new Metric<>(new MetricId(tenantId, GAUGE, id));
return metricsService.deleteTags(metric, tags.getTags()).map(ApiUtils::simpleOKResponse).toBlocking()
.last();
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -219,7 +220,7 @@ public Response addDataForMetric(
Metric<Double> metric = new Metric<>(new MetricId(tenantId, GAUGE, id), requestToGaugeDataPoints(data));
try {
Observable<Void> observable = metricsService.addGaugeData(Observable.just(metric));
return observable.map(ApiUtils::simpleOKResponse).toBlocking().last();
return observable.map(ApiUtils::simpleOKResponse).toBlocking().lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -241,7 +242,7 @@ public Response addGaugeData(@ApiParam(value = "List of metrics", required = tru

try {
Observable<Void> observable = metricsService.addGaugeData(metrics);
return observable.map(ApiUtils::simpleOKResponse).toBlocking().last();
return observable.map(ApiUtils::simpleOKResponse).toBlocking().lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -266,7 +267,7 @@ public Response findGaugeDataByTags(@ApiParam(value = "Tag list", required = tru
} else {
return Response.ok(m).build();
}
}).toBlocking().last();
}).toBlocking().lastOrDefault(null);
} catch (Exception e) {
return Response.serverError().entity(new ApiError(e.getMessage())).build();
}
Expand Down Expand Up @@ -301,7 +302,7 @@ public Response findGaugeData(
try {
return metricsService.findGaugeData(new MetricId(tenantId, GAUGE, id), startTime, endTime)
.toList()
.map(ApiUtils::collectionToResponse).toBlocking().last();
.map(ApiUtils::collectionToResponse).toBlocking().lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -328,7 +329,7 @@ public Response findGaugeData(
try {
return metricsService.findGaugeStats(metric, startTime, endTime, buckets)
.map(BucketedOutput::getData)
.map(ApiUtils::collectionToResponse).toBlocking().last();
.map(ApiUtils::collectionToResponse).toBlocking().lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand Down Expand Up @@ -394,7 +395,7 @@ public Response findPeriods(
} else {
try {
return metricsService.getPeriods(new MetricId(tenantId, GAUGE, id), predicate, startTime, endTime)
.map(ApiUtils::collectionToResponse).toBlocking().last();
.map(ApiUtils::collectionToResponse).toBlocking().lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -420,7 +421,7 @@ public Response findTaggedGaugeData(@ApiParam("Tag list") @PathParam("tags") Tag
} else {
return Response.ok(m).build();
}
}).toBlocking().last();
}).toBlocking().lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand All @@ -446,7 +447,7 @@ public Response tagGaugeData(@PathParam("id") final String id, @ApiParam(require
}

try {
return resultSetObservable.map(ApiUtils::simpleOKResponse).toBlocking().last();
return resultSetObservable.map(ApiUtils::simpleOKResponse).toBlocking().lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public Response findMetrics(
.toList()
.map(ApiUtils::collectionToResponse)
.toBlocking()
.last();
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand Down Expand Up @@ -154,7 +154,11 @@ public Response addMetricsData(
}

try {
return Observable.merge(observables).map(ApiUtils::simpleOKResponse).toBlocking().last();
return Observable
.merge(observables)
.map(ApiUtils::simpleOKResponse)
.toBlocking()
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Response createTenant(@ApiParam(required = true) TenantParam params,
try {
Observable<Void> observable = metricsService.createTenant(new Tenant(params.getId()));
observable.subscribe(observer);
observable.toBlocking().last();
observable.toBlocking().lastOrDefault(null);
return observer.getResponse();
} catch (Exception e) {
Response response = observer.getResponse();
Expand All @@ -103,7 +103,7 @@ public Response findTenants() {
.toList()
.map(ApiUtils::collectionToResponse)
.toBlocking()
.last();
.lastOrDefault(null);
} catch (Exception e) {
return ApiUtils.serverError(e);
}
Expand Down

0 comments on commit 48d6d9d

Please sign in to comment.