def test_valid(self): assert smart_timedelta('1d') == datetime.timedelta(days=1) assert smart_timedelta('14d') == datetime.timedelta(days=14)
def test_invalid(self): assert smart_timedelta('0d', 'fallback') == 'fallback' assert smart_timedelta('foo', 'fallback') == 'fallback' assert smart_timedelta('d', 'fallback') == 'fallback'
def test_valid(self): eq_(smart_timedelta('1d'), datetime.timedelta(days=1)) eq_(smart_timedelta('14d'), datetime.timedelta(days=14))
def test_invalid(self): eq_(smart_timedelta('0d', 'fallback'), 'fallback') eq_(smart_timedelta('foo', 'fallback'), 'fallback') eq_(smart_timedelta('d', 'fallback'), 'fallback')
def get(self, request): """Returns JSON feed of first 10000 results This feels like a duplication of the front-page dashboard search logic, but it's separate which allows us to handle multiple values. """ search = models.ResponseDocType.docs.search() f = F() if 'id' in request.GET: id_list = request.GET['id'].split(',') id_list = [smart_int(id_, fallback=None) for id_ in id_list] id_list = [id_ for id_ in id_list if id_] f &= F('terms', id=id_list) else: if 'happy' in request.GET: happy = {'0': False, '1': True}.get(request.GET['happy'], None) if happy is not None: f &= F('term', happy=happy) if 'platforms' in request.GET: platforms = request.GET['platforms'].split(',') if platforms: f &= F('terms', platform=platforms) if 'locales' in request.GET: locales = request.GET['locales'].split(',') if locales: f &= F('terms', locale=locales) if 'products' in request.GET: products = request.GET['products'].split(',') if products: f &= F('terms', product=products) if 'versions' in request.GET: versions = request.GET['versions'].split(',') if versions: f &= F('terms', version=versions) date_start = smart_date(request.GET.get('date_start', None)) date_end = smart_date(request.GET.get('date_end', None)) delta = smart_timedelta(request.GET.get('date_delta', None)) if delta is not None: if date_end is not None: date_start = date_end - delta elif date_start is not None: date_end = date_start + delta else: date_end = date.today() date_start = date_end - delta # We restrict public API access to the last 6 months. six_months_ago = date.today() - timedelta(days=180) if date_start: date_start = max(six_months_ago, date_start) f &= F('range', created={'gte': date_start}) if date_end: date_end = max(six_months_ago, date_end) f &= F('range', created={'lte': date_end}) search_query = request.GET.get('q', None) if search_query is not None: search = search.query('simple_query_string', query=search_query, fields=['description']) # FIXME: Probably want to make this specifyable search = search.sort('-created') search = search.filter(f) maximum = smart_int(request.GET.get('max', None)) maximum = maximum or 1000 maximum = min(max(1, maximum), 10000) responses = list(search[:maximum].execute()) responses = models.ResponseDocType.docs.to_public(responses) return rest_framework.response.Response({ 'count': len(responses), 'results': responses })
def get(self, request): # FIXME: Rewrite this to use aggs and allow multiple layers. search = models.ResponseDocType.docs.search() f = F() if 'happy' in request.GET: happy = {'0': False, '1': True}.get(request.GET['happy'], None) if happy is not None: f &= F('term', happy=happy) if 'platforms' in request.GET: platforms = request.GET['platforms'].split(',') if platforms: f &= F('terms', platform=platforms) if 'locales' in request.GET: locales = request.GET['locales'].split(',') if locales: f &= F('terms', locale=locales) if 'products' in request.GET: products = request.GET['products'].split(',') if products: f &= F('terms', product=products) if 'versions' in request.GET: versions = request.GET['versions'].split(',') if versions: f &= F('terms', version=versions) if 'source' in request.GET: # FIXME: Having a , in the source is valid, so this might not work # right. sources = request.GET['source'].split(',') if sources: f &= F('terms', source=sources) if 'api' in request.GET: # The int (as a str) or "None" apis = request.GET['api'].split(',') if apis: f &= F('terms', api=apis) date_start = smart_date(request.GET.get('date_start', None)) date_end = smart_date(request.GET.get('date_end', None)) delta = smart_timedelta(request.GET.get('date_delta', None)) # Default to 7d. if not date_start and not date_end: delta = delta or smart_timedelta('7d') if delta is not None: if date_end is not None: date_start = date_end - delta elif date_start is not None: date_end = date_start + delta else: date_end = date.today() date_start = date_end - delta # If there's no end, then the end is today. if not date_end: date_end = date.today() # Restrict to a 6 month range. Must have a start date. if (date_end - date_start) > timedelta(days=180): date_end = date_start + timedelta(days=180) # date_start up to but not including date_end. f &= F('range', created={'gte': date_start, 'lt': date_end}) search_query = request.GET.get('q', None) if search_query is not None: search = search.query( 'simple_query_string', query=search_query, fields=['description']) search = search.filter(f) # FIXME: improve validation interval = request.GET.get('interval', 'day') if interval not in ('hour', 'day'): interval = 'day' search.aggs.bucket( 'histogram', 'date_histogram', field='created', interval=interval ) resp = search.execute() data = dict((p['key'], p['doc_count']) for p in resp.aggregations['histogram']['buckets']) zero_fill(date_start, date_end - timedelta(days=1), [data]) return rest_framework.response.Response({ 'results': sorted(data.items()) })
def get(self, request): """Returns JSON feed of first 10000 results This feels like a duplication of the front-page dashboard search logic, but it's separate which allows us to handle multiple values. """ search = models.ResponseMappingType.search() f = F() if 'happy' in request.GET: happy = {'0': False, '1': True}.get(request.GET['happy'], None) if happy is not None: f &= F(happy=happy) if 'platforms' in request.GET: platforms = request.GET['platforms'].split(',') if platforms: f &= F(platform__in=platforms) if 'locales' in request.GET: locales = request.GET['locales'].split(',') if locales: f &= F(locale__in=locales) if 'products' in request.GET: products = request.GET['products'].split(',') if products: f &= F(product__in=products) if 'versions' in request.GET: versions = request.GET['versions'].split(',') if versions: f &= F(version__in=versions) date_start = smart_date(request.GET.get('date_start', None)) date_end = smart_date(request.GET.get('date_end', None)) delta = smart_timedelta(request.GET.get('date_delta', None)) if delta is not None: if date_end is not None: date_start = date_end - delta elif date_start is not None: date_end = date_start + delta else: date_end = date.today() date_start = date_end - delta # We restrict public API access to the last 6 months. six_months_ago = date.today() - timedelta(days=180) if date_start: date_start = max(six_months_ago, date_start) f &= F(created__gte=date_start) if date_end: date_end = max(six_months_ago, date_end) f &= F(created__lte=date_end) search = search.filter(f) search_query = request.GET.get('q', None) if search_query is not None: search = search.query(description__sqs=search_query) # FIXME: Probably want to make this specifyable search = search.order_by('-created') # Explicitly include only publicly visible fields search = search.values_dict(*models.ResponseMappingType.public_fields()) maximum = smart_int(request.GET.get('max', None)) maximum = maximum or 1000 maximum = min(max(1, maximum), 10000) responses = models.ResponseMappingType.reshape(search[:maximum]) return rest_framework.response.Response({ 'count': len(responses), 'results': list(responses) })
def test_invalid(self): eq_(smart_timedelta("0d", "fallback"), "fallback") eq_(smart_timedelta("foo", "fallback"), "fallback") eq_(smart_timedelta("d", "fallback"), "fallback")
def get(self, request): search = models.ResponseMappingType.search() f = F() if 'happy' in request.GET: happy = {'0': False, '1': True}.get(request.GET['happy'], None) if happy is not None: f &= F(happy=happy) if 'platforms' in request.GET: platforms = request.GET['platforms'].split(',') if platforms: f &= F(platform__in=platforms) if 'locales' in request.GET: locales = request.GET['locales'].split(',') if locales: f &= F(locale__in=locales) if 'products' in request.GET: products = request.GET['products'].split(',') if products: f &= F(product__in=products) if 'versions' in request.GET: versions = request.GET['versions'].split(',') if versions: f &= F(version__in=versions) date_start = smart_date(request.GET.get('date_start', None)) date_end = smart_date(request.GET.get('date_end', None)) delta = smart_timedelta(request.GET.get('date_delta', None)) # Default to 7d. if not date_start and not date_end: delta = delta or smart_timedelta('7d') if delta is not None: if date_end is not None: date_start = date_end - delta elif date_start is not None: date_end = date_start + delta else: date_end = date.today() date_start = date_end - delta # If there's no end, then the end is today. if not date_end: date_end = date.today() # Restrict to a 6 month range. Must have a start date. if (date_end - date_start) > timedelta(days=180): date_end = date_start + timedelta(days=180) # date_start up to but not including date_end. f &= F(created__gte=date_start, created__lt=date_end) search_query = request.GET.get('q', None) if search_query is not None: search = search.query(description__sqs=search_query) search = search.filter(f) # FIXME: improve validation interval = request.GET.get('interval', 'day') if interval not in ('hour', 'day'): interval = 'day' histograms = search.facet_raw( counts={ 'date_histogram': {'interval': interval, 'field': 'created'}, 'facet_filter': search._process_filters(f.filters) } ).facet_counts() data = dict((p['time'], p['count']) for p in histograms['counts']) zero_fill(date_start, date_end - timedelta(days=1), [data]) return rest_framework.response.Response({ 'results': sorted(data.items()) })
def get(self, request): # FIXME: Rewrite this to use aggs and allow multiple layers. search = models.ResponseDocType.docs.search() f = F() if 'happy' in request.GET: happy = {'0': False, '1': True}.get(request.GET['happy'], None) if happy is not None: f &= F('term', happy=happy) if 'platforms' in request.GET: platforms = request.GET['platforms'].split(',') if platforms: f &= F('terms', platform=platforms) if 'locales' in request.GET: locales = request.GET['locales'].split(',') if locales: f &= F('terms', locale=locales) if 'products' in request.GET: products = request.GET['products'].split(',') if products: f &= F('terms', product=products) if 'versions' in request.GET: versions = request.GET['versions'].split(',') if versions: f &= F('terms', version=versions) date_start = smart_date(request.GET.get('date_start', None)) date_end = smart_date(request.GET.get('date_end', None)) delta = smart_timedelta(request.GET.get('date_delta', None)) # Default to 7d. if not date_start and not date_end: delta = delta or smart_timedelta('7d') if delta is not None: if date_end is not None: date_start = date_end - delta elif date_start is not None: date_end = date_start + delta else: date_end = date.today() date_start = date_end - delta # If there's no end, then the end is today. if not date_end: date_end = date.today() # Restrict to a 6 month range. Must have a start date. if (date_end - date_start) > timedelta(days=180): date_end = date_start + timedelta(days=180) # date_start up to but not including date_end. f &= F('range', created={'gte': date_start, 'lt': date_end}) search_query = request.GET.get('q', None) if search_query is not None: search = search.query('simple_query_string', query=search_query, fields=['description']) search = search.filter(f) # FIXME: improve validation interval = request.GET.get('interval', 'day') if interval not in ('hour', 'day'): interval = 'day' search.aggs.bucket('histogram', 'date_histogram', field='created', interval=interval) resp = search.execute() data = dict((p['key'], p['doc_count']) for p in resp.aggregations['histogram']['buckets']) zero_fill(date_start, date_end - timedelta(days=1), [data]) return rest_framework.response.Response( {'results': sorted(data.items())})
def get(self, request): """Returns JSON feed of first 10000 results This feels like a duplication of the front-page dashboard search logic, but it's separate which allows us to handle multiple values. """ search = models.ResponseMappingType.search() f = F() if 'happy' in request.GET: happy = {'0': False, '1': True}.get(request.GET['happy'], None) if happy is not None: f &= F(happy=happy) if 'platforms' in request.GET: platforms = request.GET['platforms'].split(',') if platforms: f &= F(platform__in=platforms) if 'locales' in request.GET: locales = request.GET['locales'].split(',') if locales: f &= F(locale__in=locales) if 'products' in request.GET: products = request.GET['products'].split(',') if products: f &= F(product__in=products) if 'versions' in request.GET: versions = request.GET['versions'].split(',') if versions: f &= F(version__in=versions) date_start = smart_date(request.GET.get('date_start', None)) date_end = smart_date(request.GET.get('date_end', None)) delta = smart_timedelta(request.GET.get('date_delta', None)) if delta is not None: if date_end is not None: date_start = date_end - delta elif date_start is not None: date_end = date_start + delta else: date_end = date.today() date_start = date_end - delta # We restrict public API access to the last 6 months. six_months_ago = date.today() - timedelta(days=180) if date_start: date_start = max(six_months_ago, date_start) f &= F(created__gte=date_start) if date_end: date_end = max(six_months_ago, date_end) f &= F(created__lte=date_end) search = search.filter(f) search_query = request.GET.get('q', None) if search_query is not None: search = search.query(description__sqs=search_query) # FIXME: Probably want to make this specifyable search = search.order_by('-created') # Explicitly include only publicly visible fields search = search.values_dict(models.ResponseMappingType.public_fields()) maximum = smart_int(request.GET.get('max', None)) maximum = maximum or 1000 maximum = min(max(1, maximum), 10000) responses = models.ResponseMappingType.reshape(search[:maximum]) return rest_framework.response.Response({ 'count': len(responses), 'results': list(responses) })
def test_valid(self): eq_(smart_timedelta("1d"), datetime.timedelta(days=1)) eq_(smart_timedelta("14d"), datetime.timedelta(days=14))