-
Notifications
You must be signed in to change notification settings - Fork 1
replace Stats with proper ScheduledApiClient #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
binarynoise
wants to merge
1
commit into
main
Choose a base branch
from
scheduled-api-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
228 changes: 228 additions & 0 deletions
228
app/src/main/kotlin/de/binarynoise/captiveportalautologin/ScheduledApiClient.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| package de.binarynoise.captiveportalautologin | ||
|
|
||
| import java.util.concurrent.TimeUnit | ||
| import kotlin.time.Clock | ||
| import kotlin.time.Duration.Companion.minutes | ||
| import kotlin.time.Instant | ||
| import kotlinx.coroutines.flow.Flow | ||
| import android.content.Context | ||
| import androidx.work.Constraints | ||
| import androidx.work.CoroutineWorker | ||
| import androidx.work.ExistingPeriodicWorkPolicy | ||
| import androidx.work.NetworkType | ||
| import androidx.work.OneTimeWorkRequest | ||
| import androidx.work.OutOfQuotaPolicy | ||
| import androidx.work.PeriodicWorkRequest | ||
| import androidx.work.WorkInfo | ||
| import androidx.work.WorkManager | ||
| import androidx.work.WorkerParameters | ||
| import androidx.work.workDataOf | ||
| import de.binarynoise.captiveportalautologin.BuildConfig.API_BASE | ||
| import de.binarynoise.captiveportalautologin.api.Api | ||
| import de.binarynoise.captiveportalautologin.api.json.har.HAR | ||
| import de.binarynoise.captiveportalautologin.client.ApiClient | ||
| import de.binarynoise.captiveportalautologin.preferences.SharedPreferences | ||
| import de.binarynoise.captiveportalautologin.util.applicationContext | ||
| import de.binarynoise.filedb.JsonDB | ||
| import de.binarynoise.logger.Logger.log | ||
| import de.binarynoise.util.okhttp.HttpStatusCodeException | ||
| import de.binarynoise.util.okhttp.parseRetryAfterOrNull | ||
| import okhttp3.HttpUrl.Companion.toHttpUrlOrNull | ||
|
|
||
| private val localCacheRoot = applicationContext.cacheDir.toPath().resolve("Stats") | ||
| private val jsonDB = JsonDB(localCacheRoot) | ||
|
|
||
|
|
||
| object ScheduledApiClient : Api { | ||
| override val har: Har = Har() | ||
| override val liberator: Liberator = Liberator() | ||
|
|
||
| class Har : Api.Har { | ||
| override fun submitHar(name: String, har: HAR) { | ||
| jsonDB.store(name, har, "har") | ||
| enqueueStatsUploadWork() | ||
| } | ||
| } | ||
|
|
||
| class Liberator : Api.Liberator { | ||
| override fun getLiberatorVersion(): String { | ||
| TODO("Not yet implemented") | ||
| } | ||
|
|
||
| override fun fetchLiberatorUpdate() { | ||
| TODO("Not yet implemented") | ||
| } | ||
|
|
||
| override fun reportError(error: Api.Liberator.Error) { | ||
| val key = "${System.currentTimeMillis()}_${error.hashCode()}" | ||
| jsonDB.store(key, error) | ||
| enqueueStatsUploadWork() | ||
| } | ||
|
|
||
| override fun reportSuccess(success: Api.Liberator.Success) { | ||
| val key = "${System.currentTimeMillis()}_${success.hashCode()}" | ||
| jsonDB.store(key, success) | ||
| enqueueStatsUploadWork() | ||
| } | ||
| } | ||
|
|
||
| @Deprecated("use ApiClient directly", level = DeprecationLevel.HIDDEN) | ||
| override suspend fun getSSIDs( | ||
| limit: Int?, majorVersion: Int?, since: Instant?, minimum: Int?, | ||
| ): List<String> { | ||
| throw UnsupportedOperationException("use ApiClient directly") | ||
| } | ||
| } | ||
|
|
||
| class HarStatsWorker( | ||
| appContext: Context, | ||
| workerParams: WorkerParameters, | ||
| ) : StatsWorker<HAR>( | ||
| appContext, | ||
| workerParams, | ||
| type = "HAR", | ||
| keys = { jsonDB.listAll<HAR>("har") }, | ||
| load = { jsonDB.load<HAR>(it, "har") }, | ||
| upload = { key, har, apiClient -> apiClient.har.submitHar(key, har) }, | ||
| delete = { jsonDB.delete<HAR>(it, "har") }, | ||
| ) | ||
|
|
||
| class SuccessStatsWorker( | ||
| appContext: Context, | ||
| workerParams: WorkerParameters, | ||
| ) : StatsWorker<Api.Liberator.Success>( | ||
| appContext, | ||
| workerParams, | ||
| type = "Success", | ||
| keys = { jsonDB.listAll<Api.Liberator.Success>() }, | ||
| load = { jsonDB.load<Api.Liberator.Success>(it) }, | ||
| upload = { key, success, apiClient -> apiClient.liberator.reportSuccess(success) }, | ||
| delete = { jsonDB.delete<Api.Liberator.Success>(it) }, | ||
| ) | ||
|
|
||
| class ErrorStatsWorker( | ||
| appContext: Context, | ||
| workerParams: WorkerParameters, | ||
| ) : StatsWorker<Api.Liberator.Error>( | ||
| appContext, | ||
| workerParams, | ||
| type = "Error", | ||
| keys = { jsonDB.listAll<Api.Liberator.Error>() }, | ||
| load = { jsonDB.load<Api.Liberator.Error>(it) }, | ||
| upload = { key, error, apiClient -> apiClient.liberator.reportError(error) }, | ||
| delete = { jsonDB.delete<Api.Liberator.Error>(it) }, | ||
| ) | ||
|
|
||
|
|
||
| abstract class StatsWorker<T : Any>( | ||
|
programminghoch10 marked this conversation as resolved.
|
||
| appContext: Context, | ||
| workerParams: WorkerParameters, | ||
| val type: String, | ||
| val keys: () -> Collection<String>, | ||
| val load: (key: String) -> T, | ||
| val upload: (key: String, T, ApiClient) -> Unit, | ||
| val delete: (key: String) -> Unit, | ||
| ) : CoroutineWorker(appContext, workerParams) { | ||
|
|
||
| private val retryStatusCodes = arrayOf(429, 500, 502, 503, 504, 506, 507) | ||
|
programminghoch10 marked this conversation as resolved.
|
||
|
|
||
| override suspend fun doWork(): Result { | ||
| var savedRetryAfter by SharedPreferences.stats_retry_after(type) | ||
| val now = Clock.System.now() | ||
| if (savedRetryAfter > now && !inputData.getBoolean("skipDelay", false)) { | ||
| log("Skipping $type upload - retry after ${savedRetryAfter - now}") | ||
| return Result.retry() | ||
| } | ||
|
|
||
| val apiBaseFromPreference by SharedPreferences.api_base | ||
| val apiBaseUrl = | ||
| (apiBaseFromPreference.takeUnless { it == "" } ?: API_BASE).toHttpUrlOrNull() ?: return Result.failure() | ||
| val apiClient = ApiClient(apiBaseUrl) | ||
|
|
||
| var shouldRetry = false | ||
|
|
||
|
|
||
| for (key in keys()) { | ||
| log("Uploading $type $key") | ||
| try { | ||
| val item = load(key) | ||
| upload(key, item, apiClient) | ||
| delete(key) | ||
| log("Uploaded $type $key") | ||
| } catch (e: HttpStatusCodeException) { | ||
| log("Failed to upload $type $key: HTTP ${e.code}") | ||
| if (e.code in retryStatusCodes) { | ||
| shouldRetry = true | ||
| val now = Clock.System.now() | ||
| savedRetryAfter = now + (parseRetryAfterOrNull(e.response.headers) ?: 5.minutes) | ||
| break | ||
| } else { | ||
| delete(key) | ||
| } | ||
| } catch (e: Exception) { | ||
| log("Failed to upload $type $key", e) | ||
| shouldRetry = true | ||
| val now = Clock.System.now() | ||
| savedRetryAfter = now + 5.minutes | ||
| break | ||
| } | ||
| } | ||
|
|
||
| return if (shouldRetry) Result.retry() else Result.success() | ||
| } | ||
| } | ||
|
|
||
| private val statsWorkerClasses = listOf( | ||
| HarStatsWorker::class.java, | ||
| SuccessStatsWorker::class.java, | ||
| ErrorStatsWorker::class.java, | ||
| ) | ||
|
|
||
| private fun getStatsUploadUniqueWorkName(cls: Class<out StatsWorker<*>>): String = "StatsUpload ${cls.simpleName}" | ||
|
|
||
| private val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.UNMETERED).build() | ||
|
|
||
| fun enqueueStatsUploadWork( | ||
| context: Context = applicationContext, | ||
| singleShot: Boolean = false, | ||
| ) { | ||
| val workManager = WorkManager.getInstance(context) | ||
| if (singleShot) { | ||
| log("enqueue expedited stats upload work") | ||
| val inputData = workDataOf("skipDelay" to true) | ||
| statsWorkerClasses.forEach { | ||
| val workRequest = OneTimeWorkRequest.Builder(it).apply { | ||
| setConstraints(constraints) | ||
| addTag(StatsWorker::class.java.name) | ||
| setExpedited(OutOfQuotaPolicy.DROP_WORK_REQUEST) | ||
| setInputData(inputData) | ||
| }.build() | ||
| workManager.enqueue(workRequest) | ||
| } | ||
| } else { | ||
| log("enqueuing periodic stats upload work") | ||
|
|
||
| statsWorkerClasses.forEach { | ||
| val workRequest = PeriodicWorkRequest.Builder(it, 1, TimeUnit.HOURS).apply { | ||
| setConstraints(constraints) | ||
| addTag(StatsWorker::class.java.name) | ||
| }.build() | ||
| workManager.enqueueUniquePeriodicWork( | ||
| getStatsUploadUniqueWorkName(it), | ||
| ExistingPeriodicWorkPolicy.KEEP, | ||
| workRequest, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun dequeueStatsUploadWork(context: Context = applicationContext) { | ||
| val workManager = WorkManager.getInstance(context) | ||
| statsWorkerClasses.forEach { | ||
| workManager.cancelUniqueWork(getStatsUploadUniqueWorkName(it)) | ||
| } | ||
| } | ||
|
|
||
| fun getEnqueuedStatsUploadWork(context: Context = applicationContext): Flow<List<WorkInfo>> { | ||
| return WorkManager.getInstance(context).getWorkInfosByTagFlow(StatsWorker::class.java.name) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't type be inferred from the generic Type of the class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, but T is a generic parameter and we can't access the ::class of a generic parameter, so we have to pass that in either way