-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe_02_realtime_accessibility.py
More file actions
71 lines (56 loc) · 2.61 KB
/
Copy pathe_02_realtime_accessibility.py
File metadata and controls
71 lines (56 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# This example includes a basic adapter for the MTA realtime elevator/escalator status feed, which is a plain JSON feed
# You are invited to explore other MTA accessbility feeds, which are listed here: https://api.mta.info/#/EAndEFeeds
from datetime import datetime, timedelta
import csp
from csp_mta import (
ACCESSIBILITY_ENDPOINT,
ADA_ACCESSIBLE_STATIONS,
MTA_FEED_UPDATE_TIME,
TOTAL_SUBWAY_STATIONS,
JSONRealtimeInputAdapter,
)
class OutageStats(csp.Struct):
num_elevators_out: int = 0
num_stations_no_longer_ADA_accessible: int = 0
average_downtime_per_outage: timedelta = timedelta(seconds=-1)
@csp.node
def elevator_outages(json_feed: csp.ts[object]) -> csp.ts[OutageStats]:
stats = OutageStats()
total_outage_time = timedelta()
for outage in json_feed:
if outage["equipmenttype"] == "EL" and outage["isupcomingoutage"] == "N":
# elevators, not escalators; only current, not planned outages
stats.num_elevators_out += 1
if outage["ADA"] == "N":
stats.num_stations_no_longer_ADA_accessible += 1
# record time of outage
date_format = "%m/%d/%Y %I:%M:%S %p"
start_of_outage = datetime.strptime(outage["outagedate"], date_format)
end_of_outage = datetime.strptime(
outage["estimatedreturntoservice"], date_format
)
total_outage_time += end_of_outage - start_of_outage
stats.average_downtime_per_outage = total_outage_time / stats.num_elevators_out
return stats
@csp.node
def repr_accessibility_stats(stats: csp.ts[OutageStats]) -> csp.ts[str]:
s = f"\nTotal elevator outages: {stats.num_elevators_out}\n"
s += f"ADA critical elevator outages: {stats.num_stations_no_longer_ADA_accessible}\n"
s += f"Realtime Accessible Stations: {ADA_ACCESSIBLE_STATIONS-stats.num_stations_no_longer_ADA_accessible} of {TOTAL_SUBWAY_STATIONS}\n"
s += f"Average Time per Outage: {stats.average_downtime_per_outage.days} days\n"
return s
@csp.graph
def realtime_accessibility_stats():
realtime_elevator_status = JSONRealtimeInputAdapter(
ACCESSIBILITY_ENDPOINT, MTA_FEED_UPDATE_TIME, False
)
current_elevator_outages = elevator_outages(realtime_elevator_status)
status = repr_accessibility_stats(current_elevator_outages)
csp.print("Current Accessibility Status", status)
if __name__ == "__main__":
csp.run(
realtime_accessibility_stats,
starttime=datetime.utcnow(),
endtime=timedelta(seconds=10),
realtime=True,
)