Skip to content

Commit

Permalink
add logs to know how long sync processes take
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Jul 24, 2024
1 parent 9c567c5 commit ce4dd91
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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.logDuration
import org.ole.planet.myplanet.service.UploadToShelfService
import org.ole.planet.myplanet.utilities.AndroidDecrypter.Companion.generateIv
import org.ole.planet.myplanet.utilities.AndroidDecrypter.Companion.generateKey
Expand Down Expand Up @@ -90,6 +91,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<MyPlanet?> {
override fun onResponse(call: Call<MyPlanet?>, response: Response<MyPlanet?>) {
preferences.edit().putInt("LastWifiID", NetworkUtils.getCurrentNetworkId(context)).apply()
Expand Down Expand Up @@ -128,7 +130,11 @@ class Service(private val context: Context) {
callback.onError("Planet up to date", false)
}
}
val end = System.currentTimeMillis()
logDuration(start, end, "checkVersion")
} catch (e: Exception) {
val end = System.currentTimeMillis()
logDuration(start, end, "checkVersion")
callback.onError("New apk version required but not found on server - Contact admin", false)
}
}
Expand Down Expand Up @@ -290,7 +296,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<JsonObject?> {
override fun onResponse(call: Call<JsonObject?>, response: Response<JsonObject?>) {
if (response.isSuccessful) {
Expand Down Expand Up @@ -320,6 +326,8 @@ class Service(private val context: Context) {
val doc = firstRow.getAsJsonObject("doc")
val code = doc.getAsJsonPrimitive("code").asString
listener?.onConfigurationIdReceived(id, code)
val end = System.currentTimeMillis()
logDuration(start, end, "check app version")
} else {
showAlertDialog(context.getString(R.string.failed_to_get_configuration_id), false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,19 +77,23 @@ 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.getSystemService(Context.WIFI_SERVICE) as WifiManager
val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
val wifiInfo = wifiManager.connectionInfo
if (wifiInfo.supplicantState == SupplicantState.COMPLETED) {
settings.edit().putString("LastWifiSSID", wifiInfo.ssid).apply()
Expand Down Expand Up @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,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<DocumentResponse>? = 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", "")
Expand All @@ -51,8 +57,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")
}
}

Expand All @@ -75,6 +87,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", "")
Expand Down Expand Up @@ -185,4 +198,9 @@ object TransactionSyncManager {
e.printStackTrace()
}
}

fun logDuration(start: Long, end: Long, operation: String) {
val duration = end - start
println("Operation $operation took $duration milliseconds")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.ole.planet.myplanet.datamanager.*
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.AndroidDecrypter.Companion.AndroidDecrypter
Expand Down Expand Up @@ -614,6 +615,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
Expand All @@ -623,6 +625,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) {
Expand Down

0 comments on commit ce4dd91

Please sign in to comment.