Пример #1
0
def get_rewind(request):
    '''
        rewinds the current active Model.
    '''
    print('rewinding', request.session.session_id)
    active_model = get_active_model(request)
    ns = request.registry.get('sio_ns')
    if active_model:
        session_lock = acquire_session_lock(request)
        log.info('  session lock acquired (sess:{}, thr_id: {})'.format(
            id(session_lock),
            current_thread().ident))

        try:
            if ns:
                sio = ns.get_sockid_from_sessid(request.session.session_id)
                if (ns.active_greenlets.get(sio)):
                    with ns.session(sio) as sock_session:
                        ns.active_greenlets.get(sio).kill(block=False)
                        sock_session['num_sent'] = 0
            active_model.rewind()
        except Exception:
            raise cors_exception(request,
                                 HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            session_lock.release()
            log.info('  session lock released (sess:{}, thr_id: {})'.format(
                id(session_lock),
                current_thread().ident))
    else:
        raise cors_exception(request, HTTPPreconditionFailed)
Пример #2
0
def get_raster(request):
    '''
        Outputs the map's raster in binary format
    '''
    log_prefix = 'req({0}): get_raster():'.format(id(request))
    log.info('>>' + log_prefix)

    session_lock = acquire_session_lock(request)
    log.info('  {} session lock acquired (sess:{}, thr_id: {})'.format(
        log_prefix, id(session_lock),
        current_thread().ident))
    try:
        obj_id = request.matchdict.get('obj_id')[0]
        obj = get_session_object(obj_id, request)

        if obj is not None:
            raster = obj.raster.copy()
            #transpose for client
            bbox = obj.land_polys.bounding_box.AsPoly().reshape(-1).tolist()
            return zlib.compress(np.ascontiguousarray(
                raster.T).tobytes()), raster.T.shape, bbox
        else:
            exc = cors_exception(request, HTTPNotFound)
            raise exc
    finally:
        session_lock.release()
        log.info('  {} session lock released (sess:{}, thr_id: {})'.format(
            log_prefix, id(session_lock),
            current_thread().ident))

    log.info('<<' + log_prefix)
Пример #3
0
def get_model(request):
    '''
        Returns Model object in JSON.
        - This method varies slightly from the common object method in that
          if we don't specify a model ID, we:
          - return the current active model if it exists or...
          - return the specification.
    '''
    ret = None
    obj_id = obj_id_from_url(request)

    session_lock = acquire_session_lock(request)
    log.info('  session lock acquired (sess:{}, thr_id: {})'.format(
        id(session_lock),
        current_thread().ident))

    try:
        if obj_id is None:
            my_model = get_active_model(request)
            if my_model is not None:
                ret = my_model.serialize(options=web_ser_opts)

        if ret is None:
            ret = get_object(request, implemented_types)
    finally:
        session_lock.release()
        log.info('  session lock released (sess:{}, thr_id: {})'.format(
            id(session_lock),
            current_thread().ident))

    return ret
Пример #4
0
def get_rewind(request):
    '''
        rewinds the current active Model.
    '''
    print 'rewinding', request.session.session_id
    active_model = get_active_model(request)
    ns = sess_namespaces.get(request.session.session_id, None)
    if active_model:
        session_lock = acquire_session_lock(request)
        log.info('  session lock acquired (sess:{}, thr_id: {})'
                 .format(id(session_lock), current_thread().ident))

        try:
            if (ns and ns.active_greenlet):
                ns.active_greenlet.kill(block=False)
                ns.num_sent = 0
            active_model.rewind()
        except Exception:
            raise cors_exception(request, HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            session_lock.release()
            log.info('  session lock released (sess:{}, thr_id: {})'
                     .format(id(session_lock), current_thread().ident))
    else:
        raise cors_exception(request, HTTPPreconditionFailed)
Пример #5
0
def get_polygons(request):
    '''
        Outputs the SpatialRelease's Polygons in binary format
    '''
    log_prefix = 'req({0}): get_polygons():'.format(id(request))
    log.info('>>' + log_prefix)

    session_lock = acquire_session_lock(request)
    log.info('  {} session lock acquired (sess:{}, thr_id: {})'.format(
        log_prefix, id(session_lock),
        current_thread().ident))
    try:
        obj_id = request.matchdict.get('obj_id')[0]
        obj = get_session_object(obj_id, request)

        if obj is not None and obj.obj_type in geojson_types:
            lengths, lines = obj.get_polygons()
            lines_bytes = b''.join([l.tobytes() for l in lines])

            return (zlib.compress(lengths.tobytes() + lines_bytes),
                    len(lengths))
        else:
            exc = cors_exception(request, HTTPNotFound)
            raise exc
    finally:
        session_lock.release()
        log.info('  {} session lock released (sess:{}, thr_id: {})'.format(
            log_prefix, id(session_lock),
            current_thread().ident))
        log.info('<<' + log_prefix)
Пример #6
0
def update_model(request):
    '''
        Returns Model object in JSON.
        - This method varies slightly from the common object method in that
          if we don't specify a model ID, we:
          - update the current active model if it exists or...
          - generate a 'Not Found' exception.
    '''
    log_prefix = 'req({0}): update_model():'.format(id(request))
    log.info('>>' + log_prefix)

    ret = None
    try:
        json_request = ujson.loads(request.body.decode('utf-8'))
    except Exception:
        raise cors_exception(request, HTTPBadRequest)

    if not JSONImplementsOneOf(json_request, implemented_types):
        raise cors_exception(request, HTTPNotImplemented)

    session_lock = acquire_session_lock(request)
    log.info('  {} session lock acquired (sess:{}, thr_id: {})'.format(
        log_prefix, id(session_lock),
        current_thread().ident))

    obj_id = obj_id_from_req_payload(json_request)
    if obj_id:
        active_model = get_session_object(obj_id, request)
    else:
        active_model = get_active_model(request)

    if active_model:
        try:
            if UpdateObject(active_model, json_request,
                            get_session_objects(request)):
                set_session_object(active_model, request)
            ret = active_model.serialize(options=web_ser_opts)
        except Exception:
            raise cors_exception(request,
                                 HTTPUnsupportedMediaType,
                                 with_stacktrace=True)
        finally:
            session_lock.release()
            log.info('  ' + log_prefix + 'session lock released...')
    else:
        session_lock.release()
        log.info('  {} session lock released (sess:{}, thr_id: {})'.format(
            log_prefix, id(session_lock),
            current_thread().ident))

        msg = ("raising cors_exception() in update_model. "
               "Updating model before it exists.")
        log.warning('  ' + log_prefix + msg)

        raise cors_exception(request, HTTPNotFound)

    log.info('<<' + log_prefix)
    return ret
Пример #7
0
def get_location(request):
    '''
        Returns a List of Location objects in JSON if no object specified.
    '''
    log.info('location_api.cors_origins_for("get") = {0}'
             .format(location_api.cors_origins_for('get')))

    # first, lets just query that we can get to the data
    locations_dir = request.registry.settings['locations_dir']
    base_len = len(locations_dir.split(sep))
    location_content = []
    location_file_dirs = []

    for (path, _dirnames, filenames) in walk(locations_dir):
        if len(path.split(sep)) == base_len + 1:
            [location_content.append(ujson.load(open(join(path, f), 'r')))
             for f in filenames
             if f == 'compiled.json']

            [location_file_dirs.append(join(path, basename(path) + '_save'))
             for f in filenames
             if f == 'compiled.json']

    slug = obj_id_from_url(request)
    if slug:
        matching = [(i, c) for i, c in enumerate(location_content)
                    if slugify.slugify_url(c['name']) == slug]
        if matching:
            session_lock = acquire_session_lock(request)
            log.info('  session lock acquired (sess:{}, thr_id: {})'
                     .format(id(session_lock), current_thread().ident))

            try:
                location_file = location_file_dirs[matching[0][0]]
                log.info('load location: {0}'.format(location_file))
                load_location_file(location_file, request)
            except Exception:
                raise cors_exception(request, HTTPInternalServerError,
                                     with_stacktrace=True)
            finally:
                session_lock.release()
                log.info('  session lock released (sess:{}, thr_id: {})'
                         .format(id(session_lock), current_thread().ident))

            return matching[0][1]
        else:
            raise cors_exception(request, HTTPNotFound)
    else:
        features = [Feature(geometry=Point(c['geometry']['coordinates']),
                            properties={'title': c['name'],
                                        'slug': slugify.slugify_url(c['name']),
                                        'content': c['steps']
                                        }
                            )
                    for c in location_content]
        return FeatureCollection(features)
Пример #8
0
def create_model(request):
    '''
        Creates a new model
    '''
    log_prefix = 'req({0}): create_object():'.format(id(request))
    log.info('>>' + log_prefix)

    try:
        json_request = ujson.loads(request.body.decode('utf-8'))
    except Exception:
        json_request = None

    if json_request and not JSONImplementsOneOf(json_request,
                                                implemented_types):
        raise cors_exception(request, HTTPNotImplemented)

    session_lock = acquire_session_lock(request)
    log.info('  {} session lock acquired (sess:{}, thr_id: {})'.format(
        log_prefix, id(session_lock),
        current_thread().ident))

    try:
        clean_session_dir(request)
        init_session_objects(request, force=True)

        if json_request:
            new_model = CreateObject(json_request,
                                     get_session_objects(request))
        else:
            new_model = Model()

        set_session_object(new_model, request)

        set_active_model(request, new_model.id)
    except Exception:
        raise cors_exception(request,
                             HTTPUnsupportedMediaType,
                             with_stacktrace=True)
    finally:
        session_lock.release()
        log.info('  {} session lock released (sess:{}, thr_id: {})'.format(
            log_prefix, id(session_lock),
            current_thread().ident))

    log.info('<<' + log_prefix)
    return new_model.serialize(options=web_ser_opts)
Пример #9
0
def get_metadata(request):
    log_prefix = 'req({0}): get_metadata():'.format(id(request))
    log.info('>>' + log_prefix)

    session_lock = acquire_session_lock(request)
    log.info('  {} session lock acquired (sess:{}, thr_id: {})'.format(
        log_prefix, id(session_lock),
        current_thread().ident))
    try:
        obj_id = request.matchdict.get('obj_id')[0]
        obj = get_session_object(obj_id, request)
        if obj is not None:
            return obj.get_metadata()
        else:
            exc = cors_exception(request, HTTPNotFound)
            raise exc
    finally:
        session_lock.release()
        log.info('  {} session lock released (sess:{}, thr_id: {})'.format(
            log_prefix, id(session_lock),
            current_thread().ident))

    log.info('<<' + log_prefix)
Пример #10
0
def get_step(request):
    '''
        Generates and returns an image corresponding to the step.
    '''
    log_prefix = 'req({0}): get_step():'.format(id(request))
    log.info('>>' + log_prefix)

    active_model = get_active_model(request)
    if active_model:
        # generate the next step in the sequence.
        session_lock = acquire_session_lock(request)
        log.info('  {} session lock acquired (sess:{}, thr_id: {})'
                 .format(log_prefix, id(session_lock), current_thread().ident))

        try:
            if active_model.current_time_step == -1:
                # our first step, establish uncertain models
                drop_uncertain_models(request)

                log.info('\thas_weathering_uncertainty {0}'.
                         format(active_model.has_weathering_uncertainty))
                if active_model.has_weathering_uncertainty:
                    set_uncertain_models(request)
                else:
                    log.info('Model does not have weathering uncertainty')

            begin = time.time()
            output = active_model.step()

            begin_uncertain = time.time()
            steps = get_uncertain_steps(request)
            end = time.time()

            if steps and 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                aggregate = defaultdict(list)
                low = {}
                high = {}
                full_output = {}

                for idx, step_output in enumerate(steps):
                    # step_output could contain an exception from one
                    # of our uncertainty worker processes.  If so, then
                    # we should propagate the exception with its original
                    # context.
                    if (isinstance(step_output, tuple) and
                            len(step_output) >= 3 and
                            isinstance(step_output[1], Exception)):
                        raise step_output[1].with_traceback(step_output[2])

                    for k, v in step_output['WeatheringOutput'].items():
                        aggregate[k].append(v)

                for k, v in aggregate.items():
                    low[k] = min(v)
                    high[k] = max(v)

                full_output = {'time_stamp': nominal['time_stamp'],
                               'nominal': nominal,
                               'low': low,
                               'high': high}
                for idx, step_output in enumerate(steps):
                    full_output[idx] = step_output['WeatheringOutput']

                output['WeatheringOutput'] = full_output
                output['uncertain_response_time'] = end - begin_uncertain
                output['total_response_time'] = end - begin
            elif 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                full_output = {'time_stamp': nominal['time_stamp'],
                               'nominal': nominal,
                               'low': None,
                               'high': None}
                output['WeatheringOutput'] = full_output
                output['uncertain_response_time'] = end - begin_uncertain
                output['total_response_time'] = end - begin

        except StopIteration:
            log.info('  ' + log_prefix + 'stop iteration exception...')
            drop_uncertain_models(request)
            raise cors_exception(request, HTTPNotFound)
        except Exception:
            log.info('  ' + log_prefix + 'unknown exception...')
            raise cors_exception(request, HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            session_lock.release()
            log.info('  {} session lock released (sess:{}, thr_id: {})'
                     .format(log_prefix, id(session_lock),
                             current_thread().ident))

        return output
    else:
        raise cors_exception(request, HTTPPreconditionFailed,
                             explanation=(b'Your session timed out - the model is no longer active'))
Пример #11
0
def get_full_run(request):
    '''
        Performs a full run of the current active Model, turning off any
        response options.
        Returns the final step results.
    '''
    log_prefix = 'req({0}): get_full_run():'.format(id(request))
    log.info('>>' + log_prefix)

    response_on = request.json_body['response_on']

    active_model = get_active_model(request)
    if active_model:
        session_lock = acquire_session_lock(request)
        log.info('  session lock acquired (sess:{}, thr_id: {})'
                 .format(id(session_lock), current_thread().ident))

        try:
            weatherer_enabled_flags = [w.on for w in active_model.weatherers]

            if response_on is False:
                for w in active_model.weatherers:
                    if isinstance(w, (Skimmer, Burn, ChemicalDispersion)):
                        w.on = False

            active_model.rewind()

            drop_uncertain_models(request)

            if active_model.has_weathering_uncertainty:
                log.info('Model has weathering uncertainty')
                set_uncertain_models(request)
            else:
                log.info('Model does not have weathering uncertainty')

            begin = time.time()

            for step in active_model:
                output = step
                steps = get_uncertain_steps(request)

            end = time.time()

            if steps and 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                aggregate = defaultdict(list)
                low = {}
                high = {}
                full_output = {}

                for idx, step_output in enumerate(steps):
                    for k, v in step_output['WeatheringOutput'].items():
                        aggregate[k].append(v)

                for k, v in aggregate.items():
                    low[k] = min(v)
                    high[k] = max(v)

                full_output = {'time_stamp': nominal['time_stamp'],
                               'nominal': nominal,
                               'low': low,
                               'high': high}
                for idx, step_output in enumerate(steps):
                    full_output[idx] = step_output['WeatheringOutput']

                output['WeatheringOutput'] = full_output
                output['total_response_time'] = end - begin
            elif 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                full_output = {'time_stamp': nominal['time_stamp'],
                               'nominal': nominal,
                               'low': None,
                               'high': None}
                output['WeatheringOutput'] = full_output
                output['total_response_time'] = end - begin

            active_model.rewind()
        except Exception:
            raise cors_exception(request, HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            for a, w in zip(weatherer_enabled_flags, active_model.weatherers):
                w.on = a
            session_lock.release()
            log.info('  session lock released (sess:{}, thr_id: {})'
                     .format(id(session_lock), current_thread().ident))

        log.info('<<' + log_prefix)
        return output
    else:
        raise cors_exception(request, HTTPPreconditionFailed)