Changeset 356174b


Ignore:
Timestamp:
Apr 29, 2015, 9:16:12 PM (11 years ago)
Author:
Alex Dehnert <adehnert@…>
Branches:
master, stable
Children:
093448c
Parents:
82e595e
git-author:
Alex Dehnert <adehnert@…> (04/29/15 21:16:12)
git-committer:
Alex Dehnert <adehnert@…> (04/29/15 21:16:12)
Message:

Improve perf and correctness of export_finboard_groups.py

export_finboard_groups.py was taking about 45 seconds to run in prod, and
including former signatories.

Changes:

  • Stop using prefetch_related
    • As being used, the return value wasn't being saved as a new queryset, so it wasn't effective
    • Fixing that revealed that "officerrole" is the wrong field name
    • Finally, it requires doing filter (for role and current officers) in Python, which seemed a bit sad, and seemed like it would plausibly be appreciably slower due to needing to pull all past officers
  • Don't reuse groups, because it shadows the module name
  • Indent the JSON for improved readability
File:
1 edited

Legend:

Unmodified
Added
Removed
  • asadb/groups/export_finboard_groups.py

    r82e595e r356174b  
    2424
    2525if __name__ == '__main__':
    26     groups = groups.models.Group.objects.filter(group_status__is_active=True)
    27     groups = groups.filter(group_funding__slug='undergrad')
    28     groups = groups.filter(officeholder__role__slug__in=ROLES)
    29     groups.prefetch_related('officeholder_set', 'officeholder_set__officerrole')
    30     groups = groups.distinct()
     26    gs = groups.models.Group.objects.filter(group_status__is_active=True)
     27    gs = gs.filter(group_funding__slug='undergrad')
    3128
    32     group_dicts = []
    33     for group in groups:
    34         # do the processing in python because of the prefetch_related
    35         officers = list(group.officeholder_set.all())
     29    officers = groups.models.OfficeHolder.current_holders
     30    officers = officers.filter(group__in=gs, role__slug__in=ROLES)
     31    officers = officers.select_related('role__slug', 'group')
     32
     33    group_dicts = {}
     34    for group in gs:
    3635        group_dict = {
    3736            'id': group.id,
     
    4039        }
    4140        for role in ROLES:
    42             group_dict[role] = [officer.person for officer in officers
    43                                 if officer.role.slug == role]
    44         group_dicts.append(group_dict)
     41            group_dict[role] = []
     42        group_dicts[group.id] = group_dict
     43
     44    for officer in officers:
     45        group_dicts[officer.group.id][officer.role.slug].append(officer.person)
    4546
    4647    out = codecs.getwriter('utf-8')(sys.stdout)
    4748    json.dump({
    48         'groups': group_dicts,
     49        'groups': group_dicts.values(),
    4950        'date': datetime.datetime.now().isoformat(),
    50     }, out)
     51    }, out, indent=2)
Note: See TracChangeset for help on using the changeset viewer.