Skip to content

Commit

Permalink
Add AggregationWindowPreferences, fixes test that assume monday to st…
Browse files Browse the repository at this point in the history
…art week
  • Loading branch information
hhpmmd committed Oct 5, 2021
1 parent 32b0737 commit a3ca584
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
41 changes: 28 additions & 13 deletions app/src/main/java/com/samco/trackandgraph/statistics/Statistics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<DataPoint>()
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) }
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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))) -> {
Expand Down Expand Up @@ -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
)
}

Expand Down Expand Up @@ -459,10 +473,11 @@ internal fun getHistogramBinsForSample(
sample: DataSample,
window: TimeHistogramWindow,
feature: Feature,
sumByCount: Boolean
sumByCount: Boolean,
aggPreferences: AggregationWindowPreferences? = null
): Map<Int, List<Double>>? {
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -285,7 +287,8 @@ class Statistics_calculateDurationAccumulatedValues_KtTest {
0L,
null,
endTime,
plotTotalTime
plotTotalTime,
aggPreferences
)

//THEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!!
Expand Down
2 changes: 2 additions & 0 deletions cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cd app/src/main/java/com/samco/trackandgraph/antlr/generated
sed -i '/bobby2/d' *

0 comments on commit a3ca584

Please sign in to comment.