From 95bb42804162be4513e3fb864b5d07862dc63700 Mon Sep 17 00:00:00 2001 From: jithu Date: Tue, 5 Mar 2024 17:20:18 +0530 Subject: [PATCH] data upload duplication fixed-molbio & firebase --- .../hpostesting/data/dao/HemoCubeDao.kt | 6 ++ .../presentation/dashboard/HomeFragment.kt | 6 +- .../hemocube/HemoCubeViewModel.kt | 84 +++++++++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/example/hpostesting/data/dao/HemoCubeDao.kt b/app/src/main/java/com/example/hpostesting/data/dao/HemoCubeDao.kt index b414b3e..62f68dd 100644 --- a/app/src/main/java/com/example/hpostesting/data/dao/HemoCubeDao.kt +++ b/app/src/main/java/com/example/hpostesting/data/dao/HemoCubeDao.kt @@ -12,6 +12,12 @@ interface HemoCubeDao { @Query("SELECT * from hemo_cube_test_table") fun getAll(): LiveData> + @Query("SELECT * from hemo_cube_test_table WHERE molbioFlag=false") + fun getMolbioPending(): List + + @Query("SELECT * from hemo_cube_test_table WHERE localFlag=false") + fun getFirebasePending():List + @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun insertAll(hemoCubeTestData: HemoCubeTestData) diff --git a/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt index 79db431..17cae02 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt @@ -151,7 +151,7 @@ class HomeFragment : Fragment() { } // Now re-subscribe to allUserData - hemoCubeViewModel.allLocalData.observe(viewLifecycleOwner) { originalUserDataList -> +/* hemoCubeViewModel.allLocalData.observe(viewLifecycleOwner) { originalUserDataList -> Log.d("LOCAL_DB OBSERVE", "OBSERVE CALLED") @@ -213,7 +213,9 @@ class HomeFragment : Fragment() { Log.d("resultcount1", "Uploading sanitized results") } - } + }*/ + hemoCubeViewModel.sendDataToMolbio() + hemoCubeViewModel.sendDataToFirebase() } else { binding.internetAvailableCL.visibility = View.GONE diff --git a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt index d075631..bc9a0c5 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt @@ -39,6 +39,7 @@ import com.example.hpostesting.domain.CheckUpdateWorker import com.example.hpostesting.domain.LogFileManager import com.google.gson.GsonBuilder import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MultipartBody @@ -153,6 +154,89 @@ class HemoCubeViewModel @Inject constructor( } } + + fun sendDataToMolbio() = viewModelScope.launch(Dispatchers.IO) { + + Log.d("molbio","getting in sendMolbio") + val resultList = MolbioV2ResultRequest(mutableListOf()) + val pendingData = hemoCubeDao.getMolbioPending() + Log.d("molbio","pending size"+pendingData.size) + pendingData.forEach{ + userData -> + val currentTimeFormatted = SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ssZZZZZ", + Locale.getDefault() + ).format(Calendar.getInstance().time) + val bufferIntensityThreshold = + Constants.BUFFER_INTENSITY_THRESHOLDS[userData.deviceId]?.toString() + ?: "defaultThreshold" // Handle possible nulls safely + resultList.results?.add( + MolbioV2Result( + rawData = userData, + analysisId = userData._id, + analysisDate = currentTimeFormatted, + analysisStatus = userData.classificationResult + ?: "defaultStatus", // Handle possible nulls + thresholds = bufferIntensityThreshold, + interpretation = userData.classificationResult + ?: "defaultInterpretation", // Handle possible nulls + testId = userData._id, + testTime = currentTimeFormatted, + collectionTime = currentTimeFormatted, + expiryTime = currentTimeFormatted + ) + ) + } + + resultList.results?.forEach { result -> + result.rawData?.let { sanitizeDoubleValues(it) } + } + + if (resultList.results?.isNotEmpty() == true) { + uploadMoblioBulkResults(resultList) + } + } + + + fun uploadMoblioBulkResults(molbioV2ResultRequest: MolbioV2ResultRequest) = viewModelScope.launch { + Log.d("molbio","upload called") + repository.uploadResults(molbioV2ResultRequest).let { + when (it) { + is Result.Success -> { + it.data.data?.forEach { id -> + id.rawData?.let { it1 -> + updateMolbioFlag( + it1._id + ) + } + } + } + is Result.Error -> { + Log.d("result","result upload error") + } + else -> { + Log.d("result","result upload else") + } + } + } + } + + + fun sendDataToFirebase() = viewModelScope.launch ( Dispatchers.IO ) { + val pendingData = hemoCubeDao.getFirebasePending() + pendingData.forEach{ + userData -> + if (userData != null) { + if (!userData.localFlag) { + bulkAddResultTestToDb(userData) + } + } + } + + } + + + fun checkUpdate(checkUpdateRequest: CheckUpdateRequest) = viewModelScope.launch { checkUpdate.postValue(Result.Loading()) repository.checkUpdate(checkUpdateRequest).let {