Fix HRV Status days

This commit is contained in:
José Rebelo 2024-10-05 14:17:08 +01:00
parent f6a8d05fd6
commit 67346a06e5
2 changed files with 35 additions and 22 deletions

View File

@ -43,9 +43,11 @@ import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Optional;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
@ -57,6 +59,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.HrvSummarySample; import nodomain.freeyourgadget.gadgetbridge.model.HrvSummarySample;
import nodomain.freeyourgadget.gadgetbridge.model.HrvValueSample; import nodomain.freeyourgadget.gadgetbridge.model.HrvValueSample;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.HRVStatusWeeklyData> { public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.HRVStatusWeeklyData> {
@ -239,36 +242,35 @@ public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.H
} }
private List<HRVStatusDayData> getWeeklyData(DBHandler db, Calendar day, GBDevice device) { private List<HRVStatusDayData> getWeeklyData(DBHandler db, Calendar day, GBDevice device) {
day = (Calendar) day.clone(); // do not modify the caller's argument day = DateTimeUtils.dayStart(day);
day.add(Calendar.DATE, -TOTAL_DAYS); day.add(Calendar.DATE, -TOTAL_DAYS + 1);
List<HRVStatusDayData> weeklyData = new ArrayList<>(); List<HRVStatusDayData> weeklyData = new ArrayList<>();
for (int counter = 0; counter < TOTAL_DAYS; counter++) { for (int counter = 0; counter < TOTAL_DAYS; counter++) {
int startTs = (int) (day.getTimeInMillis() / 1000); int startTs = (int) (day.getTimeInMillis() / 1000);
int endTs = startTs + 24 * 60 * 60 - 1; int endTs = startTs + 24 * 60 * 60 - 1;
day.add(Calendar.DATE, 1); Optional<? extends HrvSummarySample> latestSummarySample = getSamples(db, device, startTs, endTs)
List<? extends HrvSummarySample> summarySamples = getSamples(db, device, startTs, endTs); .stream()
.max(Comparator.comparingLong(HrvSummarySample::getTimestamp));
List<? extends HrvValueSample> valueSamples = getHrvValueSamples(db, device, startTs, endTs); List<? extends HrvValueSample> valueSamples = getHrvValueSamples(db, device, startTs, endTs);
int avgHRV = (int) valueSamples.stream().mapToInt(HrvValueSample::getValue).average().orElse(0); int avgHRV = (int) valueSamples.stream().mapToInt(HrvValueSample::getValue).average().orElse(0);
if (!summarySamples.isEmpty()) { if (latestSummarySample.isPresent()) {
int finalCounter = counter; final HrvSummarySample sample = latestSummarySample.get();
Calendar finalDay = (Calendar) day.clone(); Calendar finalDay = (Calendar) day.clone();
summarySamples.forEach(sample -> { weeklyData.add(new HRVStatusDayData(
weeklyData.add(new HRVStatusDayData( finalDay,
finalDay, counter,
finalCounter, sample.getTimestamp(),
sample.getTimestamp(), avgHRV,
avgHRV, sample.getWeeklyAverage() != null ? sample.getWeeklyAverage() : 0,
sample.getWeeklyAverage() != null ? sample.getWeeklyAverage() : 0, sample.getLastNightAverage() != null ? sample.getLastNightAverage() : 0,
sample.getLastNightAverage() != null ? sample.getLastNightAverage() : 0, sample.getLastNight5MinHigh() != null ? sample.getLastNight5MinHigh() : 0,
sample.getLastNight5MinHigh() != null ? sample.getLastNight5MinHigh() : 0, sample.getBaselineLowUpper() != null ? sample.getBaselineLowUpper() : 0,
sample.getBaselineLowUpper() != null ? sample.getBaselineLowUpper() : 0, sample.getBaselineBalancedLower() != null ? sample.getBaselineBalancedLower() : 0,
sample.getBaselineBalancedLower() != null ? sample.getBaselineBalancedLower() : 0, sample.getBaselineBalancedUpper() != null ? sample.getBaselineBalancedUpper() : 0,
sample.getBaselineBalancedUpper() != null ? sample.getBaselineBalancedUpper() : 0, sample.getStatus() != null ? sample.getStatus() : HrvSummarySample.Status.NONE
sample.getStatus() != null ? sample.getStatus() : HrvSummarySample.Status.NONE ));
));
});
} else { } else {
HRVStatusDayData d = new HRVStatusDayData( HRVStatusDayData d = new HRVStatusDayData(
(Calendar) day.clone(), (Calendar) day.clone(),
@ -285,6 +287,8 @@ public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.H
); );
weeklyData.add(d); weeklyData.add(d);
} }
day.add(Calendar.DATE, 1);
} }
return weeklyData; return weeklyData;
} }
@ -407,4 +411,4 @@ public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.H
this.baseLineBalancedUpper = baseLineBalancedUpper; this.baseLineBalancedUpper = baseLineBalancedUpper;
} }
} }
} }

View File

@ -129,6 +129,15 @@ public class DateTimeUtils {
return calendar.getTime(); return calendar.getTime();
} }
public static Calendar dayStart(final Calendar calendar) {
final Calendar ret = (Calendar) calendar.clone();
ret.set(Calendar.HOUR_OF_DAY, 0);
ret.set(Calendar.MINUTE, 0);
ret.set(Calendar.SECOND, 0);
ret.set(Calendar.MILLISECOND, 0);
return ret;
}
public static Date dayStart(final LocalDate date) { public static Date dayStart(final LocalDate date) {
final Calendar calendar = Calendar.getInstance(); final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, date.getYear()); calendar.set(Calendar.YEAR, date.getYear());