Skip to content

Commit

Permalink
Merge pull request #73 from unicef/bugfix/map_qry_optimization
Browse files Browse the repository at this point in the history
Updated the where clause conditions
  • Loading branch information
vikashkum05 authored Nov 7, 2024
2 parents e4ce027 + 135c79b commit e4c0afb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 56 deletions.
78 changes: 35 additions & 43 deletions proco/accounts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,15 +986,14 @@ def get_info_query(self):
COUNT(DISTINCT CASE WHEN sds.{col_name} IS NULL THEN sds.school_id ELSE NULL END) AS "unknown",
""".format(**kwargs)

if len(kwargs.get('country_ids', [])) > 0:
kwargs['country_condition'] = 'AND "schools_school"."country_id" IN ({0})'.format(
','.join([str(country_id) for country_id in kwargs['country_ids']])
)

if len(kwargs.get('admin1_ids', [])) > 0:
kwargs['admin1_condition'] = 'AND "schools_school"."admin1_id" IN ({0})'.format(
','.join([str(admin1_id) for admin1_id in kwargs['admin1_ids']])
)
elif len(kwargs.get('country_ids', [])) > 0:
kwargs['country_condition'] = 'AND "schools_school"."country_id" IN ({0})'.format(
','.join([str(country_id) for country_id in kwargs['country_ids']])
)

if len(kwargs['school_filters']) > 0:
kwargs['school_condition'] = ' AND ' + kwargs['school_filters']
Expand Down Expand Up @@ -1177,20 +1176,18 @@ def get_avg_query(self, **kwargs):
kwargs['school_weekly_join'] = ''
kwargs['school_weekly_condition'] = ''

if len(kwargs.get('country_ids', [])) > 0:
kwargs['country_condition'] = '"schools_school"."country_id" IN ({0}) AND'.format(
','.join([str(country_id) for country_id in kwargs['country_ids']])
)

if len(kwargs.get('admin1_ids', [])) > 0:
kwargs['admin1_condition'] = '"schools_school"."admin1_id" IN ({0}) AND'.format(
','.join([str(admin1_id) for admin1_id in kwargs['admin1_ids']])
)

if len(kwargs.get('school_ids', [])) > 0:
kwargs['school_condition'] = '"schools_school"."id" IN ({0}) AND '.format(','.join(kwargs['school_ids']))
kwargs['school_selection'] = '"schools_school"."id", '
kwargs['school_group_by'] = ', "schools_school"."id"'
elif len(kwargs.get('admin1_ids', [])) > 0:
kwargs['admin1_condition'] = '"schools_school"."admin1_id" IN ({0}) AND'.format(
','.join([str(admin1_id) for admin1_id in kwargs['admin1_ids']])
)
elif len(kwargs.get('country_ids', [])) > 0:
kwargs['country_condition'] = '"schools_school"."country_id" IN ({0}) AND'.format(
','.join([str(country_id) for country_id in kwargs['country_ids']])
)

if len(kwargs['school_filters']) > 0:
kwargs['school_condition'] += kwargs['school_filters'] + ' AND '
Expand Down Expand Up @@ -1291,15 +1288,14 @@ def get_static_info_query(self, query_labels):
kwargs['school_weekly_join'] = ''
kwargs['school_weekly_condition'] = ''

if len(kwargs.get('country_ids', [])) > 0:
kwargs['country_condition'] = ' AND "schools_school"."country_id" IN ({0})'.format(
','.join([str(country_id) for country_id in kwargs['country_ids']])
)

if len(kwargs.get('admin1_ids', [])) > 0:
kwargs['admin1_condition'] = ' AND "schools_school"."admin1_id" IN ({0})'.format(
','.join([str(admin1_id) for admin1_id in kwargs['admin1_ids']])
)
elif len(kwargs.get('country_ids', [])) > 0:
kwargs['country_condition'] = ' AND "schools_school"."country_id" IN ({0})'.format(
','.join([str(country_id) for country_id in kwargs['country_ids']])
)

if len(kwargs['school_filters']) > 0:
kwargs['school_condition'] = ' AND ' + kwargs['school_filters']
Expand Down Expand Up @@ -1836,32 +1832,30 @@ def get_live_map_query(self, env, request):
END AS field_status,
""".format(**kwargs)

