예제 #1
0
def get_content_timeseries(user, org, content_item_id):
    """
    Query an individual content timeseries.
    """
    c = ContentItem.query\
        .filter_by(id=content_item_id)\
        .filter_by(org_id=org.id)\
        .first()

    if not c:
        raise NotFoundError(
            'A ContentItem with ID {} does not exist'
            .format(content_item_id))

    # select / exclude
    select, exclude = arg_list('select', typ=str, exclusions=True, default=['*'])
    if '*' in select:
        exclude = []
        select = "*"

    kw = dict(
        unit=arg_str('unit', default='hour'),
        sparse=arg_bool('sparse', default=True),
        sig_digits=arg_int('sig_digits', default=2),
        select=select,
        exclude=exclude,
        rm_nulls=arg_bool('rm_nulls', default=False),
        time_since_start=arg_bool('time_since_start', default=False),
        transform=arg_str('transform', default=None),
        before=arg_date('before', default=None),
        after=arg_date('after', default=None)
    )

    q = QueryContentMetricTimeseries(org, [content_item_id], **kw)
    return jsonify(list(q.execute()))
예제 #2
0
def extract(user):
    url = arg_str('url', default=None)
    type = arg_str('type', default='article')
    force_refresh = arg_bool('force_refresh', default=False)
    format = arg_str('format', default='json')

    if not url:
        raise RequestError("A url is required.")

    if force_refresh:
        extract_cache.debug = True

    cr = extract_cache.get(url, type)
    if not cr:
        extract_cache.invalidate(url, type)
        raise InternalServerError('Something went wrong. Try again.')

    resp = {
        'cache': cr,
        'data': cr.value
    }

    if format == 'html':
        return render_template(
            'extract_preview.html',
            data=resp)

    return jsonify(resp)
예제 #3
0
    def decorated_function(*args, **kw):

        # get the org
        org_id = arg_str('org', default=None)
        if not org_id:
            raise AuthError('An org is required for this request.')

        # get the user object.
        user = kw.get('user')

        org = fetch_by_id_or_field(Org, 'slug', org_id)

        # if it still doesn't exist, raise an error.
        if not org:
            raise NotFoundError(
                'An Org with ID/Slug {} does exist.'.format(org_id))

        # otherwise ensure the active user can edit this Org
        if user.id not in org.user_ids:
            raise ForbiddenError(
                'User "{}" is not allowed to access Org "{}".'.format(
                    user.name, org.name))

        # check if we should localize this request
        localize(org)

        kw['org'] = org
        return f(*args, **kw)
예제 #4
0
    def decorated_function(*args, **kw):

        # get the org
        org_id = arg_str('org', default=None)
        if not org_id:
            raise AuthError(
                'An org is required for this request.')

        # get the user object.
        user = kw.get('user')

        org = fetch_by_id_or_field(Org, 'slug', org_id)

        # if it still doesn't exist, raise an error.
        if not org:
            raise NotFoundError(
                'An Org with ID/Slug {} does exist.'
                .format(org_id))

        # otherwise ensure the active user can edit this Org
        if user.id not in org.user_ids:
            raise ForbiddenError(
                'User "{}" is not allowed to access Org "{}".'
                .format(user.name, org.name))

        # check if we should localize this request
        localize(org)

        kw['org'] = org
        return f(*args, **kw)
예제 #5
0
def get_org_timeseries(user, org_id_slug):

    # fetch org
    org = fetch_by_id_or_field(Org, 'slug', org_id_slug)

    # if it still doesn't exist, raise an error.
    if not org:
        raise NotFoundError(
            'This Org does not exist.')

    # ensure the active user can edit this Org
    if user.id not in org.user_ids:
        raise ForbiddenError(
            'You are not allowed to access this Org')

    # todo: validate which metrics you can select.

    # select / exclude
    select, exclude = arg_list(
        'select', typ=str, exclusions=True, default=['all'])
    if 'all' in select:
        exclude = []
        select = "all"

    kw = dict(
        unit=arg_str('unit', default='hour'),
        sparse=arg_bool('sparse', default=True),
        sig_digits=arg_int('sig_digits', default=2),
        select=select,
        exclude=exclude,
        group_by_id=arg_bool('group_by_id', default=True),
        rm_nulls=arg_bool('rm_nulls', default=False),
        time_since_start=arg_bool('time_since_start', default=False),
        transform=arg_str('transform', default=None),
        before=arg_date('before', default=None),
        after=arg_date('after', default=None)
    )

    q = QueryOrgMetricTimeseries(org, [org.id], **kw)
    return jsonify(list(q.execute()))
