Refactor, simplify Measurement & rename cause

This commit is contained in:
Ashvin Agrawal 2018-02-08 12:12:56 -08:00
Родитель 23836283d7
Коммит 541850f1f6
6 изменённых файлов: 77 добавлений и 86 удалений

Просмотреть файл

@ -11,17 +11,19 @@ import java.time.Instant;
/**
* A {@link Measurement} is value of a metric corresponding to an instance at a instant of time.
*/
public abstract class Measurement {
public class Measurement {
private final String component;
private final String instance;
private final String type;
private final long instantMillis; // UTC
private final double value;
private Measurement(String component, String instance, String type, Instant instant) {
public Measurement(String component, String instance, String type, Instant instant, double value) {
this.component = component;
this.instance = instance;
this.type = type;
this.instantMillis = instant.toEpochMilli();
this.value = value;
}
public String component() {
@ -40,40 +42,31 @@ public abstract class Measurement {
return Instant.ofEpochMilli(instantMillis);
}
public static class ScalarMeasurement extends Measurement {
private final double value;
public double value() {
return value;
}
public ScalarMeasurement(String component, String instance, String metricType, Instant instant, double value) {
super(component, instance, metricType, instant);
this.value = value;
}
public double value() {
return value;
}
@Override
public String toString() {
return "Measurement {" +
"component=" + component() +
", instance=" + instance() +
", type=" + type() +
", instant=" + instant() +
", value=" + value +
'}';
}
@Override
public String toString() {
return "Measurement {" +
"component=" + component() +
", instance=" + instance() +
", type=" + type() +
", instant=" + instant() +
", value=" + value +
'}';
}
public static class ObjMeasurement extends Measurement {
private final Object value;
private final Object reference;
public ObjMeasurement(String component, String instance, String metricType, Instant instant, Object value) {
super(component, instance, metricType, instant);
this.value = value;
super(component, instance, metricType, instant, 0);
this.reference = value;
}
public Object value() {
return value;
public Object reference() {
return reference;
}
}
}

Просмотреть файл

@ -6,7 +6,6 @@
*/
package com.microsoft.dhalion.core;
import com.microsoft.dhalion.core.Measurement.ScalarMeasurement;
import tech.tablesaw.api.CategoryColumn;
import tech.tablesaw.api.DoubleColumn;
import tech.tablesaw.api.LongColumn;
@ -81,7 +80,7 @@ public class MeasurementsArray {
instance.append(measurement.instance());
type.append(measurement.type());
timeStamps.append(measurement.instant().toEpochMilli());
value.append(((ScalarMeasurement) measurement).value());
value.append(measurement.value());
});
}
@ -326,11 +325,11 @@ public class MeasurementsArray {
public Collection<Measurement> get() {
ArrayList<Measurement> result = new ArrayList<>();
for (int i = 0; i < measurements.rowCount(); i++) {
result.add(new ScalarMeasurement(component.get(i),
instance.get(i),
type.get(i),
Instant.ofEpochMilli(timeStamps.get(i)),
value.get(i)));
result.add(new Measurement(component.get(i),
instance.get(i),
type.get(i),
Instant.ofEpochMilli(timeStamps.get(i)),
value.get(i)));
}
return result;
}

Просмотреть файл

@ -27,18 +27,18 @@ public class Symptom {
// instant when this symptom was created
private final Instant instant;
// cause identifiers
private final Collection<String> causeIds;
// ids to which the symptom is assigned, for e.g. instances
private final Collection<String> assignments;
public Symptom(String symptomType, Instant instant, Collection<String> causeIds) {
this(idGenerator.incrementAndGet(), symptomType, instant, causeIds);
public Symptom(String symptomType, Instant instant, Collection<String> assignments) {
this(idGenerator.incrementAndGet(), symptomType, instant, assignments);
}
public Symptom(int id, String symptomType, Instant instant, Collection<String> causeIds) {
public Symptom(int id, String symptomType, Instant instant, Collection<String> assignments) {
this.id = id;
this.type = symptomType;
this.instant = instant;
this.causeIds = new ArrayList<>(causeIds);
this.id = id;
this.assignments = new ArrayList<>(assignments);
}
public int id() {
@ -53,8 +53,8 @@ public class Symptom {
return instant;
}
public Collection<String> causeIds() {
return causeIds;
public Collection<String> assignments() {
return assignments;
}
@Override
@ -63,7 +63,7 @@ public class Symptom {
"type=" + type +
", id=" + id +
", instant=" + instant +
", causeIds=" + causeIds +
", assignments=" + assignments +
'}';
}
}

Просмотреть файл

@ -36,27 +36,27 @@ public class SymptomsArray {
private final Table symptoms;
private CategoryColumn type;
private IntColumn id;
private CategoryColumn cause;
private CategoryColumn assignment;
private LongColumn timeStamp;
public enum SortKey {
ID, CAUSE, TIME_STAMP, TYPE
ID, ASSIGNMENT, TIME_STAMP, TYPE
}
private static final String ID = SortKey.ID.name();
private static final String CAUSE = SortKey.CAUSE.name();
private static final String ASSIGNMENT = SortKey.ASSIGNMENT.name();
private static final String TIME_STAMP = SortKey.TIME_STAMP.name();
private static final String TYPE = SortKey.TYPE.name();
private SymptomsArray() {
id = new IntColumn(ID);
cause = new CategoryColumn(CAUSE);
assignment = new CategoryColumn(ASSIGNMENT);
type = new CategoryColumn(TYPE);
timeStamp = new LongColumn(TIME_STAMP);
symptoms = Table.create("Symptoms");
symptoms.addColumn(id);
symptoms.addColumn(cause);
symptoms.addColumn(assignment);
symptoms.addColumn(type);
symptoms.addColumn(timeStamp);
}
@ -64,16 +64,16 @@ public class SymptomsArray {
private SymptomsArray(Table table) {
this.symptoms = table;
id = symptoms.intColumn(ID);
cause = symptoms.categoryColumn(CAUSE);
assignment = symptoms.categoryColumn(ASSIGNMENT);
type = symptoms.categoryColumn(TYPE);
timeStamp = symptoms.longColumn(TIME_STAMP);
}
private void addAll(Collection<Symptom> symptoms) {
symptoms.forEach(symptom -> {
symptom.causeIds().forEach(causeId -> {
symptom.assignments().forEach(assignment -> {
id.append(symptom.id());
cause.append(causeId);
this.assignment.append(assignment);
type.append(symptom.type());
timeStamp.append(symptom.instant().toEpochMilli());
});
@ -109,22 +109,22 @@ public class SymptomsArray {
}
/**
* Retains all {@link Symptom}s with given cause ids.
* Retains all {@link Symptom}s with given assignment ids.
*
* @param causeIds cause ids, not null
* @param assignments assignment ids, not null
* @return {@link SymptomsArray} containing filtered {@link Symptom}s
*/
public SymptomsArray cause(Collection<String> causeIds) {
return applyCategoryFilter(causeIds, CAUSE);
public SymptomsArray assignment(Collection<String> assignments) {
return applyCategoryFilter(assignments, ASSIGNMENT);
}
/**
* @param causeId cause id
* @param assignment assignment id
* @return {@link SymptomsArray} containing filtered {@link Symptom}s
* @see #cause(Collection)
* @see #assignment(Collection)
*/
public SymptomsArray cause(String causeId) {
return cause(Collections.singletonList(causeId));
public SymptomsArray assignment(String assignment) {
return assignment(Collections.singletonList(assignment));
}
private SymptomsArray applyCategoryFilter(Collection<String> names, String column) {
@ -258,7 +258,7 @@ public class SymptomsArray {
result.add(new Symptom(id.get(i),
type.get(i),
Instant.ofEpochMilli(timeStamp.get(i)),
Collections.singletonList(cause.get(i))));
Collections.singletonList(assignment.get(i))));
}
return result;
}

Просмотреть файл

@ -7,7 +7,6 @@
package com.microsoft.dhalion.core;
import com.microsoft.dhalion.core.Measurement.ScalarMeasurement;
import com.microsoft.dhalion.core.MeasurementsArray.Builder;
import com.microsoft.dhalion.core.MeasurementsArray.SortKey;
import org.junit.Before;
@ -38,9 +37,9 @@ public class MeasurementsArrayTest {
for (String component : components) {
for (String instance : instances) {
for (String metric : metrics) {
measurements.add(new ScalarMeasurement(component, instance, metric, Instant.ofEpochMilli(value), value));
measurements.add(new Measurement(component, instance, metric, Instant.ofEpochMilli(value), value));
value += 10;
measurements.add(new ScalarMeasurement(component, instance, metric, Instant.ofEpochMilli(value), value));
measurements.add(new Measurement(component, instance, metric, Instant.ofEpochMilli(value), value));
value += 10;
}
}
@ -170,27 +169,27 @@ public class MeasurementsArrayTest {
public void slice() {
assertEquals(24, testArray.size());
Iterator<Measurement> measurements = testArray.get().iterator();
double firstValue = ((ScalarMeasurement) measurements.next()).value();
double secondValue = ((ScalarMeasurement) measurements.next()).value();
double firstValue = measurements.next().value();
double secondValue = measurements.next().value();
resultArray = testArray.component("c1").slice(0, 1);
assertEquals(2, resultArray.size());
measurements = resultArray.get().iterator();
assertEquals(firstValue, ((ScalarMeasurement) measurements.next()).value(), 0.01);
assertEquals(secondValue, ((ScalarMeasurement) measurements.next()).value(), 0.01);
assertEquals(firstValue, measurements.next().value(), 0.01);
assertEquals(secondValue, measurements.next().value(), 0.01);
resultArray = testArray.component("c3").slice(7, 7);
assertEquals(1, resultArray.size());
measurements = resultArray.get().iterator();
assertEquals(240, ((ScalarMeasurement) measurements.next()).value(), 0.01);
assertEquals(240, measurements.next().value(), 0.01);
}
@Test
public void valueBetween() {
resultArray = testArray.valueBetween(45, 65);
assertEquals(2, resultArray.size());
resultArray.get().forEach(m -> assertTrue(45 <= ((ScalarMeasurement) m).value()));
resultArray.get().forEach(m -> assertTrue(65 >= ((ScalarMeasurement) m).value()));
resultArray.get().forEach(m -> assertTrue(45 <= m.value()));
resultArray.get().forEach(m -> assertTrue(65 >= m.value()));
}
@Test

Просмотреть файл

@ -30,14 +30,14 @@ public class SymptomsArrayTest {
public void createTestArray() {
int[] ids = {1, 2, 3};
String[] types = {"s1", "s2"};
List<String> causes = Arrays.asList("c1", "c2", "c3");
List<String> attributions = Arrays.asList("c1", "c2", "c3");
Collection<Symptom> symptoms = new ArrayList<>();
int value = 10;
for (int id : ids) {
for (String type : types) {
symptoms.add(new Symptom(id, type, Instant.ofEpochMilli(value), causes));
symptoms.add(new Symptom(id, type, Instant.ofEpochMilli(value), attributions));
value += 10;
}
}
@ -69,13 +69,13 @@ public class SymptomsArrayTest {
}
@Test
public void cause() {
resultArray = testArray.cause("c1");
public void assignment() {
resultArray = testArray.assignment("c1");
assertEquals(6, resultArray.size());
resultArray.get().forEach(s -> assertEquals(1, s.causeIds().size()));
resultArray.get().forEach(s -> assertEquals("c1", s.causeIds().iterator().next()));
resultArray.get().forEach(s -> assertEquals(1, s.assignments().size()));
resultArray.get().forEach(s -> assertEquals("c1", s.assignments().iterator().next()));
resultArray = testArray.cause(Arrays.asList("c1", "c2"));
resultArray = testArray.assignment(Arrays.asList("c1", "c2"));
assertEquals(12, resultArray.size());
}
@ -123,7 +123,7 @@ public class SymptomsArrayTest {
@Test
public void sort() {
resultArray = testArray.cause("c3").between(Instant.ofEpochMilli(20), Instant.ofEpochMilli(30));
resultArray = testArray.assignment("c3").between(Instant.ofEpochMilli(20), Instant.ofEpochMilli(30));
assertEquals(2, resultArray.size());
assertEquals("s2", resultArray.first().type());
assertEquals("s1", resultArray.last().type());
@ -150,20 +150,20 @@ public class SymptomsArrayTest {
resultArray = testArray.id(1);
assertEquals(6, resultArray.size());
Iterator<Symptom> symptoms = testArray.get().iterator();
String firstCause = symptoms.next().causeIds().iterator().next();
String secondCause = symptoms.next().causeIds().iterator().next();
String assignment1 = symptoms.next().assignments().iterator().next();
String assignment2 = symptoms.next().assignments().iterator().next();
resultArray = resultArray.slice(0, 1);
assertEquals(2, resultArray.size());
symptoms = resultArray.get().iterator();
assertEquals(firstCause, symptoms.next().causeIds().iterator().next());
assertEquals(secondCause, symptoms.next().causeIds().iterator().next());
assertEquals(assignment1, symptoms.next().assignments().iterator().next());
assertEquals(assignment2, symptoms.next().assignments().iterator().next());
}
@Test
public void first() {
Symptom symptom = testArray.first();
assertEquals("c1", symptom.causeIds().iterator().next());
assertEquals("c1", symptom.assignments().iterator().next());
assertEquals(1, symptom.id());
assertEquals(10, symptom.instant().toEpochMilli());
}
@ -171,7 +171,7 @@ public class SymptomsArrayTest {
@Test
public void last() {
Symptom symptom = testArray.last();
assertEquals("c3", symptom.causeIds().iterator().next());
assertEquals("c3", symptom.assignments().iterator().next());
assertEquals(3, symptom.id());
assertEquals(60, symptom.instant().toEpochMilli());
}