From 71b13917c47fe0bf2df3ab49fca50a77145b3c2f Mon Sep 17 00:00:00 2001 From: TebbeUbben Date: Fri, 4 Sep 2020 23:11:11 +0200 Subject: [PATCH] Segmental upload --- .../androidaps/db/DatabaseHelper.java | 8 ++++++-- .../general/openhumans/OHUploadWorker.kt | 2 +- .../general/openhumans/OpenHumansUploader.kt | 20 ++++++++++++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 1030b84a95..12103067c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -2069,9 +2069,13 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { } } - public List getAllOHQueueItems() { + public List getAllOHQueueItems(Long maxEntries) { try { - return getDaoOpenHumansQueue().queryForAll(); + return getDaoOpenHumansQueue() + .queryBuilder() + .orderBy("id", true) + .limit(maxEntries) + .query(); } catch (SQLException e) { aapsLogger.error("Unhandled exception", e); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OHUploadWorker.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OHUploadWorker.kt index afe76040d2..7ce4d784e2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OHUploadWorker.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OHUploadWorker.kt @@ -42,7 +42,7 @@ class OHUploadWorker(context: Context, workerParameters: WorkerParameters) val isConnectedToWifi = wifiManager?.isWifiEnabled ?: false && wifiManager?.connectionInfo?.networkId != -1 if (!wifiOnly || (wifiOnly && isConnectedToWifi)) { setForegroundAsync(createForegroundInfo()) - openHumansUploader.uploadData() + openHumansUploader.uploadDataSegmentally() .andThen(Single.just(Result.success())) .onErrorResumeNext { Single.just(Result.retry()) } } else { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansUploader.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansUploader.kt index a6463ef518..024861cfb7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansUploader.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansUploader.kt @@ -80,6 +80,7 @@ class OpenHumansUploader @Inject constructor( private const val SUCCESS_NOTIFICATION_ID = 3124 private const val SIGNED_OUT_NOTIFICATION_ID = 3125 const val UPLOAD_NOTIFICATION_ID = 3126 + private const val UPLOAD_SEGMENT_SIZE = 10000L } private val openHumansAPI = OpenHumansAPI(OPEN_HUMANS_URL, CLIENT_ID, CLIENT_SECRET, REDIRECT_URL) @@ -440,7 +441,20 @@ class OpenHumansUploader @Inject constructor( notificationManager.notify(FAILURE_NOTIFICATION_ID, notification) } - fun uploadData(): Completable = gatherData() + fun uploadDataSegmentally(): Completable = + uploadData(UPLOAD_SEGMENT_SIZE) + .repeatUntil { MainApp.getDbHelper().ohQueueSize == 0L } + .doOnSubscribe { + aapsLogger.info(LTag.OHUPLOADER, "Starting segmental upload") + } + .doOnComplete { + aapsLogger.info(LTag.OHUPLOADER, "Segmental upload successful") + } + .doOnError { + aapsLogger.error(LTag.OHUPLOADER, "Segmental upload erroneous", it) + } + + fun uploadData(maxEntries: Long?): Completable = gatherData(maxEntries) .flatMap { data -> refreshAccessTokensIfNeeded().map { accessToken -> accessToken to data } } .flatMap { uploadFile(it.first, it.second).andThen(Single.just(it.second)) } .flatMapCompletable { @@ -481,8 +495,8 @@ class OpenHumansUploader @Inject constructor( } } - private fun gatherData() = Single.defer { - val items = MainApp.getDbHelper().allOHQueueItems + private fun gatherData(maxEntries: Long?) = Single.defer { + val items = MainApp.getDbHelper().getAllOHQueueItems(maxEntries) val baos = ByteArrayOutputStream() val zos = ZipOutputStream(baos) val tags = mutableListOf()