Skip to content

Commit

Permalink
[#2554] Refactor group_list to only query necessary fields
Browse files Browse the repository at this point in the history
Refactor organization/group_list to only query the necessary fields by default
(id, name, title, package_count) depending on the required sort. This massively
speeds up the default query without all_fields. In part this is because then we
no longer need to get all fields in all groups on all cases to do the sorting.
There is a minor drawback in that then we can't take private datasets into
account when sorting by number of datasets. The actual number displayed will
take private datasets into account, as this comes from the dictization, but
there might be inconsistencies (note that the "order by datasets option" is
not offered by default on the UI)
  • Loading branch information
amercader committed Aug 19, 2015
1 parent 4fd2baf commit bbaab15
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions ckan/logic/action/get.py
Expand Up @@ -392,8 +392,21 @@ def _group_or_org_list(context, data_dict, is_org=False):
'package_count', 'title'],
total=1)

query = model.Session.query(model.Group)
if sort_info and sort_info[0][0] == 'package_count':
query = model.Session.query(model.Group.id,
model.Group.name,
sqlalchemy.func.count(model.Group.id))

query = query.filter(model.Member.group_id == model.Group.id) \
.filter(model.Member.table_id == model.Package.id) \
.filter(model.Member.table_name == 'package') \
.filter(model.Package.state == 'active')
else:
query = model.Session.query(model.Group.id,
model.Group.name)

query = query.filter(model.Group.state == 'active')

if groups:
query = query.filter(model.Group.name.in_(groups))
if q:
Expand All @@ -407,27 +420,37 @@ def _group_or_org_list(context, data_dict, is_org=False):
query = query.filter(model.Group.is_organization == is_org)
if not is_org:
query = query.filter(model.Group.type == group_type)
if sort_info:
sort_field = sort_info[0][0]
sort_direction = sort_info[0][1]
if sort_field == 'package_count':
query = query.group_by(model.Group.id, model.Group.name)
sort_model_field = sqlalchemy.func.count(model.Group.id)
elif sort_field == 'name':
sort_model_field = model.Group.name
elif sort_field == 'title':
sort_model_field = model.Group.title

if sort_direction == 'asc':
query = query.order_by(sqlalchemy.asc(sort_model_field))
else:
query = query.order_by(sqlalchemy.desc(sort_model_field))

groups = query.all()

action = 'organization_show' if is_org else 'group_show'

group_list = []
for group in groups:
data_dict['id'] = group.id

for key in ('include_extras', 'include_tags', 'include_users',
'include_groups', 'include_followers'):
if key not in data_dict:
data_dict[key] = False

group_list.append(logic.get_action(action)(context, data_dict))

group_list = sorted(group_list, key=lambda x: x[sort_info[0][0]],
reverse=sort_info[0][1] == 'desc')

if not all_fields:
group_list = [group[ref_group_by] for group in group_list]
if all_fields:
action = 'organization_show' if is_org else 'group_show'
group_list = []
for group in groups:
data_dict['id'] = group.id
for key in ('include_extras', 'include_tags', 'include_users',
'include_groups', 'include_followers'):
if key not in data_dict:
data_dict[key] = False

group_list.append(logic.get_action(action)(context, data_dict))
else:
group_list = [getattr(group, ref_group_by) for group in groups]

return group_list

Expand Down

0 comments on commit bbaab15

Please sign in to comment.