Skip to content

Commit

Permalink
Change DataPoint refs to its interface, refactor data accumulation fo…
Browse files Browse the repository at this point in the history
…r reusability
  • Loading branch information
hhpmmd committed Oct 5, 2021
1 parent 0c85590 commit 1d68771
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataPointInterface>
val parents: List<DataPointInterface>,
override val label: String = "",
override val note: String = "",
): DataPointInterface
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AverageTimeBetweenDataFactory :
now: OffsetDateTime,
endDate: OffsetDateTime?,
duration: Duration?,
dataPoints: List<DataPoint>
dataPoints: List<DataPointInterface>
): Double? {
if (duration == null && dataPoints.size < 2) return null
val last = dataPoints.lastOrNull()?.timestamp
Expand All @@ -65,7 +65,7 @@ class AverageTimeBetweenDataFactory :
override suspend fun createViewData(
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
onDataSampled: (List<DataPoint>) -> Unit
onDataSampled: (List<DataPointInterface>) -> Unit
): IAverageTimeBetweenViewData {
val timeBetweenStat = dataSource.getAverageTimeBetweenStatByGraphStatId(graphOrStat.id)
?: return notEnoughData(graphOrStat)
Expand All @@ -76,7 +76,7 @@ class AverageTimeBetweenDataFactory :
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
config: AverageTimeBetweenStat,
onDataSampled: (List<DataPoint>) -> Unit
onDataSampled: (List<DataPointInterface>) -> Unit
): IAverageTimeBetweenViewData {
val feature = dataSource.getFeatureById(config.featureId)
val dataPoints = getRelevantDataPoints(dataSource, config, feature)
Expand Down Expand Up @@ -111,7 +111,7 @@ class AverageTimeBetweenDataFactory :
dataSource: TrackAndGraphDatabaseDao,
timeBetweenStat: AverageTimeBetweenStat,
feature: Feature
): List<DataPoint> {
): List<DataPointInterface> {
val endDate = timeBetweenStat.endDate ?: when (feature.featureType) {
FeatureType.CONTINUOUS, FeatureType.DURATION -> {
dataSource.getLastDataPointBetween(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class LineGraphDataFactory : ViewDataFactory<LineGraphWithFeatures, ILineGraphVi
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
config: LineGraphWithFeatures,
onDataSampled: (List<DataPoint>) -> Unit
onDataSampled: (List<DataPointInterface>) -> Unit
): ILineGraphViewData {
val endTime = config.endDate ?: OffsetDateTime.now()
val allReferencedDataPoints = mutableListOf<DataPoint>()
val allReferencedDataPoints = mutableListOf<DataPointInterface>()
val plottableData = generatePlottingData(
dataSource,
config,
Expand Down Expand Up @@ -91,7 +91,7 @@ class LineGraphDataFactory : ViewDataFactory<LineGraphWithFeatures, ILineGraphVi
override suspend fun createViewData(
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
onDataSampled: (List<DataPoint>) -> Unit
onDataSampled: (List<DataPointInterface>) -> Unit
): ILineGraphViewData {
val lineGraph = dataSource.getLineGraphByGraphStatId(graphOrStat.id)
?: return object : ILineGraphViewData {
Expand All @@ -108,7 +108,7 @@ class LineGraphDataFactory : ViewDataFactory<LineGraphWithFeatures, ILineGraphVi
private suspend fun generatePlottingData(
dataSource: TrackAndGraphDatabaseDao,
lineGraph: LineGraphWithFeatures,
allReferencedDataPoints: MutableList<DataPoint>,
allReferencedDataPoints: MutableList<DataPointInterface>,
endTime: OffsetDateTime
): Map<LineGraphFeature, FastXYSeries?> {
return lineGraph.features.map { lgf ->
Expand All @@ -123,7 +123,7 @@ class LineGraphDataFactory : ViewDataFactory<LineGraphWithFeatures, ILineGraphVi
private suspend fun tryGetPlottingData(
dataSource: TrackAndGraphDatabaseDao,
lineGraph: LineGraphWithFeatures,
allReferencedDataPoints: MutableList<DataPoint>,
allReferencedDataPoints: MutableList<DataPointInterface>,
lineGraphFeature: LineGraphFeature
): DataSample? {
val movingAvDuration = movingAverageDurations[lineGraphFeature.averagingMode]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,7 +36,7 @@ class PieChartDataFactory : ViewDataFactory<PieChart, IPieChartViewData>() {
override suspend fun createViewData(
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
onDataSampled: (List<DataPoint>) -> Unit
onDataSampled: (List<DataPointInterface>) -> Unit
): IPieChartViewData {
val pieChart = dataSource.getPieChartByGraphStatId(graphOrStat.id)
?: return object : IPieChartViewData {
Expand All @@ -53,7 +54,7 @@ class PieChartDataFactory : ViewDataFactory<PieChart, IPieChartViewData>() {
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
config: PieChart,
onDataSampled: (List<DataPoint>) -> Unit
onDataSampled: (List<DataPointInterface>) -> Unit
): IPieChartViewData {
val plottingData = tryGetPlottableDataForPieChart(dataSource, config)
?: return object : IPieChartViewData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TimeHistogramDataFactory : ViewDataFactory<TimeHistogram, ITimeHistogramVi
override suspend fun createViewData(
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
onDataSampled: (List<DataPoint>) -> Unit
onDataSampled: (List<DataPointInterface>) -> Unit
): ITimeHistogramViewData {
val timeHistogram = dataSource.getTimeHistogramByGraphStatId(graphOrStat.id)
?: return object : ITimeHistogramViewData {
Expand All @@ -52,7 +52,7 @@ class TimeHistogramDataFactory : ViewDataFactory<TimeHistogram, ITimeHistogramVi
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
config: TimeHistogram,
onDataSampled: (List<DataPoint>) -> Unit
onDataSampled: (List<DataPointInterface>) -> Unit
): ITimeHistogramViewData {
val discreteValue = getDiscreteValues(dataSource, config) ?: listOf(DiscreteValue(0, ""))
val barValues = getBarValues(dataSource, config, config.endDate, onDataSampled)
Expand Down Expand Up @@ -95,7 +95,7 @@ class TimeHistogramDataFactory : ViewDataFactory<TimeHistogram, ITimeHistogramVi
dataSource: TrackAndGraphDatabaseDao,
config: TimeHistogram,
endDate: OffsetDateTime?,
onDataSampled: (List<DataPoint>) -> Unit
onDataSampled: (List<DataPointInterface>) -> Unit
): Map<Int, List<Double>>? {
val feature = dataSource.getFeatureById(config.featureId)
val sample = sampleData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,7 +28,7 @@ class TimeSinceViewDataFactory : ViewDataFactory<TimeSinceLastStat, ITimeSinceVi
override suspend fun createViewData(
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
onDataSampled: (List<DataPoint>) -> Unit
onDataSampled: (List<DataPointInterface>) -> Unit
): ITimeSinceViewData {
val timeSinceStat = dataSource.getTimeSinceLastStatByGraphStatId(graphOrStat.id)
?: return object : ITimeSinceViewData {
Expand All @@ -50,12 +47,12 @@ class TimeSinceViewDataFactory : ViewDataFactory<TimeSinceLastStat, ITimeSinceVi
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
config: TimeSinceLastStat,
onDataSampled: (List<DataPoint>) -> Unit
onDataSampled: (List<DataPointInterface>) -> 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
Expand All @@ -67,7 +64,7 @@ class TimeSinceViewDataFactory : ViewDataFactory<TimeSinceLastStat, ITimeSinceVi
private fun getLastDataPoint(
dataSource: TrackAndGraphDatabaseDao,
timeSinceLastStat: TimeSinceLastStat
): DataPoint? {
): DataPointInterface? {
val feature = dataSource.getFeatureById(timeSinceLastStat.featureId)
return when (feature.featureType) {
FeatureType.CONTINUOUS, FeatureType.DURATION -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,22 +36,22 @@ abstract class ViewDataFactory<in I, out T : IGraphStatViewData> {
protected abstract suspend fun createViewData(
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
onDataSampled: (List<DataPoint>) -> Unit
onDataSampled: (List<DataPointInterface>) -> Unit
): T

protected abstract suspend fun createViewData(
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
config: I,
onDataSampled: (List<DataPoint>) -> Unit
onDataSampled: (List<DataPointInterface>) -> Unit
): T

@Suppress("UNCHECKED_CAST")
suspend fun getViewData(
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
config: Any,
onDataSampled: (List<DataPoint>) -> Unit = {}
onDataSampled: (List<DataPointInterface>) -> Unit = {}
): T =
withContext(Dispatchers.IO) {
return@withContext createViewData(dataSource, graphOrStat, config as I, onDataSampled)
Expand All @@ -59,7 +60,7 @@ abstract class ViewDataFactory<in I, out T : IGraphStatViewData> {
suspend fun getViewData(
dataSource: TrackAndGraphDatabaseDao,
graphOrStat: GraphOrStat,
onDataSampled: (List<DataPoint>) -> Unit = {}
onDataSampled: (List<DataPointInterface>) -> Unit = {}
): T =
withContext(Dispatchers.IO) {
return@withContext createViewData(dataSource, graphOrStat, onDataSampled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 24 additions & 3 deletions app/src/main/java/com/samco/trackandgraph/statistics/Statistics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,28 @@ internal suspend fun calculateDurationAccumulatedValues(
plotTotalTime: TemporalAmount,
aggPreferences: AggregationWindowPreferences? = null
): DataSample {
val newData = mutableListOf<DataPoint>()
return calculateDurationAggregatedValues(sampleData, featureId, sampleDuration, endTime, plotTotalTime, aggPreferences,
{ points: List<DataPointInterface>, 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<DataPointInterface>, OffsetDateTime) -> AggregatedDataPoint
): DataSample {
val newData = mutableListOf<DataPointInterface>()
val latest = getEndTimeNowOrLatest(sampleData, endTime)
val firstDataPointTime = getStartTimeOrFirst(sampleData, latest, endTime, sampleDuration)
var currentTimeStamp = findBeginningOfTemporal(firstDataPointTime, plotTotalTime, aggPreferences).minusNanos(1)
Expand All @@ -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,
Expand Down

0 comments on commit 1d68771

Please sign in to comment.