Skip to content

Commit

Permalink
Merge pull request #335 from opensrp/v0_10-optimize-filter-selection-…
Browse files Browse the repository at this point in the history
…condition

Optimize Filter Selection Condition - v0.10
  • Loading branch information
qiarie authored May 9, 2023
2 parents 302d675 + 06a3dc8 commit 3b1f4c7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 101 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=0.10.2-SNAPSHOT
VERSION_NAME=0.10.3-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Child Library
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@ public String getObjectIdsQuery(String mainCondition, String filters) {
String strFilters = getFilter(filters);

if (StringUtils.isNotBlank(strFilters) && StringUtils.isBlank(strMainCondition)) {
strFilters = String.format(" WHERE " + getDemographicTable() + ".phrase MATCH '*%s*'", filters);
strFilters = String.format(" WHERE (" + getDemographicTable() + ".first_name LIKE '%%%s%%' OR "+getDemographicTable()+ ".last_name LIKE '%%%s%%')", filters, filters);
}

return "SELECT " + getDemographicTable() + ".object_id " +
"FROM " + CommonFtsObject.searchTableName(getDemographicTable()) + " " + getDemographicTable() + " " +
"LEFT JOIN " + getChildDetailsTable() + " ON " + getDemographicTable() + ".object_id = " + getChildDetailsTable() + ".id " +
"LEFT JOIN " + CommonFtsObject.searchTableName(getChildDetailsTable()) + " ON " + getDemographicTable() + ".object_id = " + CommonFtsObject.searchTableName(getChildDetailsTable()) + ".object_id " +
return "SELECT " + getDemographicTable() + ".id " +
"FROM " + getDemographicTable() + " " + getDemographicTable() + " " +
"LEFT JOIN " + getChildDetailsTable() + " ON " + getDemographicTable() + ".id = " + getChildDetailsTable() + ".base_entity_id " +
strMainCondition + strFilters;
}

