Skip to content

Commit

Permalink
[HWKMETRICS-114] Move tenantId and metricType from Metric to MetricId
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Burman committed Jul 27, 2015
1 parent 07c2988 commit 58c8aa9
Show file tree
Hide file tree
Showing 17 changed files with 226 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void createAvailabilityMetric(
@Context UriInfo uriInfo
) {
URI location = uriInfo.getBaseUriBuilder().path("/availability/{id}").build(metricDefinition.getId());
Metric metric = new Metric(tenantId, AVAILABILITY, new MetricId(metricDefinition.getId()),
Metric metric = new Metric(new MetricId(tenantId, AVAILABILITY, metricDefinition.getId()),
metricDefinition.getTags(), metricDefinition.getDataRetention());
metricsService.createMetric(metric).subscribe(new MetricCreatedObserver(asyncResponse, location));
}
Expand All @@ -131,7 +131,7 @@ public void createAvailabilityMetric(
public void getAvailabilityMetric(@Suspended final AsyncResponse asyncResponse,
@HeaderParam("tenantId") String tenantId, @PathParam("id") String id) {

metricsService.findMetric(tenantId, AVAILABILITY, new MetricId(id))
metricsService.findMetric(new MetricId(tenantId, AVAILABILITY, id))
.map(MetricDefinition::new)
.map(metricDef -> Response.ok(metricDef).build())
.switchIfEmpty(Observable.just(noContent()))
Expand All @@ -151,7 +151,7 @@ public void getAvailabilityMetricTags(
@Suspended final AsyncResponse asyncResponse,
@PathParam("id") String id
) {
metricsService.getMetricTags(tenantId, AVAILABILITY, new MetricId(id)).subscribe(
metricsService.getMetricTags(new MetricId(tenantId, AVAILABILITY, id)).subscribe(
optional -> asyncResponse.resume(valueToResponse(optional)),
t -> asyncResponse.resume(serverError(t)));
}
Expand All @@ -168,7 +168,7 @@ public void updateAvailabilityMetricTags(
@PathParam("id") String id,
@ApiParam(required = true) Map<String, String> tags
) {
Metric<AvailabilityType> metric = new Metric<>(tenantId, AVAILABILITY, new MetricId(id));
Metric<AvailabilityType> metric = new Metric<>(new MetricId(tenantId, AVAILABILITY, id));
metricsService.addTags(metric, tags).subscribe(new ResultSetObserver(asyncResponse));
}

Expand All @@ -185,7 +185,7 @@ public void deleteAvailabilityMetricTags(
@PathParam("id") String id,
@ApiParam("Tag list") @PathParam("tags") Tags tags
) {
Metric<AvailabilityType> metric = new Metric<>(tenantId, AVAILABILITY, new MetricId(id));
Metric<AvailabilityType> metric = new Metric<>(new MetricId(tenantId, AVAILABILITY, id));
metricsService.deleteTags(metric, tags.getTags()).subscribe(new ResultSetObserver(asyncResponse));
}

Expand All @@ -202,7 +202,7 @@ public void addAvailabilityForMetric(
@Suspended final AsyncResponse asyncResponse, @PathParam("id") String id,
@ApiParam(value = "List of availability datapoints", required = true) List<AvailabilityDataPoint> data
) {
Metric<AvailabilityType> metric = new Metric<>(tenantId, AVAILABILITY, new MetricId(id),
Metric<AvailabilityType> metric = new Metric<>(new MetricId(tenantId, AVAILABILITY, id),
requestToAvailabilityDataPoints(data));
metricsService.addAvailabilityData(Observable.just(metric)).subscribe(new ResultSetObserver(asyncResponse));
}
Expand Down Expand Up @@ -278,9 +278,9 @@ public void findAvailabilityData(
Long startTime = start == null ? now - EIGHT_HOURS : start;
Long endTime = end == null ? now : end;

Metric<AvailabilityType> metric = new Metric<>(tenantId, AVAILABILITY, new MetricId(id));
Metric<AvailabilityType> metric = new Metric<>(new MetricId(tenantId, AVAILABILITY, id));
if (bucketsCount == null && bucketDuration == null) {
metricsService.findAvailabilityData(tenantId, metric.getId(), startTime, endTime, distinct)
metricsService.findAvailabilityData(metric.getId(), startTime, endTime, distinct)
.map(AvailabilityDataPoint::new)
.toList()
.map(ApiUtils::collectionToResponse)
Expand Down Expand Up @@ -325,7 +325,7 @@ public void tagAvailabilityData(
@ApiParam(required = true) TagRequest params
) {
Observable<Void> resultSetObservable;
Metric<AvailabilityType> metric = new Metric<>(tenantId, AVAILABILITY, new MetricId(id));
Metric<AvailabilityType> metric = new Metric<>(new MetricId(tenantId, AVAILABILITY, id));
if (params.getTimestamp() != null) {
resultSetObservable = metricsService.tagAvailabilityData(metric, params.getTags(), params.getTimestamp());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.requestToCounterDataPoints;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.requestToCounters;
import static org.hawkular.metrics.core.api.MetricType.COUNTER;
import static org.hawkular.metrics.core.api.MetricType.COUNTER_RATE;

import java.net.URI;
import java.util.List;
Expand Down Expand Up @@ -107,7 +108,7 @@ public void createCounter(
@ApiParam(required = true) MetricDefinition metricDefinition,
@Context UriInfo uriInfo
) {
Metric<Double> metric = new Metric<>(tenantId, COUNTER, new MetricId(metricDefinition.getId()),
Metric<Double> metric = new Metric<>(new MetricId(tenantId, COUNTER, metricDefinition.getId()),
metricDefinition.getTags(), metricDefinition.getDataRetention());
URI location = uriInfo.getBaseUriBuilder().path("/counters/{id}").build(metric.getId().getName());
metricsService.createMetric(metric).subscribe(new MetricCreatedObserver(asyncResponse, location));
Expand All @@ -123,7 +124,7 @@ public void createCounter(
response = ApiError.class) })
public void getCounter(@Suspended final AsyncResponse asyncResponse, @PathParam("id") String id) {

metricsService.findMetric(tenantId, COUNTER, new MetricId(id))
metricsService.findMetric(new MetricId(tenantId, COUNTER, id))
.map(MetricDefinition::new)
.map(metricDef -> Response.ok(metricDef).build())
.switchIfEmpty(Observable.just(ApiUtils.noContent()))
Expand Down Expand Up @@ -162,7 +163,8 @@ public void addData(
@ApiParam(value = "List of data points containing timestamp and value", required = true)
List<CounterDataPoint> data
) {
Metric<Long> metric = new Metric<>(tenantId, COUNTER, new MetricId(id), requestToCounterDataPoints(data));
Metric<Long> metric = new Metric<>(new MetricId(tenantId, COUNTER, id),
requestToCounterDataPoints(data));
Observable<Void> observable = metricsService.addCounterData(Observable.just(metric));
observable.subscribe(new ResultSetObserver(asyncResponse));
}
Expand All @@ -187,7 +189,7 @@ public void findCounterData(
long startTime = start == null ? now - EIGHT_HOURS : start;
long endTime = end == null ? now : end;

metricsService.findCounterData(tenantId, new MetricId(id), startTime, endTime)
metricsService.findCounterData(new MetricId(tenantId, COUNTER, id), startTime, endTime)
.map(CounterDataPoint::new)
.toList()
.map(ApiUtils::collectionToResponse)
Expand Down Expand Up @@ -222,7 +224,7 @@ public void findRate(
long startTime = start == null ? now - EIGHT_HOURS : start;
long endTime = end == null ? now : end;

metricsService.findRateData(tenantId, new MetricId(id), startTime, endTime)
metricsService.findRateData(new MetricId(tenantId, COUNTER_RATE, id), startTime, endTime)
.map(GaugeDataPoint::new)
.toList()
.map(ApiUtils::collectionToResponse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void createGaugeMetric(
@ApiParam(required = true) MetricDefinition metricDefinition,
@Context UriInfo uriInfo
) {
Metric<Double> metric = new Metric<>(tenantId, GAUGE, new MetricId(metricDefinition.getId()),
Metric<Double> metric = new Metric<>(new MetricId(tenantId, GAUGE, metricDefinition.getId()),
metricDefinition.getTags(), metricDefinition.getDataRetention());
URI location = uriInfo.getBaseUriBuilder().path("/gauges/{id}").build(metric.getId().getName());
metricsService.createMetric(metric).subscribe(new MetricCreatedObserver(asyncResponse, location));
Expand All @@ -124,7 +124,7 @@ public void createGaugeMetric(
response = ApiError.class) })
public void getGaugeMetric(@Suspended final AsyncResponse asyncResponse, @PathParam("id") String id) {

metricsService.findMetric(tenantId, GAUGE, new MetricId(id))
metricsService.findMetric(new MetricId(tenantId, GAUGE, id))
.map(MetricDefinition::new)
.map(metricDef -> Response.ok(metricDef).build())
.switchIfEmpty(Observable.just(ApiUtils.noContent()))
Expand All @@ -144,7 +144,7 @@ public void getGaugeMetricTags(
@Suspended final AsyncResponse asyncResponse,
@PathParam("id") String id
) {
metricsService.getMetricTags(tenantId, GAUGE, new MetricId(id))
metricsService.getMetricTags(new MetricId(tenantId, GAUGE, id))
.subscribe(
optional -> asyncResponse.resume(ApiUtils.valueToResponse(optional)),
t ->asyncResponse.resume(ApiUtils.serverError(t))
Expand All @@ -163,7 +163,7 @@ public void updateGaugeMetricTags(
@PathParam("id") String id,
@ApiParam(required = true) Map<String, String> tags
) {
Metric<Double> metric = new Metric<>(tenantId, GAUGE, new MetricId(id));
Metric<Double> metric = new Metric<>(new MetricId(tenantId, GAUGE, id));
metricsService.addTags(metric, tags).subscribe(new ResultSetObserver(asyncResponse));
}

Expand All @@ -180,7 +180,7 @@ public void deleteGaugeMetricTags(
@PathParam("id") String id,
@ApiParam("Tag list") @PathParam("tags") Tags tags
) {
Metric<Double> metric = new Metric<>(tenantId, GAUGE, new MetricId(id));
Metric<Double> metric = new Metric<>(new MetricId(tenantId, GAUGE, id));
metricsService.deleteTags(metric, tags.getTags()).subscribe(new ResultSetObserver(asyncResponse));
}

Expand All @@ -199,7 +199,7 @@ public void addDataForMetric(
@ApiParam(value = "List of datapoints containing timestamp and value", required = true)
List<GaugeDataPoint> data
) {
Metric<Double> metric = new Metric<>(tenantId, GAUGE, new MetricId(id), requestToGaugeDataPoints(data));
Metric<Double> metric = new Metric<>(new MetricId(tenantId, GAUGE, id), requestToGaugeDataPoints(data));
Observable<Void> observable = metricsService.addGaugeData(Observable.just(metric));
observable.subscribe(new ResultSetObserver(asyncResponse));
}
Expand Down Expand Up @@ -270,7 +270,7 @@ public void findGaugeData(
long startTime = start == null ? now - EIGHT_HOURS : start;
long endTime = end == null ? now : end;

metricsService.findGaugeData(tenantId, new MetricId(id), startTime, endTime)
metricsService.findGaugeData(new MetricId(tenantId, GAUGE, id), startTime, endTime)
.toList()
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> asyncResponse.resume(ApiUtils.serverError(t)));
Expand All @@ -287,7 +287,7 @@ public void findGaugeData(
long startTime = start == null ? now - EIGHT_HOURS : start;
long endTime = end == null ? now : end;

Metric<Double> metric = new Metric<>(tenantId, GAUGE, new MetricId(id));
Metric<Double> metric = new Metric<>(new MetricId(tenantId, GAUGE, id));

Buckets buckets;
try {
Expand Down Expand Up @@ -369,7 +369,7 @@ public void findPeriods(
)
));
} else {
metricsService.getPeriods(tenantId, new MetricId(id), predicate, startTime, endTime)
metricsService.getPeriods(new MetricId(tenantId, GAUGE, id), predicate, startTime, endTime)
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> asyncResponse.resume(ApiUtils.serverError(t)));
}
Expand Down Expand Up @@ -410,7 +410,7 @@ public void tagGaugeData(
@PathParam("id") final String id, @ApiParam(required = true) TagRequest params
) {
Observable<Void> resultSetObservable;
Metric<Double> metric = new Metric<>(tenantId, GAUGE, new MetricId(id));
Metric<Double> metric = new Metric<>(new MetricId(tenantId, GAUGE, id));
if (params.getTimestamp() != null) {
resultSetObservable = metricsService.tagGaugeData(metric, params.getTags(), params.getTimestamp());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private static Metric<Double> influxToGauge(String tenantId, InfluxObject influx
}
dataPoints.add(new DataPoint<>(timestamp, value));
}
return new Metric<>(tenantId, GAUGE, new MetricId(influxObject.getName()), dataPoints);
return new Metric<>(new MetricId(tenantId, GAUGE, influxObject.getName()), dataPoints);
}

@GET
Expand Down Expand Up @@ -266,7 +266,7 @@ private void select(AsyncResponse asyncResponse, String tenantId, SelectQueryCon
return Observable.just(null);
}
return metricsService.findGaugeData(
tenantId, new MetricId(metric),
new MetricId(tenantId, GAUGE, metric),
timeInterval.getStartMillis(), timeInterval.getEndMillis()
).toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ public static List<DataPoint<Double>> requestToGaugeDataPoints(List<GaugeDataPoi

public static Observable<Metric<Double>> requestToGauges(String tenantId, List<Gauge> gauges) {
return Observable.from(gauges).map(g ->
new Metric<>(tenantId, GAUGE, new MetricId(g.getId()), requestToGaugeDataPoints(g.getData())));
new Metric<>(new MetricId(tenantId, GAUGE, g.getId()), requestToGaugeDataPoints(g.getData())));
}

public static Observable<Metric<Long>> requestToCounters(String tenantId, List<Counter> counters) {
return Observable.from(counters).map(c ->
new Metric<>(tenantId, COUNTER, new MetricId(c.getId()), requestToCounterDataPoints(c.getData())));
new Metric<>(new MetricId(tenantId, COUNTER, c.getId()), requestToCounterDataPoints(c.getData())));
}

public static Observable<Metric<AvailabilityType>> requestToAvailabilities(String tenantId,
List<Availability> avails) {
return Observable.from(avails).map(a -> new Metric<>(tenantId, AVAILABILITY, new MetricId(a.getId()),
return Observable.from(avails).map(a -> new Metric<>(new MetricId(tenantId, AVAILABILITY, a.getId()),
requestToAvailabilityDataPoints(a.getData())));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/**
* Predefined aggregate functions usable with
* {@link MetricsService#findGaugeData(String, MetricId, Long, Long, Func1...)}.
* {@link MetricsService#findGaugeData(MetricId, Long, Long, Func1[])}.
*
* @author john sanda
* @author jay shaughnessy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,16 @@
*/
public class Metric<T> {

private String tenantId;
private MetricType type;
private MetricId id;
private Map<String, String> tags = Collections.emptyMap();
private Integer dataRetention;
private List<DataPoint<T>> dataPoints = new ArrayList<>();

public Metric(String tenantId, MetricType type, MetricId id) {
this.tenantId = tenantId;
this.type = type;
public Metric(MetricId id) {
this.id = id;
}

public Metric(String tenantId, MetricType type, MetricId id, Map<String, String> tags, Integer dataRetention) {
this.tenantId = tenantId;
this.type = type;
public Metric(MetricId id, Map<String, String> tags, Integer dataRetention) {
this.id = id;
this.tags = unmodifiableMap(tags);
// If the data_retention column is not set, the driver returns zero instead of null.
Expand All @@ -58,17 +52,13 @@ public Metric(String tenantId, MetricType type, MetricId id, Map<String, String>
}
}

public Metric(String tenantId, MetricType type, MetricId id, List<DataPoint<T>> dataPoints) {
this.tenantId = tenantId;
this.type = type;
public Metric(MetricId id, List<DataPoint<T>> dataPoints) {
this.id = id;
this.dataPoints = unmodifiableList(dataPoints);
}

public Metric(String tenantId, MetricType type, MetricId id, Map<String, String> tags, Integer dataRetention,
public Metric(MetricId id, Map<String, String> tags, Integer dataRetention,
List<DataPoint<T>> dataPoints) {
this.tenantId = tenantId;
this.type = type;
this.id = id;
this.tags = unmodifiableMap(tags);
// If the data_retention column is not set, the driver returns zero instead of null.
Expand All @@ -82,12 +72,14 @@ public Metric(String tenantId, MetricType type, MetricId id, Map<String, String>
this.dataPoints = unmodifiableList(dataPoints);
}

public MetricType getType() {
return type;
@Deprecated
public String getTenantId() {
return getId().getTenantId();
}

public String getTenantId() {
return tenantId;
@Deprecated
public MetricType getType() {
return getId().getType();
}

public MetricId getId() {
Expand All @@ -111,22 +103,19 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Metric<?> metric = (Metric<?>) o;
return Objects.equals(tenantId, metric.tenantId) &&
Objects.equals(type, metric.type) &&
Objects.equals(id, metric.id) &&
return Objects.equals(id, metric.id) &&
Objects.equals(tags, metric.tags) &&
Objects.equals(dataRetention, metric.dataRetention);
}

@Override
public int hashCode() {
return Objects.hash(tenantId, type, id, tags, dataRetention);
return Objects.hash(id, tags, dataRetention);
}

@Override
public String toString() {
return com.google.common.base.Objects.toStringHelper(this)
.add("tenantId", tenantId)
.add("id", id)
.add("tags", tags)
.add("dataRetention", dataRetention)
Expand Down

0 comments on commit 58c8aa9

Please sign in to comment.