NSCv3: improve loading

This commit is contained in:
Milos Kozak 2023-02-01 14:05:56 +01:00
parent 78295cf827
commit e2d91f6203
7 changed files with 29 additions and 24 deletions

View file

@ -126,7 +126,7 @@ class NSAndroidClientImpl(
throw UnsuccessfullNightscoutException()
}
override suspend fun getSgvsModifiedSince(from: Long, limit: Long): NSAndroidClient.ReadResponse<List<NSSgvV3>> = callWrapper(dispatcher) {
override suspend fun getSgvsModifiedSince(from: Long, limit: Int): NSAndroidClient.ReadResponse<List<NSSgvV3>> = 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<List<NSSgvV3>> = callWrapper(dispatcher) {
override suspend fun getSgvsNewerThan(from: Long, limit: Int): NSAndroidClient.ReadResponse<List<NSSgvV3>> = 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<List<NSTreatment>> = callWrapper(dispatcher) {
override suspend fun getTreatmentsNewerThan(createdAt: String, limit: Int): NSAndroidClient.ReadResponse<List<NSTreatment>> = 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<List<NSTreatment>> = callWrapper(dispatcher) {
override suspend fun getTreatmentsModifiedSince(from: Long, limit: Int): NSAndroidClient.ReadResponse<List<NSTreatment>> = 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<List<NSFood>> = callWrapper(dispatcher) {
override suspend fun getFoods(limit: Int): NSAndroidClient.ReadResponse<List<NSFood>> = 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<List<NSFood>> = callWrapper(dispatcher) {
override suspend fun getFoodsModifiedSince(from: Long, limit: Int): NSAndroidClient.ReadResponse<List<NSFood>> = callWrapper(dispatcher) {
val response = api.getFoodsModifiedSince(from, limit)
val eTagString = response.headers()["ETag"]

View file

@ -23,13 +23,13 @@ interface NSAndroidClient {
suspend fun getLastModified(): LastModified
suspend fun getSgvs(): ReadResponse<List<NSSgvV3>>
suspend fun getSgvsModifiedSince(from: Long, limit: Long): ReadResponse<List<NSSgvV3>>
suspend fun getSgvsNewerThan(from: Long, limit: Long): ReadResponse<List<NSSgvV3>>
suspend fun getSgvsModifiedSince(from: Long, limit: Int): ReadResponse<List<NSSgvV3>>
suspend fun getSgvsNewerThan(from: Long, limit: Int): ReadResponse<List<NSSgvV3>>
suspend fun createSvg(nsSgvV3: NSSgvV3): CreateUpdateResponse
suspend fun updateSvg(nsSgvV3: NSSgvV3): CreateUpdateResponse
suspend fun getTreatmentsNewerThan(createdAt: String, limit: Long): ReadResponse<List<NSTreatment>>
suspend fun getTreatmentsModifiedSince(from: Long, limit: Long): ReadResponse<List<NSTreatment>>
suspend fun getTreatmentsNewerThan(createdAt: String, limit: Int): ReadResponse<List<NSTreatment>>
suspend fun getTreatmentsModifiedSince(from: Long, limit: Int): ReadResponse<List<NSTreatment>>
suspend fun createDeviceStatus(nsDeviceStatus: NSDeviceStatus): CreateUpdateResponse
suspend fun getDeviceStatusModifiedSince(from: Long): List<NSDeviceStatus>
@ -40,9 +40,9 @@ interface NSAndroidClient {
suspend fun createTreatment(nsTreatment: NSTreatment): CreateUpdateResponse
suspend fun updateTreatment(nsTreatment: NSTreatment): CreateUpdateResponse
suspend fun getFoods(limit: Long): ReadResponse<List<NSFood>>
suspend fun getFoods(limit: Int): ReadResponse<List<NSFood>>
//suspend fun getFoodsModifiedSince(from: Long, limit: Long): ReadResponse<List<NSFood>>
//suspend fun getFoodsModifiedSince(from: Long, limit: Int): ReadResponse<List<NSFood>>
suspend fun createFood(nsFood: NSFood): CreateUpdateResponse
suspend fun updateFood(nsFood: NSFood): CreateUpdateResponse
}

View file

@ -42,10 +42,10 @@ internal interface NightscoutRemoteService {
suspend fun getSgvs(): Response<NSResponse<List<RemoteEntry>>>
@GET("v3/entries")
suspend fun getSgvsNewerThan(@Query(value = "date\$gt", encoded = true) date: Long, @Query("limit") limit: Long): Response<NSResponse<List<RemoteEntry>>>
suspend fun getSgvsNewerThan(@Query(value = "date\$gt", encoded = true) date: Long, @Query("limit") limit: Int): Response<NSResponse<List<RemoteEntry>>>
@GET("v3/entries/history/{from}")
suspend fun getSgvsModifiedSince(@Path("from") from: Long, @Query("limit") limit: Long): Response<NSResponse<List<RemoteEntry>>>
suspend fun getSgvsModifiedSince(@Path("from") from: Long, @Query("limit") limit: Int): Response<NSResponse<List<RemoteEntry>>>
@POST("v3/entries")
suspend fun createEntry(@Body remoteEntry: RemoteEntry): Response<NSResponse<RemoteCreateUpdateResponse>>
@ -57,10 +57,10 @@ internal interface NightscoutRemoteService {
suspend fun deleteEntry(@Path("identifier") identifier: String): Response<NSResponse<RemoteCreateUpdateResponse>>
@GET("v3/treatments")
suspend fun getTreatmentsNewerThan(@Query(value = "created_at\$gt", encoded = true) createdAt: String, @Query("limit") limit: Long): Response<NSResponse<List<RemoteTreatment>>>
suspend fun getTreatmentsNewerThan(@Query(value = "created_at\$gt", encoded = true) createdAt: String, @Query("limit") limit: Int): Response<NSResponse<List<RemoteTreatment>>>
@GET("v3/treatments/history/{from}")
suspend fun getTreatmentsModifiedSince(@Path("from") from: Long, @Query("limit") limit: Long): Response<NSResponse<List<RemoteTreatment>>>
suspend fun getTreatmentsModifiedSince(@Path("from") from: Long, @Query("limit") limit: Int): Response<NSResponse<List<RemoteTreatment>>>
@POST("v3/treatments")
suspend fun createTreatment(@Body remoteTreatment: RemoteTreatment): Response<NSResponse<RemoteCreateUpdateResponse>>
@ -78,11 +78,11 @@ internal interface NightscoutRemoteService {
suspend fun getDeviceStatusModifiedSince(@Path("from") from: Long): Response<NSResponse<List<RemoteDeviceStatus>>>
@GET("v3/food")
suspend fun getFoods(@Query("limit") limit: Long): Response<NSResponse<List<RemoteFood>>>
suspend fun getFoods(@Query("limit") limit: Int): Response<NSResponse<List<RemoteFood>>>
/*
@GET("v3/food/history/{from}")
suspend fun getFoodsModifiedSince(@Path("from") from: Long, @Query("limit") limit: Long): Response<NSResponse<List<RemoteFood>>>
suspend fun getFoodsModifiedSince(@Path("from") from: Long, @Query("limit") limit: Int): Response<NSResponse<List<RemoteFood>>>
*/
@POST("v3/food")
suspend fun createFood(@Body remoteFood: RemoteFood): Response<NSResponse<RemoteCreateUpdateResponse>>
@ -94,7 +94,7 @@ internal interface NightscoutRemoteService {
suspend fun deleteFood(@Path("identifier") identifier: String): Response<NSResponse<RemoteCreateUpdateResponse>>
@GET("v3/profile/history/{from}")
suspend fun getProfileModifiedSince(@Path("from") from: Long, @Query("limit") limit: Long = 10): Response<NSResponse<List<JSONObject>>>
suspend fun getProfileModifiedSince(@Path("from") from: Long, @Query("limit") limit: Int = 10): Response<NSResponse<List<JSONObject>>>
@GET("v3/profile?sort\$desc=date&limit=1")

View file

@ -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()

View file

@ -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

View file

@ -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 } ?:

View file

@ -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