From 1d687712ea4dfed871d72f070df763aef538f7c0 Mon Sep 17 00:00:00 2001 From: hhpmmd Date: Tue, 5 Oct 2021 19:53:28 +0200 Subject: [PATCH] Change DataPoint refs to its interface, refactor data accumulation for reusability --- .../database/entity/DataPoint.kt | 6 ++--- .../AverageTimeBetweenDataFactory.kt | 8 +++--- .../factories/LineGraphDataFactory.kt | 10 +++---- .../factories/PieChartDataFactory.kt | 5 ++-- .../factories/TimeHistogramDataFactory.kt | 6 ++--- .../factories/TimeSinceViewDataFactory.kt | 13 ++++----- .../factories/ViewDataFactory.kt | 9 ++++--- .../factories/viewdto/ITimeSinceViewData.kt | 3 ++- .../trackandgraph/statistics/Statistics.kt | 27 ++++++++++++++++--- 9 files changed, 54 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/samco/trackandgraph/database/entity/DataPoint.kt b/app/src/main/java/com/samco/trackandgraph/database/entity/DataPoint.kt index f7f34a2e..9b9aa5e8 100644 --- a/app/src/main/java/com/samco/trackandgraph/database/entity/DataPoint.kt +++ b/app/src/main/java/com/samco/trackandgraph/database/entity/DataPoint.kt @@ -77,7 +77,7 @@ data class AggregatedDataPoint ( override val timestamp: OffsetDateTime, override val featureId: Long, override val value: Double, - override val label: String, - override val note: String, - val parents: List + val parents: List, + override val label: String = "", + override val note: String = "", ): DataPointInterface \ No newline at end of file diff --git a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/AverageTimeBetweenDataFactory.kt b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/AverageTimeBetweenDataFactory.kt index bab549ee..64e6afd9 100644 --- a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/AverageTimeBetweenDataFactory.kt +++ b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/AverageTimeBetweenDataFactory.kt @@ -41,7 +41,7 @@ class AverageTimeBetweenDataFactory : now: OffsetDateTime, endDate: OffsetDateTime?, duration: Duration?, - dataPoints: List + dataPoints: List ): Double? { if (duration == null && dataPoints.size < 2) return null val last = dataPoints.lastOrNull()?.timestamp @@ -65,7 +65,7 @@ class AverageTimeBetweenDataFactory : override suspend fun createViewData( dataSource: TrackAndGraphDatabaseDao, graphOrStat: GraphOrStat, - onDataSampled: (List) -> Unit + onDataSampled: (List) -> Unit ): IAverageTimeBetweenViewData { val timeBetweenStat = dataSource.getAverageTimeBetweenStatByGraphStatId(graphOrStat.id) ?: return notEnoughData(graphOrStat) @@ -76,7 +76,7 @@ class AverageTimeBetweenDataFactory : dataSource: TrackAndGraphDatabaseDao, graphOrStat: GraphOrStat, config: AverageTimeBetweenStat, - onDataSampled: (List) -> Unit + onDataSampled: (List) -> Unit ): IAverageTimeBetweenViewData { val feature = dataSource.getFeatureById(config.featureId) val dataPoints = getRelevantDataPoints(dataSource, config, feature) @@ -111,7 +111,7 @@ class AverageTimeBetweenDataFactory : dataSource: TrackAndGraphDatabaseDao, timeBetweenStat: AverageTimeBetweenStat, feature: Feature - ): List { + ): List { val endDate = timeBetweenStat.endDate ?: when (feature.featureType) { FeatureType.CONTINUOUS, FeatureType.DURATION -> { dataSource.getLastDataPointBetween( diff --git a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/LineGraphDataFactory.kt b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/LineGraphDataFactory.kt index 81b5e770..8465bd95 100644 --- a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/LineGraphDataFactory.kt +++ b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/LineGraphDataFactory.kt @@ -42,10 +42,10 @@ class LineGraphDataFactory : ViewDataFactory) -> Unit + onDataSampled: (List) -> Unit ): ILineGraphViewData { val endTime = config.endDate ?: OffsetDateTime.now() - val allReferencedDataPoints = mutableListOf() + val allReferencedDataPoints = mutableListOf() val plottableData = generatePlottingData( dataSource, config, @@ -91,7 +91,7 @@ class LineGraphDataFactory : ViewDataFactory) -> Unit + onDataSampled: (List) -> Unit ): ILineGraphViewData { val lineGraph = dataSource.getLineGraphByGraphStatId(graphOrStat.id) ?: return object : ILineGraphViewData { @@ -108,7 +108,7 @@ class LineGraphDataFactory : ViewDataFactory, + allReferencedDataPoints: MutableList, endTime: OffsetDateTime ): Map { return lineGraph.features.map { lgf -> @@ -123,7 +123,7 @@ class LineGraphDataFactory : ViewDataFactory, + allReferencedDataPoints: MutableList, lineGraphFeature: LineGraphFeature ): DataSample? { val movingAvDuration = movingAverageDurations[lineGraphFeature.averagingMode] diff --git a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/PieChartDataFactory.kt b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/PieChartDataFactory.kt index cff1fa1f..7c67871d 100644 --- a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/PieChartDataFactory.kt +++ b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/PieChartDataFactory.kt @@ -21,6 +21,7 @@ import com.androidplot.pie.Segment import com.samco.trackandgraph.R import com.samco.trackandgraph.database.TrackAndGraphDatabaseDao import com.samco.trackandgraph.database.entity.DataPoint +import com.samco.trackandgraph.database.entity.DataPointInterface import com.samco.trackandgraph.database.entity.GraphOrStat import com.samco.trackandgraph.database.entity.PieChart import com.samco.trackandgraph.graphstatview.GraphStatInitException @@ -35,7 +36,7 @@ class PieChartDataFactory : ViewDataFactory() { override suspend fun createViewData( dataSource: TrackAndGraphDatabaseDao, graphOrStat: GraphOrStat, - onDataSampled: (List) -> Unit + onDataSampled: (List) -> Unit ): IPieChartViewData { val pieChart = dataSource.getPieChartByGraphStatId(graphOrStat.id) ?: return object : IPieChartViewData { @@ -53,7 +54,7 @@ class PieChartDataFactory : ViewDataFactory() { dataSource: TrackAndGraphDatabaseDao, graphOrStat: GraphOrStat, config: PieChart, - onDataSampled: (List) -> Unit + onDataSampled: (List) -> Unit ): IPieChartViewData { val plottingData = tryGetPlottableDataForPieChart(dataSource, config) ?: return object : IPieChartViewData { diff --git a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/TimeHistogramDataFactory.kt b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/TimeHistogramDataFactory.kt index 94f64ca6..3c88c509 100644 --- a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/TimeHistogramDataFactory.kt +++ b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/TimeHistogramDataFactory.kt @@ -34,7 +34,7 @@ class TimeHistogramDataFactory : ViewDataFactory) -> Unit + onDataSampled: (List) -> Unit ): ITimeHistogramViewData { val timeHistogram = dataSource.getTimeHistogramByGraphStatId(graphOrStat.id) ?: return object : ITimeHistogramViewData { @@ -52,7 +52,7 @@ class TimeHistogramDataFactory : ViewDataFactory) -> Unit + onDataSampled: (List) -> Unit ): ITimeHistogramViewData { val discreteValue = getDiscreteValues(dataSource, config) ?: listOf(DiscreteValue(0, "")) val barValues = getBarValues(dataSource, config, config.endDate, onDataSampled) @@ -95,7 +95,7 @@ class TimeHistogramDataFactory : ViewDataFactory) -> Unit + onDataSampled: (List) -> Unit ): Map>? { val feature = dataSource.getFeatureById(config.featureId) val sample = sampleData( diff --git a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/TimeSinceViewDataFactory.kt b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/TimeSinceViewDataFactory.kt index 3f417362..b1bdc98e 100644 --- a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/TimeSinceViewDataFactory.kt +++ b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/TimeSinceViewDataFactory.kt @@ -19,10 +19,7 @@ package com.samco.trackandgraph.graphstatview.factories import com.samco.trackandgraph.R import com.samco.trackandgraph.database.TrackAndGraphDatabaseDao -import com.samco.trackandgraph.database.entity.DataPoint -import com.samco.trackandgraph.database.entity.FeatureType -import com.samco.trackandgraph.database.entity.GraphOrStat -import com.samco.trackandgraph.database.entity.TimeSinceLastStat +import com.samco.trackandgraph.database.entity.* import com.samco.trackandgraph.graphstatview.GraphStatInitException import com.samco.trackandgraph.graphstatview.factories.viewdto.IGraphStatViewData import com.samco.trackandgraph.graphstatview.factories.viewdto.ITimeSinceViewData @@ -31,7 +28,7 @@ class TimeSinceViewDataFactory : ViewDataFactory) -> Unit + onDataSampled: (List) -> Unit ): ITimeSinceViewData { val timeSinceStat = dataSource.getTimeSinceLastStatByGraphStatId(graphOrStat.id) ?: return object : ITimeSinceViewData { @@ -50,12 +47,12 @@ class TimeSinceViewDataFactory : ViewDataFactory) -> Unit + onDataSampled: (List) -> Unit ): ITimeSinceViewData { val dataPoint = getLastDataPoint(dataSource, config) onDataSampled.invoke(dataPoint?.let { listOf(dataPoint) } ?: emptyList()) return object : ITimeSinceViewData { - override val lastDataPoint: DataPoint? + override val lastDataPoint: DataPointInterface? get() = dataPoint override val state: IGraphStatViewData.State get() = IGraphStatViewData.State.READY @@ -67,7 +64,7 @@ class TimeSinceViewDataFactory : ViewDataFactory { diff --git a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/ViewDataFactory.kt b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/ViewDataFactory.kt index aa0dd89a..b7d1b1fd 100644 --- a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/ViewDataFactory.kt +++ b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/ViewDataFactory.kt @@ -19,6 +19,7 @@ package com.samco.trackandgraph.graphstatview.factories import com.samco.trackandgraph.database.TrackAndGraphDatabaseDao import com.samco.trackandgraph.database.entity.DataPoint +import com.samco.trackandgraph.database.entity.DataPointInterface import com.samco.trackandgraph.database.entity.GraphOrStat import com.samco.trackandgraph.graphstatview.factories.viewdto.IGraphStatViewData import kotlinx.coroutines.Dispatchers @@ -35,14 +36,14 @@ abstract class ViewDataFactory { protected abstract suspend fun createViewData( dataSource: TrackAndGraphDatabaseDao, graphOrStat: GraphOrStat, - onDataSampled: (List) -> Unit + onDataSampled: (List) -> Unit ): T protected abstract suspend fun createViewData( dataSource: TrackAndGraphDatabaseDao, graphOrStat: GraphOrStat, config: I, - onDataSampled: (List) -> Unit + onDataSampled: (List) -> Unit ): T @Suppress("UNCHECKED_CAST") @@ -50,7 +51,7 @@ abstract class ViewDataFactory { dataSource: TrackAndGraphDatabaseDao, graphOrStat: GraphOrStat, config: Any, - onDataSampled: (List) -> Unit = {} + onDataSampled: (List) -> Unit = {} ): T = withContext(Dispatchers.IO) { return@withContext createViewData(dataSource, graphOrStat, config as I, onDataSampled) @@ -59,7 +60,7 @@ abstract class ViewDataFactory { suspend fun getViewData( dataSource: TrackAndGraphDatabaseDao, graphOrStat: GraphOrStat, - onDataSampled: (List) -> Unit = {} + onDataSampled: (List) -> Unit = {} ): T = withContext(Dispatchers.IO) { return@withContext createViewData(dataSource, graphOrStat, onDataSampled) diff --git a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/viewdto/ITimeSinceViewData.kt b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/viewdto/ITimeSinceViewData.kt index 6c654f00..af4ccd70 100644 --- a/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/viewdto/ITimeSinceViewData.kt +++ b/app/src/main/java/com/samco/trackandgraph/graphstatview/factories/viewdto/ITimeSinceViewData.kt @@ -18,11 +18,12 @@ package com.samco.trackandgraph.graphstatview.factories.viewdto import com.samco.trackandgraph.database.entity.DataPoint +import com.samco.trackandgraph.database.entity.DataPointInterface import com.samco.trackandgraph.database.entity.GraphOrStat import org.threeten.bp.OffsetDateTime interface ITimeSinceViewData : IGraphStatViewData { - val lastDataPoint: DataPoint? + val lastDataPoint: DataPointInterface? get() = null companion object { 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 ca6e849d..d4727ab5 100644 --- a/app/src/main/java/com/samco/trackandgraph/statistics/Statistics.kt +++ b/app/src/main/java/com/samco/trackandgraph/statistics/Statistics.kt @@ -124,7 +124,28 @@ internal suspend fun calculateDurationAccumulatedValues( plotTotalTime: TemporalAmount, aggPreferences: AggregationWindowPreferences? = null ): DataSample { - val newData = mutableListOf() + return calculateDurationAggregatedValues(sampleData, featureId, sampleDuration, endTime, plotTotalTime, aggPreferences, + { points: List, timestamp: OffsetDateTime -> + AggregatedDataPoint( + value = points.sumByDouble { dp -> dp.value }, + timestamp = timestamp, + featureId = featureId, + parents = points + ) + } + ) +} + +internal suspend fun calculateDurationAggregatedValues( + sampleData: DataSample, + featureId: Long, + sampleDuration: Duration?, + endTime: OffsetDateTime?, + plotTotalTime: TemporalAmount, + aggPreferences: AggregationWindowPreferences? = null, + aggFunction: (List, OffsetDateTime) -> AggregatedDataPoint +): DataSample { + val newData = mutableListOf() val latest = getEndTimeNowOrLatest(sampleData, endTime) val firstDataPointTime = getStartTimeOrFirst(sampleData, latest, endTime, sampleDuration) var currentTimeStamp = findBeginningOfTemporal(firstDataPointTime, plotTotalTime, aggPreferences).minusNanos(1) @@ -133,14 +154,14 @@ internal suspend fun calculateDurationAccumulatedValues( currentTimeStamp = currentTimeStamp.with { ld -> ld.plus(plotTotalTime) } val points = sampleData.dataPoints.drop(index) .takeWhile { dp -> dp.timestamp.isBefore(currentTimeStamp) } - val total = points.sumByDouble { dp -> dp.value } index += points.size - newData.add(DataPoint(currentTimeStamp, featureId, total, "", "")) + newData.add(aggFunction(points, currentTimeStamp)) yield() } return DataSample(newData) } + private fun getStartTimeOrFirst( sampleData: DataSample, latest: OffsetDateTime,