Beispiel #1
0
def upsert_query(query, is_new=False):
    queries = snooper.parse_queries(conf.queries)
    if not queries:
        raise ApiException('No queries found')
    if is_new and query in queries:
        raise ApiException('Query {} already exists'.format(query))
    elif not is_new and query not in queries:
        raise ApiException('Query {} not found'.format(query))
    try:
        query_json = json.loads(request.data)
        if snooper.validate_query(query_json, queries):
            queries[query] = query_json
    except Exception as e:
        app.logger.error('Cannot parse data {} into valid JSON: {}'.format(
            request.data, e))
        raise ApiException(e)
    app.logger.debug('Query {} created/updated: {}'.format(
        query, queries[query]['sql']))
    with open(conf.queries, 'w') as fd:
        json.dump({"queries": queries},
                  fd,
                  sort_keys=True,
                  indent=4,
                  separators=(',', ': '))
    # TODO: return a 201 CREATED code instead or 200 OK (for a PUT)
    return json.dumps({'result': 'success'})
Beispiel #2
0
def get_all_queries():
    all_queries = snooper.parse_queries(conf.queries)
    queries = []
    for name, query in all_queries.iteritems():
        query['name'] = name
        queries.append(query)
    return json.dumps(queries)
Beispiel #3
0
def get_query_info(query):
    """Returns all info about a query """
    queries = snooper.parse_queries(conf.queries)
    if not queries:
        raise ApiException('No queries found')
    elif query not in queries:
        abort(404)
    result = queries.get(query)
    result['name'] = query
    return json.dumps(result)
Beispiel #4
0
def get_all():
    """ Gets all existing queries names

        @return: a list of all queries in the "queries" key:
            {"queries": [ "get_user", "get_migrations", .... ]}
        @rtype: string
    """
    all_queries = snooper.parse_queries(conf.queries)
    queries = []
    for query in all_queries:
        queries.append(query)
    return json.dumps({"queries": queries})
Beispiel #5
0
def delete_query(query):
    queries = snooper.parse_queries(conf.queries)
    if not queries:
        raise ApiException('No queries found')
    elif query not in queries:
        raise ApiException('Query {} not found'.format(query))
    queries.pop(query)
    app.logger.debug('Query {} removed'.format(query))
    with open(conf.queries, 'w') as fd:
        json.dump({"queries": queries},
                  fd,
                  sort_keys=True,
                  indent=4,
                  separators=(',', ': '))
    # TODO: return a 205 RESET CONTENT instead
    return json.dumps({'result': 'removed'})
Beispiel #6
0
def get(query, args=None):
    query_dict = snooper.parse_queries(conf.queries).get(query)
    if query_dict:
        params = None
        if args:
            params = snooper.parse_query_params(args.split('/'))
        res = run_query(query_dict.get("sql"), params)
        res["drill_down"] = query_dict.get("drill_down")
        return json.dumps(res)
    else:
        app.logger.error('Could not find query %s', query_dict)
        return redirect(
            url_for('/'.join([
                'error',
                urllib.quote('Query Not Found'),
                urllib.quote(
                    'Could not find query {} in the given query JSON file [{}]'
                    .format(query_dict, conf.queries))
            ])))