private String getFilter(String filters) {
if (StringUtils.isNotBlank(filters)) {
return String.format(" AND " + getDemographicTable() + ".phrase MATCH '*%s*'", filters);
return String.format(" AND (" + getDemographicTable() + ".first_name LIKE '%%%s%%' OR "+getDemographicTable()+ ".last_name LIKE '%%%s%%')", filters, filters);
}
return "";
}
Expand All @@ -44,13 +43,12 @@ public String getCountExecuteQuery(String mainCondition, String filters) {
String strFilters = getFilter(filters);

if (StringUtils.isNotBlank(strFilters) && StringUtils.isBlank(strMainCondition)) {
strFilters = String.format(" WHERE " + getDemographicTable() + ".phrase MATCH '*%s*'", filters);
strFilters = String.format(" WHERE (" + getDemographicTable() + ".first_name LIKE '%%%s%%' OR "+getDemographicTable()+ ".last_name LIKE '%%%s%%')", filters, filters);
}

return "SELECT count(" + getDemographicTable() + ".object_id) " +
"FROM " + CommonFtsObject.searchTableName(getDemographicTable()) + " " + getDemographicTable() + " " +
"LEFT JOIN " + getChildDetailsTable() + " ON " + getDemographicTable() + ".object_id = " + getChildDetailsTable() + ".id " +
"LEFT JOIN " + CommonFtsObject.searchTableName(getChildDetailsTable()) + " ON " + getDemographicTable() + ".object_id = " + CommonFtsObject.searchTableName(getChildDetailsTable()) + ".object_id " +
return "SELECT count(" + getChildDetailsTable() + ".id) " +
"FROM " + getChildDetailsTable() + " " + getChildDetailsTable() + " " +
"LEFT JOIN " + getDemographicTable() + " ON " + getChildDetailsTable() + ".id = " + getDemographicTable() + ".base_entity_id " +
strMainCondition + strFilters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
*/
public class VaccineOverdueCountRepositoryHelper {

public static final String MIGRATE_VACCINES_QUERY = "INSERT INTO vaccine_overdue_count SELECT DISTINCT ec_child_details.id FROM alerts a INNER JOIN ec_child_details ON a.caseID = ec_child_details.id INNER JOIN ec_client ON a.caseID = ec_client.base_entity_id \n" +
public static final String MIGRATE_VACCINES_QUERY = "INSERT INTO vaccine_overdue_count SELECT DISTINCT ec_child_details.base_entity_id, status FROM alerts a INNER JOIN ec_child_details ON a.caseID = ec_child_details.id INNER JOIN ec_client ON a.caseID = ec_client.base_entity_id \n" +
"WHERE (ec_client.dod IS NULL AND ec_client.date_removed IS NULL AND ec_client.is_closed IS NOT '1' AND ec_child_details.is_closed IS NOT '1') \n" +
"AND ( ec_child_details.inactive IS NULL OR ec_child_details.inactive != 'true' ) AND ( ec_child_details.lost_to_follow_up IS NULL \n" +
"OR ec_child_details.lost_to_follow_up != 'true' ) AND status ='urgent' AND ( ec_child_details.lost_to_follow_up IS NULL \n" +
"OR ec_child_details.lost_to_follow_up != 'true' ) " +
"AND a.scheduleName in ( \"OPV_0\",\"BCG\",\"OPV_1\",\"PENTA_1\",\"PCV_1 \",\"ROTA_1\",\"OPV_2\",\"PENTA_2\",\"PCV_2\",\"ROTA_2\",\"OPV_3\",\"PCV_3\",\"PENTA_3\",\"IPV\",\"MR_1\",\"OPV_4\",\"MR_2\"," +
"\"OPV 0\",\"OPV 1\",\"PENTA 1\",\"PCV 1 \",\"ROTA 1\",\"OPV 2\",\"PENTA 2\",\"PCV 2\",\"ROTA 2\",\"OPV 3\",\"PCV 3\",\"PENTA 3\",\"IPV\",\"MR 1\",\"OPV 4\",\"MR 2\" )\n";
"OR ec_child_details.lost_to_follow_up != 'true' ) AND (status ='urgent' OR status ='normal') AND ( ec_child_details.lost_to_follow_up IS NULL \n" +
"OR ec_child_details.lost_to_follow_up != 'true' ) AND a.scheduleName in ( \"OPV_0\",\"BCG\",\"OPV_1\",\"PENTA_1\",\"PCV_1 \",\"ROTA_1\",\"OPV_2\",\"PENTA_2\",\"PCV_2\",\"ROTA_2\",\"OPV_3\",\"PCV_3\",\"PENTA_3\",\"IPV\",\"MR_1\",\"OPV_4\",\"MR_2\",\"OPV 0\",\"OPV 1\",\"PENTA 1\",\"PCV 1 \",\"ROTA 1\",\"OPV 2\",\"PENTA 2\",\"PCV 2\",\"ROTA 2\",\"OPV 3\",\"PCV 3\",\"PENTA 3\",\"IPV\",\"MR 1\",\"OPV 4\",\"MR 2\" )\n";

public static final String COUNT_QUERY_SQL = "SELECT COUNT(1) FROM " + VaccineOverdueCountRepository.TABLE_NAME + " ci INNER JOIN ec_client ON ec_client.id = ci.base_entity_id " +
"INNER JOIN ec_child_details ON ec_client.id = ec_child_details.id " +
"WHERE (ec_client.dod IS NULL AND ec_client.date_removed is null AND ec_client.is_closed IS NOT '1' AND ec_child_details.is_closed IS NOT '1') " +
"AND ( ec_child_details.inactive IS NULL OR ec_child_details.inactive != 'true');";
"AND ( ec_child_details.inactive IS NULL OR ec_child_details.inactive != 'true') AND ci.status = 'urgent';";

public static int getOverdueCount() {
return ImmunizationLibrary.getInstance().getVaccineOverdueCountRepository().getOverdueCount(COUNT_QUERY_SQL);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.smartregister.child.provider;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.view.LayoutInflater;

import org.junit.Test;
import org.smartregister.child.BaseUnitTest;
import org.smartregister.child.R;

public class AdvancedSearchClientsProviderTest extends BaseUnitTest {

@Test
public void testInflateLayoutForCursorAdapterInflatesCorrectView() {
AdvancedSearchClientsProvider provider = mock(AdvancedSearchClientsProvider.class);
LayoutInflater inflater = mock(LayoutInflater.class);
doReturn(inflater).when(provider).inflater();
when(provider.inflatelayoutForCursorAdapter()).thenCallRealMethod();
provider.inflatelayoutForCursorAdapter();
verify(inflater).inflate(eq(R.layout.advanced_search_client), any());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ public class RegisterQueryProviderTest extends BaseUnitTest {

private RegisterQueryProvider queryProvider;

private String expectedQuery = "SELECT ec_client.object_id FROM ec_client_search ec_client " +
"LEFT JOIN ec_child_details ON ec_client.object_id = ec_child_details.id " +
"LEFT JOIN ec_child_details_search ON ec_client.object_id = ec_child_details_search.object_id ";
private String expectedQuery = "SELECT ec_client.id FROM ec_client ec_client LEFT JOIN ec_child_details " +
"ON ec_client.id = ec_child_details.base_entity_id ";

private String registerQuery = "SELECT %s FROM ec_child_details " +
"JOIN ec_mother_details ON ec_child_details.relational_id = ec_mother_details.base_entity_id " +
Expand All @@ -31,9 +30,8 @@ public class RegisterQueryProviderTest extends BaseUnitTest {
"ec_child_details.pmtct_status,ec_client.last_interacted_with,ec_child_details.inactive," +
"ec_child_details.lost_to_follow_up,ec_child_details.mother_guardian_phone_number,ec_client.address1";

private String countQuery = "SELECT count(ec_client.object_id) FROM ec_client_search ec_client " +
"LEFT JOIN ec_child_details ON ec_client.object_id = ec_child_details.id " +
"LEFT JOIN ec_child_details_search ON ec_client.object_id = ec_child_details_search.object_id ";
private String countQuery = "SELECT count(ec_child_details.id) FROM ec_child_details ec_child_details " +
"LEFT JOIN ec_client ON ec_child_details.id = ec_client.base_entity_id ";

private final String activeChildrenIdsQuery = "SELECT ec_child_details.id FROM ec_child_details " +
"INNER JOIN ec_client ON ec_child_details.id = ec_client.id " +
Expand Down Expand Up @@ -71,7 +69,7 @@ public void testGetFilterWithNonEmptyFilterShouldReturnFormattedFilterCondition(
RegisterQueryProvider provider = Mockito.spy(queryProvider);

String filters = "something";
Assert.assertEquals(" AND ec_client.phrase MATCH '*something*'", method.invoke(provider, filters));
Assert.assertEquals(" AND (ec_client.first_name LIKE '%something%' OR ec_client.last_name LIKE '%something%')", method.invoke(provider, filters));
}

@Test
Expand Down Expand Up @@ -114,7 +112,7 @@ public void testGetObjectIdsQueryWithNonEmptyConditionAndEmptyFilterReturnsQuery

@Test
public void testGetObjectIdsQueryWithEmptyConditionAndNonEmptyFilterReturnsQueryWithWhereClause() {
expectedQuery += " WHERE ec_client.phrase MATCH '*123*'";
expectedQuery += " WHERE (ec_client.first_name LIKE '%123%' OR ec_client.last_name LIKE '%123%')";

String query = queryProvider.getObjectIdsQuery("", "123");

Expand Down Expand Up @@ -167,7 +165,7 @@ public void testGetCountExecuteQueryWithNonEmptyConditionAndEmptyFiltersReturnsC

@Test
public void testGetCountExecuteQueryWithEmptyConditionAndNonEmptyFiltersReturnsCountQueryWithWhereClause() {
countQuery += " WHERE ec_client.phrase MATCH '*4567*'";
countQuery += " WHERE (ec_client.first_name LIKE '%4567%' OR ec_client.last_name LIKE '%4567%')";

String query = queryProvider.getCountExecuteQuery("", "4567");

Expand All @@ -176,7 +174,7 @@ public void testGetCountExecuteQueryWithEmptyConditionAndNonEmptyFiltersReturnsC

@Test
public void testGetCountExecuteQueryWithNonEmptyConditionAndFiltersReturnsCountQueryWithWhereClause() {
countQuery += " WHERE condition = 'condition_value' AND ec_client.phrase MATCH '*98765*'";
countQuery += " WHERE condition = 'condition_value' AND (ec_client.first_name LIKE '%98765%' OR ec_client.last_name LIKE '%98765%')";

String query = queryProvider.getCountExecuteQuery("condition = 'condition_value'", "98765");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.smartregister.domain.AlertStatus;
import org.smartregister.immunization.ImmunizationLibrary;
import org.smartregister.immunization.db.VaccineRepo;
import org.smartregister.immunization.repository.VaccineOverdueCountRepository;
import org.smartregister.immunization.util.VaccinateActionUtils;

import java.util.List;
Expand All @@ -22,88 +23,22 @@ public static String getHomeRegisterCondition() {
}

public static String getFilterSelectionCondition(boolean urgentOnly) {

final String AND = " AND ";
final String OR = " OR ";
final String IS_NULL_OR = " IS NULL OR ";
final String TRUE = "'true'";

String mainCondition = " ( " + Constants.KEY.DOD + " is NULL OR " + Constants.KEY.DOD + " = '' ) " +
AND + " (" + Constants.CHILD_STATUS.INACTIVE + IS_NULL_OR + Constants.CHILD_STATUS.INACTIVE + " != " + TRUE + " ) " +
AND + " (" + Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP + IS_NULL_OR + Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP + " != " + TRUE + " ) " +
AND + " ( ";

List<VaccineRepo.Vaccine> vaccines = ImmunizationLibrary.getVaccineCacheMap().get(Constants.CHILD_TYPE).vaccineRepo;

vaccines.remove(VaccineRepo.Vaccine.opv0);
vaccines.remove(VaccineRepo.Vaccine.opv4);
vaccines.remove(VaccineRepo.Vaccine.measles1);
vaccines.remove(VaccineRepo.Vaccine.mr1);
vaccines.remove(VaccineRepo.Vaccine.measles2);
vaccines.remove(VaccineRepo.Vaccine.mr2);

final String URGENT = "'" + AlertStatus.urgent.value() + "'";
final String NORMAL = "'" + AlertStatus.normal.value() + "'";
final String COMPLETE = "'" + AlertStatus.complete.value() + "'";


for (int i = 0; i < vaccines.size(); i++) {
VaccineRepo.Vaccine vaccine = vaccines.get(i);
if (i == vaccines.size() - 1) {
mainCondition += " " + VaccinateActionUtils.addHyphen(vaccine.display()) + " = " + URGENT + " ";
} else {
mainCondition += " " + VaccinateActionUtils.addHyphen(vaccine.display()) + " = " + URGENT + OR;
}
}

mainCondition += OR + " ( " + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.opv0.display()) + " = " + URGENT +
AND + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.opv4.display()) + " != " + COMPLETE + " ) ";
mainCondition += OR + " ( " + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.opv4.display()) + " = " + URGENT +
AND + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.opv0.display()) + " != " + COMPLETE + " ) ";

mainCondition += OR + " ( " + VaccinateActionUtils
.addHyphen(VaccineRepo.Vaccine.measles1.display()) + " = " + URGENT +
AND + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.mr1.display()) + " != " + COMPLETE + " ) ";
mainCondition += OR + " ( " + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.mr1.display()) + " = " + URGENT +
AND + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.measles1.display()) + " != " + COMPLETE + " ) ";

mainCondition += OR + " ( " + VaccinateActionUtils
.addHyphen(VaccineRepo.Vaccine.measles2.display()) + " = " + URGENT +
AND + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.mr2.display()) + " != " + COMPLETE + " ) ";
mainCondition += OR + " ( " + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.mr2.display()) + " = " + URGENT +
AND + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.measles2.display()) + " != " + COMPLETE + " ) ";
String childDetailsTable = Utils.metadata().getRegisterQueryProvider().getChildDetailsTable();

String vaccineOverDueCountSelect = "SELECT " + Constants.KEY.BASE_ENTITY_ID + " FROM " + VaccineOverdueCountRepository.TABLE_NAME;
if (urgentOnly) {
return mainCondition + " ) ";
}

mainCondition += OR;
for (int i = 0; i < vaccines.size(); i++) {
VaccineRepo.Vaccine vaccine = vaccines.get(i);
if (i == vaccines.size() - 1) {
mainCondition += " " + VaccinateActionUtils.addHyphen(vaccine.display()) + " = " + NORMAL + " ";
} else {
mainCondition += " " + VaccinateActionUtils.addHyphen(vaccine.display()) + " = " + NORMAL + OR;
}
vaccineOverDueCountSelect += " WHERE " + VaccineOverdueCountRepository.STATUS + " = 'urgent'";
}

mainCondition += OR + " ( " + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.opv0.display()) + " = " + NORMAL +
AND + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.opv4.display()) + " != " + COMPLETE + " ) ";
mainCondition += OR + " ( " + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.opv4.display()) + " = " + NORMAL +
AND + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.opv0.display()) + " != " + COMPLETE + " ) ";

