Improve performance of copy operation

This commit is contained in:
TebbeUbben 2020-05-21 00:21:31 +02:00
parent db1f9366aa
commit 3fc2aede23
2 changed files with 23 additions and 13 deletions

View file

@ -39,6 +39,7 @@ import java.util.*
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import java.util.zip.ZipEntry import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream import java.util.zip.ZipOutputStream
import kotlin.math.max
object OpenHumansUploader : PluginBase( object OpenHumansUploader : PluginBase(
PluginDescription() PluginDescription()
@ -58,7 +59,10 @@ object OpenHumansUploader : PluginBase(
const val REDIRECT_URL = "androidaps://setup-openhumans" const val REDIRECT_URL = "androidaps://setup-openhumans"
const val AUTH_URL = "https://www.openhumans.org/direct-sharing/projects/oauth2/authorize/?client_id=$CLIENT_ID&response_type=code" const val AUTH_URL = "https://www.openhumans.org/direct-sharing/projects/oauth2/authorize/?client_id=$CLIENT_ID&response_type=code"
const val WORK_NAME = "Open Humans" const val WORK_NAME = "Open Humans"
const val NOTIFICATION_ID = 3122 const val COPY_NOTIFICATION_ID = 3122
const val FAILURE_NOTIFICATION_ID = 3123
const val SUCCESS_NOTIFICATION_ID = 3124
const val SIGNED_OUT_NOTIFICATION_ID = 3125
private val openHumansAPI = OpenHumansAPI(OPEN_HUMANS_URL, CLIENT_ID, CLIENT_SECRET, REDIRECT_URL) private val openHumansAPI = OpenHumansAPI(OPEN_HUMANS_URL, CLIENT_ID, CLIENT_SECRET, REDIRECT_URL)
private val FILE_NAME_DATE_FORMAT = SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.US).apply { timeZone = TimeZone.getTimeZone("UTC") } private val FILE_NAME_DATE_FORMAT = SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.US).apply { timeZone = TimeZone.getTimeZone("UTC") }
@ -269,29 +273,34 @@ object OpenHumansUploader : PluginBase(
copyDisposable?.dispose() copyDisposable?.dispose()
var currentProgress = 0L var currentProgress = 0L
var maxProgress = 0L var maxProgress = 0L
val increaseCounter = {
currentProgress++
//Updating the notification for every item drastically slows down the operation
if (currentProgress % 1000L == 0L) showOngoingNotification(maxProgress, currentProgress)
}
copyDisposable = Completable.fromCallable { MainApp.getDbHelper().clearOpenHumansQueue() } copyDisposable = Completable.fromCallable { MainApp.getDbHelper().clearOpenHumansQueue() }
.andThen(Single.defer { Single.just(MainApp.getDbHelper().countOfAllRows) }) .andThen(Single.defer { Single.just(MainApp.getDbHelper().countOfAllRows) })
.doOnSuccess { maxProgress = it } .doOnSuccess { maxProgress = it }
.flatMapObservable { Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allBgReadings) } } .flatMapObservable { Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allBgReadings) } }
.map { queueBGReading(it); showOngoingNotification(maxProgress, ++currentProgress) } .map { queueBGReading(it); increaseCounter() }
.ignoreElements() .ignoreElements()
.andThen(Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allCareportalEvents) }) .andThen(Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allCareportalEvents) })
.map { queueCareportalEvent(it); showOngoingNotification(maxProgress, ++currentProgress) } .map { queueCareportalEvent(it); increaseCounter() }
.ignoreElements() .ignoreElements()
.andThen(Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allExtendedBoluses) }) .andThen(Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allExtendedBoluses) })
.map { queueExtendedBolus(it); showOngoingNotification(maxProgress, ++currentProgress) } .map { queueExtendedBolus(it); increaseCounter() }
.ignoreElements() .ignoreElements()
.andThen(Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allProfileSwitches) }) .andThen(Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allProfileSwitches) })
.map { queueProfileSwitch(it); showOngoingNotification(maxProgress, ++currentProgress) } .map { queueProfileSwitch(it); increaseCounter() }
.ignoreElements() .ignoreElements()
.andThen(Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allTDDs) }) .andThen(Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allTDDs) })
.map { queueTotalDailyDose(it); showOngoingNotification(maxProgress, ++currentProgress) } .map { queueTotalDailyDose(it); increaseCounter() }
.ignoreElements() .ignoreElements()
.andThen(Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allTemporaryBasals) }) .andThen(Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allTemporaryBasals) })
.map { queueTemporaryBasal(it); showOngoingNotification(maxProgress, ++currentProgress) } .map { queueTemporaryBasal(it); increaseCounter() }
.ignoreElements() .ignoreElements()
.andThen(Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allTempTargets) }) .andThen(Observable.defer { Observable.fromIterable(MainApp.getDbHelper().allTempTargets) })
.map { queueTempTarget(it); showOngoingNotification(maxProgress, ++currentProgress) } .map { queueTempTarget(it); increaseCounter() }
.ignoreElements() .ignoreElements()
.doOnSubscribe { .doOnSubscribe {
wakeLock.acquire(TimeUnit.MINUTES.toMillis(20)) wakeLock.acquire(TimeUnit.MINUTES.toMillis(20))
@ -307,6 +316,7 @@ object OpenHumansUploader : PluginBase(
} }
.doFinally { .doFinally {
copyDisposable = null copyDisposable = null
NotificationManagerCompat.from(MainApp.instance()).cancel(COPY_NOTIFICATION_ID)
wakeLock.release() wakeLock.release()
} }
.onErrorComplete() .onErrorComplete()
@ -324,7 +334,7 @@ object OpenHumansUploader : PluginBase(
.setAutoCancel(false) .setAutoCancel(false)
.setSmallIcon(R.drawable.notif_icon) .setSmallIcon(R.drawable.notif_icon)
.build() .build()
NotificationManagerCompat.from(MainApp.instance()).notify(NOTIFICATION_ID, notification) NotificationManagerCompat.from(MainApp.instance()).notify(COPY_NOTIFICATION_ID, notification)
} }
private fun showSetupFinishedNotification() { private fun showSetupFinishedNotification() {
@ -335,7 +345,7 @@ object OpenHumansUploader : PluginBase(
.setSmallIcon(R.drawable.notif_icon) .setSmallIcon(R.drawable.notif_icon)
.build() .build()
val notificationManager = NotificationManagerCompat.from(MainApp.instance()) val notificationManager = NotificationManagerCompat.from(MainApp.instance())
notificationManager.notify(NOTIFICATION_ID, notification) notificationManager.notify(SUCCESS_NOTIFICATION_ID, notification)
} }
private fun showSetupFailedNotification() { private fun showSetupFailedNotification() {
@ -346,7 +356,7 @@ object OpenHumansUploader : PluginBase(
.setSmallIcon(R.drawable.notif_icon) .setSmallIcon(R.drawable.notif_icon)
.build() .build()
val notificationManager = NotificationManagerCompat.from(MainApp.instance()) val notificationManager = NotificationManagerCompat.from(MainApp.instance())
notificationManager.notify(NOTIFICATION_ID, notification) notificationManager.notify(FAILURE_NOTIFICATION_ID, notification)
} }
fun uploadData(): Completable = gatherData() fun uploadData(): Completable = gatherData()
@ -484,7 +494,7 @@ object OpenHumansUploader : PluginBase(
0 0
)) ))
.build() .build()
NotificationManagerCompat.from(MainApp.instance()).notify(NOTIFICATION_ID, notification) NotificationManagerCompat.from(MainApp.instance()).notify(SIGNED_OUT_NOTIFICATION_ID, notification)
logout() logout()
} }

View file

@ -1707,7 +1707,7 @@
<string name="setup_failed">Setup failed</string> <string name="setup_failed">Setup failed</string>
<string name="there_was_an_error">There was an error.</string> <string name="there_was_an_error">There was an error.</string>
<string name="open_humans_terms">This is an open source tool that will copy your data to Open Humans. We retain no rights to share your data with third parties without your explicit authorization. The data the project and app receive are identified via a random user ID and will only be securely transmitted to an Open Humans account with your authorization of that process. You can stop uploading and delete your upload data at any time via www.openhumans.org.</string> <string name="open_humans_terms">This is an open source tool that will copy your data to Open Humans. We retain no rights to share your data with third parties without your explicit authorization. The data the project and app receive are identified via a random user ID and will only be securely transmitted to an Open Humans account with your authorization of that process. You can stop uploading and delete your upload data at any time via www.openhumans.org.</string>
<string name="i_understand_and_agree">I understand and agree</string> <string name="i_understand_and_agree">I understand and agree.</string>
<string name="login">Login</string> <string name="login">Login</string>
<string name="logout">Logout</string> <string name="logout">Logout</string>
<string name="project_member_id">Project Member ID: %s</string> <string name="project_member_id">Project Member ID: %s</string>