Skip to content

Commit

Permalink
Merge branch 'master' into encrypt-shared-preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
qiarie authored Feb 3, 2021
2 parents 3f8a56f + 806f26a commit 2af44c8
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=4.2.1-SNAPSHOT
VERSION_NAME=4.2.2-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
5 changes: 4 additions & 1 deletion opensrp-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ tasks.withType(Test) {
showCauses true
showStackTraces true
}
maxHeapSize = "2g"
maxHeapSize = "4g"
}

def filesToCopy = copySpec {
Expand Down Expand Up @@ -256,6 +256,9 @@ dependencies {
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'xerces:xercesImpl:2.12.0'

// Add the dependency for the Performance Monitoring library
implementation 'com.google.firebase:firebase-perf:19.0.7'

implementation fileTree(include: ['*.jar'], dir: 'libs')

androidTestImplementation 'junit:junit:4.13'
Expand Down
14 changes: 14 additions & 0 deletions opensrp-app/src/main/java/org/smartregister/AllConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,18 @@ public interface GpsConstants {
String LATITUDE = "latitude";
String LONGITUDE = "longitude";
}

public interface PerformanceMonitoring {
String TEAM = "team";
String PUSH = "push";
String FETCH = "fetch";
String ACTION = "action";
String STRUCTURE = "structure";
String LOCATION = "location";
String TASK_SYNC = "task_sync";
String PLAN_SYNC = "plan_sync";
String EVENT_SYNC = "event_sync";
String LOCATION_SYNC = "location_sync";
String CLIENT_PROCESSING = "client_processing";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,8 @@ public int getMaxAuthenticationRetries() {
}

public abstract Class<? extends BaseLoginActivity> getAuthenticationActivity();

public boolean firebasePerformanceMonitoringEnabled() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public FormDataRepository() {
.put(MotherRepository.MOTHER_TABLE_NAME, MotherRepository.MOTHER_TABLE_COLUMNS);
TABLE_COLUMN_MAP.put(ChildRepository.CHILD_TABLE_NAME, ChildRepository.CHILD_TABLE_COLUMNS);

if (CoreLibrary.getInstance().context().configuration().appName().equals(APP_NAME_INDONESIA)) {
if (APP_NAME_INDONESIA.equals(CoreLibrary.getInstance().context().configuration().appName())) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import androidx.annotation.Nullable;

import com.google.firebase.perf.metrics.Trace;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
Expand Down Expand Up @@ -44,6 +45,7 @@

import timber.log.Timber;

import static org.smartregister.AllConstants.COUNT;
import static org.smartregister.AllConstants.JURISDICTION_IDS;
import static org.smartregister.AllConstants.LocationConstants.DISPLAY;
import static org.smartregister.AllConstants.LocationConstants.LOCATION;
Expand All @@ -52,7 +54,18 @@
import static org.smartregister.AllConstants.LocationConstants.TEAM;
import static org.smartregister.AllConstants.LocationConstants.UUID;
import static org.smartregister.AllConstants.OPERATIONAL_AREAS;
import static org.smartregister.AllConstants.PerformanceMonitoring.ACTION;
import static org.smartregister.AllConstants.PerformanceMonitoring.FETCH;
import static org.smartregister.AllConstants.PerformanceMonitoring.LOCATION_SYNC;
import static org.smartregister.AllConstants.PerformanceMonitoring.PUSH;
import static org.smartregister.AllConstants.PerformanceMonitoring.STRUCTURE;
import static org.smartregister.AllConstants.RETURN_COUNT;
import static org.smartregister.AllConstants.TYPE;
import static org.smartregister.util.PerformanceMonitoringUtils.addAttribute;
import static org.smartregister.util.PerformanceMonitoringUtils.clearTraceAttributes;
import static org.smartregister.util.PerformanceMonitoringUtils.initTrace;
import static org.smartregister.util.PerformanceMonitoringUtils.startTrace;
import static org.smartregister.util.PerformanceMonitoringUtils.stopTrace;

public class LocationServiceHelper extends BaseHelper {

Expand All @@ -78,6 +91,8 @@ public class LocationServiceHelper extends BaseHelper {
private LocationRepository locationRepository;
private LocationTagRepository locationTagRepository;
private StructureRepository structureRepository;
private Trace locationSyncTrace;
private String team;
private long totalRecords;
private SyncProgress syncProgress;

Expand All @@ -86,6 +101,9 @@ public LocationServiceHelper(LocationRepository locationRepository, LocationTagR
this.locationRepository = locationRepository;
this.locationTagRepository = locationTagRepository;
this.structureRepository = structureRepository;
this.locationSyncTrace = initTrace(LOCATION_SYNC);
String providerId = allSharedPreferences.fetchRegisteredANM();
team = allSharedPreferences.fetchDefaultTeam(providerId);
}

public static LocationServiceHelper getInstance() {
Expand Down Expand Up @@ -121,10 +139,20 @@ private List<Location> batchSyncLocationsStructures(boolean isJurisdiction, List
if (serverVersion > 0) serverVersion += 1;
try {
List<String> parentIds = locationRepository.getAllLocationIds();

if (isJurisdiction) {
startLocationTrace(FETCH, AllConstants.PerformanceMonitoring.LOCATION, 0);
} else {
startLocationTrace(FETCH, STRUCTURE, 0);
}
startTrace(locationSyncTrace);
String featureResponse = fetchLocationsOrStructures(isJurisdiction, serverVersion, TextUtils.join(",", parentIds), returnCount);
List<Location> locations = locationGson.fromJson(featureResponse, new TypeToken<List<Location>>() {
}.getType());

addAttribute(locationSyncTrace, COUNT, String.valueOf(locations.size()));
stopTrace(locationSyncTrace);

for (Location location : locations) {
try {
location.setSyncStatus(BaseRepository.TYPE_Synced);
Expand Down Expand Up @@ -278,6 +306,7 @@ public void syncCreatedStructureToServer() {
List<Location> locations = structureRepository.getAllUnsynchedCreatedStructures();
if (!locations.isEmpty()) {
String jsonPayload = locationGson.toJson(locations);
startLocationTrace(PUSH, STRUCTURE, locations.size());
String baseUrl = getFormattedBaseUrl();
Response<String> response = getHttpAgent().postWithJsonResponse(
MessageFormat.format("{0}/{1}",
Expand All @@ -288,7 +317,7 @@ public void syncCreatedStructureToServer() {
Timber.e("Failed to create new locations on server: %s", response.payload());
return;
}

stopTrace(locationSyncTrace);
Set<String> unprocessedIds = new HashSet<>();
if (StringUtils.isNotBlank(response.payload())) {
if (response.payload().startsWith(LOCATIONS_NOT_PROCESSED)) {
Expand All @@ -302,6 +331,15 @@ public void syncCreatedStructureToServer() {
}
}

private void startLocationTrace(String action, String type, int count) {
clearTraceAttributes(locationSyncTrace);
addAttribute(locationSyncTrace, AllConstants.PerformanceMonitoring.TEAM, team);
addAttribute(locationSyncTrace, ACTION, action);
addAttribute(locationSyncTrace, TYPE, type);
addAttribute(locationSyncTrace, COUNT, String.valueOf(count));
startTrace(locationSyncTrace);
}

public void syncUpdatedLocationsToServer() {
HTTPAgent httpAgent = getHttpAgent();
List<Location> locations = locationRepository.getAllUnsynchedLocation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import android.content.Context;

import androidx.annotation.VisibleForTesting;

import com.google.firebase.perf.metrics.Trace;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
Expand Down Expand Up @@ -33,6 +32,16 @@

import timber.log.Timber;

import static org.smartregister.AllConstants.COUNT;
import static org.smartregister.AllConstants.PerformanceMonitoring.ACTION;
import static org.smartregister.AllConstants.PerformanceMonitoring.FETCH;
import static org.smartregister.AllConstants.PerformanceMonitoring.PLAN_SYNC;
import static org.smartregister.AllConstants.PerformanceMonitoring.TEAM;
import static org.smartregister.util.PerformanceMonitoringUtils.addAttribute;
import static org.smartregister.util.PerformanceMonitoringUtils.initTrace;
import static org.smartregister.util.PerformanceMonitoringUtils.startTrace;
import static org.smartregister.util.PerformanceMonitoringUtils.stopTrace;

/**
* Created by Vincent Karuri on 08/05/2019
*/
Expand All @@ -53,6 +62,8 @@ public class PlanIntentServiceHelper extends BaseHelper {
private long totalRecords;
private SyncProgress syncProgress;

private Trace planSyncTrace;

public static PlanIntentServiceHelper getInstance() {
if (instance == null) {
instance = new PlanIntentServiceHelper(CoreLibrary.getInstance().context().getPlanDefinitionRepository());
Expand All @@ -63,6 +74,7 @@ public static PlanIntentServiceHelper getInstance() {
private PlanIntentServiceHelper(PlanDefinitionRepository planRepository) {
this.context = CoreLibrary.getInstance().context().applicationContext();
this.planDefinitionRepository = planRepository;
this.planSyncTrace = initTrace(PLAN_SYNC);
this.allSharedPreferences = CoreLibrary.getInstance().context().allSharedPreferences();
}

Expand Down Expand Up @@ -93,9 +105,15 @@ private int batchFetchPlansFromServer(boolean returnCount) {
Long maxServerVersion = 0l;

String organizationIds = allSharedPreferences.getPreference(AllConstants.ORGANIZATION_IDS);

startPlanTrace(FETCH);

String plansResponse = fetchPlans(Arrays.asList(organizationIds.split(",")), serverVersion, returnCount);
List<PlanDefinition> plans = gson.fromJson(plansResponse, new TypeToken<List<PlanDefinition>>() {
}.getType());

addAttribute(planSyncTrace, COUNT, String.valueOf(plans.size()));
stopTrace(planSyncTrace);
for (PlanDefinition plan : plans) {
try {
planDefinitionRepository.addOrUpdate(plan);
Expand All @@ -121,6 +139,14 @@ private int batchFetchPlansFromServer(boolean returnCount) {
return batchFetchCount;
}

private void startPlanTrace(String action) {
String providerId = allSharedPreferences.fetchRegisteredANM();
String team = allSharedPreferences.fetchDefaultTeam(providerId);
addAttribute(planSyncTrace, TEAM, team);
addAttribute(planSyncTrace, ACTION, action);
startTrace(planSyncTrace);
}

private String fetchPlans(List<String> organizationIds, long serverVersion, boolean returnCount) throws Exception {
HTTPAgent httpAgent = getHttpAgent();
String baseUrl = CoreLibrary.getInstance().context().configuration().dristhiBaseURL();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

import com.google.firebase.perf.metrics.Trace;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
Expand Down Expand Up @@ -38,6 +39,18 @@

import timber.log.Timber;

import static org.smartregister.AllConstants.COUNT;
import static org.smartregister.AllConstants.PerformanceMonitoring.ACTION;
import static org.smartregister.AllConstants.PerformanceMonitoring.FETCH;
import static org.smartregister.AllConstants.PerformanceMonitoring.PUSH;
import static org.smartregister.AllConstants.PerformanceMonitoring.TASK_SYNC;
import static org.smartregister.AllConstants.PerformanceMonitoring.TEAM;
import static org.smartregister.util.PerformanceMonitoringUtils.addAttribute;
import static org.smartregister.util.PerformanceMonitoringUtils.clearTraceAttributes;
import static org.smartregister.util.PerformanceMonitoringUtils.initTrace;
import static org.smartregister.util.PerformanceMonitoringUtils.startTrace;
import static org.smartregister.util.PerformanceMonitoringUtils.stopTrace;

public class TaskServiceHelper extends BaseHelper {

private final AllSharedPreferences allSharedPreferences;
Expand All @@ -57,6 +70,10 @@ public class TaskServiceHelper extends BaseHelper {

private boolean syncByGroupIdentifier = true;

private Trace taskSyncTrace;

private String team;

private long totalRecords;

private SyncProgress syncProgress;
Expand Down Expand Up @@ -87,6 +104,9 @@ public TaskServiceHelper(TaskRepository taskRepository) {
this.context = CoreLibrary.getInstance().context().applicationContext();
this.taskRepository = taskRepository;
this.allSharedPreferences = CoreLibrary.getInstance().context().allSharedPreferences();
this.taskSyncTrace = initTrace(TASK_SYNC);
String providerId = allSharedPreferences.fetchRegisteredANM();
team = allSharedPreferences.fetchDefaultTeam(providerId);
}

public List<Task> syncTasks() {
Expand Down Expand Up @@ -130,8 +150,12 @@ private List<Task> batchFetchTasksFromServer(Set<String> planDefinitions, List<S
try {
long maxServerVersion = 0L;
String tasksResponse = fetchTasks(planDefinitions, groups, serverVersion, returnCount);
startTaskTrace(FETCH, 0);
List<Task> tasks = taskGson.fromJson(tasksResponse, new TypeToken<List<Task>>() {
}.getType());

addAttribute(taskSyncTrace, COUNT, String.valueOf(tasks.size()));
stopTrace(taskSyncTrace);
if (tasks != null && tasks.size() > 0) {
for (Task task : tasks) {
try {
Expand Down Expand Up @@ -259,13 +283,15 @@ public void syncCreatedTaskToServer() {
HTTPAgent httpAgent = getHttpAgent();
List<Task> tasks = taskRepository.getAllUnsynchedCreatedTasks();
if (!tasks.isEmpty()) {
startTaskTrace(PUSH, tasks.size());
String jsonPayload = taskGson.toJson(tasks);
String baseUrl = CoreLibrary.getInstance().context().configuration().dristhiBaseURL();
Response<String> response = httpAgent.postWithJsonResponse(
MessageFormat.format("{0}/{1}",
baseUrl,
ADD_TASK_URL),
jsonPayload);
stopTrace(taskSyncTrace);
if (response.isFailure()) {
Timber.e("Failed to create new tasks on server.: %s", response.payload());
return;
Expand All @@ -287,5 +313,13 @@ public void syncCreatedTaskToServer() {
private HTTPAgent getHttpAgent() {
return CoreLibrary.getInstance().context().getHttpAgent();
}

private void startTaskTrace(String action, int count) {
clearTraceAttributes(taskSyncTrace);
addAttribute(taskSyncTrace, TEAM, team);
addAttribute(taskSyncTrace, ACTION, action);
addAttribute(taskSyncTrace, COUNT, String.valueOf(count));
startTrace(taskSyncTrace);
}
}

Loading

0 comments on commit 2af44c8

Please sign in to comment.