def remove_dead_events(self): notifications_ref = self.db.child( ParkingEventRepository._parking_event_notification_store_node_name) dead_notifications = notifications_ref\ .order_by_child('liveUntilTime')\ .start_at('0')\ .end_at(TimeUtils.get_epoch_timestamp_plus_seconds(0)).get() # a notification is considered dead if it is already stored to the long term data store OR never will be dead_notifications = [ (dn.key(), dn.val()) for dn in dead_notifications.each() if 'willBeStoredToLongTermDataStore' not in dn.val() or dn.val()['willBeStoredToLongTermDataStore'] == False ] for dn_id, dn in dead_notifications: # Remove dead events self.__remove_parking_events_from_ods_by( dn['parkingAreaId'], dn['registerNumber'], dn['parkingAreaParkingEvent']['timestamp']) # Remove dead notifications self.db.child(ParkingEventRepository._parking_event_notification_store_node_name)\ .child(dn_id)\ .remove()
def store_parking_event(self, request_json, live_time): register_number = request_json['registerNumber'] parking_context_type = request_json['parkingContextType'] timestamp = TimeUtils.get_local_timestamp() # parking_event_json is the json stored under parkingAreaParkingEvent node parking_event_json = { 'timestamp': timestamp, 'parkingType': parking_context_type, 'registerNumber': register_number, } if parking_context_type == 'PAID': parking_area_id = request_json['parkingAreaId'] parking_event_json['parkingDurationInMinutes'] = request_json[ 'parkingDurationInMinutes'] elif parking_context_type == 'PARKING_DISC': parking_area_id = 'PARKING_DISC_AREA' # Remove previous events from the ODS if any exist self.__remove_parking_event_from_ods_by(register_number) # Store the incoming event to ODS add_results = self.__add_parking_event_to_ods(parking_area_id, parking_event_json) # Store notification about the event for event consumption and clean up # > Notifications are stored in a flattened format # > Better use of indexing for server side event consumers notification_json = { 'parkingAreaId': parking_area_id, 'registerNumber': register_number, 'parkingEventId': add_results['odsId'], 'liveUntilTime': TimeUtils.get_epoch_timestamp_plus_seconds(live_time), 'parkingAreaParkingEvent': parking_event_json } # Only PAID context events are stored long term, because they are the only # types of events that one gets location information of. if parking_context_type == 'PAID': notification_json['willBeStoredToLongTermDataStore'] = True notification_add_results = self.__add_parking_event_to_notification_store( notification_json) add_results['notificationId'] = notification_add_results['name'] add_results['timestamp'] = timestamp return json.dumps(add_results)