Skip to content

Commit

Permalink
[HWKMETRICS-113] make model classes immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
John Sanda committed Jun 5, 2015
1 parent 8937d3e commit f51fc91
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.hawkular.metrics.api.jaxrs.request;

import static java.util.Collections.unmodifiableList;

import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -43,7 +45,7 @@ public String getId() {
}

public List<AvailabilityDataPoint> getData() {
return data;
return unmodifiableList(data);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.hawkular.metrics.api.jaxrs.request;

import static java.util.Collections.unmodifiableList;

import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -43,7 +45,7 @@ public String getId() {
}

public List<GaugeDataPoint> getData() {
return data;
return unmodifiableList(data);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.hawkular.metrics.api.jaxrs.request;

import static java.util.Collections.emptyMap;
import static java.util.Collections.unmodifiableMap;

import java.util.Map;
import java.util.Objects;

Expand All @@ -30,7 +33,7 @@
@ApiModel(description = "The definition of a metric to create")
public class MetricDefinition {

// TODO Do we need this?
// TODO Do we need this?
@JsonProperty
private String tenantId;

Expand All @@ -46,10 +49,10 @@ public class MetricDefinition {
@JsonCreator
public MetricDefinition(
@JsonProperty("id") String id,
@JsonProperty("tags") Map<String, String> tags,
@JsonProperty(value = "tags") Map<String, String> tags,
@JsonProperty("dataRetention") Integer dataRetention) {
this.id = id;
this.tags = tags;
this.tags = tags == null ? emptyMap() : unmodifiableMap(tags);
this.dataRetention = dataRetention;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.hawkular.metrics.core.api;

import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -44,7 +47,7 @@ public Metric(String tenantId, MetricType type, MetricId id, Map<String, String>
this.tenantId = tenantId;
this.type = type;
this.id = id;
this.tags = tags;
this.tags = unmodifiableMap(tags);
// If the data_retention column is not set, the driver returns zero instead of null.
// We are (at least for now) using null to indicate that the metric does not have
// the data retention set.
Expand All @@ -59,7 +62,24 @@ public Metric(String tenantId, MetricType type, MetricId id, List<T> dataPoints)
this.tenantId = tenantId;
this.type = type;
this.id = id;
this.dataPoints = dataPoints;
this.dataPoints = unmodifiableList(dataPoints);
}

public Metric(String tenantId, MetricType type, MetricId id, Map<String, String> tags, Integer dataRetention,
List<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.
// We are (at least for now) using null to indicate that the metric does not have
// the data retention set.
if (dataRetention == null || dataRetention == 0) {
this.dataRetention = null;
} else {
this.dataRetention = dataRetention;
}
this.dataPoints = unmodifiableList(dataPoints);
}

public MetricType getType() {
Expand All @@ -86,12 +106,6 @@ public List<T> getDataPoints() {
return dataPoints;
}

// TODO Remove this - we want the class to be immutable
public Metric<T> addDataPoint(T dataPoint) {
dataPoints.add(dataPoint);
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public MetricId(String name, Interval interval) {
this.interval = interval;
}

// @JsonValue
public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;

import java.util.Collections;
import java.util.List;

import com.datastax.driver.core.PreparedStatement;
Expand All @@ -40,8 +39,8 @@
import org.hawkular.metrics.core.api.Counter;
import org.hawkular.metrics.core.api.GaugeDataPoint;
import org.hawkular.metrics.core.api.Interval;
import org.hawkular.metrics.core.api.MetricId;
import org.hawkular.metrics.core.api.Metric;
import org.hawkular.metrics.core.api.MetricId;
import org.hawkular.metrics.core.api.Tenant;
import org.joda.time.DateTime;
import org.joda.time.Days;
Expand Down Expand Up @@ -123,12 +122,12 @@ public void insertAndFindGaugeRawData() throws Exception {
DateTime start = now().minusMinutes(10);
DateTime end = start.plusMinutes(6);

Metric<GaugeDataPoint> metric = new Metric<>("tenant-1", GAUGE, new MetricId("metric-1"),
Collections.emptyMap(), DEFAULT_TTL);
metric.addDataPoint(new GaugeDataPoint(start.getMillis(), 1.23));
metric.addDataPoint(new GaugeDataPoint(start.plusMinutes(1).getMillis(), 1.234));
metric.addDataPoint(new GaugeDataPoint(start.plusMinutes(2).getMillis(), 1.234));
metric.addDataPoint(new GaugeDataPoint(end.getMillis(), 1.234));
Metric<GaugeDataPoint> metric = new Metric<>("tenant-1", GAUGE, new MetricId("metric-1"), asList(
new GaugeDataPoint(start.getMillis(), 1.23),
new GaugeDataPoint(start.plusMinutes(1).getMillis(), 1.234),
new GaugeDataPoint(start.plusMinutes(2).getMillis(), 1.234),
new GaugeDataPoint(end.getMillis(), 1.234)
));

dataAccess.insertData(Observable.just(new GaugeAndTTL(metric, DEFAULT_TTL))).toBlocking().last();

Expand Down Expand Up @@ -160,10 +159,13 @@ public void addMetadataToGaugeRawData() throws Exception {

dataAccess.addTagsAndDataRetention(metric).toBlocking().last();

metric.addDataPoint(new GaugeDataPoint(start.getMillis(), 1.23));
metric.addDataPoint(new GaugeDataPoint(start.plusMinutes(2).getMillis(), 1.234));
metric.addDataPoint(new GaugeDataPoint(start.plusMinutes(4).getMillis(), 1.234));
metric.addDataPoint(new GaugeDataPoint(end.getMillis(), 1.234));
metric = new Metric<>(tenantId, GAUGE, new MetricId("metric-1"), asList(
new GaugeDataPoint(start.getMillis(), 1.23),
new GaugeDataPoint(start.plusMinutes(2).getMillis(), 1.234),
new GaugeDataPoint(start.plusMinutes(4).getMillis(), 1.234),
new GaugeDataPoint(end.getMillis(), 1.234)
));

dataAccess.insertData(Observable.just(new GaugeAndTTL(metric, DEFAULT_TTL))).toBlocking().last();

Observable<ResultSet> observable = dataAccess.findData("tenant-1", new MetricId("metric-1"), start.getMillis(),
Expand Down Expand Up @@ -257,8 +259,8 @@ public void insertAndFindAvailabilities() throws Exception {
DateTime start = now().minusMinutes(10);
DateTime end = start.plusMinutes(6);
String tenantId = "avail-test";
Metric<AvailabilityDataPoint> metric = new Metric<>(tenantId, AVAILABILITY, new MetricId("m1"));
metric.addDataPoint(new AvailabilityDataPoint(start.getMillis(), "up"));
Metric<AvailabilityDataPoint> metric = new Metric<>(tenantId, AVAILABILITY, new MetricId("m1"),
singletonList(new AvailabilityDataPoint(start.getMillis(), "up")));

dataAccess.insertAvailabilityData(metric, 360).toBlocking().lastOrDefault(null);

Expand Down

0 comments on commit f51fc91

Please sign in to comment.