Skip to content

Commit

Permalink
good stopping point
Browse files Browse the repository at this point in the history
  • Loading branch information
idreyn committed Sep 2, 2024
1 parent 76ec75d commit f63cf80
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 644 deletions.
2 changes: 1 addition & 1 deletion dash.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ingestor/chalicelib/service_ridership_dashboard/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dash.json
90 changes: 50 additions & 40 deletions ingestor/chalicelib/service_ridership_dashboard/generate.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,51 @@
import json
from datetime import date, datetime, timedelta
from typing import TypedDict, Literal
from typing import cast
from pathlib import PurePath

from .config import (
START_DATE,
TIME_ZONE,
PRE_COVID_DATE,
)
from .service_levels import get_service_level_entries_by_line_id, ServiceLevelsByDate, ServiceLevelsEntry
from .ridership import ridership_by_line_id, RidershipByDate, RidershipEntry
from .service_summaries import ServiceSummary, summarize_weekly_service_around_date
from .ridership import ridership_by_line_id, RidershipEntry
from .service_summaries import summarize_weekly_service_around_date
from .util import date_to_string
from .time_series import get_time_series
from .summary import get_summary_data
from .types import ServiceRegimes, LineData, DashJSON, LineKind

LineKind = Literal["bus", "subway", "commuter-rail", "ferry"]
parent_dir = PurePath(__file__).parent
debug_file_name = parent_dir / "dash.json"


class ServiceRegimes(TypedDict):
present: ServiceSummary
oneYearAgo: ServiceSummary
preCovid: ServiceSummary


class LineData(TypedDict):
id: str
shortName: str
longName: str
routeIds: list[str]
startDate: str
lineKind: LineKind
ridershipHistory: list[float]
serviceHistory: list[float]
serviceRegimes: ServiceRegimes


class DashJSON(TypedDict):
lineData: dict[str, LineData]
def get_line_kind(route_ids: list[str], line_id: str) -> LineKind:
if line_id.startswith("line-Boat"):
return "boat"
if any((r for r in route_ids if r.lower().startswith("cr-"))):
return "regional-rail"
if line_id.startswith("line-SL"):
return "silver"
if line_id in ("line-Red", "line-Orange", "line-Blue", "line-Green"):
return cast(LineKind, line_id.split("-")[1].lower())
return "bus"


def create_service_regimes(
service_levels: ServiceLevelsByDate,
date: date,
) -> ServiceRegimes:
return {
"present": summarize_weekly_service_around_date(
"current": summarize_weekly_service_around_date(
date=date,
service_levels=service_levels,
),
"oneYearAgo": summarize_weekly_service_around_date(
date=date - timedelta(days=365),
service_levels=service_levels,
),
"preCovid": summarize_weekly_service_around_date(
"baseline": summarize_weekly_service_around_date(
date=PRE_COVID_DATE,
service_levels=service_levels,
),
Expand All @@ -72,7 +66,10 @@ def create_line_data(
"longName": service_level_entry.line_long_name,
"routeIds": service_level_entry.route_ids,
"startDate": date_to_string(start_date),
"lineKind": "bus",
"lineKind": get_line_kind(
route_ids=service_level_entry.route_ids,
line_id=service_level_entry.line_id,
),
"ridershipHistory": get_time_series(
entries=ridership,
entry_value_getter=lambda entry: entry.ridership,
Expand All @@ -81,7 +78,7 @@ def create_line_data(
),
"serviceHistory": get_time_series(
entries=service_levels,
entry_value_getter=lambda entry: sum(entry.service_levels),
entry_value_getter=lambda entry: round(sum(entry.service_levels)),
start_date=start_date,
max_end_date=end_date,
),
Expand All @@ -95,31 +92,44 @@ def create_line_data(
def create_service_ridership_dash_json(
start_date: date = START_DATE,
end_date: date = datetime.now(TIME_ZONE).date(),
write_debug_file: bool = False,
):
with open("./dash.json", "w") as f:
json.dump({"hi": 3}, f)
service_level_entries = get_service_level_entries_by_line_id(
start_date=start_date,
end_date=end_date,
)
print("service_level_entries", service_level_entries)
ridership_entries = ridership_by_line_id(
start_date=start_date,
end_date=end_date,
line_ids=list(service_level_entries.keys()),
)
line_data_by_line_id = {
line_id: create_line_data(
start_date=start_date,
end_date=end_date,
service_levels=service_level_entries[line_id],
ridership=ridership_entries[line_id],
)
for line_id in service_level_entries.keys()
if service_level_entries[line_id]
and ridership_entries[line_id]
and len(service_level_entries[line_id])
and len(ridership_entries[line_id])
}
summary_data = get_summary_data(
line_data=list(line_data_by_line_id.values()),
start_date=start_date,
end_date=end_date,
)
dash_json: DashJSON = {
"lineData": {
line_id: create_line_data(
start_date=start_date,
end_date=end_date,
service_levels=service_level_entries[line_id],
ridership=ridership_entries[line_id],
)
for line_id in service_level_entries.keys()
},
"summaryData": summary_data,
"lineData": line_data_by_line_id,
}
with open("dash.json", "w") as f:
with open(debug_file_name, "w") as f:
json.dump(dash_json, f)


if __name__ == "__main__":
create_service_ridership_dash_json()
create_service_ridership_dash_json(write_debug_file=True)
Loading

0 comments on commit f63cf80

Please sign in to comment.