-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HWKMETRICS-130] refactor task execution
Prior to this commit the TaskType class provided a factory for producing an object to exectue tasks. There are better ways to do this with RxJava. Now clients register a subscriber with TaskService, and that subscriber is notified of tasks to execute. The code for computing rates has been pulled out of MetricsServiceImpl and put into a new class, GenerateRate. This decouples MetricsServiceInmpl from the computation of rates which will make it a lot easier to execute these various components in isolation.
- Loading branch information
John Sanda
committed
Jun 19, 2015
1 parent
89bf6b3
commit 657cf13
Showing
8 changed files
with
174 additions
and
205 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
core/metrics-core-impl/src/main/java/org/hawkular/metrics/core/impl/GenerateRate.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,64 @@ | ||
/* | ||
* 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.impl; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.hawkular.metrics.core.api.MetricType.COUNTER_RATE; | ||
|
||
import org.hawkular.metrics.core.api.DataPoint; | ||
import org.hawkular.metrics.core.api.Metric; | ||
import org.hawkular.metrics.core.api.MetricId; | ||
import org.hawkular.metrics.core.api.MetricsService; | ||
import org.hawkular.metrics.tasks.api.Task; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import rx.Observable; | ||
import rx.functions.Action1; | ||
|
||
/** | ||
* @author jsanda | ||
*/ | ||
public class GenerateRate implements Action1<Task> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(GenerateRate.class); | ||
|
||
private MetricsService metricsService; | ||
|
||
public GenerateRate(MetricsService metricsService) { | ||
this.metricsService = metricsService; | ||
} | ||
|
||
@Override | ||
public void call(Task task) { | ||
logger.info("Generating rate for {}", task); | ||
MetricId id = new MetricId(task.getSources().iterator().next()); | ||
long end = task.getTimeSlice().getMillis(); | ||
long start = task.getTimeSlice().minusSeconds(task.getWindow()).getMillis(); | ||
logger.debug("start = {}, end = {}", start, end); | ||
metricsService.findCounterData(task.getTenantId(), id, start, end) | ||
.take(1) | ||
.map(dataPoint -> ((dataPoint.getValue().doubleValue() / (end - start) * 1000))) | ||
.map(rate -> new Metric<>(task.getTenantId(), COUNTER_RATE, id, | ||
singletonList(new DataPoint<>(start, rate)))) | ||
.flatMap(metric -> metricsService.addGaugeData(Observable.just(metric))) | ||
.subscribe( | ||
aVoid -> {}, | ||
t -> logger.warn("Failed to persist rate data", t), | ||
() -> logger.debug("Successfully persisted rate data") | ||
); | ||
} | ||
} |
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.