def add_prev_stop(tracker_id, stop_id, timestamp): next_id = cl.incr(get_train_tracker_prev_stops_counter_key(tracker_id)) #p = get_redis_pipeline() #p.set("train_tracker:%s:%d:stop_id:" % (tracker_id, next_id), stop_id) unix_timestamp = ot_utils.dt_time_to_unix_time(timestamp) p.zadd(get_train_tracker_timestamp_sorted_stop_ids_key(tracker_id), unix_timestamp, "%d_%s" % (next_id, stop_id)) p.zremrangebyrank(get_train_tracker_timestamp_sorted_stop_ids_key(tracker_id), 0, -HISTORY_LENGTH-1) p.execute() #p.set("train_tracker:%s:%d:timestamp:" % (tracker_id, next_id), timestamp) #p.execute() return next_id
def try_update_coords(report, tracker_id): loc = report.get_my_loc() coords = [loc.lat, loc.lon] res_shape_sampled_point_ids, _ = shapes.all_shapes.query_sampled_points(coords, loc.accuracy_in_coords) added_count = cl.sadd(get_train_tracker_visited_shape_sampled_point_ids_key(tracker_id), res_shape_sampled_point_ids) trips = load_by_key(get_train_tracker_trip_ids_key(tracker_id)) trip = trips[0] if trips else None if added_count > 0: save_by_key(get_train_tracker_coords_key(tracker_id), coords, cl=p) if trip is not None and len(trips) <= TRACKER_REPORT_FOR_TRIP_COUNT_LOWER_THAN: save_by_key(get_current_trip_id_coords_key(trip), coords, timeout=TRACKER_TTL, cl=p) save_by_key(get_current_trip_id_coords_timestamp_key(trip), ot_utils.dt_time_to_unix_time(report.timestamp), timeout=TRACKER_TTL, cl=p) p.execute() if trip is not None: cl.setex(get_current_trip_id_report_timestamp_key(trip), TRACKER_TTL, ot_utils.dt_time_to_unix_time(report.timestamp))
def _update_stop_time(tracker_id, arrival_timestamp, stop_id, departure_time, prev_stop_time, is_report_timegap=False): if not prev_stop_time: prev_stop_time = get_last_detected_stop_time(tracker_id) if prev_stop_time and prev_stop_time.stop_id == stop_id and not is_report_timegap: arrival_timestamp = prev_stop_time.arrival arrival_unix_timestamp = int(ot_utils.dt_time_to_unix_time(arrival_timestamp)) departure_time = departure_time.isoformat() if departure_time else None stop_id_and_departure_time = json.dumps((str(stop_id), departure_time)) p.zremrangebyscore(get_train_tracker_tracked_stop_times_key( tracker_id), arrival_unix_timestamp, arrival_unix_timestamp) p.zadd(get_train_tracker_tracked_stop_times_key(tracker_id), arrival_unix_timestamp, stop_id_and_departure_time)
def update_coords(report, tracker_id): loc = report.get_my_loc() coords = [loc.lat, loc.lon] res_shape_sampled_point_ids, _ = shapes.all_shapes.query_sampled_points(coords, loc.accuracy_in_coords) added_count = cl.sadd(get_train_tracker_visited_shape_sampled_point_ids_key(tracker_id), res_shape_sampled_point_ids) trips, time_deviation_in_seconds = get_trips(tracker_id) trip = get_trusted_trip_or_none(trips, time_deviation_in_seconds) if trip: cl.setex(get_current_trip_id_report_timestamp_key(trip), TRACKER_TTL, ot_utils.dt_time_to_unix_time(report.timestamp)) if added_count > 0: save_by_key(get_current_trip_id_coords_key(trip),\ coords,\ timeout=TRACKER_TTL, cl=p) save_by_key(get_current_trip_id_coords_timestamp_key(trip),\ ot_utils.dt_time_to_unix_time(report.timestamp),\ timeout=TRACKER_TTL, cl=p) else: # extend expiration: p.expire(get_current_trip_id_coords_key(trip), TRACKER_TTL) p.expire(get_current_trip_id_coords_timestamp_key(trip), TRACKER_TTL) p.execute()
def update_coords(report, tracker_id): loc = report.get_my_loc() coords = [loc.lat, loc.lon] logger.info('Updating coords for tracker_id={} by report={} to coords={}'.format(tracker_id, report, coords)) res_shape_sampled_point_ids, _ = shapes.all_shapes.query_sampled_points(coords, loc.accuracy_in_coords) added_count = cl.sadd(get_train_tracker_visited_shape_sampled_point_ids_key(tracker_id), res_shape_sampled_point_ids) trip_delays_ids_list_of_lists = load_by_key(get_train_tracker_trip_delays_ids_list_of_lists_key(tracker_id)) trips = get_trusted_trips(trip_delays_ids_list_of_lists) for trip in trips: cl.setex(get_current_trip_id_report_timestamp_key(trip), TRACKER_TTL, ot_utils.dt_time_to_unix_time(report.timestamp)) if added_count > 0: save_by_key(get_current_trip_id_coords_key(trip),\ coords,\ timeout=TRACKER_TTL, cl=p) save_by_key(get_current_trip_id_coords_timestamp_key(trip),\ ot_utils.dt_time_to_unix_time(report.timestamp),\ timeout=TRACKER_TTL, cl=p) else: # extend expiration: p.expire(get_current_trip_id_coords_key(trip), TRACKER_TTL) p.expire(get_current_trip_id_coords_timestamp_key(trip), TRACKER_TTL) p.execute()
def add_report_to_tracker(tracker_id, report): if not load_by_key(get_train_tracker_relevant_services_key(tracker_id)): start_date = report.timestamp.strftime("%Y-%m-%d") relevant_services = gtfs.models.Service.objects.filter(start_date = start_date) relevant_service_ids = [x[0] for x in relevant_services.all().values_list('service_id')] save_by_key(get_train_tracker_relevant_services_key(tracker_id), relevant_service_ids) # update train position if report.get_my_loc(): try_update_coords(report, tracker_id) ##commented out below is code that filters trips based on shape #coords_updated = False #p = get_redis_pipeline() #p.zincrby("train_tracker:%s:counters" % (tracker_id), res_shape_ids[i], inc_by) #p.incr("train_tracker:%s:total" % (tracker_id), inc_by) #p.execute() #for i in xrange(len(res_shape_point_ids)): #cl = get_redis_client() #if cl.sadd("train_tracker:%s:visited_shape_point_ids" % (tracker_id), res_shape_point_ids[i]) == 0: #if not coords_updated: #update if report adds new points on tracks #self.coords = coords #coords_updated = True #p = get_redis_pipeline() #p.zincrby("train_tracker:%s:counters" % (tracker_id), res_shape_ids[i], 1) #p.incr("train_tracker:%s:total" % (tracker_id)) #p.execute() ##self.shape_counts[res_shape_ids[i]] = self.shape_counts[res_shape_ids[i]] + 1 # 1) add stop or non-stop to prev_stops and prev_stops_timestamps # 2) set calc_hmm to true if according to wifis and/or location, our state changed from stop to non-stop or vice versa prev_current_stop_id_by_hmm = cl.get(get_train_tracker_current_stop_id_key(tracker_id)) if not prev_current_stop_id_by_hmm: prev_state = tracker_states.INITIAL elif prev_current_stop_id_by_hmm == stops.NOSTOP: prev_state = tracker_states.NOSTOP else: prev_state = tracker_states.STOP stop_id = try_get_stop_id(report) if not stop_id: current_state = tracker_states.UNKNOWN elif stop_id == nostop_id: current_state = tracker_states.NOSTOP else: current_state = tracker_states.STOP if current_state != tracker_states.UNKNOWN: timestamp = report.get_timestamp_israel_time() prev_stop_id = add_prev_stop(tracker_id, stop_id, timestamp) # calculate hmm to get state_sequence, update stop_times and current_stop if needed if current_state != tracker_states.UNKNOWN and prev_state != current_state: prev_stops_and_timestamps = cl.zrange(get_train_tracker_timestamp_sorted_stop_ids_key(tracker_id), 0, -1, withscores=True) prev_stop_ids_order = [int(x[0].split("_")[0]) for x in prev_stops_and_timestamps] prev_stops_and_timestamps = [x for (y,x) in sorted(zip(prev_stop_ids_order,prev_stops_and_timestamps))] prev_stop_ids = [x[0].split("_")[1] for x in prev_stops_and_timestamps] prev_stop_int_ids = np.array([stops.all_stops.id_list.index(x) for x in prev_stop_ids]) #assert np.array_equal(prev_stop_int_ids, np.array(self.prev_stops)) prev_stop_hmm_logprob, prev_stop_int_ids_by_hmm = hmm.decode(prev_stop_int_ids) prev_stop_int_ids_by_hmm_for_debug = prev_stop_int_ids_by_hmm # update current_stop_id_by_hmm and current_state by hmm: current_stop_id_by_hmm = stops.all_stops.id_list[prev_stop_int_ids_by_hmm[-1]] cl.set(get_train_tracker_current_stop_id_key(tracker_id), current_stop_id_by_hmm) if current_stop_id_by_hmm == stops.NOSTOP: current_state = tracker_states.NOSTOP else: current_state = tracker_states.STOP if prev_state != current_state: # change in state prev_stops_by_hmm = [stops.all_stops.id_list[x] for x in prev_stop_int_ids_by_hmm] prev_stops_timestamps = [ot_utils.unix_time_to_localtime((x[1])) for x in prev_stops_and_timestamps] index_of_oldest_current_state = max(0, find_index_of_first_consecutive_value(prev_stops_by_hmm, len(prev_stops_by_hmm)-1)) index_of_most_recent_previous_state = index_of_oldest_current_state-1 if current_state == tracker_states.NOSTOP: stop_id = prev_stops_by_hmm[index_of_most_recent_previous_state] unix_timestamp = ot_utils.dt_time_to_unix_time(prev_stops_timestamps[index_of_most_recent_previous_state]) if prev_state == tracker_states.INITIAL: pass #do nothing else: # previous_state == tracker_states.STOP - need to set stop_time departure stop_time = cl.zrange(get_train_tracker_tracked_stops_key(tracker_id), -1, -1, withscores=True) departure_unix_timestamp = unix_timestamp stop_id_and_departure_time = "%s_%d" % (prev_current_stop_id_by_hmm, departure_unix_timestamp) update_stop_time(tracker_id, prev_stop_id, stop_time[0][1], stop_id_and_departure_time) update_trips(tracker_id) else: # current_state == tracker_states.STOP stop_id = prev_stops_by_hmm[index_of_oldest_current_state] unix_timestamp = ot_utils.dt_time_to_unix_time(prev_stops_timestamps[index_of_oldest_current_state]) arrival_unix_timestamp = unix_timestamp stop_id_and_departure_time = "%s_" % (current_stop_id_by_hmm) update_stop_time(tracker_id, prev_stop_id, arrival_unix_timestamp, stop_id_and_departure_time) update_trips(tracker_id) prev_timestamp = unix_timestamp print_tracked_stop_times(tracker_id)
def add_report(tracker_id, report): # 1) add stop or non-stop to prev_stops and prev_stops_timestamps # 2) set calc_hmm to true if according to wifis and/or location, our # state changed from stop to non-stop or vice versa prev_current_stop_id_by_hmm = cl.get(\ get_train_tracker_current_stop_id_key(tracker_id)) prev_current_stop_id_by_hmm = int(prev_current_stop_id_by_hmm) if\ prev_current_stop_id_by_hmm else None if not prev_current_stop_id_by_hmm: prev_state = tracker_states.INITIAL else: prev_state = prev_current_stop_id_by_hmm stop_id = try_get_stop_id(report) if not stop_id: current_state = tracker_states.UNKNOWN else: current_state = stop_id if current_state != tracker_states.UNKNOWN: timestamp = report.get_timestamp_israel_time() prev_stop_id = add_prev_stop(tracker_id, stop_id, timestamp) # calculate hmm to get state_sequence, update stop_times and current_stop if needed if current_state != tracker_states.UNKNOWN and prev_state != current_state: prev_stops_and_timestamps = cl.zrange(get_train_tracker_timestamp_sorted_stop_ids_key(tracker_id), 0, -1, withscores=True) prev_stop_ids_order = [int(x[0].split("_")[0]) for x in prev_stops_and_timestamps] prev_stops_and_timestamps = [x for (y,x) in sorted(zip(prev_stop_ids_order,prev_stops_and_timestamps))] prev_stop_ids = [int(x[0].split("_")[1]) for x in prev_stops_and_timestamps] prev_stop_int_ids = np.array([stops.all_stops.id_list.index(x) for x in prev_stop_ids]) #assert np.array_equal(prev_stop_int_ids, np.array(self.prev_stops)) prev_stop_hmm_logprob, prev_stop_int_ids_by_hmm = hmm.decode(prev_stop_int_ids) prev_stop_int_ids_by_hmm_for_debug = prev_stop_int_ids_by_hmm # update current_stop_id_by_hmm and current_state by hmm: current_stop_id_by_hmm = stops.all_stops.id_list[prev_stop_int_ids_by_hmm[-1]] cl.set(get_train_tracker_current_stop_id_key(tracker_id), current_stop_id_by_hmm) current_state = current_stop_id_by_hmm if prev_state != current_state: # change in state prev_stops_by_hmm = [stops.all_stops.id_list[x] for x in prev_stop_int_ids_by_hmm] prev_stops_timestamps = [ot_utils.unix_time_to_localtime((x[1])) for x in prev_stops_and_timestamps] index_of_oldest_current_state = max(0, find_index_of_first_consecutive_value(prev_stops_by_hmm, len(prev_stops_by_hmm)-1)) index_of_most_recent_previous_state = index_of_oldest_current_state-1 if current_state == stops.NOSTOP: stop_id = prev_stops_by_hmm[index_of_most_recent_previous_state] unix_timestamp = ot_utils.dt_time_to_unix_time(prev_stops_timestamps[index_of_most_recent_previous_state]) if prev_state == tracker_states.INITIAL: pass #do nothing else: # previous_state == tracker_states.STOP - need to set stop_time departure stop_time = cl.zrange(get_train_tracker_tracked_stops_key(tracker_id), -1, -1, withscores=True) departure_unix_timestamp = unix_timestamp stop_id_and_departure_time = "%s_%d" % (prev_current_stop_id_by_hmm, departure_unix_timestamp) update_stop_time(tracker_id, prev_stop_id, stop_time[0][1], stop_id_and_departure_time) else: # current_state == tracker_states.STOP arrival_unix_timestamp_prev_stop = None stop_id_and_departure_time_prev_stop = None if (prev_state != tracker_states.INITIAL and prev_state != stops.NOSTOP): last_prev_end_ind = len(prev_stops_by_hmm) - prev_stops_by_hmm[::-1].index(prev_state) - 1 stop_time = cl.zrange(get_train_tracker_tracked_stops_key(tracker_id), -1, -1, withscores=True) departure_unix_timestamp = ot_utils.dt_time_to_unix_time(prev_stops_timestamps[last_prev_end_ind]) stop_id_and_departure_time = "%s_%d" % (prev_current_stop_id_by_hmm, departure_unix_timestamp) arrival_unix_timestamp_prev_stop = stop_time[0][1] stop_id_and_departure_time_prev_stop = stop_id_and_departure_time stop_id = prev_stops_by_hmm[index_of_oldest_current_state] unix_timestamp = ot_utils.dt_time_to_unix_time(prev_stops_timestamps[index_of_oldest_current_state]) arrival_unix_timestamp = unix_timestamp stop_id_and_departure_time = "%s_" % (current_stop_id_by_hmm) update_stop_time(tracker_id, prev_stop_id, arrival_unix_timestamp, stop_id_and_departure_time, arrival_unix_timestamp_prev_stop, stop_id_and_departure_time_prev_stop) prev_timestamp = unix_timestamp stop_times = get_detected_stop_times(tracker_id) is_stops_updated = (prev_state != current_state) and len(stop_times) > 0 return stop_times, is_stops_updated