if len(kwargs.get('country_ids', [])) > 0:
if len(kwargs.get('school_ids', [])) > 0:
add_random_condition = False
kwargs['country_condition'] = 'AND "schools_school"."country_id" IN ({0})'.format(
','.join([str(country_id) for country_id in kwargs['country_ids']])
)
kwargs['country_outer_condition'] = 'AND "schools_school"."country_id" IN ({0})'.format(
','.join([str(country_id) for country_id in kwargs['country_ids']])
kwargs['school_condition'] = 'AND "schools_school"."id" IN ({0})'.format(
','.join([str(school_id) for school_id in kwargs['school_ids']])
)

if len(kwargs.get('admin1_ids', [])) > 0:
kwargs['school_outer_condition'] = 'AND "schools_school"."id" IN ({0})'.format(
','.join([str(school_id) for school_id in kwargs['school_ids']])
)
elif len(kwargs.get('admin1_ids', [])) > 0:
add_random_condition = False
kwargs['admin1_condition'] = 'AND "schools_school"."admin1_id" IN ({0})'.format(
','.join([str(admin1_id) for admin1_id in kwargs['admin1_ids']])
)
kwargs['admin1_outer_condition'] = 'AND "schools_school"."admin1_id" IN ({0})'.format(
','.join([str(admin1_id) for admin1_id in kwargs['admin1_ids']])
)

if len(kwargs.get('school_ids', [])) > 0:
elif len(kwargs.get('country_ids', [])) > 0:
add_random_condition = False
kwargs['school_condition'] = 'AND "schools_school"."id" IN ({0})'.format(
','.join([str(school_id) for school_id in kwargs['school_ids']])
kwargs['country_condition'] = 'AND "schools_school"."country_id" IN ({0})'.format(
','.join([str(country_id) for country_id in kwargs['country_ids']])
)

kwargs['school_outer_condition'] = 'AND "schools_school"."id" IN ({0})'.format(
','.join([str(school_id) for school_id in kwargs['school_ids']])
kwargs['country_outer_condition'] = 'AND "schools_school"."country_id" IN ({0})'.format(
','.join([str(country_id) for country_id in kwargs['country_ids']])
)

if len(kwargs['school_filters']) > 0:
Expand Down Expand Up @@ -1933,22 +1927,20 @@ def get_static_map_query(self, env, request):

add_random_condition = True

if len(kwargs.get('country_ids', [])) > 0:
if len(kwargs.get('school_ids', [])) > 0:
add_random_condition = False
kwargs['country_condition'] = 'AND schools_school."country_id" IN ({0})'.format(
','.join([str(country_id) for country_id in kwargs['country_ids']])
kwargs['school_condition'] = 'AND schools_school."id" IN ({0})'.format(
','.join([str(school_id) for school_id in kwargs['school_ids']])
)

if len(kwargs.get('admin1_ids', [])) > 0:
elif len(kwargs.get('admin1_ids', [])) > 0:
add_random_condition = False
kwargs['admin1_condition'] = 'AND schools_school."admin1_id" IN ({0})'.format(
','.join([str(admin1_id) for admin1_id in kwargs['admin1_ids']])
)

if len(kwargs.get('school_ids', [])) > 0:
elif len(kwargs.get('country_ids', [])) > 0:
add_random_condition = False
kwargs['school_condition'] = 'AND schools_school."id" IN ({0})'.format(
','.join([str(school_id) for school_id in kwargs['school_ids']])
kwargs['country_condition'] = 'AND schools_school."country_id" IN ({0})'.format(
','.join([str(country_id) for country_id in kwargs['country_ids']])
)

if len(kwargs['school_filters']) > 0:
Expand Down
25 changes: 12 additions & 13 deletions proco/schools/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,25 +324,24 @@ def query_filters(self, request, table_configs):
'school_id' in request.query_params or
'school_id__in' in request.query_params
):
if 'country_id' in request.query_params:
table_configs[
'country_condition'] = f" AND schools_school.country_id = {request.query_params['country_id']}"
elif 'country_id__in' in request.query_params:
country_ids = ','.join([c.strip() for c in request.query_params['country_id__in'].split(',')])
table_configs['country_condition'] = f" AND schools_school.country_id IN ({country_ids})"

if 'admin1_id' in request.query_params:
if 'school_id' in request.query_params:
table_configs['school_condition'] = f" AND schools_school.id = {request.query_params['school_id']}"
elif 'school_id__in' in request.query_params:
school_ids = ','.join([c.strip() for c in request.query_params['school_id__in'].split(',')])
table_configs['school_condition'] = f" AND schools_school.id IN ({school_ids})"
elif 'admin1_id' in request.query_params:
table_configs[
'admin1_condition'] = f" AND schools_school.admin1_id = {request.query_params['admin1_id']}"
elif 'admin1_id__in' in request.query_params:
admin1_ids = ','.join([c.strip() for c in request.query_params['admin1_id__in'].split(',')])
table_configs['admin1_condition'] = f" AND schools_school.admin1_id IN ({admin1_ids})"
elif 'country_id' in request.query_params:
table_configs[
'country_condition'] = f" AND schools_school.country_id = {request.query_params['country_id']}"
elif 'country_id__in' in request.query_params:
country_ids = ','.join([c.strip() for c in request.query_params['country_id__in'].split(',')])
table_configs['country_condition'] = f" AND schools_school.country_id IN ({country_ids})"

if 'school_id' in request.query_params:
table_configs['school_condition'] = f" AND schools_school.id = {request.query_params['school_id']}"
elif 'school_id__in' in request.query_params:
school_ids = ','.join([c.strip() for c in request.query_params['school_id__in'].split(',')])
table_configs['school_condition'] = f" AND schools_school.id IN ({school_ids})"
else:
zoom_level = int(request.query_params.get('z', '0'))
if zoom_level == 0:
Expand Down

0 comments on commit e4c0afb

Please sign in to comment.