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, frequency = frequency,
isNumerical = isNumerical, isNumerical = isNumerical,
) )
val to = DateUtils.getTodayWithOffset()
val entries = computedEntries.getKnown()
var from = entries.lastOrNull()?.timestamp ?: to
if (from.isNewerThan(to)) from = to
scores.recompute( scores.recompute(
this.frequency, this.frequency,
this.isNumerical, this.isNumerical,
this.targetValue, this.targetValue,
this.computedEntries this.computedEntries,
from,
to
) )
} }

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

@ -249,6 +249,22 @@ public class ScoreListTest extends BaseUnitTest
assertThat(habit.getScores().get(today).getValue(), closeTo(0.054816, E)); 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) private void check(final int offset)
{ {
EntryList entries = habit.getOriginalEntries(); EntryList entries = habit.getOriginalEntries();

Loading…
Cancel
Save