From f454de7799e074692cab0d1ecf94f8f14209a229 Mon Sep 17 00:00:00 2001 From: Devin Matte Date: Wed, 24 Jul 2024 19:55:09 -0400 Subject: [PATCH 1/2] Adding commuter rail ridership to the landing page --- ingestor/chalicelib/constants.py | 19 +++++++++++++++++++ ingestor/chalicelib/landing.py | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/ingestor/chalicelib/constants.py b/ingestor/chalicelib/constants.py index ec31625..ffb04b2 100644 --- a/ingestor/chalicelib/constants.py +++ b/ingestor/chalicelib/constants.py @@ -248,6 +248,21 @@ def get_route_metadata(line: str, date: date, include_terminals: bool, route: st "line-blue": "line-Blue", "line-green": "line-Green", } +COMMUTER_RAIL_LINES = [ + "CR-Fairmount", + "CR-Fitchburg", + "CR-Worcester", + "CR-Franklin", + "CR-Greenbush", + "CR-Haverhill", + "CR-Kingston", + "CR-Lowell", + "CR-Middleborough", + "CR-Needham", + "CR-Newburyport", + "CR-Providence", +] + DATE_FORMAT = "%Y-%m-%dT%H:%M:%S" DATE_FORMAT_BACKEND = "%Y-%m-%d" GLX_EXTENSION_DATE = datetime.strptime("2023-03-19", DATE_FORMAT_BACKEND).date() @@ -261,6 +276,10 @@ def get_route_metadata(line: str, date: date, include_terminals: bool, route: st DD_URL_SINGLE_TT = "https://dashboard-api.labs.transitmatters.org/api/traveltimes/{date}?{parameters}" +def commuter_rail_ridership_key(line: str): + return f"line-{line[3:]}" + + def get_monthly_table_update_start(): """Get 1st of current month""" yesterday = datetime.today() - timedelta(days=1) diff --git a/ingestor/chalicelib/landing.py b/ingestor/chalicelib/landing.py index 5f8015a..eb8be90 100644 --- a/ingestor/chalicelib/landing.py +++ b/ingestor/chalicelib/landing.py @@ -43,6 +43,23 @@ def get_ridership_data(): for line in constants.LINES: data = query_landing_ridership_data(constants.RIDERSHIP_KEYS[line]) ridership_object[line] = data + + ridership_object["line-commuter-rail"] = [None] * 10 + for line in constants.COMMUTER_RAIL_LINES: + data = query_landing_ridership_data(constants.commuter_rail_ridership_key(line)) + for index, week in enumerate(data): + if ridership_object["line-commuter-rail"][index] is None: + ridership_object["line-commuter-rail"][index] = week + continue + else: + data = { + "lineId": "line-commuter-rail", + "count": ridership_object["line-commuter-rail"][index]["count"] + week["count"], + "timestamp": week["timestamp"], + "date": week["date"], + } + ridership_object["line-commuter-rail"][index] = data + return ridership_object From f456da3b114a8cfbb27ca561191da1c42ee18b15 Mon Sep 17 00:00:00 2001 From: Devin Matte Date: Thu, 25 Jul 2024 19:53:07 -0400 Subject: [PATCH 2/2] Filter out null values --- ingestor/chalicelib/landing.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ingestor/chalicelib/landing.py b/ingestor/chalicelib/landing.py index eb8be90..b069101 100644 --- a/ingestor/chalicelib/landing.py +++ b/ingestor/chalicelib/landing.py @@ -44,6 +44,7 @@ def get_ridership_data(): data = query_landing_ridership_data(constants.RIDERSHIP_KEYS[line]) ridership_object[line] = data + # get data for commuter rail (treated as one line) ridership_object["line-commuter-rail"] = [None] * 10 for line in constants.COMMUTER_RAIL_LINES: data = query_landing_ridership_data(constants.commuter_rail_ridership_key(line)) @@ -59,6 +60,8 @@ def get_ridership_data(): "date": week["date"], } ridership_object["line-commuter-rail"][index] = data + # filter out None values + ridership_object["line-commuter-rail"] = [x for x in ridership_object["line-commuter-rail"] if x is not None] return ridership_object