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

cleanup duplicate unique ids #2107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion opensrp-chw/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ dependencies {

implementation('com.google.android.gms:play-services-vision:17.0.2')

implementation('org.smartregister:opensrp-client-chw-core:2.1.4-SNAPSHOT@aar') {
implementation('org.smartregister:opensrp-client-chw-core:2.1.5-SNAPSHOT@aar') {
transitive = true
exclude group: 'com.android.support', module: 'appcompat-v7'
exclude group: 'androidx.legacy', module: 'legacy-support-v4'
Expand All @@ -387,6 +387,7 @@ dependencies {
exclude group: 'com.google.guava', module: 'guava'
exclude group: 'com.rengwuxian.materialedittext', module: 'library'
exclude group: 'stax', module: 'stax-api'
exclude group: 'com.google.guava', module:'listenablefuture'
}

implementation 'com.mapbox.mapboxsdk:mapbox-sdk-turf:5.1.0'
Expand Down
4 changes: 3 additions & 1 deletion opensrp-chw/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@
<service android:name=".sync.ChwSyncIntentService" />
<service android:name=".core.service.StockUsageReportService" />
<service android:name="org.smartregister.growthmonitoring.service.intent.WeightForHeightIntentService" />

<service
android:name=".service.ZeirIdCleanupService"
android:exported="false" />
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.smartregister.chw.interactor;

import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import androidx.work.WorkRequest;

import org.smartregister.chw.BuildConfig;
import org.smartregister.chw.application.ChwApplication;
import org.smartregister.chw.contract.LoginJobScheduler;
Expand All @@ -11,6 +15,7 @@
import org.smartregister.chw.job.ScheduleJob;
import org.smartregister.immunization.job.VaccineServiceJob;
import org.smartregister.job.DocumentConfigurationServiceJob;
import org.smartregister.job.DuplicateCleanerWorker;
import org.smartregister.job.ImageUploadServiceJob;
import org.smartregister.job.PlanIntentServiceJob;
import org.smartregister.job.PullUniqueIdsServiceJob;
Expand Down Expand Up @@ -53,6 +58,10 @@ public void scheduleJobsPeriodically() {

if (BuildConfig.USE_UNIFIED_REFERRAL_APPROACH)
DocumentConfigurationServiceJob.scheduleJob(DocumentConfigurationServiceJob.TAG,TimeUnit.MINUTES.toMinutes(BuildConfig.DATA_SYNC_DURATION_MINUTES), getFlexValue(BuildConfig.DATA_SYNC_DURATION_MINUTES));

// WorkRequest cleanZeirIdsWorkRequest = new PeriodicWorkRequest.Builder(DuplicateCleanerWorker.class, 15, TimeUnit.MINUTES)
// .build();
// WorkManager.getInstance(this.getApplicationContext()).enqueue(cleanZeirIdsWorkRequest);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.smartregister.chw.repository;

import android.database.Cursor;

import org.apache.commons.lang3.StringUtils;
import org.smartregister.repository.BaseRepository;

import java.util.HashMap;
import java.util.Map;

import timber.log.Timber;

public class ZeirIdCleanupRepository extends BaseRepository {

private static final String BASE_ENTITY_ID = "baseEntityId";

private static final String DUPLICATES_SQL =
"WITH duplicates AS ( " +
" WITH clients AS ( " +
" SELECT baseEntityId, COALESCE(json_extract(json, '$.identifiers.ZEIR_ID'), json_extract(json, '$.identifiers.M_ZEIR_ID')) zeir_id " +
" FROM client " +
" ) " +
" SELECT b.* FROM (SELECT baseEntityId, zeir_id FROM clients GROUP BY zeir_id HAVING count(zeir_id) > 1) a " +
" INNER JOIN clients b ON a.zeir_id=b.zeir_id " +
" UNION " +
" SELECT * FROM clients WHERE zeir_id IS NULL " +
") " +
"SELECT baseEntityId, zeir_id, lag(zeir_id) over(order by zeir_id) AS prev_zeir_id FROM duplicates";

public Map<String, String> getClientsWithDuplicateZeirIds() {
Map<String, String> duplicates = new HashMap<>();

Cursor cursor = null;
try {
cursor = getWritableDatabase().rawQuery(DUPLICATES_SQL, new String[]{});

while (cursor.moveToNext()) {
String baseEntityId = cursor.getString(cursor.getColumnIndex(BASE_ENTITY_ID));
String zeirId = cursor.getString(cursor.getColumnIndex("zeir_id"));

duplicates.put(baseEntityId, zeirId);

String prevZeirId = null;
try {
prevZeirId = cursor.getString(cursor.getColumnIndex("prev_zeir_id"));
} catch (NullPointerException e) {
Timber.e(e, "null prev_zeir_id");
}

if (StringUtils.isNotEmpty(prevZeirId) && (prevZeirId.equals(zeirId))) {
duplicates.put(baseEntityId, prevZeirId);
}
}
} catch (Exception e) {
Timber.e(e);
} finally {
if (cursor != null && !cursor.isClosed())
cursor.close();
}

return duplicates;
}
}