def metric(): metric_id = request.args.get('id') if not metric_id: abort(jsonify(status=400, message='id parameter required')) metric = report_util.get_metric(metric_id) # A metric has a histogram if it is not explicitly disabled. has_histogram = metric and metric.get('histogram', {}).get('enabled', True) latest = report_util.get_latest_date( metric_id) if metric and has_histogram else None return jsonify(status=200, metric=metric, latest=latest)
def report(report_id): report = report_util.get_report(report_id) if not report: abort(404) dates = report_util.get_dates() if not dates: abort(500) min_date = report.get('minDate') max_date = report.get('maxDate') date_pattern = report.get('datePattern') max_date_metric = report.get('maxDateMetric') # TODO: If a report doesn't explicitly have a min/max date, # but all of its metrics do, take the min/max of the metrics # and set that as the report's implicit min/max date. # Omit dates for which this report has no data. if max_date_metric: max_date = report_util.get_latest_date(max_date_metric) if min_date: dates = dates[:dates.index(min_date) + 1] if max_date: dates = dates[dates.index(max_date):] if date_pattern: date_pattern = re.compile(date_pattern) dates = [d for d in dates if date_pattern.match(d)] report['dates'] = dates report['lenses'] = report_util.get_lenses() start = request.args.get('start') end = request.args.get('end') # Canonicalize single-date formats. if end and not start: start, end = end, start # Canonicalize aliases. if start == 'latest': start = dates[0] elif start == 'earliest': start = dates[-1] if end == 'latest': end = dates[0] elif end == 'earliest': end = dates[-1] # This is longhand for the snapshot (histogram) view. if start == end: end = None # This is shorthand for the trends (timeseries) view. if not start and not end: # The default date range is 24 crawls (1 year). # May be shorter if the report's minimum date is more recent. start = dates[min(24, len(dates) - 1)] end = dates[0] if start and start not in dates: abort(400) if end and end not in dates: abort(400) viz = report_util.VizTypes.HISTOGRAM if (start and not end) else report_util.VizTypes.TIMESERIES if viz == report_util.VizTypes.TIMESERIES and report.get('timeseries') and not report.get('timeseries').get('enabled'): end = None viz = report_util.VizTypes.HISTOGRAM # The default for histograms should be the latest date. if not request.args.get('start'): start = dates[0] lens_id = get_lens_id(request) lens = report_util.get_lens(lens_id) if lens: report['lens'] = lens report['view'] = get_report_view(report, request) # Determine which metrics should be enabled for this report. for metric in report['metrics']: # Get a list of reports that also contain this metric. metric['similar_reports'] = report_util.get_similar_reports(metric['id'], report_id) # Mark the lens used for this metric, if applicable. if lens: metric['lens'] = lens metric[viz] = metric.get(viz, {}) enabled = metric[viz].get('enabled', True) min_date = metric[viz].get('minDate', start) max_date = metric[viz].get('maxDate', end) # Disabled metrics should stay that way. if not enabled: continue # Disable the metric if it start/end is outside of the min/max window. enabled = start >= min_date if max_date and enabled: enabled = start <= max_date if end and enabled: enabled = end <= max_date metric[viz]['enabled'] = enabled if not request.script_root: request.script_root = url_for('report', report_id=report_id, _external=True) # Return as JSON if requested. if get_format(request) == 'json': return jsonify(status=200, report=report, start=start, end=end, viz=viz) return render_template('report/%s.html' % viz, viz=viz, reports=report_util.get_reports(), report=report, start=start, end=end)