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. * 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 component;
private final String instance; private final String instance;
private final String type; private final String type;
private final long instantMillis; // UTC 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.component = component;
this.instance = instance; this.instance = instance;
this.type = type; this.type = type;
this.instantMillis = instant.toEpochMilli(); this.instantMillis = instant.toEpochMilli();
this.value = value;
} }
public String component() { public String component() {
@ -40,40 +42,31 @@ public abstract class Measurement {
return Instant.ofEpochMilli(instantMillis); return Instant.ofEpochMilli(instantMillis);
} }
public static class ScalarMeasurement extends Measurement { public double value() {
private final double value; return value;
}
public ScalarMeasurement(String component, String instance, String metricType, Instant instant, double value) { @Override
super(component, instance, metricType, instant); public String toString() {
this.value = value; return "Measurement {" +
} "component=" + component() +
", instance=" + instance() +
public double value() { ", type=" + type() +
return value; ", 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 { 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) { public ObjMeasurement(String component, String instance, String metricType, Instant instant, Object value) {
super(component, instance, metricType, instant); super(component, instance, metricType, instant, 0);
this.value = value; this.reference = value;
} }
public Object value() { public Object reference() {
return value; return reference;
} }
} }
} }

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

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

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

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

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

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

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

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

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

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