From e65df1f1336fd48b9f490075ce4b5154f77a333e Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Wed, 24 Jul 2024 15:31:37 +0300 Subject: [PATCH 1/2] add logs to know how long sync processes take --- .../planet/myplanet/datamanager/Service.kt | 14 ++++++++++--- .../planet/myplanet/service/SyncManager.kt | 7 +++++++ .../service/TransactionSyncManager.kt | 20 ++++++++++++++++++- .../planet/myplanet/ui/sync/SyncActivity.kt | 4 ++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt b/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt index 51eebe04b9..03a8b5cf20 100644 --- a/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt +++ b/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt @@ -25,7 +25,8 @@ import org.ole.planet.myplanet.model.MyPlanet import org.ole.planet.myplanet.model.RealmCommunity import org.ole.planet.myplanet.model.RealmUserModel.Companion.isUserExists import org.ole.planet.myplanet.model.RealmUserModel.Companion.populateUsersTable -import org.ole.planet.myplanet.service.TransactionSyncManager +import org.ole.planet.myplanet.service.TransactionSyncManager.logDuration +import org.ole.planet.myplanet.service.TransactionSyncManager.syncDb import org.ole.planet.myplanet.service.UploadToShelfService import org.ole.planet.myplanet.ui.sync.ProcessUserDataActivity import org.ole.planet.myplanet.ui.sync.SyncActivity @@ -105,6 +106,7 @@ class Service(private val context: Context) { callback.onError(context.getString(R.string.config_not_available), true) return } + val start = System.currentTimeMillis() retrofitInterface?.checkVersion(Utilities.getUpdateUrl(settings))?.enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { preferences.edit().putInt("LastWifiID", NetworkUtils.getCurrentNetworkId(context)).apply() @@ -143,8 +145,12 @@ class Service(private val context: Context) { callback.onError("Planet up to date", false) } } + val end = System.currentTimeMillis() + logDuration(start, end, "checkVersion") } catch (e: Exception) { e.printStackTrace() + val end = System.currentTimeMillis() + logDuration(start, end, "checkVersion") callback.onError("New apk version required but not found on server - Contact admin", false) } } @@ -269,7 +275,7 @@ class Service(private val context: Context) { context.startUpload("becomeMember") } } - TransactionSyncManager.syncDb(realm, "tablet_users") + syncDb(realm, "tablet_users") } } } @@ -326,7 +332,7 @@ class Service(private val context: Context) { setText(context.getString(R.string.check_apk_version)) show() } - + val start = System.currentTimeMillis() retrofitInterface?.getConfiguration("$url/versions")?.enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { if (response.isSuccessful) { @@ -356,6 +362,8 @@ class Service(private val context: Context) { val code = doc.getAsJsonPrimitive("code").asString listener?.onConfigurationIdReceived(id, code) activity.setSyncFailed(false) + val end = System.currentTimeMillis() + logDuration(start, end, "check app version") } else { activity.setSyncFailed(true) showAlertDialog(context.getString(R.string.failed_to_get_configuration_id), false) diff --git a/app/src/main/java/org/ole/planet/myplanet/service/SyncManager.kt b/app/src/main/java/org/ole/planet/myplanet/service/SyncManager.kt index 4766a88e3f..909424e3f6 100644 --- a/app/src/main/java/org/ole/planet/myplanet/service/SyncManager.kt +++ b/app/src/main/java/org/ole/planet/myplanet/service/SyncManager.kt @@ -26,6 +26,7 @@ import org.ole.planet.myplanet.model.RealmMyLibrary.Companion.save import org.ole.planet.myplanet.model.RealmMyTeam.Companion.insertMyTeams import org.ole.planet.myplanet.model.RealmResourceActivity.Companion.onSynced import org.ole.planet.myplanet.model.Rows +import org.ole.planet.myplanet.service.TransactionSyncManager.logDuration import org.ole.planet.myplanet.utilities.Constants import org.ole.planet.myplanet.utilities.Constants.PREFS_NAME import org.ole.planet.myplanet.utilities.Constants.ShelfData @@ -76,17 +77,21 @@ class SyncManager private constructor(private val context: Context) { private fun authenticateAndSync() { td = Thread { + val start = System.currentTimeMillis() if (TransactionSyncManager.authenticate()) { startSync() } else { handleException(context.getString(R.string.invalid_configuration)) destroy() } + val end = System.currentTimeMillis() + logDuration(start, end, "authenticateAndSync") } td?.start() } private fun startSync() { + val start = System.currentTimeMillis() try { val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager val wifiInfo = wifiManager.connectionInfo @@ -123,6 +128,8 @@ class SyncManager private constructor(private val context: Context) { err.printStackTrace() handleException(err.message) } finally { + val end = System.currentTimeMillis() + logDuration(start, end, "startSync") destroy() } } diff --git a/app/src/main/java/org/ole/planet/myplanet/service/TransactionSyncManager.kt b/app/src/main/java/org/ole/planet/myplanet/service/TransactionSyncManager.kt index b7284adc50..5bea10567e 100644 --- a/app/src/main/java/org/ole/planet/myplanet/service/TransactionSyncManager.kt +++ b/app/src/main/java/org/ole/planet/myplanet/service/TransactionSyncManager.kt @@ -49,18 +49,24 @@ import java.io.IOException object TransactionSyncManager { fun authenticate(): Boolean { val apiInterface = client?.create(ApiInterface::class.java) + val start = System.currentTimeMillis() try { val response: Response? = apiInterface?.getDocuments(Utilities.header, Utilities.getUrl() + "/tablet_users/_all_docs")?.execute() if (response != null) { + val end = System.currentTimeMillis() + logDuration(start, end, "authenticate") return response.code() == 200 } } catch (e: IOException) { e.printStackTrace() } + val end = System.currentTimeMillis() + logDuration(start, end, "authenticate") return false } fun syncAllHealthData(mRealm: Realm, settings: SharedPreferences, listener: SyncListener) { + val start = System.currentTimeMillis() listener.onSyncStarted() val userName = settings.getString("loginUserName", "") val password = settings.getString("loginUserPassword", "") @@ -70,8 +76,14 @@ object TransactionSyncManager { for (userModel in users) { syncHealthData(userModel, header) } - }, { listener.onSyncComplete() }) { error: Throwable -> + }, { + listener.onSyncComplete() + val end = System.currentTimeMillis() + logDuration(start, end, "syncAllHealthData") + }) { error: Throwable -> error.message?.let { listener.onSyncFailed(it) } + val end = System.currentTimeMillis() + logDuration(start, end, "syncAllHealthData") } } @@ -94,6 +106,7 @@ object TransactionSyncManager { } fun syncKeyIv(mRealm: Realm, settings: SharedPreferences, listener: SyncListener) { + val start = System.currentTimeMillis() listener.onSyncStarted() val model = UserProfileDbHandler(MainApplication.context).userModel val userName = settings.getString("loginUserName", "") @@ -228,4 +241,9 @@ object TransactionSyncManager { e.printStackTrace() } } + + fun logDuration(start: Long, end: Long, operation: String) { + val duration = end - start + println("Operation $operation took $duration milliseconds") + } } diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt b/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt index f7265cda84..ae07095f79 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt @@ -38,6 +38,7 @@ import org.ole.planet.myplanet.datamanager.ApiClient.client import org.ole.planet.myplanet.datamanager.Service.* import org.ole.planet.myplanet.model.* import org.ole.planet.myplanet.service.* +import org.ole.planet.myplanet.service.TransactionSyncManager.logDuration import org.ole.planet.myplanet.ui.dashboard.DashboardActivity import org.ole.planet.myplanet.ui.team.AdapterTeam.OnUserSelectedListener import org.ole.planet.myplanet.utilities.* @@ -724,6 +725,7 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers } private fun continueSync(dialog: MaterialDialog) { + val start = System.currentTimeMillis() processedUrl = saveConfigAndContinue(dialog) if (TextUtils.isEmpty(processedUrl)) return isSync = true @@ -733,6 +735,8 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers Service(this).isPlanetAvailable(object : PlanetAvailableListener { override fun isAvailable() { Service(context).checkVersion(this@SyncActivity, settings) + val end = System.currentTimeMillis() + logDuration(start, end, "continueSync") } override fun notAvailable() { if (!isFinishing) { From e4ed9143b0e02ddd8791bc1f75e332bf824d427f Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Wed, 24 Jul 2024 16:12:02 +0300 Subject: [PATCH 2/2] add more logs --- .../planet/myplanet/service/TransactionSyncManager.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/ole/planet/myplanet/service/TransactionSyncManager.kt b/app/src/main/java/org/ole/planet/myplanet/service/TransactionSyncManager.kt index 5bea10567e..5266673a0e 100644 --- a/app/src/main/java/org/ole/planet/myplanet/service/TransactionSyncManager.kt +++ b/app/src/main/java/org/ole/planet/myplanet/service/TransactionSyncManager.kt @@ -117,8 +117,14 @@ object TransactionSyncManager { mRealm.executeTransactionAsync({ realm: Realm -> val userModel = realm.where(RealmUserModel::class.java).equalTo("id", id).findFirst() syncHealthData(userModel, header) - }, { listener.onSyncComplete() }) { error: Throwable -> - error.message?.let { listener.onSyncFailed(it) } + }, { + listener.onSyncComplete() + val end = System.currentTimeMillis() + logDuration(start, end, "syncKeyIv") + }) { error: Throwable -> + error.message?.let { + listener.onSyncFailed(it) + } } }