def collect_stats(session):
	# Get the congress and start/end dates of the session that this corresponds to.
	for congress, s, startdate, enddate in us.get_all_sessions():
		if s == session:
			break
	else:
		raise ValueError("Invalid session: " + session)

	# Who was serving on the last day of the session?
	people = [(r.person, r) for r in PersonRole.objects
		.filter(
			role_type__in=(RoleType.representative, RoleType.senator),
			startdate__lt=enddate, # use __lt and not __lte in case of multiple roles on the same day
			enddate__gte=enddate, # use __lte in case anyone's term ended exactly on this day
			)
		.select_related("person")]

	# Do a sponsorship analysis for bills in this session only.
	run_sponsorship_analysis(people, congress, startdate, enddate)

	# Get the committee members.
	from bill.prognosis import load_committee_membership
	committee_membership = load_committee_membership(congress)

	# Pre-fetch all of the votes in this session.
	votes_this_year = Vote.objects.filter(congress=congress, session=session)
	votes_this_year = {
		RoleType.representative: set(votes_this_year.filter(chamber=CongressChamber.house).values_list("id", flat=True)),
		RoleType.senator: set(votes_this_year.filter(chamber=CongressChamber.senate).values_list("id", flat=True)),
	}


	# Generate raw statistics.
	AllStats = { }
	for person, role in people:
		AllStats[person.id] = {
			"id": person.id,

			"role_id": role.id,
			"role_type": role.role_type,
			"role_start": role.startdate.isoformat(),
			"role_end": role.enddate.isoformat(),

			"stats": { },
			"cohorts": get_cohorts(person, role, congress, session, committee_membership),
		}

		stats = AllStats[person.id]["stats"]
		get_vote_stats(person, role, stats, votes_this_year)
		get_sponsor_stats(person, role, stats, congress, startdate, enddate, committee_membership)
		get_cosponsor_stats(person, role, stats, congress, startdate, enddate)
		get_cosponsored_stats(person, role, stats, congress, startdate, enddate)
		get_sponsorship_analysis_stats(person, role, stats)
		get_committee_stats(person, role, stats, committee_membership)
		get_transparency_stats(person, role, stats, congress, startdate, enddate)

	return AllStats
def collect_stats(session):
	# Get the congress and start/end dates of the session that this corresponds to.
	if int(session) < 1000:
		# Specifies a Congress.
		congress = int(session)
		session = None
		is_full_congress_stats = True

		# Get the last session in the Congress.
		for c, s, x, y in us.get_all_sessions():
			if c == congress:
				session2 = s
				last_day_of_session = y

		# Dummy dates. Don't want to use the congress dates because a bill can be
		# enacted after the end of the Congress.
		startdate = datetime.date.min
		enddate = datetime.date.max
	else:
		is_full_congress_stats = False
		session2 = session
		for congress, s, startdate, enddate in us.get_all_sessions():
			if s == session:
				break
		else:
			raise ValueError("Invalid session: " + session)
		last_day_of_session = enddate

	# Who was serving on the last day of the session?
	people = [(r.person, r) for r in PersonRole.objects
		.filter(
			role_type__in=(RoleType.representative, RoleType.senator),
			startdate__lt=last_day_of_session, # use __lt and not __lte in case of multiple roles on the same day
			enddate__gte=last_day_of_session, # use __lte in case anyone's term ended exactly on this day
			)
		.select_related("person")]

	# Do a sponsorship analysis for bills in this session only.
	run_sponsorship_analysis(people, congress, startdate, enddate)

	# Get the committee members.
	from bill.prognosis import load_committee_membership
	committee_membership = load_committee_membership(congress)

	# Pre-fetch all of the votes in this session.
	votes_this_year = Vote.objects.filter(congress=congress)
	if session:
		votes_this_year = votes_this_year.filter(session=session)
	votes_this_year = {
		RoleType.representative: set(votes_this_year.filter(chamber=CongressChamber.house).values_list("id", flat=True)),
		RoleType.senator: set(votes_this_year.filter(chamber=CongressChamber.senate).values_list("id", flat=True)),
	}


	# Generate raw statistics.
	AllStats = { }
	for person, role in people:
		AllStats[person.id] = {
			"id": person.id,

			"role_id": role.id,
			"role_type": role.role_type,
			"role_start": role.startdate.isoformat(),
			"role_end": role.enddate.isoformat(),

			"stats": { },
			"cohorts": get_cohorts(person, role, congress, session2, committee_membership),
		}

		stats = AllStats[person.id]["stats"]
		get_vote_stats(person, role, stats, votes_this_year)
		get_sponsor_stats(person, role, stats, congress, startdate, enddate, committee_membership)
		get_cosponsor_stats(person, role, stats, congress, startdate, enddate)
		get_cosponsored_stats(person, role, stats, congress, startdate, enddate)
		get_sponsorship_analysis_stats(person, role, stats)
		get_committee_stats(person, role, stats, committee_membership)
		get_transparency_stats(person, role, stats, congress, startdate, enddate)

	return AllStats, congress, is_full_congress_stats
Example #3
0
def collect_stats(session):
    # Get the congress and start/end dates of the session that this corresponds to.
    for congress, s, startdate, enddate in us.get_all_sessions():
        if s == session:
            break
    else:
        raise ValueError("Invalid session: " + session)

    # Who was serving on the last day of the session?
    people = [
        (r.person, r) for r in PersonRole.objects.filter(
            role_type__in=(RoleType.representative, RoleType.senator),
            startdate__lt=
            enddate,  # use __lt and not __lte in case of multiple roles on the same day
            enddate__gte=
            enddate,  # use __lte in case anyone's term ended exactly on this day
        ).select_related("person")
    ]

    # Do a sponsorship analysis for bills in this session only.
    run_sponsorship_analysis(people, congress, startdate, enddate)

    # Get the committee members.
    from bill.prognosis import load_committee_membership
    committee_membership = load_committee_membership(congress)

    # Pre-fetch all of the votes in this session.
    votes_this_year = Vote.objects.filter(congress=congress, session=session)
    votes_this_year = {
        RoleType.representative:
        set(
            votes_this_year.filter(chamber=CongressChamber.house).values_list(
                "id", flat=True)),
        RoleType.senator:
        set(
            votes_this_year.filter(chamber=CongressChamber.senate).values_list(
                "id", flat=True)),
    }

    # Generate raw statistics.
    AllStats = {}
    for person, role in people:
        AllStats[person.id] = {
            "id":
            person.id,
            "role_id":
            role.id,
            "role_type":
            role.role_type,
            "role_start":
            role.startdate.isoformat(),
            "role_end":
            role.enddate.isoformat(),
            "stats": {},
            "cohorts":
            get_cohorts(person, role, congress, session, committee_membership),
        }

        stats = AllStats[person.id]["stats"]
        get_vote_stats(person, role, stats, votes_this_year)
        get_sponsor_stats(person, role, stats, congress, startdate, enddate,
                          committee_membership)
        get_cosponsor_stats(person, role, stats, congress, startdate, enddate)
        get_cosponsored_stats(person, role, stats, congress, startdate,
                              enddate)
        get_sponsorship_analysis_stats(person, role, stats)
        get_committee_stats(person, role, stats, committee_membership)
        get_transparency_stats(person, role, stats, congress, startdate,
                               enddate)

    return AllStats