From d9ceeb043ed12c80fc9599dd707aab82c088b271 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Tue, 30 Mar 2021 23:19:53 +0200 Subject: [PATCH] expandable carbs query --- .../info/nightscout/androidaps/database/AppRepository.kt | 6 ++++-- .../info/nightscout/androidaps/database/daos/CarbsDao.kt | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt index a467cf1ecd..e0b8b39b0e 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt @@ -330,6 +330,8 @@ open class AppRepository @Inject internal constructor( private fun Single>.expand() = this.map { it.map(::expandCarbs).flatten() } private fun Single>.filterOutExtended() = this.map { it.filter { c -> c.duration == 0L } } private fun Single>.fromTo(from: Long, to: Long) = this.map { it.filter { c -> c.timestamp in from..to } } + private fun Single>.until(to: Long) = this.map { it.filter { c -> c.timestamp <= to } } + private fun Single>.from(start: Long) = this.map { it.filter { c -> c.timestamp >= start } } private fun Single>.sort() = this.map { it.sortedBy { c -> c.timestamp } } /* @@ -386,7 +388,7 @@ open class AppRepository @Inject internal constructor( .subscribeOn(Schedulers.io()) fun getCarbsDataFromTimeToTimeExpanded(from: Long, to: Long, ascending: Boolean): Single> = - database.carbsDao.getCarbsFromTimeToTime(from - timeBackForExpand, to) + database.carbsDao.getCarbsFromTimeToTimeExpandable(from, to) .expand() .fromTo(from, to) .sort() @@ -399,7 +401,7 @@ open class AppRepository @Inject internal constructor( .subscribeOn(Schedulers.io()) fun getCarbsIncludingInvalidFromTimeToTimeExpanded(from: Long, to: Long, ascending: Boolean): Single> = - database.carbsDao.getCarbsIncludingInvalidFromTimeToTime(from - timeBackForExpand, to) + database.carbsDao.getCarbsIncludingInvalidFromTimeToTimeExpandable(from, to) .expand() .fromTo(from, to) .sort() diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/CarbsDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/CarbsDao.kt index e85d68885a..f52d0d3a60 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/CarbsDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/CarbsDao.kt @@ -38,12 +38,18 @@ internal interface CarbsDao : TraceableDao { @Query("SELECT * FROM $TABLE_CARBS WHERE isValid = 1 AND timestamp >= :from AND timestamp <= :to AND referenceId IS NULL ORDER BY id DESC") fun getCarbsFromTimeToTime(from: Long, to: Long): Single> + @Query("SELECT * FROM $TABLE_CARBS WHERE isValid = 1 AND timestamp + duration >= :from AND timestamp <= :to AND referenceId IS NULL ORDER BY id DESC") + fun getCarbsFromTimeToTimeExpandable(from: Long, to: Long): Single> + @Query("SELECT * FROM $TABLE_CARBS WHERE timestamp >= :timestamp AND referenceId IS NULL ORDER BY id DESC") fun getCarbsIncludingInvalidFromTime(timestamp: Long): Single> @Query("SELECT * FROM $TABLE_CARBS WHERE timestamp >= :from AND timestamp <= :to AND referenceId IS NULL ORDER BY id DESC") fun getCarbsIncludingInvalidFromTimeToTime(from: Long, to: Long): Single> + @Query("SELECT * FROM $TABLE_CARBS WHERE timestamp >= :from AND timestamp <= :to AND referenceId IS NULL ORDER BY id DESC") + fun getCarbsIncludingInvalidFromTimeToTimeExpandable(from: Long, to: Long): Single> + // This query will be used with v3 to get all changed records @Query("SELECT * FROM $TABLE_CARBS WHERE id > :id AND referenceId IS NULL OR id IN (SELECT DISTINCT referenceId FROM $TABLE_CARBS WHERE id > :id) ORDER BY id ASC") fun getModifiedFrom(id: Long): Single>