def get(self): increase_counter('req_trajectory') origin_id = self.request.get('from') if not self.validateID(origin_id): return destination_id = self.request.get('to') if not self.validateID(destination_id): return time_string = self.request.get('start', None) if time_string is None: start_time = now_cet() - timedelta(hours=1) else: if not self.validateDatetime(time_string): return start_time = cet_from_string(time_string) span_string = self.request.get('span', None) if span_string is None: time_span = timedelta(hours=3) else: if not self.validateDigit(span_string): return time_span = timedelta(hours=int(span_string)) output_string = json.dumps( self.trajectory_dict(origin_id, destination_id, start_time, time_span)) self.response.out.write(output_string)
def get(self): increase_counter('req_mission') mission_id = self.request.get('id') if not self.validateID(mission_id): return mission = TAMission.get(mission_id) if not mission: self.reject() return output_string = json.dumps(mission.repr) self.response.out.write(output_string)
def perform_check(self, mission_id, now, expected, test_file=None): logging.info('Check stop of mission %s' % mission_id) comps = mission_id.split('.') stop_code = '%s_%s' % (comps[1], self.code) stop = self.stops_dictionary.get(stop_code) if stop: increase_counter('req_check_confirmed') self.forward_changed_stops([stop], now) else: if self.last_departure is not None and expected < self.last_departure: increase_counter('req_check_denied') self.forward_changed_stops( [TAStop.revoked_stop(mission_id, self.station_id)], now) else: self.updated = now changed_stops = self.changed_stops(test_file) if changed_stops is not None: if not self.stops_dictionary.get(stop_code): increase_counter('req_check_revoked') logging.warning('stop was revoked') changed_stops.append( TAStop.revoked_stop(mission_id, self.station_id)) else: increase_counter('req_check_refetched') self.forward_changed_stops(changed_stops, now) else: logging.warning('No stops were fetched')
def execute_request(self, request): instruction = request.get('inst') now_string = request.get('now', None) if now_string: now = cet_from_string(now_string) test_file = request.get('file', 'ignore') else: now = now_cet() test_file = None if instruction == 'avt': if self.answer_avt(now): increase_counter('req_avt_answered') self.perform_avt(now, test_file) else: increase_counter('req_avt_denied') elif instruction == 'prio': if self.answer_prio(now): increase_counter('req_prio_answered') self.perform_avt(now, test_file) else: increase_counter('req_prio_denied') elif instruction == 'check': mission_id = request.get('sender') exp_s = request.get('expected') if exp_s: expected = cet_from_string(exp_s) else: expected = now_cet() + timedelta(days=1) self.perform_check(mission_id, now, expected, test_file) elif instruction == 'console': self.perform_avt(now, test_file)
def save_objects(self): for mission_code in self.replaced_mission_codes: stop_code = '%s_%s' % (mission_code, self.delegate.code) if not self.new_objects.get(stop_code, None): stop = self.pop_from_old_objects(stop_code) if stop: stop.status = StopStatuses.canceled self.updated_objects[stop_code] = stop self.new_objects[stop_code] = stop self.delegate.stops_dictionary = self.new_objects self.delegate.nr_of_fetches += 1 if hasattr(self, 'last_departure'): self.delegate.last_departure = self.last_departure increase_counter('req_api_success') self.delegate.cache_set()
def get(self): increase_counter('req_departures') series_id = self.request.get('series') if not self.validateID(series_id): self.reject() return origin_id = self.request.get('from') if not self.validateID(origin_id): self.reject() return direction_string = self.request.get('dir') if direction_string == 'up': direction = Direction.up elif direction_string == 'down': direction = Direction.down else: self.reject() return time_string = self.request.get('start', None) if time_string is None: start_time = now_cet() - timedelta(hours=1) else: if not self.validateDatetime(time_string): return start_time = cet_from_string(time_string) span_string = self.request.get('span', None) if span_string is None: time_span = timedelta(hours=3) else: if not self.validateDigit(span_string): return time_span = timedelta(hours=int(span_string)) series = TASeries.get(series_id) if not series: self.reject() return output_string = json.dumps( self.departures_dict(series, origin_id, direction, start_time, time_span)) self.response.out.write(output_string)
def changed_stops(self, file_name=None): """ Acquires an xml-string with stops, either from NS-API or from the specified file, parses the stops, compares them with the current stops and returns a list with changed stops. :param file_name: Name of the source ('None' redirects to NS-API; specified in unit-tests) :return: A list of TAStop objects """ xml_string = None if file_name: if file_name != 'ignore': fp = open(file_name, 'r') xml_string = fp.read() else: increase_counter('req_api_total') url = config.NSAPI_AVT_URL % self.code xml_string = remote_fetch(url, headers=config.NSAPI_HEADER, deadline=config.NSAPI_DEADLINE) if xml_string: return TAStop.parse_avt(xml_string, delegate=self)
def test_counter(self): self.assertEqual(read_counter('test_id'), 0) increase_counter('test_id') increase_counter('test_id') self.assertEqual(read_counter('test_id'), 2) self.assertEqual( counter_dict(), { 'mission_changes': 0, 'mission_no_changes': 0, 'mission_small_changes': 0, 'req_api_success': 0, 'req_api_total': 0, 'req_avt_answered': 0, 'req_avt_denied': 0, 'req_check_confirmed': 0, 'req_check_denied': 0, 'req_check_refetched': 0, 'req_check_revoked': 0, 'req_departures': 0, 'req_mission': 0, 'req_prio_answered': 0, 'req_prio_denied': 0, 'req_trajectory': 0 })
def update_stop(self, updated): now = updated.now if now is None: now = now_cet() status, delay = self.status_at_time(now) if status == MissionStatuses.arrived: logging.info('Update was ignored because mission has already arrived') return changes = False small_changes = False self.issue_time = now index = self.index_for_stop(updated) if index is not None: existing = self.stops[index] self.tasks = [] if existing.status != updated.status: logging.info('Change status at %s from %s to %s.' % (existing.station_id, StopStatuses.s[existing.status], StopStatuses.s[updated.status])) if updated.status == StopStatuses.revoked: self.remove_stop(index) existing = None changes = True else: if existing.status == StopStatuses.planned and updated.status == StopStatuses.announced: small_changes = True elif existing.status == StopStatuses.altDestination and updated.status == StopStatuses.announced: self.reset_destination() changes = True else: if updated.status == StopStatuses.canceled: self.check_for_canceled(index - 1) self.check_for_canceled(index + 1) elif existing.status == StopStatuses.canceled: self.check_for_uncanceled(index - 1) self.check_for_uncanceled(index + 1) changes = True existing.status = updated.status if existing is not None: if existing.delay_dep != updated.delay_dep: logging.info('Change delay at %s from %.1f to %.1f.' % (existing.station_id, existing.delay_dep, updated.delay_dep)) next_index = self.next_stop_index(now) if index == next_index: increasing = bool(existing.delay_dep < updated.delay_dep) self.update_delay(index, updated.delay_dep, increasing) self.schedule_more_updates(updated, now) else: if next_index is not None and existing.delay_dep == 0: next_stop = self.stops[next_index] self.issue_time += timedelta(seconds=config.INTERVAL_BETWEEN_UPDATE_MSG) self.tasks.append(self.instruction_task(next_stop.station_url, 'prio', self.issue_time)) existing.delay_dep = updated.delay_dep changes = True if existing.platform != updated.platform and updated.platform is not None: logging.info('Change platform at %s from %s to %s.' % (existing.station_id, existing.platform, updated.platform)) existing.platform = updated.platform changes = True if existing.platformChange != updated.platformChange: existing.platformChange = updated.platformChange if existing.destination != updated.destination and updated.destination is not None: logging.info('Change destination at %s from %s to %s.' % (existing.station_id, existing.destination, updated.destination)) existing.destination = updated.destination changes = True self.update_destination(updated.destination) if existing.alteredDestination != updated.alteredDestination: logging.info('Change altered destination at %s from %s to %s.' % (existing.station_id, existing.alteredDestination, updated.alteredDestination)) if updated.alteredDestination is None: self.reset_destination() else: self.alter_destination(updated.alteredDestination) existing.alteredDestination = updated.alteredDestination changes = True if existing.departure != updated.departure and updated.departure is not None: logging.info('Change departure at %s from %s to %s.' % (existing.station_id, existing.departure.strftime('%H:%M'), updated.departure.strftime('%H:%M'))) logging.info('%s ==> %s' % (existing.departure, updated.departure)) delta = updated.departure - existing.departure existing.arrival += delta existing.departure = updated.departure changes = True issue_tasks(self.tasks) self.tasks = None else: if updated.status == StopStatuses.announced or updated.status == StopStatuses.extra: self.anterior_stops(updated) changes = True if changes: increase_counter('mission_changes') self.put() else: if small_changes: increase_counter('mission_small_changes') self.cache_set() else: increase_counter('mission_no_changes')