ScoreList: Recompute given interval

pull/699/head
Alinson S. Xavier 5 years ago
parent d0c37fef67
commit 4a3a767cb2

@ -76,11 +76,19 @@ data class Habit(
frequency = frequency,
isNumerical = isNumerical,
)
val to = DateUtils.getTodayWithOffset()
val entries = computedEntries.getKnown()
var from = entries.lastOrNull()?.timestamp ?: to
if (from.isNewerThan(to)) from = to
scores.recompute(
this.frequency,
this.isNumerical,
this.targetValue,
this.computedEntries
this.computedEntries,
from,
to
)
}

@ -80,25 +80,21 @@ public class ScoreList
Frequency frequency,
boolean isNumerical,
double targetValue,
EntryList computedEntries
EntryList computedEntries,
Timestamp from,
Timestamp to
)
{
list.clear();
List<Entry> entries = computedEntries.getKnown();
if (entries.isEmpty()) return;
Entry oldest = entries.get(entries.size() - 1);
Timestamp today = DateUtils.getTodayWithOffset();
Timestamp from = oldest.getTimestamp();
if (from.isNewerThan(today)) return;
if (computedEntries.getKnown().isEmpty()) return;
if (from.isNewerThan(to)) return;
double rollingSum = 0.0;
int numerator = frequency.getNumerator();
int denominator = frequency.getDenominator();
final double freq = frequency.toDouble();
final Integer[] values = computedEntries
.getByInterval(from, today)
.getByInterval(from, to)
.stream()
.map(Entry::getValue)
.toArray(Integer[]::new);

@ -249,6 +249,22 @@ public class ScoreListTest extends BaseUnitTest
assertThat(habit.getScores().get(today).getValue(), closeTo(0.054816, E));
}
@Test
public void test_addThenRemove()
{
Habit habit = fixtures.createEmptyHabit();
habit.recompute();
assertThat(habit.getScores().get(today).getValue(), closeTo(0.0, E));
habit.getOriginalEntries().add(new Entry(today, YES_MANUAL));
habit.recompute();
assertThat(habit.getScores().get(today).getValue(), closeTo(0.051922, E));
habit.getOriginalEntries().add(new Entry(today, UNKNOWN));
habit.recompute();
assertThat(habit.getScores().get(today).getValue(), closeTo(0.0, E));
}
private void check(final int offset)
{
EntryList entries = habit.getOriginalEntries();

Loading…
Cancel
Save