11from ast import Return
2- #import imp -- MARCH 7 2026 .. removed this depricated core util (any errors?)
32from re import S
43from sqlalchemy import Column , String , Boolean , Integer , Float , func , and_
54from sqlalchemy .orm import relationship
@@ -54,11 +53,12 @@ class ProcessedRequests(Base):
5453 uselist = False , viewonly = True ,
5554 )
5655
57- def __init__ (self , raw_rec ):
56+ def __init__ (self , raw_rec , ignore_test_system = False ):
57+ #import pdb; pdb.set_trace()
5858 super (ProcessedRequests , self )
5959 self .log_id = raw_rec .id
6060 self .ip_hash = utils .obfuscate (raw_rec .ip )
61- self .app_name = self .get_app_name (raw_rec )
61+ self .app_name = self .get_app_name (raw_rec , ignore_test_system )
6262
6363 # TODO - refactor, this is a confusing mix of model and controller / parser
6464 try :
@@ -81,13 +81,76 @@ def __init__(self, raw_rec):
8181 self .parse_modes (modes )
8282 self .parse_companies (qs )
8383 self .apply_filters (raw_rec .url )
84+ self .check_response (raw_rec .response )
8485 except :
8586 self .filter_request = - 111
8687 log .debug ("couldn't parse " + raw_rec .url )
8788
89+ @classmethod
90+ def get_agency_map (cls , tm_only = False ):
91+ tm_map = {
92+ "TRIMET:TRAM" :"Aerial Tram" ,
93+ "TRIMET:PSC" :"Streetcar" ,
94+ "TRIMET:TRIMET" :"TriMet" ,
95+ }
96+ rtp_map = {
97+ "CLACKAMAS:ADULT" :"Clackamas" ,
98+ "CTRAN:ADULT" :"C-TRAN" ,
99+ "CTRAN_FLEX:ADULT" :"The Current" ,
100+ "MULT:ADULT" :"Multnomah" ,
101+ "RIDECONNECTION:ADULT" :"Ride Connection" ,
102+ "SAM:ADULT" :"SAM" ,
103+ "SMART:ADULT" :"SMART" ,
104+ "WASH_FLEX" :"SPOT" ,
105+ "WAPARK" :"Washington Park" ,
106+ }
107+
108+ if tm_only :
109+ agency_map = tm_map
110+ else :
111+ agency_map = tm_map | rtp_map
112+
113+ return agency_map
114+
115+ def check_response (self , response ):
116+ def find_agencies ():
117+ agency_map = self .get_agency_map ()
118+ ag = []
119+ for ak in agency_map .keys ():
120+ if ak in response :
121+ ag .append (agency_map .get (ak ))
122+
123+ ret_val = "" if len (ag ) <= 0 else "," .join (ag )
124+ return ret_val
125+
126+ def filter_modes (def_mode = "WALK" ):
127+ #import pdb; pdb.set_trace()
128+ m = self .modes
129+ if "BUS" not in response : m = m .replace ('BUS' , '' )
130+ if not utils .is_match_any (["RAIL" , "SUBWAY" , "TRAIN" , "TRAM" , "GONDOLA" ], response ): m = m .replace ('RAIL' , '' )
131+ if not utils .is_match_any (["CALL_AGENCY" , "COORDINATE_WITH_DRIVER" ], response ): m = m .replace ('FLEX' , '' )
132+ m = m .replace (',,' , ',' )
133+ m = m .strip (",$" )
134+ if m is None or m == "" or m == "," :
135+ m = def_mode
136+ return m
137+
138+ #import pdb; pdb.set_trace()
139+ if response :
140+ if '"itineraries":[{' in response :
141+ self .agencies = find_agencies ()
142+ self .modes = filter_modes ()
143+ elif utils .is_match_all (['errors":[{"message"' ], response ):
144+ self .agencies = None
145+ elif utils .is_match_all (['"itineraries":[]' , 'routingErrors' , 'code' ], response ):
146+ self .agencies = None
147+ elif utils .is_match_all (['"itineraries":[]' , 'routingErrors' ], response ):
148+ self .agencies = None
149+
150+
88151 def apply_filters (self , url , fltval = - 222 ):
89152 """ filter out uptime test urls, etc... """
90- #import pdb; pdb.set_trace()
153+ #import pdb; pdb.set_trace()
91154 if self .filter_request is None :
92155 if 'fromPlace=PDX' in url and ('toPlace=ZOO' in url or 'toPlace=SW%20Zoo%20Rd' in url ):
93156 self .filter_request = fltval
@@ -116,7 +179,7 @@ def apply_filters(self, url, fltval=-222):
116179 self .filter_request = fltval + 55
117180
118181 @classmethod
119- def get_app_name (cls , rec , def_val = "no idea what app..." ):
182+ def get_app_name (cls , rec , ignore_test_system = False , def_val = "no idea what app..." ):
120183 """ trimet specific -- override me for other agencies / uses """
121184 app_name = def_val
122185
@@ -136,7 +199,7 @@ def get_app_name(cls, rec, def_val="no idea what app..."):
136199
137200 if len (rec .referer ) > 3 :
138201 referer = rec .referer .lower ()
139- if 'localhost:8000' in referer or 'labs' in referer or 'test.trimet' in referer :
202+ if ignore_test_system is False and ( 'localhost:8000' in referer or 'labs' in referer or 'test.trimet' in referer ) :
140203 app_name = TEST_SYSTEM
141204 elif 'call-test' in referer :
142205 app_name = call2
@@ -158,7 +221,7 @@ def get_app_name(cls, rec, def_val="no idea what app..."):
158221 elif utils .is_old_trimet (rec .url ):
159222 app_name = old
160223
161- if utils .is_developer_api (rec .url ):
224+ if ignore_test_system is False and utils .is_developer_api (rec .url ):
162225 rec .is_api = True
163226 if app_name is def_val :
164227 app_name = api
@@ -222,26 +285,7 @@ def parse_agencies(self, qs, tm_only=False):
222285 return the list of agencies implied in the request
223286 will look at the banned agencies param, and trim the list of possible request agencies
224287 """
225- tm_map = {
226- "TRIMET:TRAM" :"Aerial Tram" ,
227- "TRIMET:PSC" :"Streetcar" ,
228- "TRIMET:TRIMET" :"TriMet" ,
229- }
230- rtp_map = {
231- "CLACKAMAS" :"Clackamas" ,
232- "CTRAN" :"C-TRAN" ,
233- "CTRAN_FLEX" :"The Current" ,
234- "MULT" :"Multnomah" ,
235- "RIDECONNECTION:" :"Ride Connection" ,
236- "SAM" :"SAM" ,
237- "SMART" :"SMART" ,
238- "WASH_FLEX" :"SPOT" ,
239- "WAPARK" :"Washington Park" ,
240- }
241- if tm_only :
242- agency_map = tm_map
243- else :
244- agency_map = tm_map | rtp_map
288+ agency_map = self .get_agency_map (tm_only )
245289
246290 # filter banned agencies from the above list
247291 for b in utils .get_banned_agencies (qs ):
@@ -322,6 +366,7 @@ def to_csv_dict(self):
322366 - request datetime
323367 - ???
324368 """
369+ #import pdb; pdb.set_trace()
325370 ua = utils .clean_useragent (self .log .browser )
326371 browser = utils .get_browser (ua )
327372 url = utils .to_url (self .log )
@@ -347,7 +392,7 @@ def to_csv_dict(self):
347392 return ret_val
348393
349394 @classmethod
350- def process (cls , chunk_size = 10000 ):
395+ def process (cls , chunk_size = 10000 , ignore_test_system = False ):
351396 """
352397 process logs from log file(s)
353398 """
@@ -361,7 +406,7 @@ def process(cls, chunk_size=10000):
361406 # step 2: loop thru raw log file entries
362407 processed = []
363408 for l in logs :
364- p = ProcessedRequests (l )
409+ p = ProcessedRequests (l , ignore_test_system )
365410 processed .append (p )
366411 # step 2b: save off the post-process data in 'chunks'
367412 if len (processed ) > chunk_size :
0 commit comments