Ejemplo n.º 1
0
def process_batch(request, data, errors):
    stats_client = request.registry.stats_client

    nickname = request.headers.get('X-Nickname', u'')
    email = request.headers.get('X-Email', u'')
    upload_items = flatten_items(data)
    errors = process_upload(
        nickname,
        email,
        upload_items,
        stats_client,
        api_key_log=getattr(request, 'api_key_log', False),
        api_key_name=getattr(request, 'api_key_name', None),
    )

    if errors is SENTINEL:  # pragma: no cover
        return HTTPServiceUnavailable()

    if errors:  # pragma: no cover
        stats_client.incr('geosubmit.upload.errors', len(errors))

    result = HTTPOk()
    result.content_type = 'application/json'
    result.body = '{}'
    return result
Ejemplo n.º 2
0
def geosubmit_view(request):
    stats_client = request.registry.stats_client
    api_key_log = getattr(request, 'api_key_log', False)
    api_key_name = getattr(request, 'api_key_name', None)

    try:
        data, errors = preprocess_request(
            request,
            schema=GeoSubmitBatchSchema(),
            response=JSONParseError,
        )
    except JSONParseError:
        # capture JSON exceptions for submit calls
        request.registry.heka_client.raven(RAVEN_ERROR)
        raise

    items = map_items(data['items'])
    nickname = request.headers.get('X-Nickname', u'')
    if isinstance(nickname, str):
        nickname = nickname.decode('utf-8', 'ignore')

    email = request.headers.get('X-Email', u'')
    if isinstance(email, str):
        email = email.decode('utf-8', 'ignore')

    # count the number of batches and emit a pseudo-timer to capture
    # the number of reports per batch
    length = len(items)
    stats_client.incr('items.uploaded.batches')
    stats_client.timing('items.uploaded.batch_size', length)

    if api_key_log:
        stats_client.incr('items.api_log.%s.uploaded.batches' % api_key_name)
        stats_client.timing(
            'items.api_log.%s.uploaded.batch_size' % api_key_name, length)

    # batch incoming data into multiple tasks, in case someone
    # manages to submit us a huge single request
    for i in range(0, length, 100):
        batch = kombu_dumps(items[i:i + 100])
        # insert observations, expire the task if it wasn't processed
        # after six hours to avoid queue overload
        try:
            insert_measures.apply_async(kwargs={
                'email': email,
                'items': batch,
                'nickname': nickname,
                'api_key_log': api_key_log,
                'api_key_name': api_key_name,
            },
                                        expires=21600)
        except ConnectionError:  # pragma: no cover
            return HTTPServiceUnavailable()

    result = HTTPOk()
    result.content_type = 'application/json'
    result.body = '{}'
    return result
Ejemplo n.º 3
0
def country_view(request):
    client_addr = request.client_addr
    geoip_db = request.registry.geoip_db

    if request.body in EMPTY and client_addr and geoip_db is not None:
        # Optimize common case of geoip-only request
        country = geoip_db.country_lookup(client_addr)
        if country:
            result = HTTPOk()
            result.content_type = 'application/json'
            result.text = '{"country_code": "%s", "country_name": "%s"}' % (
                country.code, country.name)
            return result
        else:
            result = HTTPNotFound()
            result.content_type = 'application/json'
            result.body = NOT_FOUND
            return result

    data, errors = preprocess_request(
        request,
        schema=GeoLocateSchema(),
        response=JSONParseError,
        accept_empty=True,
    )
    data = map_data(data, client_addr=client_addr)

    session = request.db_slave_session
    result = CountrySearcher(
        {
            'geoip': geoip_db,
            'session': session
        },
        api_key_log=False,
        api_key_name=None,
        api_name='country',
    ).search(data)

    if not result:
        result = HTTPNotFound()
        result.content_type = 'application/json'
        result.body = NOT_FOUND
        return result

    return result
Ejemplo n.º 4
0
 def index(self):
     logger.debug("======= index =======")
     deployment_status = DeploymentStatus()
     deployment_list = deployment_status.get_all()
     # ToDo [zhuyux]: To render content as application/ja
     body = json.dumps(deployment_list, sort_keys=True)
     response = HTTPOk()
     response.content_type = 'application/json; charset=UTF-8'
     response.body = body
     return response
Ejemplo n.º 5
0
def process_batch(request, data, errors):
    nickname = request.headers.get('X-Nickname', u'')
    upload_items = flatten_items(data)
    errors = process_upload(nickname, upload_items)

    if errors is SENTINEL:
        return HTTPServiceUnavailable()

    if errors:
        get_stats_client().incr('geosubmit.upload.errors', len(errors))

    result = HTTPOk()
    result.content_type = 'application/json'
    result.body = '{}'
    return result
Ejemplo n.º 6
0
def process_batch(request, data, errors):
    nickname = request.headers.get('X-Nickname', u'')
    email = request.headers.get('X-Email', u'')
    upload_items = flatten_items(data)
    errors = process_upload(nickname, email, upload_items)

    if errors is SENTINEL:  # pragma: no cover
        return HTTPServiceUnavailable()

    if errors:  # pragma: no cover
        get_stats_client().incr('geosubmit.upload.errors', len(errors))

    result = HTTPOk()
    result.content_type = 'application/json'
    result.body = '{}'
    return result
Ejemplo n.º 7
0
def geosubmit_view(request, api_key):
    submitter = GeoSubmitter(request, api_key)

    # may raise HTTP error
    request_data = submitter.preprocess()

    try:
        submitter.insert_measures(request_data)
    except ConnectionError:  # pragma: no cover
        return HTTPServiceUnavailable()

    try:
        submitter.submit(request_data)
    except ConnectionError:  # pragma: no cover
        # secondary pipeline is considered non-essential for now
        pass

    result = HTTPOk()
    result.content_type = 'application/json'
    result.body = '{}'
    return result
Ejemplo n.º 8
0
def geosubmit_view(request, api_key):
    submitter = GeoSubmitter(request, api_key)

    # may raise HTTP error
    request_data = submitter.preprocess()

    try:
        submitter.insert_measures(request_data)
    except ConnectionError:  # pragma: no cover
        return HTTPServiceUnavailable()

    try:
        submitter.submit(request_data)
    except ConnectionError:  # pragma: no cover
        # secondary pipeline is considered non-essential for now
        pass

    result = HTTPOk()
    result.content_type = 'application/json'
    result.body = '{}'
    return result
Ejemplo n.º 9
0
 def success(self):
     response = HTTPOk()
     response.content_type = 'application/json'
     response.body = '{}'
     return response