-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3_alerts_cr.py
204 lines (154 loc) · 7.48 KB
/
s3_alerts_cr.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import json
from chalicelib import MbtaPerformanceAPI, s3
def routes_for_alert(alert):
routes = set()
try:
for alert_version in alert["alert_versions"]:
for informed_entity in alert_version["informed_entity"]:
if "route_id" in informed_entity:
routes.add(informed_entity["route_id"])
except KeyError as e:
print(f"Handled KeyError: Couldn't access {e} from alert {alert}")
return routes
def key(day):
return f"Alerts/{str(day)}.json.gz"
def get_alerts(day, routes):
alerts_str = s3.download(key(day), "utf8")
alerts = json.loads(alerts_str)[0]["past_alerts"]
def matches_route(alert):
targets = routes_for_alert(alert)
return any(r in targets for r in routes)
return list(filter(matches_route, alerts))
def store_alerts(day):
api_data = MbtaPerformanceAPI.get_api_data("pastalerts", {}, day)
alerts = json.dumps(api_data).encode("utf8")
s3.upload(key(day), alerts, True)
##========= == Testing get alerts with start date as 10/1 and end date as today - timedelta(days = 2) == ========##
# set date and time
import datetime
import sys
from datetime import date
from datetime import timedelta
# alt_names = [Framingham/Worcester, Middleborough/Lakeville, Newburyport/Rockport, Franklin/Foxboro, Providence/Stoughton,
# Foxboro Event Service]
#cr_lines = ["CR-Fairmount", "CR-Framingham/Worcester", "CR-Greenbush", "CR-Kingston", "CR-Middleborough/Lakeville", "CR-Newburyport/Rockport", "CR-Fitchburg", "CR-Franklin/Foxboro", "CR-Haverhill", "CR-Lowell", "CR-Needham", "CR-Providence/Stoughton", "CR-Foxboro Event Service"]
cr_lines = ["CR-Fairmount", "CR-Franklin", "CR-Framingham/Worcester", "CR-Greenbush", "CR-Kingston", "CR-Middleborough/Lakeville", "CR-Newburyport/Rockport", "CR-Fitchburg", "CR-Franklin/Foxboro", "CR-Haverhill", "CR-Lowell","CR-Needham","CR-Providence/Stoughton", "CR-Foxboro Event Service"] # "CR-Fairmount",
canned_reasons = ["signal issue", "train traffic", "mechanical issue", "late arrival of equipment", "speed restriction", "draw bridge issue", "drawbridge issue", "crew availability issue", "late arrival of train", "bus traffic", "street traffic", "bus/street traffic", "passenger accommodation", "disabled bus", "accident", "track work", "fire or police dept. activity"]
"""
def cr_loop(cr_lines, action_to_do):
iterator = iter(cr_lines)
done_looping = False
while not done_looping:
try:
item = next(iterator)
except StopIteration:
done_looping = True
else:
action_to_do(item)
"""
# for line in cr_lines:
# iterator = iter(cr_lines)
# while start_date <= end_date:
# item = next(l_iter, "end")
# alerts = (get_alerts(start_date, [line]))
# start_date += delta
# alerts = (get_alerts(start_date, [line]))
# start_date += delta
#alerts = (get_alerts("2022-09-12", ["CR-Franklin"]))
# def delay_reasons():
start_date = datetime.date(2022, 10, 1)
end_date = date.today() - datetime.timedelta(days = 2) # datetime.date(2022, 10, 19)
# delta time
delta = datetime.timedelta(days=1)
#cr_iter = iter(cr_lines)
import pandas as pd
a = pd.DataFrame()
df = pd.DataFrame(columns=["line_name","alert_ids","full_text","start_time","end_time","reasons_set", "mins_delay"])
# i < 0
# while i < len(cr_lines):
while start_date <= end_date:
for line in cr_lines:
print(line)
alerts = (get_alerts(start_date, [line]))
#start_date += delta
for alert in alerts:
#df['line'].concat(line)
#a.append(alert)
#df.append(pd.DataFrame([line], columns=['line']))
print(type(alert))
row = {}
row['line_name'] = line
print(alert["alert_id"])
row['alert_ids'] = (alert["alert_id"])
full_text = ""
end_time = 0
start_time = 9999999999
for version in alert["alert_versions"]:
full_text = full_text + " " + version["header_text"]
if (len(version["active_period"]) > 0):
start = int(version["active_period"][0]["start"])
print(version["active_period"][0])
end = int(version['active_period'][0]['end']) if 'end' in version['active_period'][0] else sys.maxsize
if start < start_time:
start_time = start
if end > end_time:
end_time = end
print(full_text)
row['full_text'] = full_text
# df['full_text'].append(full_text)
print(start_time)
try:
row['start_time'] = datetime.datetime.fromtimestamp(start_time)
except:
print("An exception occurred")
# df['start_time'].append(start_time)
print(end_time)
try:
row['end_time'] = datetime.datetime.fromtimestamp(end_time)
except:
print("An exception occurred")
if "late" or "behind schedule" in full_text.lower():
# might want to add a set of delay range in the alert
reasons_set = set()
for reason in canned_reasons:
if reason in full_text.lower():
reasons_set.add(reason)
print(reasons_set)
row['reasons_set'] = str(reasons_set)
row['reasons_set'] = row['reasons_set'].replace('set()', 'None')
#split_row = pd.DataFrame(row['reasons_set'].tolist())
#row = pd.append([row, split_row], axis=1)
#row[['reason_1', 'reason_2', 'reason_3']] = row['reasons_set'].apply(lambda x: pd.Series(str(x).split(",")))
reasons_list = list(reasons_set)
list_length = len(reasons_list)
for i in range(len(reasons_list)):
print(reasons_list[i])
row["reason_"+str(i)] = reasons_list[i]
for delay in row['full_text']:
if 'minutes' in row['full_text']:
delay = row['full_text']
delay2 = delay.split(' minutes')[:-1]
delay2 = str(delay2)
delay3 = delay2.rsplit(' ',1)[1]
delay3 = delay3.replace("']", '')
row['mins_delay'] = delay3
#for index in reasons_list:
# row["reason_"+index] = reasons_list[index]
#row[['reason_1','reason_2']]=row['reasons_set'].str.split(',',expand=True)
#expand = row['reasons_set'].apply(pd.Series)
#row = row.append(expand)
#row = pd.DataFrame(row['reasons_set'].tolist()).add_prefix('reason_') # last piece
#if reasons_set is not None:
# row['reasons_set'] = str(reasons_set)
#else:
# row['reasons_set'] = 'None'
print(row)
# df.join(df['reasons_set'].apply(pd.Series)) # last piece
df = df.append(row, ignore_index=True)
start_date += delta
print('==========')
print(df)
df.to_csv('cr_delay.csv', encoding = 'utf-8')
#else next(cr_iter)
# run function
#cr_loop(cr_lines, delay_reasons())