def get(self, request, pk, metric): if metric not in APP_STATS: raise http.Http404('No metric by that name.') if not waffle.switch_is_active('stats-api'): raise NotImplemented('Stats not enabled for this host.') app = self.get_object() stat = APP_STATS[metric] # Perform form validation. form = StatsForm(request.GET) if not form.is_valid(): raise ParseError(dict(form.errors.items())) qs = form.cleaned_data dimensions = {'app-id': app.id} if 'dimensions' in stat: for key, default in stat['dimensions'].items(): val = request.GET.get(key, default) if val is not None: # Avoid passing kwargs to the monolith client when the # dimension is None to avoid facet filters being applied. dimensions[key] = request.GET.get(key, default) return Response( _get_monolith_data(stat, qs.get('start'), qs.get('end'), qs.get('interval'), dimensions))
def get(self, request, metric): if metric not in STATS: raise http.Http404('No metric by that name.') if not waffle.switch_is_active('stats-api'): raise NotImplemented('Stats not enabled for this host.') # Perform form validation. form = GlobalStatsForm(request.GET) if not form.is_valid(): raise ParseError(dict(form.errors.items())) data = form.cleaned_data client = get_monolith_client() try: metric_data = list(client(STATS[metric]['metric'], data.get('start'), data.get('end'), data.get('interval'))) except ValueError: # This occurs if monolith doesn't have our metric and we get an # elasticsearch SearchPhaseExecutionException error. log.info('Monolith ValueError for metric {0}'.format( STATS[metric]['metric'])) raise ParseError('Invalid metric at this time. Try again later.') return Response({'objects': metric_data})
def get(self, request, metric): if metric not in STATS: raise http.Http404('No metric by that name.') if not waffle.switch_is_active('stats-api'): raise NotImplemented('Stats not enabled for this host.') stat = STATS[metric] # Perform form validation. form = GlobalStatsForm(request.GET) if not form.is_valid(): raise ParseError(dict(form.errors.items())) qs = form.cleaned_data client = get_monolith_client() dimensions = {} if 'dimensions' in stat: for key, default in stat['dimensions'].items(): val = request.GET.get(key, default) if val is not None: # Avoid passing kwargs to the monolith client when the # dimension is None to avoid facet filters being applied. dimensions[key] = request.GET.get(key, default) # If stat has a 'lines' attribute, it's a multi-line graph. Do a # request for each item in 'lines' and compose them in a single # response. try: data = {} if 'lines' in stat: for line_name, line_dimension in stat['lines'].items(): dimensions.update(line_dimension) data[line_name] = list( client(stat['metric'], qs.get('start'), qs.get('end'), qs.get('interval'), **dimensions)) else: data['objects'] = list( client(stat['metric'], qs.get('start'), qs.get('end'), qs.get('interval'), **dimensions)) except ValueError as e: # This occurs if monolith doesn't have our metric and we get an # elasticsearch SearchPhaseExecutionException error. log.info('Monolith ValueError for metric {0}: {1}'.format( stat['metric'], e)) raise ParseError('Invalid metric at this time. Try again later.') return Response(data)