def test_push_to_realtime(self): # only do the test if realtime test server is configured if is_realtime_rest_configured(): working_dir = shakemap_extract_dir() shake_event = ShakeEvent(working_dir=working_dir, event_id=SHAKE_ID, locale="en", data_is_local_flag=True) # generate report shake_event.render_map() # push to realtime django push_shake_event_to_rest(shake_event) # check shake event exists session = get_realtime_session() earthquake_url = generate_earthquake_detail_url(SHAKE_ID) response = session.get(earthquake_url) self.assertEqual(response.status_code, requests.codes.ok) event_dict = shake_event.event_dict() earthquake_data = { "shake_id": shake_event.event_id, "magnitude": float(event_dict.get("mmi")), "depth": float(event_dict.get("depth-value")), "time": shake_event.shake_grid.time.strftime(INASAFE_REALTIME_DATETIME_FORMAT), "location": { "type": "Point", "coordinates": [shake_event.shake_grid.longitude, shake_event.shake_grid.latitude], }, "location_description": event_dict.get("place-name"), } for key, value in earthquake_data.iteritems(): self.assertEqual(response.json()[key], value)
def test_push_to_realtime(self): # only do the test if realtime test server is configured if is_realtime_rest_configured(): working_dir = shakemap_extract_dir() shake_event = ShakeEvent(working_dir=working_dir, event_id=SHAKE_ID, locale='en', data_is_local_flag=True) # generate report shake_event.render_map() # push to realtime django push_shake_event_to_rest(shake_event) # check shake event exists session = get_realtime_session() earthquake_url = generate_earthquake_detail_url(SHAKE_ID) response = session.get(earthquake_url) self.assertEqual(response.status_code, requests.codes.ok) event_dict = shake_event.event_dict() earthquake_data = { 'shake_id': shake_event.event_id, 'magnitude': float(event_dict.get('mmi')), 'depth': float(event_dict.get('depth-value')), 'time': shake_event.shake_grid.time.strftime( INASAFE_REALTIME_DATETIME_FORMAT), 'location': { 'type': 'Point', 'coordinates': [ shake_event.shake_grid.longitude, shake_event.shake_grid.latitude ] }, 'location_description': event_dict.get('place-name') } for key, value in earthquake_data.iteritems(): self.assertEqual(response.json()[key], value)
def process_event(working_dir=None, event_id=None, locale='en'): """Launcher that actually runs the event processing. :param event_id: The event id to process. If None the latest event will be downloaded and processed. :type event_id: str :param locale: The locale that will be used. Default to en. :type locale: str """ population_path = os.path.join( data_dir(), 'exposure', 'population.tif') # Use cached data where available # Whether we should always regenerate the products force_flag = False if 'INASAFE_FORCE' in os.environ: force_string = os.environ['INASAFE_FORCE'] if str(force_string).capitalize() == 'Y': force_flag = True # We always want to generate en products too so we manipulate the locale # list and loop through them: locale_list = [locale] if 'en' not in locale_list: locale_list.append('en') # Now generate the products for locale in locale_list: # Extract the event # noinspection PyBroadException try: shake_events = create_shake_events( event_id=event_id, force_flag=force_flag, locale=locale, population_path=population_path, working_dir=working_dir) except (BadZipfile, URLError): # retry with force flag true shake_events = create_shake_events( event_id=event_id, force_flag=True, locale=locale, population_path=population_path, working_dir=working_dir) except EmptyShakeDirectoryError as ex: LOGGER.info(ex) return except Exception: # pylint: disable=broad-except LOGGER.exception('An error occurred setting up the shake event.') return LOGGER.info('Event Id: %s', [s.event_id for s in shake_events]) LOGGER.info('-------------------------------------------') for shake_event in shake_events: shake_event.render_map(force_flag) # push the shakemap to realtime server push_shake_event_to_rest(shake_event)