Skip to content
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

Remove UploadWorkManager #2166

Merged
merged 9 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ val defaultRetryConfiguration =
object SyncDataParams {
const val SORT_KEY = "_sort"
const val LAST_UPDATED_KEY = "_lastUpdated"
const val ADDRESS_COUNTRY_KEY = "address-country"
jingtang10 marked this conversation as resolved.
Show resolved Hide resolved
const val SUMMARY_KEY = "_summary"
const val SUMMARY_COUNT_VALUE = "count"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import com.google.android.fhir.FhirEngine
import com.google.android.fhir.FhirEngineProvider
import com.google.android.fhir.OffsetDateTimeTypeAdapter
import com.google.android.fhir.sync.download.DownloaderImpl
import com.google.android.fhir.sync.upload.SquashedChangesUploadWorkManager
import com.google.android.fhir.sync.upload.UploadWorkManager
import com.google.android.fhir.sync.upload.UploaderImpl
import com.google.android.fhir.sync.upload.Uploader
import com.google.gson.ExclusionStrategy
import com.google.gson.FieldAttributes
import com.google.gson.GsonBuilder
Expand All @@ -44,9 +42,7 @@ abstract class FhirSyncWorker(appContext: Context, workerParams: WorkerParameter
CoroutineWorker(appContext, workerParams) {
abstract fun getFhirEngine(): FhirEngine
abstract fun getDownloadWorkManager(): DownloadWorkManager
private fun getUploadWorkManager(): UploadWorkManager {
return SquashedChangesUploadWorkManager()
}

abstract fun getConflictResolver(): ConflictResolver

private val gson =
Expand Down Expand Up @@ -88,7 +84,7 @@ abstract class FhirSyncWorker(appContext: Context, workerParams: WorkerParameter
FhirSynchronizer(
applicationContext,
getFhirEngine(),
UploaderImpl(dataSource, getUploadWorkManager()),
Uploader(dataSource),
DownloaderImpl(dataSource, getDownloadWorkManager()),
getConflictResolver()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package com.google.android.fhir.sync
import android.content.Context
import com.google.android.fhir.DatastoreUtil
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.sync.download.DownloadState
import com.google.android.fhir.sync.download.Downloader
import com.google.android.fhir.sync.upload.UploadState
import com.google.android.fhir.sync.upload.Uploader
import java.time.OffsetDateTime
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Google LLC
* Copyright 2022-2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,8 +14,9 @@
* limitations under the License.
*/

package com.google.android.fhir.sync
package com.google.android.fhir.sync.download

import com.google.android.fhir.sync.ResourceSyncException
import kotlinx.coroutines.flow.Flow
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ package com.google.android.fhir.sync.download
import com.google.android.fhir.sync.BundleDownloadRequest
import com.google.android.fhir.sync.DataSource
import com.google.android.fhir.sync.DownloadRequest
import com.google.android.fhir.sync.DownloadState
import com.google.android.fhir.sync.DownloadWorkManager
import com.google.android.fhir.sync.Downloader
import com.google.android.fhir.sync.ResourceSyncException
import com.google.android.fhir.sync.UrlDownloadRequest
import kotlinx.coroutines.flow.Flow
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,83 @@ package com.google.android.fhir.sync.upload

import com.google.android.fhir.LocalChange
import com.google.android.fhir.LocalChangeToken
import com.google.android.fhir.sync.DataSource
import com.google.android.fhir.sync.ResourceSyncException
import com.google.android.fhir.sync.upload.patch.PerResourcePatchGenerator
import com.google.android.fhir.sync.upload.request.TransactionBundleGenerator
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import org.hl7.fhir.exceptions.FHIRException
import org.hl7.fhir.r4.model.Bundle
import org.hl7.fhir.r4.model.OperationOutcome
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
import timber.log.Timber

/** Module for uploading local changes to a [DataSource]. */
internal interface Uploader {
/**
* Implementation of the [Uploader]. It orchestrates the pre processing of [LocalChange] and
jingtang10 marked this conversation as resolved.
Show resolved Hide resolved
* constructing appropriate upload requests via [UploadWorkManager] and uploading of requests via
* [DataSource]. [Uploader] clients should call upload and listen to the various states emitted by
* [UploadWorkManager] as [UploadState].
jingtang10 marked this conversation as resolved.
Show resolved Hide resolved
*/
internal class Uploader(
private val dataSource: DataSource,
) {
private val patchGenerator = PerResourcePatchGenerator
private val requestGenerator = TransactionBundleGenerator.getDefault()
jingtang10 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Uploads the local changes to the [DataSource]. Particular implementations should take care of
* transforming the [LocalChange]s to particular network operations. If [ProgressCallback] is
* provided it also reports the intermediate progress
jingtang10 marked this conversation as resolved.
Show resolved Hide resolved
*/
suspend fun upload(localChanges: List<LocalChange>): Flow<UploadState>
suspend fun upload(localChanges: List<LocalChange>): Flow<UploadState> = flow {
val patches = patchGenerator.generate(localChanges)
val requests = requestGenerator.generateUploadRequests(patches)
val token = LocalChangeToken(localChanges.flatMap { it.token.ids })
jingtang10 marked this conversation as resolved.
Show resolved Hide resolved
val total = requests.size
emit(UploadState.Started(total))
requests.forEachIndexed { index, uploadRequest ->
try {
val response = dataSource.upload(uploadRequest)
emit(
getUploadResult(uploadRequest.resource.resourceType, response, token, total, index + 1)
)
} catch (e: Exception) {
Timber.e(e)
emit(UploadState.Failure(ResourceSyncException(ResourceType.Bundle, e)))
}
}
}

private fun getUploadResult(
requestResourceType: ResourceType,
response: Resource,
localChangeToken: LocalChangeToken,
total: Int,
completed: Int
) =
when {
response is Bundle && response.type == Bundle.BundleType.TRANSACTIONRESPONSE -> {
UploadState.Success(localChangeToken, response, total, completed)
}
response is OperationOutcome && response.issue.isNotEmpty() -> {
UploadState.Failure(
ResourceSyncException(
requestResourceType,
FHIRException(response.issueFirstRep.diagnostics)
)
)
}
else -> {
UploadState.Failure(
ResourceSyncException(
requestResourceType,
FHIRException("Unknown response for ${response.resourceType}")
)
)
}
}
}

internal sealed class UploadState {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.google.android.fhir.logicalId
import com.google.android.fhir.sync.BundleDownloadRequest
import com.google.android.fhir.sync.DataSource
import com.google.android.fhir.sync.DownloadRequest
import com.google.android.fhir.sync.DownloadState
import com.google.android.fhir.sync.DownloadWorkManager
import com.google.android.fhir.sync.UploadRequest
import com.google.android.fhir.sync.UrlDownloadRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ class UploaderImplTest {
@Test
fun `upload Bundle transaction should emit Success`() = runBlocking {
val result =
UploaderImpl(
Uploader(
BundleDataSource { Bundle().apply { type = Bundle.BundleType.TRANSACTIONRESPONSE } },
SquashedChangesUploadWorkManager()
)
.upload(localChanges)
.toList()
Expand All @@ -61,7 +60,9 @@ class UploaderImplTest {
@Test
fun `upload Bundle transaction should emit Started state`() = runBlocking {
val result =
UploaderImpl(BundleDataSource { Bundle() }, SquashedChangesUploadWorkManager())
Uploader(
BundleDataSource { Bundle() },
)
.upload(localChanges)
.toList()

Expand All @@ -71,7 +72,7 @@ class UploaderImplTest {
@Test
fun `upload Bundle Transaction server error should emit Failure`() = runBlocking {
val result =
UploaderImpl(
Uploader(
BundleDataSource {
OperationOutcome().apply {
addIssue(
Expand All @@ -83,7 +84,6 @@ class UploaderImplTest {
)
}
},
SquashedChangesUploadWorkManager()
)
.upload(localChanges)
.toList()
Expand All @@ -95,9 +95,8 @@ class UploaderImplTest {
@Test
fun `upload Bundle transaction error during upload should emit Failure`() = runBlocking {
val result =
UploaderImpl(
Uploader(
BundleDataSource { throw ConnectException("Failed to connect to server.") },
SquashedChangesUploadWorkManager()
)
.upload(localChanges)
.toList()
Expand Down
Loading
Loading