ScoreList: Remove groupBy functions

This commit is contained in:
2020-12-29 14:10:27 -06:00
parent 4a3a767cb2
commit 9bc0f44777
7 changed files with 44 additions and 102 deletions

View File

@@ -77,7 +77,7 @@ data class Habit(
isNumerical = isNumerical,
)
val to = DateUtils.getTodayWithOffset()
val to = DateUtils.getTodayWithOffset().plus(30)
val entries = computedEntries.getKnown()
var from = entries.lastOrNull()?.timestamp ?: to
if (from.isNewerThan(to)) from = to

View File

@@ -21,8 +21,6 @@ package org.isoron.uhabits.core.models;
import androidx.annotation.*;
import org.isoron.uhabits.core.utils.*;
import java.util.*;
import static org.isoron.uhabits.core.models.Entry.*;
@@ -68,22 +66,13 @@ public class ScoreList
return result;
}
public List<Score> groupBy(DateUtils.TruncateField field, int firstWeekday)
{
HashMap<Timestamp, ArrayList<Double>> groups = getGroupedValues(field, firstWeekday);
List<Score> scores = groupsToAvgScores(groups);
Collections.sort(scores, (s1, s2) -> s2.compareNewer(s1));
return scores;
}
public void recompute(
Frequency frequency,
boolean isNumerical,
double targetValue,
EntryList computedEntries,
Timestamp from,
Timestamp to
)
Timestamp to)
{
list.clear();
if (computedEntries.getKnown().isEmpty()) return;
@@ -139,47 +128,4 @@ public class ScoreList
list.put(timestamp, new Score(timestamp, previousValue));
}
}
@NonNull
private HashMap<Timestamp, ArrayList<Double>> getGroupedValues(DateUtils.TruncateField field,
int firstWeekday)
{
HashMap<Timestamp, ArrayList<Double>> groups = new HashMap<>();
for (Score s : list.values())
{
Timestamp groupTimestamp = new Timestamp(
DateUtils.truncate(
field,
s.getTimestamp().getUnixTime(),
firstWeekday));
if (!groups.containsKey(groupTimestamp))
groups.put(groupTimestamp, new ArrayList<>());
groups.get(groupTimestamp).add(s.getValue());
}
return groups;
}
@NonNull
private List<Score> groupsToAvgScores(HashMap<Timestamp, ArrayList<Double>> groups)
{
List<Score> scores = new LinkedList<>();
for (Timestamp timestamp : groups.keySet())
{
double meanValue = 0.0;
ArrayList<Double> groupValues = groups.get(timestamp);
for (Double v : groupValues) meanValue += v;
meanValue /= groupValues.size();
scores.add(new Score(timestamp, meanValue));
}
return scores;
}
}

View File

@@ -266,6 +266,13 @@ public abstract class DateUtils
return Locale.getDefault();
}
public static Timestamp truncate(TruncateField field,
Timestamp timestamp,
int firstWeekday)
{
return new Timestamp(truncate(field, timestamp.getUnixTime(), firstWeekday));
}
public static Long truncate(TruncateField field,
long timestamp,
int firstWeekday)
@@ -275,6 +282,9 @@ public abstract class DateUtils
switch (field)
{
case DAY:
return cal.getTimeInMillis();
case MONTH:
cal.set(DAY_OF_MONTH, 1);
return cal.getTimeInMillis();
@@ -318,6 +328,6 @@ public abstract class DateUtils
public enum TruncateField
{
MONTH, WEEK_NUMBER, YEAR, QUARTER
DAY, MONTH, WEEK_NUMBER, YEAR, QUARTER
}
}

View File

@@ -222,19 +222,6 @@ public class ScoreListTest extends BaseUnitTest
assertThat(habit.getScores().get(today).getValue(), greaterThan(0.99));
}
@Test
public void test_groupBy()
{
Habit habit = fixtures.createLongHabit();
List<Score> list =
habit.getScores().groupBy(DateUtils.TruncateField.MONTH, Calendar.SATURDAY);
assertThat(list.size(), equalTo(5));
assertThat(list.get(0).getValue(), closeTo(0.644120, E));
assertThat(list.get(1).getValue(), closeTo(0.713651, E));
assertThat(list.get(2).getValue(), closeTo(0.571922, E));
}
@Test
public void test_recompute()
{