예제 #6
0
def list_authors(user, org):
    """
    Get all authors.
    """
    incl_content = arg_bool('incl_content', default=False)
    q = arg_str('q', default=None)

    authors = Author.query\
        .filter_by(org_id=org.id)
    if q:
        authors = authors.search(q, vector=Author.search_vector, sort=True)

    return jsonify([a.to_dict(incl_content=incl_content) for a in authors.all()])
예제 #7
0
def list_authors(user, org):
    """
    Get all authors.
    """
    incl_content = arg_bool('incl_content', default=False)
    q = arg_str('q', default=None)

    authors = Author.query\
        .filter_by(org_id=org.id)
    if q:
        authors = authors.search(q, vector=Author.search_vector, sort=True)

    return jsonify(
        [a.to_dict(incl_content=incl_content) for a in authors.all()])
예제 #8
0
def exec_query(user):
    """
    Only the super user can access the sql api.
    This is primarily intended for internal recipes
    which may operate on machines without access to
    the databse.
    """
    if not user.super_user:
        raise ForbiddenError(
            "Only the super user can access the SQL API.")
    if request.method == "POST":
        q = request_data().get('query', None)
    if request.method == "GET":
        q = arg_str('query', default=None)
    if not q:
        raise RequestError('A query - "q" is required.')
    stream = arg_bool('stream', default=True)
    try:
        results = db.session.execute(q)
    except Exception as e:
        raise RequestError(
            "There was an error executing this query: "
            "{}".format(e.message))

    def generate():
        try:
            for row in ResultIter(results):
                if stream:
                    yield obj_to_json(row) + "\n"
                else:
                    yield row
        except ResourceClosedError:
            resp = {'success': True}
            if stream:
                yield obj_to_json(resp) + "\n"
            else:
                yield resp
    if stream:
        return Response(stream_with_context(generate()))

    data = list(generate())
    if len(data) == 1:
        if data[0]['success']:
            data = data[0]
    return jsonify(data)
예제 #9
0
    def decorated_function(*args, **kw):

        # if we got an apikey...
        apikey = arg_str('apikey', default=None)
        if not apikey:
            raise AuthError('An apikey is required for this request.')

        # get the user object.
        user = User.query\
            .filter_by(apikey=apikey)\
            .first()

        # if it doesn't exist, throw an error
        if not user:
            raise ForbiddenError('Invalid apikey')

        kw['user'] = user
        return f(*args, **kw)
예제 #10
0
def exec_query(user):
    """
    Only the super user can access the sql api.
    This is primarily intended for internal recipes
    which may operate on machines without access to
    the databse.
    """
    if not user.super_user:
        raise ForbiddenError("Only the super user can access the SQL API.")
    if request.method == "POST":
        q = request_data().get('query', None)
    if request.method == "GET":
        q = arg_str('query', default=None)
    if not q:
        raise RequestError('A query - "q" is required.')
    stream = arg_bool('stream', default=True)
    try:
        results = db.session.execute(q)
    except Exception as e:
        raise RequestError("There was an error executing this query: "
                           "{}".format(e.message))

    def generate():
        try:
            for row in ResultIter(results):
                if stream:
                    yield obj_to_json(row) + "\n"
                else:
                    yield row
        except ResourceClosedError:
            resp = {'success': True}
            if stream:
                yield obj_to_json(resp) + "\n"
            else:
                yield resp

    if stream:
        return Response(stream_with_context(generate()))

    data = list(generate())
    if len(data) == 1:
        if data[0]['success']:
            data = data[0]
    return jsonify(data)
예제 #11
0
    def decorated_function(*args, **kw):

        # if we got an apikey...
        apikey = arg_str('apikey', default=None)
        if not apikey:
            raise AuthError(
                'An apikey is required for this request.')

        # get the user object.
        user = User.query\
            .filter_by(apikey=apikey)\
            .first()

        # if it doesn't exist, throw an error
        if not user:
            raise ForbiddenError(
                'Invalid apikey')

        kw['user'] = user
        return f(*args, **kw)