Skip to content

Commit

Permalink
Add all_session_cache for event<>bill matching
Browse files Browse the repository at this point in the history
  • Loading branch information
alexobaseki committed Oct 17, 2024
1 parent 62c99b1 commit 914c556
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
28 changes: 15 additions & 13 deletions openstates/importers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def __init__(self, jurisdiction_id: str, do_postimport=True) -> None:
self.pseudo_id_cache: typing.Dict[str, typing.Optional[_ID]] = {}
self.person_cache: typing.Dict[_PersonCacheKey, typing.Optional[str]] = {}
self.session_cache: typing.Dict[str, LegislativeSession] = {}
self.all_sessions_cache: typing.List[LegislativeSession] = []
self.logger = logging.getLogger("openstates")
self.info = self.logger.info
self.debug = self.logger.debug
Expand All @@ -139,6 +140,12 @@ def __init__(self, jurisdiction_id: str, do_postimport=True) -> None:
if settings.IMPORT_TRANSFORMERS.get(self._type):
self.cached_transformers = settings.IMPORT_TRANSFORMERS[self._type]

def get_all_sessions(self) -> None:
if not self.all_sessions_cache:
self.all_sessions_cache = LegislativeSession.objects.filter(
jurisdiction_id=self.jurisdiction_id
).order_by("-start_date")

def get_session(self, identifier: str) -> LegislativeSession:
if identifier not in self.session_cache:
self.session_cache[identifier] = LegislativeSession.objects.get(
Expand Down Expand Up @@ -171,23 +178,18 @@ def resolve_bill(self, bill_id: str, *, date: str) -> typing.Optional[_ID]:

# Some steps here to first find the session that matches the incoming entity using the entity date
# If a unique session is not found, then use the session with the latest "start_date"
date = datetime.fromisoformat(date)
legislative_session = LegislativeSession.objects.filter(
Q(end_date__gte=date) | Q(end_date=""),
start_date__lte=date,
jurisdiction_id=self.jurisdiction_id,
)
session_ids = {each.id for each in legislative_session}
date = datetime.fromisoformat(date).strftime("%Y-%m-%d")
session_ids = [
session.id
for session in self.all_sessions_cache
if session.start_date <= date
and (session.end_date >= date or not session.end_date)
]

if len(session_ids) == 1:
session_id = session_ids.pop()
else:
legislative_session = (
LegislativeSession.objects.filter(jurisdiction_id=self.jurisdiction_id)
.order_by("-start_date")
.first()
)
session_id = legislative_session.id if legislative_session else None
session_id = self.all_sessions_cache[0].id

objects = Bill.objects.filter(
legislative_session__id=session_id,
Expand Down
1 change: 1 addition & 0 deletions openstates/importers/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def get_chamber_name_from_event_name(
return None

def prepare_for_db(self, data: _JsonDict) -> _JsonDict:
self.get_all_sessions()
data["jurisdiction_id"] = self.jurisdiction_id
data["location"] = self.get_location(data["location"])
org_classification = self.get_chamber_name_from_event_name(data["name"])
Expand Down

0 comments on commit 914c556

Please sign in to comment.