Skip to content

Commit

Permalink
Worker alternatives to sync intent services
Browse files Browse the repository at this point in the history
  • Loading branch information
LZRS committed Nov 1, 2022
1 parent 3a6bf41 commit f1943aa
Show file tree
Hide file tree
Showing 44 changed files with 1,753 additions and 128 deletions.
11 changes: 8 additions & 3 deletions opensrp-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,16 @@ dependencies {
compileOnly 'com.google.firebase:firebase-crashlytics'
compileOnly 'com.google.firebase:firebase-perf'

def work_version = "2.7.1"
implementation "androidx.work:work-runtime:$work_version"

// Add the dependency for the Performance Monitoring library

// WorkManager
def work_version = "2.7.1"
api "androidx.work:work-runtime:$work_version"
implementation "androidx.work:work-gcm:$work_version"
implementation "androidx.work:work-multiprocess:$work_version"
implementation "com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava"
testImplementation "androidx.work:work-testing:$work_version"

//Mockito
def mockitoVersion = '4.6.1'
testImplementation("org.mockito:mockito-core:$mockitoVersion")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.smartregister.domain.db;

import androidx.annotation.NonNull;

import java.util.List;

public class EventClientQueryResult {

private List<EventClient> eventClientList;
private int maxRowId;

public EventClientQueryResult(int maxRowId, @NonNull List<EventClient> eventClients) {
this.maxRowId = maxRowId;
this.eventClientList = eventClients;
}

public List<EventClient> getEventClientList() {
return eventClientList;
}

public void setEventClientList(List<EventClient> eventClientList) {
this.eventClientList = eventClientList;
}

public int getMaxRowId() {
return maxRowId;
}

public void setMaxRowId(int maxRowId) {
this.maxRowId = maxRowId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import org.smartregister.domain.db.Column;
import org.smartregister.domain.db.ColumnAttribute;
import org.smartregister.domain.db.EventClient;
import org.smartregister.domain.db.EventClientQueryResult;
import org.smartregister.p2p.sync.data.JsonData;
import org.smartregister.sync.intent.P2pProcessRecordsService;
import org.smartregister.sync.intent.PullUniqueIdsIntentService;
import org.smartregister.util.DatabaseMigrationUtils;
import org.smartregister.util.JsonFormUtils;
Expand Down Expand Up @@ -882,7 +882,7 @@ public List<EventClient> fetchEventClients(long startServerVersion, long lastSer
new String[]{String.valueOf(startServerVersion), String.valueOf(lastServerVersion)});
}

public P2pProcessRecordsService.EventClientQueryResult fetchEventClientsByRowId(
public EventClientQueryResult fetchEventClientsByRowId(
long lastProcessedRowId) {
List<EventClient> list = new ArrayList<>();
Cursor cursor = null;
Expand Down Expand Up @@ -925,7 +925,7 @@ public P2pProcessRecordsService.EventClientQueryResult fetchEventClientsByRowId(
cursor.close();
}
}
return new P2pProcessRecordsService.EventClientQueryResult(maxRowId, list);
return new EventClientQueryResult(maxRowId, list);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.smartregister.sync;

import org.json.JSONException;
import org.json.JSONObject;
import org.smartregister.AllConstants;
import org.smartregister.CoreLibrary;
import org.smartregister.sync.intent.BaseSyncIntentService;

import java.util.LinkedHashMap;
import java.util.Map;

import timber.log.Timber;

/**
* A helper class for building the url request for intent services
*/
public class RequestParamsBuilder {

private final Map<String, Object> paramMap;
private final StringBuilder getSyncParamsBuilder;
private final JSONObject postSyncParamsBuilder;

public RequestParamsBuilder() {
this.paramMap = new LinkedHashMap<>();
this.getSyncParamsBuilder = new StringBuilder();
this.postSyncParamsBuilder = new JSONObject();
}

public RequestParamsBuilder addServerVersion(long value) {
paramMap.put(AllConstants.SERVER_VERSION, value);
return this;
}

public RequestParamsBuilder addEventPullLimit(int value) {
paramMap.put(AllConstants.LIMIT, value);
return this;
}

public RequestParamsBuilder configureSyncFilter(String syncFilterParam, String syncFilterValue) {
paramMap.put(syncFilterParam, syncFilterValue);
return this;
}

public RequestParamsBuilder returnCount(boolean value) {
paramMap.put(AllConstants.RETURN_COUNT, value);
return this;
}

public RequestParamsBuilder addParam(String key, Object value) {
paramMap.put(key, value);
return this;
}

public RequestParamsBuilder removeParam(String key) {
paramMap.remove(key);
return this;
}

public String build() {

for (Map.Entry<String, Object> entry : paramMap.entrySet()) {

if (CoreLibrary.getInstance().getSyncConfiguration().isSyncUsingPost()) {

try {
postSyncParamsBuilder.put(entry.getKey(), entry.getValue());
} catch (JSONException e) {
Timber.e(e);
}

} else {

if (0 != getSyncParamsBuilder.length()) {
getSyncParamsBuilder.append('&');
}

getSyncParamsBuilder.append(entry.getKey()).append('=').append(entry.getValue());
}

}

return CoreLibrary.getInstance().getSyncConfiguration().isSyncUsingPost() ? postSyncParamsBuilder.toString() : getSyncParamsBuilder.toString();
}

@Override
public String toString() {
return build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.smartregister.domain.SyncStatus;
import org.smartregister.repository.AllSharedPreferences;
import org.smartregister.service.HTTPAgent;
import org.smartregister.sync.intent.BaseSyncIntentService;
import org.smartregister.sync.RequestParamsBuilder;
import org.smartregister.sync.intent.SettingsSyncIntentService;
import org.smartregister.util.JsonFormUtils;
import org.smartregister.util.Utils;
Expand Down Expand Up @@ -95,7 +95,7 @@ private void getExtraSettings(JSONArray settings, String accessToken) throws JSO
JSONArray completeExtraSettings = new JSONArray();
if (getInstance().getSyncConfiguration() != null && getInstance().getSyncConfiguration().hasExtraSettingsSync()) {
String syncParams = getInstance().getSyncConfiguration().getExtraStringSettingsParameters();
BaseSyncIntentService.RequestParamsBuilder builder = new BaseSyncIntentService.RequestParamsBuilder().addParam(AllConstants.SERVER_VERSION, "0").addParam(AllConstants.RESOLVE, getInstance().getSyncConfiguration().resolveSettings());
RequestParamsBuilder builder = new RequestParamsBuilder().addParam(AllConstants.SERVER_VERSION, "0").addParam(AllConstants.RESOLVE, getInstance().getSyncConfiguration().resolveSettings());
String url = SettingsSyncIntentService.SETTINGS_URL + "?" + syncParams + "&" + builder.toString();
JSONArray extraSettings = pullSettings(url, accessToken);
if (extraSettings != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/**
* Created by Vincent Karuri on 26/08/2019
*/

@Deprecated
public class BaseSyncIntentService extends IntentService {

public BaseSyncIntentService(String name) {
Expand All @@ -31,81 +33,4 @@ protected void onHandleIntent(Intent intent) {
httpAgent.setReadTimeout(coreLibrary.getSyncConfiguration().getReadTimeout());
}

/**
* A helper class for building the url request for intent services
*/
public static class RequestParamsBuilder {

private final Map<String, Object> paramMap;
private final StringBuilder getSyncParamsBuilder;
private final JSONObject postSyncParamsBuilder;

public RequestParamsBuilder() {
this.paramMap = new LinkedHashMap<>();
this.getSyncParamsBuilder = new StringBuilder();
this.postSyncParamsBuilder = new JSONObject();
}

public RequestParamsBuilder addServerVersion(long value) {
paramMap.put(AllConstants.SERVER_VERSION, value);
return this;
}

public RequestParamsBuilder addEventPullLimit(int value) {
paramMap.put(AllConstants.LIMIT, value);
return this;
}

public RequestParamsBuilder configureSyncFilter(String syncFilterParam, String syncFilterValue) {
paramMap.put(syncFilterParam, syncFilterValue);
return this;
}

public RequestParamsBuilder returnCount(boolean value) {
paramMap.put(AllConstants.RETURN_COUNT, value);
return this;
}

public RequestParamsBuilder addParam(String key, Object value) {
paramMap.put(key, value);
return this;
}

public RequestParamsBuilder removeParam(String key) {
paramMap.remove(key);
return this;
}

public String build() {

for (Map.Entry<String, Object> entry : paramMap.entrySet()) {

if (CoreLibrary.getInstance().getSyncConfiguration().isSyncUsingPost()) {

try {
postSyncParamsBuilder.put(entry.getKey(), entry.getValue());
} catch (JSONException e) {
Timber.e(e);
}

} else {

if (0 != getSyncParamsBuilder.length()) {
getSyncParamsBuilder.append('&');
}

getSyncParamsBuilder.append(entry.getKey()).append('=').append(entry.getValue());
}

}

return CoreLibrary.getInstance().getSyncConfiguration().isSyncUsingPost() ? postSyncParamsBuilder.toString() : getSyncParamsBuilder.toString();
}

@Override
public String toString() {
return build();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import static org.smartregister.AllConstants.CAMPAIGNS;

@Deprecated
public class CampaignIntentService extends BaseSyncIntentService {
public static final String CAMPAIGN_URL = "/rest/campaign/";
private static final String TAG = "CampaignIntentService";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*
* @author cozej4 https://github.com/cozej4
*/
@Deprecated
public class DocumentConfigurationIntentService extends BaseSyncIntentService {
private HTTPAgent httpAgent;
private ManifestRepository manifestRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import timber.log.Timber;


@Deprecated
public class ExtendedSyncIntentService extends BaseSyncIntentService {

private ActionService actionService = CoreLibrary.getInstance().context().actionService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.smartregister.util.DateTimeTypeConverter;
import org.smartregister.util.PropertiesConverter;

@Deprecated
public class LocationIntentService extends BaseSyncIntentService {
private static final String TAG = "LocationIntentService";
public static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.smartregister.sync.intent;

import android.content.Intent;
import androidx.annotation.NonNull;

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import org.smartregister.CoreLibrary;
import org.smartregister.domain.FetchStatus;
import org.smartregister.domain.db.EventClient;
import org.smartregister.domain.db.EventClientQueryResult;
import org.smartregister.repository.AllSharedPreferences;
import org.smartregister.repository.EventClientRepository;
import org.smartregister.util.Utils;
Expand All @@ -21,6 +22,7 @@
* Created by Ephraim Kigamba - [email protected] on 10/05/2019
*/

@Deprecated
public class P2pProcessRecordsService extends BaseSyncIntentService {

/**
Expand Down Expand Up @@ -57,12 +59,12 @@ protected void onHandleIntent(@Nullable Intent intent) {
DrishtiApplication.getInstance().getClientProcessor().processClient(eventClientList);
int tableMaxRowId = eventClientRepository.getMaxRowId(EventClientRepository.Table.event);

if (tableMaxRowId == eventClientQueryResult.maxRowId) {
if (tableMaxRowId == eventClientQueryResult.getMaxRowId()) {
eventsMaxRowId = -1;
allSharedPreferences.resetLastPeerToPeerSyncProcessedEvent();
} else {
eventsMaxRowId = eventClientQueryResult.maxRowId;
allSharedPreferences.setLastPeerToPeerSyncProcessedEvent(eventClientQueryResult.maxRowId);
eventsMaxRowId = eventClientQueryResult.getMaxRowId();
allSharedPreferences.setLastPeerToPeerSyncProcessedEvent(eventClientQueryResult.getMaxRowId());
}

// Profile images do not have a foreign key to the clients and can therefore be saved during the sync.
Expand All @@ -87,33 +89,6 @@ protected void sendSyncStatusBroadcastMessage(FetchStatus fetchStatus) {
CoreLibrary.getInstance().context().applicationContext().sendBroadcast(Utils.completeSync(fetchStatus));
}

public static class EventClientQueryResult {

private List<EventClient> eventClientList;
private int maxRowId;

public EventClientQueryResult(int maxRowId, @NonNull List<EventClient> eventClients) {
this.maxRowId = maxRowId;
this.eventClientList = eventClients;
}

public List<EventClient> getEventClientList() {
return eventClientList;
}

public void setEventClientList(List<EventClient> eventClientList) {
this.eventClientList = eventClientList;
}

public int getMaxRowId() {
return maxRowId;
}

public void setMaxRowId(int maxRowId) {
this.maxRowId = maxRowId;
}
}

@Override
public void onDestroy() {
// This ensure that even if the `onHandleIntent` is closed prematurely, we remove the Snackbar since
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/**
* Created by Vincent Karuri on 08/05/2019
*/
@Deprecated
public class PlanIntentService extends BaseSyncIntentService {

private static final String TAG = "PlanIntentService";
Expand Down
Loading

0 comments on commit f1943aa

Please sign in to comment.