mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
ScoreList: Recompute given interval
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user