Example #1
0
 def aggregate_in_redis(self, record):
     """
     Aggregate the values for all permutations of the records facets.
     """
     with self.redis.pipeline(True) as pipe:
         for facet in all_permutations(record['facets']):   
             facet = split_facet(facet)                       
             self.insert_to_redis(pipe, facet, record['values'])                          
         pipe.execute()
     return True
Example #2
0
def get_values(bucket):
    """
    Retrieves the values stored in a single facet.

    It accepts a JSON dictionary of query names to facets, e.g.:

        {
            'tablet-dayone': {'device': ['tablet', 'apple'],
                              'time': ['2013', '1', '15']}
            'tablet-daytwo': {'device': ['tablet', 'apple'],
                              'time': ['2013', '1', '16']}
        }

    On success it will respond with the an API standard response including the
    'results' key, a dictionary of the values queried for, e.g.:

        {
            'ok': true,
            'status': 200,
            'results': {
                'tablet-dayone': {
                    'duration': 1212421,
                    'datapoints': 238282
                },
                'tablet-daytwo': {
                    'duration': 3292382,
                    'datapoints': 38283
                }
            }
        }

    This allows effecient retrieve of any number of facet values from  the DB.
    """
    start_time = unixtime()
    request = bottle.request
    query = request.json

    if query is None or type(query) != dict:
        bottle.abort(400, 'Must POST application/json dictionary')

    facet_keys = {}
    results = {}

    # Prepare facets for query
    for key, facet in query.items():
        try:
            facet = sanitized_facets(query['facets'])
        except ValidationError, oops:
            LOG.info("Query for '%s' contained invalid facet", key, exc_info=True)
            bottle.abort(400, "%s: %s" % (key, oops.message))
        facet_keys[key] = split_facet(facet)    
Example #3
0
def find_values(bucket):
    """
    Retrieves a list of the facet values underneath a parent facet.

    It accepts a JSON dictionry of the parent facet names and a limit on how 
    many sub-facets to return.

        {
            'tablet-days': {'facet': {'device': ['tablet', 'apple'],
                                      'time': ['2013', '1', '15']},
                            'limit': 50}
        }

    On success it will return the a standard API response including the
    'results' key which contains a 

        {
            'ok': true,
            'status': 200,
            'results': {
                'tablet-days': {'1': {'duration': 38289323,
                                      'datapoints': 4919}},
                               {'2': {'duration': 382829,
                                      'datapoints': 1234}}
            }
        }
    """
    start_time = unixtime()
    request = bottle.request
    query = request.json

    if query is None or type(query) != dict:
        bottle.abort(400, 'Must POST application/json dictionary')

    # Validate the searches
    searches = {}
    for name, search in query.items():
        if 'facet' not in search:
            bottle.abort(400, '"facet" key required for "%s"' % (name,))
        if 'limit' not in search:
            bottle.abort(400, '"limit" key required for "%s"' % (name,))
        try:
            limit = int(search.get('limit'))
        except ValueError:
            bottle.abort(400, 'Invalid "limit" for "%s"' % (name,))

        withvalues = bool(search.get('withvalues'))

        startkey = None
        if 'startkey' in search:
            try:
                startkey = to_utf8_str(search['startkey'])
            except TypeError:
                bottle.abort(400, 'Start key for "%s" is invalid type' % (name,))

        try:
            facet = sanitized_facets(search['facet'])
        except ValidationError, oops:
            LOG.info("'%s' contained invalid facet", name, exc_info=True)
            bottle.abort(400, "%s: %s" % (name, oops.message))
        searches[name] = {
            'facet': split_facet(facet),
            'limit': limit,
            'startkey': start,
            'withvalues': withvalues
        }