mainCondition += OR + " ( " + VaccinateActionUtils
.addHyphen(VaccineRepo.Vaccine.measles1.display()) + " = " + NORMAL +
AND + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.mr1.display()) + " != " + COMPLETE + " ) ";
mainCondition += OR + " ( " + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.mr1.display()) + " = " + NORMAL +
AND + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.measles1.display()) + " != " + COMPLETE + " ) ";

mainCondition += OR + " ( " + VaccinateActionUtils
.addHyphen(VaccineRepo.Vaccine.measles2.display()) + " = " + NORMAL +
AND + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.mr2.display()) + " != " + COMPLETE + " ) ";
mainCondition += OR + " ( " + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.mr2.display()) + " = " + NORMAL +
AND + VaccinateActionUtils.addHyphen(VaccineRepo.Vaccine.measles2.display()) + " != " + COMPLETE + " ) ";

String mainCondition = " ( " + Utils.metadata().getRegisterQueryProvider().getDemographicTable() + "." + Constants.KEY.DOD + " IS NULL OR " + Utils.metadata().getRegisterQueryProvider().getDemographicTable() + "." + Constants.KEY.DOD + " = '' ) " +
AND + " ( " + childDetailsTable + "." + Constants.CHILD_STATUS.INACTIVE + IS_NULL_OR + childDetailsTable + "." + Constants.CHILD_STATUS.INACTIVE + " != " + TRUE + " ) " +
AND + " ( " + childDetailsTable + "." + Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP + IS_NULL_OR + childDetailsTable + "." + Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP + " != " + TRUE + " ) " +
AND + " " + childDetailsTable + "." + Constants.KEY.BASE_ENTITY_ID + "+ IN (" + vaccineOverDueCountSelect ;
return mainCondition + " ) ";
}

Expand Down

0 comments on commit 3b1f4c7

Please sign in to comment.