<commit_before>from cornice import Service
from statsd import StatsdTimer

from ichnaea.db import Cell


cell_location = Service(
    name='cell_location',
    path='/v1/cell',
    description="Get cell location information.",
    cors_policy={'origins': ('*',), 'credentials': True})


@cell_location.get(renderer='json')
def get_cell_location(request):
    # TODO validation
    mcc = int(request.GET['mcc'])
    mnc = int(request.GET['mnc'])
    lac = int(request.GET.get('lac', -1))
    cid = int(request.GET.get('cid', -1))

    session = request.db_session
    query = session.query(Cell).filter(Cell.mcc == mcc).filter(Cell.mnc == mnc)

    if lac >= 0:
        query.filter(Cell.lac == lac)
    if cid >= 0:
        query.filter(Cell.cid == cid)

    with StatsdTimer('get_cell_location'):
        result = query.first()
        if result is None:
            # TODO raise error
            return {
                'latitude': 0,
                'longitude': 0,
                'accuracy': 0,
            }
        else:
            return {
                'latitude': result.lat,
                'longitude': result.lon,
                # TODO figure out actual meaning of `range`
                # we want to return accuracy in meters at 95% percentile
                'accuracy': result.range,
            }

heartbeat = Service(name='heartbeat', path='/__heartbeat__')


@heartbeat.get(renderer='json')
def get_heartbeat(request):
    return {'status': 'OK'}
<commit_msg>deal with stupid float formatting under Python 2.6<commit_after>from cornice import Service
from statsd import StatsdTimer

from ichnaea.db import Cell


cell_location = Service(
    name='cell_location',
    path='/v1/cell',
    description="Get cell location information.",
    cors_policy={'origins': ('*',), 'credentials': True})


@cell_location.get(renderer='json')
def get_cell_location(request):
    # TODO validation
    mcc = int(request.GET['mcc'])
    mnc = int(request.GET['mnc'])
    lac = int(request.GET.get('lac', -1))
    cid = int(request.GET.get('cid', -1))

    session = request.db_session
    query = session.query(Cell).filter(Cell.mcc == mcc).filter(Cell.mnc == mnc)

    if lac >= 0:
        query.filter(Cell.lac == lac)
    if cid >= 0:
        query.filter(Cell.cid == cid)

    with StatsdTimer('get_cell_location'):
        result = query.first()
        if result is None:
            # TODO raise error
            return {
                'latitude': 0,
                'longitude': 0,
                'accuracy': 0,
            }
        else:
            return {
                'latitude': str(result.lat),
                'longitude': str(result.lon),
                # TODO figure out actual meaning of `range`
                # we want to return accuracy in meters at 95% percentile
                'accuracy': result.range,
            }

heartbeat = Service(name='heartbeat', path='/__heartbeat__')


@heartbeat.get(renderer='json')
def get_heartbeat(request):
    return {'status': 'OK'}

{'fraction_non_alphanumeric': 0.1335764003977461, 'fraction_numerical': 0.007292011932383162, 'mean_word_length': 2.2627027027027027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1018822', 'n_tokens_mistral': 947, 'n_tokens_neox': 901, 'n_words': 219}