From a3ca58444d14fe0aaa4e3dd06033070cb9d88a12 Mon Sep 17 00:00:00 2001 From: hhpmmd Date: Tue, 5 Oct 2021 17:20:47 +0200 Subject: [PATCH] Add AggregationWindowPreferences, fixes test that assume monday to start week --- .../trackandgraph/statistics/Statistics.kt | 41 +++++++++++++------ ...lculateDurationAccumulatedValues_KtTest.kt | 5 ++- ...atistics_findBeginningOfTemporal_KtTest.kt | 13 +++--- ...istics_getHistogramBinsForSample_KtTest.kt | 5 ++- cleanup.sh | 2 + 5 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 cleanup.sh diff --git a/app/src/main/java/com/samco/trackandgraph/statistics/Statistics.kt b/app/src/main/java/com/samco/trackandgraph/statistics/Statistics.kt index 504edb3c..59848d4e 100644 --- a/app/src/main/java/com/samco/trackandgraph/statistics/Statistics.kt +++ b/app/src/main/java/com/samco/trackandgraph/statistics/Statistics.kt @@ -27,6 +27,7 @@ import com.samco.trackandgraph.database.entity.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import kotlinx.coroutines.yield +import org.threeten.bp.DayOfWeek import org.threeten.bp.Duration import org.threeten.bp.OffsetDateTime import org.threeten.bp.Period @@ -120,12 +121,13 @@ internal suspend fun calculateDurationAccumulatedValues( featureId: Long, sampleDuration: Duration?, endTime: OffsetDateTime?, - plotTotalTime: TemporalAmount + plotTotalTime: TemporalAmount, + aggPreferences: AggregationWindowPreferences? = null ): DataSample { val newData = mutableListOf() val latest = getEndTimeNowOrLatest(sampleData, endTime) val firstDataPointTime = getStartTimeOrFirst(sampleData, latest, endTime, sampleDuration) - var currentTimeStamp = findBeginningOfTemporal(firstDataPointTime, plotTotalTime).minusNanos(1) + var currentTimeStamp = findBeginningOfTemporal(firstDataPointTime, plotTotalTime, aggPreferences).minusNanos(1) var index = 0 while (currentTimeStamp.isBefore(latest)) { currentTimeStamp = currentTimeStamp.with { ld -> ld.plus(plotTotalTime) } @@ -186,20 +188,29 @@ private fun getEndTimeNowOrLatest(rawData: DataSample, endTime: OffsetDateTime?) * Duration.ofDays(365) or a year * */ + +internal data class AggregationWindowPreferences(val firstDayOfWeek: DayOfWeek) { + constructor() : this(firstDayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek) +} + internal fun findBeginningOfTemporal( dateTime: OffsetDateTime, - temporalAmount: TemporalAmount + temporalAmount: TemporalAmount, + aggregationWindowPreferences: AggregationWindowPreferences? = null ): OffsetDateTime { + // if no preferences are give, use the default ones from default constructor + val aggPreferences = aggregationWindowPreferences ?: AggregationWindowPreferences() return when (temporalAmount) { - is Duration -> findBeginningOfDuration(dateTime, temporalAmount) - is Period -> findBeginningOfPeriod(dateTime, temporalAmount) + is Duration -> findBeginningOfDuration(dateTime, temporalAmount, aggPreferences ) + is Period -> findBeginningOfPeriod(dateTime, temporalAmount, aggPreferences) else -> dateTime } } private fun findBeginningOfDuration( dateTime: OffsetDateTime, - duration: Duration + duration: Duration, + aggregationWindowPreferences: AggregationWindowPreferences ): OffsetDateTime { val dt = dateTime .withMinute(0) @@ -215,7 +226,7 @@ private fun findBeginningOfDuration( duration.minus(nano.plus(Duration.ofDays(7))).isNegative -> { dt.with( TemporalAdjusters.previousOrSame( - WeekFields.of(Locale.getDefault()).firstDayOfWeek + aggregationWindowPreferences.firstDayOfWeek ) ).withHour(0) } @@ -248,7 +259,8 @@ internal fun getBiYearForMonthValue(monthValue: Int) = if (monthValue < 7) 1 els private fun findBeginningOfPeriod( dateTime: OffsetDateTime, - period: Period + period: Period, + aggregationWindowPreferences: AggregationWindowPreferences ): OffsetDateTime { val dt = dateTime.withHour(0) .withMinute(0) @@ -258,7 +270,7 @@ private fun findBeginningOfPeriod( return when { isPeriodNegativeOrZero(period.minus(Period.ofDays(1))) -> dt isPeriodNegativeOrZero(period.minus(Period.ofWeeks(1))) -> { - val firstDay = WeekFields.of(Locale.getDefault()).firstDayOfWeek + val firstDay = aggregationWindowPreferences.firstDayOfWeek dt.with(TemporalAdjusters.previousOrSame(firstDay)) } isPeriodNegativeOrZero(period.minus(Period.ofMonths(1))) -> { @@ -424,12 +436,14 @@ internal fun getXYSeriesFromDataSample( */ internal fun getNextEndOfWindow( window: TimeHistogramWindow, - endDate: OffsetDateTime? + endDate: OffsetDateTime?, + aggregationWindowPreferences: AggregationWindowPreferences? = null ): OffsetDateTime { val end = endDate ?: OffsetDateTime.now() return findBeginningOfTemporal( end.plus(window.period), - window.period + window.period, + aggregationWindowPreferences ) } @@ -459,10 +473,11 @@ internal fun getHistogramBinsForSample( sample: DataSample, window: TimeHistogramWindow, feature: Feature, - sumByCount: Boolean + sumByCount: Boolean, + aggPreferences: AggregationWindowPreferences? = null ): Map>? { if (sample.dataPoints.isEmpty()) return null - val endTime = getNextEndOfWindow(window, sample.dataPoints.maxBy { it.timestamp }!!.timestamp) + val endTime = getNextEndOfWindow(window, sample.dataPoints.maxBy { it.timestamp }!!.timestamp, aggPreferences) val isDiscrete = feature.featureType == FeatureType.DISCRETE val keys = if (isDiscrete) feature.discreteValues.map { it.index }.toSet() diff --git a/app/src/test/java/com/samco/trackandgraph/statistics/Statistics_calculateDurationAccumulatedValues_KtTest.kt b/app/src/test/java/com/samco/trackandgraph/statistics/Statistics_calculateDurationAccumulatedValues_KtTest.kt index d5a2dc90..3b7ffc53 100644 --- a/app/src/test/java/com/samco/trackandgraph/statistics/Statistics_calculateDurationAccumulatedValues_KtTest.kt +++ b/app/src/test/java/com/samco/trackandgraph/statistics/Statistics_calculateDurationAccumulatedValues_KtTest.kt @@ -22,12 +22,14 @@ import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test +import org.threeten.bp.DayOfWeek import org.threeten.bp.Duration import org.threeten.bp.OffsetDateTime import org.threeten.bp.Period import org.threeten.bp.temporal.TemporalAmount class Statistics_calculateDurationAccumulatedValues_KtTest { + private val aggPreferences = AggregationWindowPreferences(DayOfWeek.MONDAY) @Test fun calculateDurationAccumulatedValues_hourly_plot_totals() { @@ -285,7 +287,8 @@ class Statistics_calculateDurationAccumulatedValues_KtTest { 0L, null, endTime, - plotTotalTime + plotTotalTime, + aggPreferences ) //THEN diff --git a/app/src/test/java/com/samco/trackandgraph/statistics/Statistics_findBeginningOfTemporal_KtTest.kt b/app/src/test/java/com/samco/trackandgraph/statistics/Statistics_findBeginningOfTemporal_KtTest.kt index 0d7ca00d..d27551a9 100644 --- a/app/src/test/java/com/samco/trackandgraph/statistics/Statistics_findBeginningOfTemporal_KtTest.kt +++ b/app/src/test/java/com/samco/trackandgraph/statistics/Statistics_findBeginningOfTemporal_KtTest.kt @@ -19,12 +19,11 @@ package com.samco.trackandgraph.statistics import org.junit.Assert.assertEquals import org.junit.Test -import org.threeten.bp.Duration -import org.threeten.bp.OffsetDateTime -import org.threeten.bp.Period -import org.threeten.bp.ZoneOffset +import org.threeten.bp.* class Statistics_findBeginningOfTemporal_KtTest { + private val aggPreferences = AggregationWindowPreferences(DayOfWeek.MONDAY) + @Test fun testDurationHour() { //GIVEN @@ -88,7 +87,7 @@ class Statistics_findBeginningOfTemporal_KtTest { val temporal = Duration.ofDays(1).plus(Duration.ofNanos(1)) //WHEN - val answer = findBeginningOfTemporal(dateTime, temporal) + val answer = findBeginningOfTemporal(dateTime, temporal, aggPreferences) //THEN val expected = OffsetDateTime.of( @@ -106,7 +105,7 @@ class Statistics_findBeginningOfTemporal_KtTest { val temporal = Duration.ofDays(7) //WHEN - val answer = findBeginningOfTemporal(dateTime, temporal) + val answer = findBeginningOfTemporal(dateTime, temporal, aggPreferences) //THEN val expected = OffsetDateTime.of( @@ -286,7 +285,7 @@ class Statistics_findBeginningOfTemporal_KtTest { val temporal = Period.ofWeeks(1) //WHEN - val answer = findBeginningOfTemporal(dateTime, temporal) + val answer = findBeginningOfTemporal(dateTime, temporal, aggPreferences) //THEN val expected = OffsetDateTime.of( diff --git a/app/src/test/java/com/samco/trackandgraph/statistics/Statistics_getHistogramBinsForSample_KtTest.kt b/app/src/test/java/com/samco/trackandgraph/statistics/Statistics_getHistogramBinsForSample_KtTest.kt index af08ba1e..98a5fbab 100644 --- a/app/src/test/java/com/samco/trackandgraph/statistics/Statistics_getHistogramBinsForSample_KtTest.kt +++ b/app/src/test/java/com/samco/trackandgraph/statistics/Statistics_getHistogramBinsForSample_KtTest.kt @@ -20,11 +20,14 @@ package com.samco.trackandgraph.statistics import com.samco.trackandgraph.database.entity.* import org.junit.Assert.* import org.junit.Test +import org.threeten.bp.DayOfWeek import org.threeten.bp.OffsetDateTime import org.threeten.bp.ZoneOffset import kotlin.random.Random class Statistics_getHistogramBinsForSample_KtTest { + private val aggPreferences = AggregationWindowPreferences(DayOfWeek.MONDAY) + @Test fun test_getHistogramBinsForSample_sumByVal_week_cont() { //GIVEN @@ -38,7 +41,7 @@ class Statistics_getHistogramBinsForSample_KtTest { val sumByCount = false //WHEN - val answer = getHistogramBinsForSample(sample, window, feature, sumByCount) + val answer = getHistogramBinsForSample(sample, window, feature, sumByCount, aggPreferences) //THEN answer!! diff --git a/cleanup.sh b/cleanup.sh new file mode 100644 index 00000000..7c0ee318 --- /dev/null +++ b/cleanup.sh @@ -0,0 +1,2 @@ +cd app/src/main/java/com/samco/trackandgraph/antlr/generated +sed -i '/bobby2/d' *