From e2d91f6203d7fdf60a6ce1666ed11cc9f284e181 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 1 Feb 2023 14:05:56 +0100 Subject: [PATCH] NSCv3: improve loading --- .../info/nightscout/sdk/NSAndroidClientImpl.kt | 12 ++++++------ .../nightscout/sdk/interfaces/NSAndroidClient.kt | 12 ++++++------ .../sdk/networking/NightscoutRemoteService.kt | 14 +++++++------- .../plugins/sync/nsclientV3/NSClientV3Plugin.kt | 2 +- .../sync/nsclientV3/workers/LoadBgWorker.kt | 6 ++++-- .../nsclientV3/workers/LoadProfileStoreWorker.kt | 1 + .../nsclientV3/workers/LoadTreatmentsWorker.kt | 6 ++++-- 7 files changed, 29 insertions(+), 24 deletions(-) diff --git a/core/ns-sdk/src/main/java/info/nightscout/sdk/NSAndroidClientImpl.kt b/core/ns-sdk/src/main/java/info/nightscout/sdk/NSAndroidClientImpl.kt index 6287d2f028..e56b3ae64f 100644 --- a/core/ns-sdk/src/main/java/info/nightscout/sdk/NSAndroidClientImpl.kt +++ b/core/ns-sdk/src/main/java/info/nightscout/sdk/NSAndroidClientImpl.kt @@ -126,7 +126,7 @@ class NSAndroidClientImpl( throw UnsuccessfullNightscoutException() } - override suspend fun getSgvsModifiedSince(from: Long, limit: Long): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { + override suspend fun getSgvsModifiedSince(from: Long, limit: Int): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { val response = api.getSgvsModifiedSince(from, limit) if (response.isSuccessful) { @@ -139,7 +139,7 @@ class NSAndroidClientImpl( throw UnsuccessfullNightscoutException() } - override suspend fun getSgvsNewerThan(from: Long, limit: Long): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { + override suspend fun getSgvsNewerThan(from: Long, limit: Int): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { val response = api.getSgvsNewerThan(from, limit) if (response.isSuccessful) { @@ -230,7 +230,7 @@ class NSAndroidClientImpl( throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message()) } - override suspend fun getTreatmentsNewerThan(createdAt: String, limit: Long): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { + override suspend fun getTreatmentsNewerThan(createdAt: String, limit: Int): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { val response = api.getTreatmentsNewerThan(createdAt, limit) if (response.isSuccessful) { @@ -241,7 +241,7 @@ class NSAndroidClientImpl( throw UnsuccessfullNightscoutException() } - override suspend fun getTreatmentsModifiedSince(from: Long, limit: Long): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { + override suspend fun getTreatmentsModifiedSince(from: Long, limit: Int): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { val response = api.getTreatmentsModifiedSince(from, limit) if (response.isSuccessful) { @@ -380,7 +380,7 @@ class NSAndroidClientImpl( throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message()) } - override suspend fun getFoods(limit: Long): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { + override suspend fun getFoods(limit: Int): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { val response = api.getFoods(limit) if (response.isSuccessful) { @@ -392,7 +392,7 @@ class NSAndroidClientImpl( } /* - override suspend fun getFoodsModifiedSince(from: Long, limit: Long): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { + override suspend fun getFoodsModifiedSince(from: Long, limit: Int): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { val response = api.getFoodsModifiedSince(from, limit) val eTagString = response.headers()["ETag"] diff --git a/core/ns-sdk/src/main/java/info/nightscout/sdk/interfaces/NSAndroidClient.kt b/core/ns-sdk/src/main/java/info/nightscout/sdk/interfaces/NSAndroidClient.kt index 290d05b4e8..7c6a5c3f80 100644 --- a/core/ns-sdk/src/main/java/info/nightscout/sdk/interfaces/NSAndroidClient.kt +++ b/core/ns-sdk/src/main/java/info/nightscout/sdk/interfaces/NSAndroidClient.kt @@ -23,13 +23,13 @@ interface NSAndroidClient { suspend fun getLastModified(): LastModified suspend fun getSgvs(): ReadResponse> - suspend fun getSgvsModifiedSince(from: Long, limit: Long): ReadResponse> - suspend fun getSgvsNewerThan(from: Long, limit: Long): ReadResponse> + suspend fun getSgvsModifiedSince(from: Long, limit: Int): ReadResponse> + suspend fun getSgvsNewerThan(from: Long, limit: Int): ReadResponse> suspend fun createSvg(nsSgvV3: NSSgvV3): CreateUpdateResponse suspend fun updateSvg(nsSgvV3: NSSgvV3): CreateUpdateResponse - suspend fun getTreatmentsNewerThan(createdAt: String, limit: Long): ReadResponse> - suspend fun getTreatmentsModifiedSince(from: Long, limit: Long): ReadResponse> + suspend fun getTreatmentsNewerThan(createdAt: String, limit: Int): ReadResponse> + suspend fun getTreatmentsModifiedSince(from: Long, limit: Int): ReadResponse> suspend fun createDeviceStatus(nsDeviceStatus: NSDeviceStatus): CreateUpdateResponse suspend fun getDeviceStatusModifiedSince(from: Long): List @@ -40,9 +40,9 @@ interface NSAndroidClient { suspend fun createTreatment(nsTreatment: NSTreatment): CreateUpdateResponse suspend fun updateTreatment(nsTreatment: NSTreatment): CreateUpdateResponse - suspend fun getFoods(limit: Long): ReadResponse> + suspend fun getFoods(limit: Int): ReadResponse> - //suspend fun getFoodsModifiedSince(from: Long, limit: Long): ReadResponse> + //suspend fun getFoodsModifiedSince(from: Long, limit: Int): ReadResponse> suspend fun createFood(nsFood: NSFood): CreateUpdateResponse suspend fun updateFood(nsFood: NSFood): CreateUpdateResponse } \ No newline at end of file diff --git a/core/ns-sdk/src/main/java/info/nightscout/sdk/networking/NightscoutRemoteService.kt b/core/ns-sdk/src/main/java/info/nightscout/sdk/networking/NightscoutRemoteService.kt index 5206bfa14d..902874df7e 100644 --- a/core/ns-sdk/src/main/java/info/nightscout/sdk/networking/NightscoutRemoteService.kt +++ b/core/ns-sdk/src/main/java/info/nightscout/sdk/networking/NightscoutRemoteService.kt @@ -42,10 +42,10 @@ internal interface NightscoutRemoteService { suspend fun getSgvs(): Response>> @GET("v3/entries") - suspend fun getSgvsNewerThan(@Query(value = "date\$gt", encoded = true) date: Long, @Query("limit") limit: Long): Response>> + suspend fun getSgvsNewerThan(@Query(value = "date\$gt", encoded = true) date: Long, @Query("limit") limit: Int): Response>> @GET("v3/entries/history/{from}") - suspend fun getSgvsModifiedSince(@Path("from") from: Long, @Query("limit") limit: Long): Response>> + suspend fun getSgvsModifiedSince(@Path("from") from: Long, @Query("limit") limit: Int): Response>> @POST("v3/entries") suspend fun createEntry(@Body remoteEntry: RemoteEntry): Response> @@ -57,10 +57,10 @@ internal interface NightscoutRemoteService { suspend fun deleteEntry(@Path("identifier") identifier: String): Response> @GET("v3/treatments") - suspend fun getTreatmentsNewerThan(@Query(value = "created_at\$gt", encoded = true) createdAt: String, @Query("limit") limit: Long): Response>> + suspend fun getTreatmentsNewerThan(@Query(value = "created_at\$gt", encoded = true) createdAt: String, @Query("limit") limit: Int): Response>> @GET("v3/treatments/history/{from}") - suspend fun getTreatmentsModifiedSince(@Path("from") from: Long, @Query("limit") limit: Long): Response>> + suspend fun getTreatmentsModifiedSince(@Path("from") from: Long, @Query("limit") limit: Int): Response>> @POST("v3/treatments") suspend fun createTreatment(@Body remoteTreatment: RemoteTreatment): Response> @@ -78,11 +78,11 @@ internal interface NightscoutRemoteService { suspend fun getDeviceStatusModifiedSince(@Path("from") from: Long): Response>> @GET("v3/food") - suspend fun getFoods(@Query("limit") limit: Long): Response>> + suspend fun getFoods(@Query("limit") limit: Int): Response>> /* @GET("v3/food/history/{from}") - suspend fun getFoodsModifiedSince(@Path("from") from: Long, @Query("limit") limit: Long): Response>> + suspend fun getFoodsModifiedSince(@Path("from") from: Long, @Query("limit") limit: Int): Response>> */ @POST("v3/food") suspend fun createFood(@Body remoteFood: RemoteFood): Response> @@ -94,7 +94,7 @@ internal interface NightscoutRemoteService { suspend fun deleteFood(@Path("identifier") identifier: String): Response> @GET("v3/profile/history/{from}") - suspend fun getProfileModifiedSince(@Path("from") from: Long, @Query("limit") limit: Long = 10): Response>> + suspend fun getProfileModifiedSince(@Path("from") from: Long, @Query("limit") limit: Int = 10): Response>> @GET("v3/profile?sort\$desc=date&limit=1") diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt index 3acb4c2dcd..bd1019d213 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt @@ -119,7 +119,7 @@ class NSClientV3Plugin @Inject constructor( val JOB_NAME: String = this::class.java.simpleName val REFRESH_INTERVAL = T.secs(30).msecs() - const val RECORDS_TO_LOAD = 500L + const val RECORDS_TO_LOAD = 500 } private val disposable = CompositeDisposable() diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorker.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorker.kt index 93afbb4d5f..bf5d253bc2 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorker.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorker.kt @@ -62,6 +62,7 @@ class LoadBgWorker( if (isFirstLoad) response = nsAndroidClient.getSgvsNewerThan(lastLoaded, NSClientV3Plugin.RECORDS_TO_LOAD) else { response = nsAndroidClient.getSgvsModifiedSince(lastLoaded, NSClientV3Plugin.RECORDS_TO_LOAD) + aapsLogger.debug(LTag.NSCLIENT, "lastLoadedSrvModified: ${response.lastServerModified}") response.lastServerModified?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.entries = it } nsClientV3Plugin.storeLastLoadedSrvModified() nsClientV3Plugin.scheduleIrregularExecution() // Idea is to run after 5 min after last BG @@ -74,6 +75,7 @@ class LoadBgWorker( // Objective0 sp.putBoolean(info.nightscout.core.utils.R.string.key_objectives_bg_is_available_in_ns, true) // Schedule processing of fetched data and continue of loading + val stopLoading = sgvs.size != NSClientV3Plugin.RECORDS_TO_LOAD || response.code == 304 workManager .beginUniqueWork( NSClientV3Plugin.JOB_NAME, @@ -81,8 +83,8 @@ class LoadBgWorker( OneTimeWorkRequest.Builder(workerClasses.nsClientSourceWorker).setInputData(dataWorkerStorage.storeInputData(sgvs)).build() ) // response 304 == Not modified (happens when date > srvModified => bad time on phone or server during upload - .then(response.code != 304, OneTimeWorkRequest.Builder(LoadBgWorker::class.java).build()) - .then(response.code == 304, OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build()) + .then(!stopLoading, OneTimeWorkRequest.Builder(LoadBgWorker::class.java).build()) + .then(stopLoading, OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build()) .enqueue() } else { // End first load diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadProfileStoreWorker.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadProfileStoreWorker.kt index 0acae4ffc4..2428816bfc 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadProfileStoreWorker.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadProfileStoreWorker.kt @@ -48,6 +48,7 @@ class LoadProfileStoreWorker( if (profiles.isNotEmpty()) { val profile = profiles[profiles.size - 1] // if srvModified found in response + aapsLogger.debug(LTag.NSCLIENT, "lastLoadedSrvModified: ${response.lastServerModified}") response.lastServerModified?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.profile = it } ?: // if srvModified found in record JsonHelper.safeGetLongAllowNull(profile, "srvModified")?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.profile = it } ?: diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadTreatmentsWorker.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadTreatmentsWorker.kt index b5159fa9bf..de4682b4c3 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadTreatmentsWorker.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadTreatmentsWorker.kt @@ -50,6 +50,7 @@ class LoadTreatmentsWorker( response = nsAndroidClient.getTreatmentsNewerThan(lastLoadedIso, NSClientV3Plugin.RECORDS_TO_LOAD) } else { response = nsAndroidClient.getTreatmentsModifiedSince(lastLoaded, NSClientV3Plugin.RECORDS_TO_LOAD) + aapsLogger.debug(LTag.NSCLIENT, "lastLoadedSrvModified: ${response.lastServerModified}") response.lastServerModified?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.treatments = it } nsClientV3Plugin.storeLastLoadedSrvModified() } @@ -59,6 +60,7 @@ class LoadTreatmentsWorker( val action = if (isFirstLoad) "RCV-FIRST" else "RCV" rxBus.send(EventNSClientNewLog(action, "${treatments.size} TRs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}")) // Schedule processing of fetched data and continue of loading + val stopLoading = treatments.size != NSClientV3Plugin.RECORDS_TO_LOAD || response.code == 304 WorkManager.getInstance(context) .beginUniqueWork( NSClientV3Plugin.JOB_NAME, @@ -68,8 +70,8 @@ class LoadTreatmentsWorker( .build() ) // response 304 == Not modified (happens when date > srvModified => bad time on phone or server during upload - .then(response.code != 304, OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build()) - .then(response.code == 304, OneTimeWorkRequest.Builder(LoadFoodsWorker::class.java).build()) + .then(!stopLoading, OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build()) + .then(stopLoading, OneTimeWorkRequest.Builder(LoadFoodsWorker::class.java).build()) .enqueue() } else { // End first load