Fix table.add for empty assignments and time fitler

This commit is contained in:
Ashvin Agrawal 2018-02-21 14:08:09 -08:00
Родитель 75ff6d2a36
Коммит 1cf3173c4c
6 изменённых файлов: 94 добавлений и 80 удалений

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

@ -38,14 +38,7 @@ public class ActionTable extends OutcomeTable<Action> {
}
private void addAll(Collection<Action> actions) {
actions.forEach(action -> {
action.assignments().forEach(assignment -> {
id.append(action.id());
this.assignment.append(assignment);
type.append(action.type());
timeStamp.append(action.instant().toEpochMilli());
});
});
actions.forEach(this::add);
}
/**

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

@ -38,14 +38,7 @@ public class DiagnosisTable extends OutcomeTable<Diagnosis> {
}
private void addAll(Collection<Diagnosis> diagnosis) {
diagnosis.forEach(diagnoses -> {
diagnoses.assignments().forEach(assignment -> {
id.append(diagnoses.id());
this.assignment.append(assignment);
type.append(diagnoses.type());
timeStamp.append(diagnoses.instant().toEpochMilli());
});
});
diagnosis.forEach(this::add);
}
/**

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

@ -10,10 +10,7 @@ import tech.tablesaw.api.CategoryColumn;
import tech.tablesaw.api.DoubleColumn;
import tech.tablesaw.api.LongColumn;
import tech.tablesaw.api.Table;
import tech.tablesaw.columns.ColumnReference;
import tech.tablesaw.filtering.Filter;
import tech.tablesaw.filtering.LongGreaterThanOrEqualTo;
import tech.tablesaw.filtering.LongLessThanOrEqualTo;
import java.time.Instant;
import java.util.ArrayList;
@ -165,11 +162,7 @@ public class MeasurementsTable {
* @return {@link MeasurementsTable} containing filtered {@link Measurement}s
*/
public MeasurementsTable between(Instant oldest, Instant newest) {
Table result = measurements.selectWhere(
both(new LongGreaterThanOrEqualTo(new ColumnReference(TIME_STAMP), oldest.toEpochMilli()),
new LongLessThanOrEqualTo(new ColumnReference(TIME_STAMP), newest.toEpochMilli())));
return new MeasurementsTable(result);
return new MeasurementsTable(TableUtils.filterTime(measurements, TIME_STAMP, oldest, newest));
}
/**
@ -250,40 +243,28 @@ public class MeasurementsTable {
* @return unique components names in this collection of {@link Measurement}s
*/
public Collection<String> uniqueComponents() {
return findUniqueCategory(component);
return TableUtils.uniqueCategory(component);
}
/**
* @return unique instance names in this collection of {@link Measurement}s
*/
public Collection<String> uniqueInstances() {
return findUniqueCategory(instance);
return TableUtils.uniqueCategory(instance);
}
/**
* @return unique metric types in this collection of {@link Measurement}s
*/
public Collection<String> uniqueTypes() {
return findUniqueCategory(type);
return TableUtils.uniqueCategory(type);
}
/**
* @return unique {@link Instant}s in this collection of {@link Measurement}s
*/
public Collection<Instant> uniqueInstants() {
ArrayList<Instant> result = new ArrayList<>();
LongColumn uniqueColumn = timeStamps.unique();
for (Long ts : uniqueColumn) {
result.add(Instant.ofEpochMilli(ts));
}
return result;
}
private Collection<String> findUniqueCategory(CategoryColumn column) {
ArrayList<String> result = new ArrayList<>();
CategoryColumn uniqueColumn = column.unique();
uniqueColumn.forEach(result::add);
return result;
return TableUtils.uniqueInstants(timeStamps);
}
/**
@ -299,13 +280,7 @@ public class MeasurementsTable {
columns[i] = sortKeys[i].name();
}
Table result;
if (descending) {
result = measurements.sortDescendingOn(columns);
} else {
result = measurements.sortAscendingOn(columns);
}
return new MeasurementsTable(result);
return new MeasurementsTable(TableUtils.sort(measurements, descending, columns));
}
/**

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

@ -10,10 +10,7 @@ import tech.tablesaw.api.CategoryColumn;
import tech.tablesaw.api.IntColumn;
import tech.tablesaw.api.LongColumn;
import tech.tablesaw.api.Table;
import tech.tablesaw.columns.ColumnReference;
import tech.tablesaw.filtering.Filter;
import tech.tablesaw.filtering.LongGreaterThanOrEqualTo;
import tech.tablesaw.filtering.LongLessThanOrEqualTo;
import java.time.Instant;
import java.util.ArrayList;
@ -22,7 +19,6 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static tech.tablesaw.api.QueryHelper.both;
import static tech.tablesaw.api.QueryHelper.column;
import static tech.tablesaw.api.QueryHelper.or;
@ -33,7 +29,7 @@ import static tech.tablesaw.api.QueryHelper.or;
* {@link Outcome} instances.
*/
public abstract class OutcomeTable<T extends Outcome> {
final Table table;
private final Table table;
final CategoryColumn type;
final IntColumn id;
final CategoryColumn assignment;
@ -44,6 +40,8 @@ public abstract class OutcomeTable<T extends Outcome> {
private static final String TIME_STAMP = SortKey.TIME_STAMP.name();
private static final String TYPE = SortKey.TYPE.name();
static final Collection<String> EMPTY_ASSIGNMENT = Collections.singletonList(CategoryColumn.MISSING_VALUE);
public enum SortKey {
ID, ASSIGNMENT, TIME_STAMP, TYPE
}
@ -69,6 +67,16 @@ public abstract class OutcomeTable<T extends Outcome> {
timeStamp = this.table.longColumn(TIME_STAMP);
}
protected final void add(Outcome outcome) {
Collection<String> assignments = outcome.assignments().isEmpty() ? EMPTY_ASSIGNMENT : outcome.assignments();
assignments.forEach(assignedComponent -> {
id.append(outcome.id());
assignment.append(assignedComponent);
type.append(outcome.type());
timeStamp.append(outcome.instant().toEpochMilli());
});
}
Table filterId(int id) {
return table.selectWhere(column(ID).isEqualTo(id));
}
@ -95,9 +103,7 @@ public abstract class OutcomeTable<T extends Outcome> {
}
Table filterTime(Instant oldest, Instant newest) {
return table.selectWhere(
both(new LongGreaterThanOrEqualTo(new ColumnReference(TIME_STAMP), oldest.toEpochMilli()),
new LongLessThanOrEqualTo(new ColumnReference(TIME_STAMP), newest.toEpochMilli())));
return TableUtils.filterTime(table, TIME_STAMP, oldest, newest);
}
/**
@ -123,22 +129,14 @@ public abstract class OutcomeTable<T extends Outcome> {
* @return unique {@link Outcome} types in this collection
*/
public Collection<String> uniqueTypes() {
ArrayList<String> result = new ArrayList<>();
CategoryColumn uniqueColumn = type.unique();
uniqueColumn.forEach(result::add);
return result;
return TableUtils.uniqueCategory(type);
}
/**
* @return unique {@link Instant}s at which {@link Outcome} objects were created
*/
public Collection<Instant> uniqueInstants() {
ArrayList<Instant> result = new ArrayList<>();
LongColumn uniqueColumn = timeStamp.unique();
for (Long ts : uniqueColumn) {
result.add(Instant.ofEpochMilli(ts));
}
return result;
return TableUtils.uniqueInstants(timeStamp);
}
Table sortTable(boolean descending, SortKey... sortKeys) {
@ -147,13 +145,7 @@ public abstract class OutcomeTable<T extends Outcome> {
columns[i] = sortKeys[i].name();
}
Table result;
if (descending) {
result = table.sortDescendingOn(columns);
} else {
result = table.sortAscendingOn(columns);
}
return result;
return TableUtils.sort(table, descending, columns);
}
Table sliceTable(int first, int last) {

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

@ -38,14 +38,7 @@ public class SymptomsTable extends OutcomeTable<Symptom> {
}
private void addAll(Collection<Symptom> symptoms) {
symptoms.forEach(symptom -> {
symptom.assignments().forEach(assignment -> {
id.append(symptom.id());
this.assignment.append(assignment);
type.append(symptom.type());
timeStamp.append(symptom.instant().toEpochMilli());
});
});
symptoms.forEach(this::add);
}
/**

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

@ -0,0 +1,68 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion.core;
import tech.tablesaw.api.CategoryColumn;
import tech.tablesaw.api.LongColumn;
import tech.tablesaw.api.Table;
import tech.tablesaw.columns.ColumnReference;
import tech.tablesaw.filtering.Filter;
import tech.tablesaw.filtering.LongGreaterThanOrEqualTo;
import tech.tablesaw.filtering.LongLessThanOrEqualTo;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static tech.tablesaw.api.QueryHelper.allOf;
class TableUtils {
static Table sort(Table table, boolean descending, String[] columns) {
Table result;
if (descending) {
result = table.sortDescendingOn(columns);
} else {
result = table.sortAscendingOn(columns);
}
return result;
}
static Table filterTime(Table table, String column, Instant oldest, Instant newest) {
List<Filter> filters = new ArrayList<>();
if (oldest != null) {
filters.add(new LongGreaterThanOrEqualTo(new ColumnReference(column), oldest.toEpochMilli()));
}
if (newest != null) {
filters.add(new LongLessThanOrEqualTo(new ColumnReference(column), newest.toEpochMilli()));
}
if (filters.isEmpty()) {
return table;
}
return table.selectWhere(allOf(filters));
}
static Collection<Instant> uniqueInstants(LongColumn timeStamps) {
ArrayList<Instant> result = new ArrayList<>();
LongColumn uniqueColumn = timeStamps.unique();
for (Long ts : uniqueColumn) {
result.add(Instant.ofEpochMilli(ts));
}
return result;
}
static Collection<String> uniqueCategory(CategoryColumn column) {
ArrayList<String> result = new ArrayList<>();
CategoryColumn uniqueColumn = column.unique();
uniqueColumn.forEach(result::add);
return result;
}
}