def test_fetch_tag_support(self): class TestFinderTags(BaseFinder): tags = True def find_nodes(self, query): pass def fetch(self, patterns, start_time, end_time, now=None, requestContext=None): if patterns != ['seriesByTag("hello=tiger")', 'seriesByTag("name=notags")', 'seriesByTag("name=testtags")', 'testtags;hello=tiger']: raise Exception('Unexpected patterns %s' % str(patterns)) return [ { 'pathExpression': 'testtags;hello=tiger', 'name': 'testtags;hello=tiger', 'time_info': (0, 60, 1), 'values': [], }, { 'pathExpression': 'seriesByTag("hello=tiger")', 'name': 'testtags;hello=tiger', 'time_info': (0, 60, 1), 'values': [], }, { 'pathExpression': 'seriesByTag("name=testtags")', 'name': 'testtags;hello=tiger', 'time_info': (0, 60, 1), 'values': [], }, ] tagdb = Mock() store = Store( finders=[TestFinderTags()], tagdb=tagdb ) request_context = { 'startTime': epoch_to_dt(0), 'endTime': epoch_to_dt(60), 'now': epoch_to_dt(60), } with patch('graphite.render.datalib.STORE', store): results = evaluateTarget(request_context, ['testtags;hello=tiger', 'seriesByTag("hello=tiger")', 'seriesByTag("name=testtags")', 'seriesByTag("name=notags")']) self.assertEqual(results, [ TimeSeries('testtags;hello=tiger', 0, 60, 1, []), TimeSeries('testtags;hello=tiger', 0, 60, 1, [], pathExpression='seriesByTag("hello=tiger")'), TimeSeries('testtags;hello=tiger', 0, 60, 1, [], pathExpression='seriesByTag("name=testtags")'), ])
def test_fetch_no_tag_support(self): class TestFinderNoTags(BaseFinder): tags = False def find_nodes(self, query): pass def fetch(self, patterns, start_time, end_time, now=None, requestContext=None): if patterns != ['notags;hello=tiger']: raise Exception('Unexpected patterns %s' % str(patterns)) return [ { 'pathExpression': 'notags;hello=tiger', 'name': 'notags;hello=tiger', 'time_info': (0, 60, 1), 'values': [], } ] tagdb = Mock() def mockFindSeries(exprs, requestContext=None): self.assertEqual(requestContext, request_context) if exprs == ('hello=tiger',) or exprs == ('name=notags',): return ['notags;hello=tiger'] if exprs == ('name=testtags',): return [] raise Exception('Unexpected exprs %s' % str(exprs)) tagdb.find_series.side_effect = mockFindSeries store = Store( finders=[TestFinderNoTags()], tagdb=tagdb ) with patch('graphite.render.datalib.STORE', store): request_context = { 'startTime': epoch_to_dt(0), 'endTime': epoch_to_dt(60), 'now': epoch_to_dt(60), } results = evaluateTarget(request_context, ['notags;hello=tiger', 'seriesByTag("hello=tiger")', 'seriesByTag("name=testtags")', 'seriesByTag("name=notags")']) self.assertEqual(tagdb.find_series.call_count, 3) self.assertEqual(results, [ TimeSeries('notags;hello=tiger', 0, 60, 1, []), TimeSeries('notags;hello=tiger', 0, 60, 1, [], pathExpression='seriesByTag("hello=tiger")'), TimeSeries('notags;hello=tiger', 0, 60, 1, [], pathExpression='seriesByTag("name=notags")'), ])
def post_event(request): if request.method == 'POST': event = json.loads(request.body) assert isinstance(event, dict) tags = event.get('tags') if tags is not None: if isinstance(tags, list): tags = ' '.join(tags) elif not isinstance(tags, six.string_types): return HttpResponse(json.dumps({ 'error': '"tags" must be an array or space-separated string' }), status=400) else: tags = None if 'when' in event: when = epoch_to_dt(event['when']) else: when = now() Event.objects.create( what=event.get('what'), tags=tags, when=when, data=event.get('data', ''), ) return HttpResponse(status=200) else: return HttpResponse(status=405)
def fetch(request): if request.GET.get('from') is not None: time_from = parseATTime(request.GET['from']) else: time_from = epoch_to_dt(0) if request.GET.get('until') is not None: time_until = parseATTime(request.GET['until']) else: time_until = now() set_operation = request.GET.get('set') tags = request.GET.get('tags') if tags is not None: tags = request.GET.get('tags').split(' ') result = [] for x in Event.find_events(time_from, time_until, tags=tags, set_operation=set_operation): # django-tagging's with_intersection() returns matches with unknown tags # this is a workaround to ensure we only return positive matches if set_operation == 'intersection': if len(set(tags) & set(x.as_dict()['tags'])) == len(tags): result.append(x.as_dict()) else: result.append(x.as_dict()) return result
def post_event(request): if request.method == 'POST': event = json.loads(request.body) assert isinstance(event, dict) tags = event.get('tags') if tags is not None: if isinstance(tags, list): tags = ' '.join(tags) elif not isinstance(tags, six.string_types): return HttpResponse( json.dumps({'error': '"tags" must be an array or space-separated string'}), status=400) else: tags = None if 'when' in event: when = epoch_to_dt(event['when']) else: when = now() Event.objects.create( what=event.get('what'), tags=tags, when=when, data=event.get('data', ''), ) return HttpResponse(status=200) else: return HttpResponse(status=405)
def test_epoch_to_dt(self): dt = pytz.utc.localize(datetime(1970, 1, 1, 0, 10, 0, 0)) self.assertEqual(util.epoch_to_dt(600), dt)
def test_fetch_no_tag_support(self): class TestFinderNoTags(BaseFinder): tags = False def find_nodes(self, query): pass def fetch(self, patterns, start_time, end_time, now=None, requestContext=None): if patterns != ['notags;hello=tiger']: raise Exception('Unexpected patterns %s' % str(patterns)) return [{ 'pathExpression': 'notags;hello=tiger', 'name': 'notags;hello=tiger', 'time_info': (0, 60, 1), 'values': [], }] tagdb = Mock() def mockFindSeries(exprs, requestContext=None): self.assertEqual(requestContext, request_context) if exprs == ('hello=tiger', ) or exprs == ('name=notags', ): return ['notags;hello=tiger'] if exprs == ('name=testtags', ): return [] raise Exception('Unexpected exprs %s' % str(exprs)) tagdb.find_series.side_effect = mockFindSeries store = Store(finders=[TestFinderNoTags()], tagdb=tagdb) with patch('graphite.render.datalib.STORE', store): request_context = { 'startTime': epoch_to_dt(0), 'endTime': epoch_to_dt(60), 'now': epoch_to_dt(60), } results = evaluateTarget(request_context, [ 'notags;hello=tiger', 'seriesByTag("hello=tiger")', 'seriesByTag("name=testtags")', 'seriesByTag("name=notags")' ]) self.assertEqual(tagdb.find_series.call_count, 3) self.assertEqual(results, [ TimeSeries('notags;hello=tiger', 0, 60, 1, []), TimeSeries('notags;hello=tiger', 0, 60, 1, [], pathExpression='seriesByTag("hello=tiger")'), TimeSeries('notags;hello=tiger', 0, 60, 1, [], pathExpression='seriesByTag("name=notags")'), ])
def test_fetch_tag_support(self): class TestFinderTags(BaseFinder): tags = True def find_nodes(self, query): pass def fetch(self, patterns, start_time, end_time, now=None, requestContext=None): if patterns != [ 'seriesByTag("hello=tiger")', 'seriesByTag("name=notags")', 'seriesByTag("name=testtags")', 'testtags;hello=tiger' ]: raise Exception('Unexpected patterns %s' % str(patterns)) return [ { 'pathExpression': 'testtags;hello=tiger', 'name': 'testtags;hello=tiger', 'time_info': (0, 60, 1), 'values': [], }, { 'pathExpression': 'seriesByTag("hello=tiger")', 'name': 'testtags;hello=tiger', 'time_info': (0, 60, 1), 'values': [], }, { 'pathExpression': 'seriesByTag("name=testtags")', 'name': 'testtags;hello=tiger', 'time_info': (0, 60, 1), 'values': [], }, ] tagdb = Mock() store = Store(finders=[TestFinderTags()], tagdb=tagdb) request_context = { 'startTime': epoch_to_dt(0), 'endTime': epoch_to_dt(60), 'now': epoch_to_dt(60), } with patch('graphite.render.datalib.STORE', store): results = evaluateTarget(request_context, [ 'testtags;hello=tiger', 'seriesByTag("hello=tiger")', 'seriesByTag("name=testtags")', 'seriesByTag("name=notags")' ]) self.assertEqual(results, [ TimeSeries('testtags;hello=tiger', 0, 60, 1, []), TimeSeries('testtags;hello=tiger', 0, 60, 1, [], pathExpression='seriesByTag("hello=tiger")'), TimeSeries('testtags;hello=tiger', 0, 60, 1, [], pathExpression='seriesByTag("name=testtags")'), ])