Пример #1
0
    def get(self):
        """Get the route metrics"""
        args = metrics_route_parser.parse_args()
        if args.get("limit") is None:
            args["limit"] = 168
        else:
            args["limit"] = min(args.get("limit"), 168)

        if args.get("bucket_size") is None:
            args["bucket_size"] = "hour"
        if args.get("bucket_size") not in valid_date_buckets:
            abort_bad_request_param("bucket_size", ns)

        try:
            args["start_time"] = parse_unix_epoch_param(args.get("start_time"), 0)
        except Exception:
            abort_bad_request_param("start_time", ns)

        if args.get("exact") is not None:
            args["exact"] = parse_bool_param(args.get("exact"))
            if args.get("exact") is None:
                abort_bad_request_param("exact", ns)
        else:
            args["exact"] = False

        args["path"] = args.get("path") if args.get("path") is not None else ""
        route_metrics = get_route_metrics(args)
        response = success_response(route_metrics)
        return response
Пример #2
0
    def get(self):
        """Get the route metrics"""
        args = metrics_route_parser.parse_args()
        if args.get('limit') is None:
            args['limit'] = 168
        else:
            args['limit'] = min(args.get('limit'), 168)

        if args.get('bucket_size') is None:
            args['bucket_size'] = 'hour'
        if args.get('bucket_size') not in valid_date_buckets:
            abort_bad_request_param('bucket_size', ns)

        try:
            args['start_time'] = parse_unix_epoch_param(args.get('start_time'), 0)
        except:
            abort_bad_request_param('start_time', ns)

        if args.get('exact') is not None:
            args['exact'] = parse_bool_param(args.get('exact'))
            if args.get('exact') is None:
                abort_bad_request_param('exact', ns)
        else:
            args['exact'] = False

        args['path'] = args.get('path') if args.get('path') is not None else ''
        route_metrics = get_route_metrics(args)
        response = success_response(route_metrics)
        return response
Пример #3
0
    def get(self):
        """Get the route metrics"""
        args = metrics_route_parser.parse_args()
        if args.get('limit') is None:
            args['limit'] = 48
        else:
            args['limit'] = min(args.get('limit'), 48)
        try:
            args['start_time'] = datetime.utcfromtimestamp(args['start_time'])
        except:
            return api_helpers.error_response(
                'Poorly formated start_time parameter', 400)

        route_metrics = get_route_metrics(args)
        response = success_response(route_metrics)
        return response
def test_get_route_metrics_no_matches(db_mock):
    """Tests that route metrics returns nothing if no matching query_string"""

    date = datetime.utcnow().replace(minute=0, second=0, microsecond=0)
    before_date = (date + timedelta(hours=-1))

    populate_mock_db(db_mock, date)

    args = {
        'limit': 10,
        'start_time': before_date,
        'path': 'tracks/some_hash',
        'query_string': 'with_users=WRONG',
        'exact': False
    }
    metrics = get_route_metrics(args)

    assert not metrics
def test_get_route_metrics_non_exact(db_mock):
    """Tests that the route metrics can be queried by non-exact path match"""

    date = datetime.utcnow().replace(minute=0, second=0, microsecond=0)
    before_date = (date + timedelta(hours=-1))

    populate_mock_db(db_mock, date)

    args = {
        'limit': 10,
        'start_time': before_date,
        'path': 'tracks/some_hash',
        'exact': False
    }
    metrics = get_route_metrics(args)

    assert len(metrics) == 1
    assert metrics[0]['count'] == 11
    assert metrics[0]['unique_count'] == 2
def test_get_route_metrics_query_string(db_mock):
    """Tests that the route metrics are queried with query_string parameter"""

    date = datetime.utcnow().replace(minute=0, second=0, microsecond=0)
    before_date = (date + timedelta(hours=-1))

    populate_mock_db(db_mock, date)

    args = {
        'limit': 10,
        'start_time': before_date,
        'path': 'tracks/some_hash',
        'query_string': 'with_users=true',
        'exact': False
    }
    metrics = get_route_metrics(args)

    assert len(metrics) == 1
    assert metrics[0]['count'] == 2
    assert metrics[0]['unique_count'] == 1
Пример #7
0
def test_get_route_metrics(db_mock):
    """Tests that the route metrics are queried correctly from db data"""

    date = datetime.utcnow().replace(minute=0, second=0, microsecond=0)
    before_all_date = (date + timedelta(hours=-2))
    before_date = (date + timedelta(hours=-1))

    route_metrics = [{
        'version': '1',
        'route_path': 'tracks/some_hash',
        'query_string': '',
        'count': 3,
        'timestamp': before_date
    }, {
        'version': '1',
        'route_path': 'tracks/some_hash',
        'query_string': 'with_users=true',
        'count': 2,
        'timestamp': before_date
    }]

    RouteMetrics.__table__.create(db_mock._engine)

    # Set up db state
    with db_mock.scoped_session() as session:
        route_metric_obj = [RouteMetrics(
            version=metric['version'],
            route_path=metric['route_path'],
            query_string=metric['query_string'],
            count=metric['count'],
            timestamp=metric['timestamp']
        ) for metric in route_metrics]

        session.bulk_save_objects(route_metric_obj)

    args_1 = {
        'limit': 10,
        'start_time': before_all_date,
        'path': 'tracks/some_hash'
    }
    metrics_1 = get_route_metrics(args_1)

    assert len(metrics_1) == 1
    assert metrics_1[0]['count'] == 5

    args_2 = {
        'limit': 10,
        'start_time': before_all_date,
        'path': 'tracks/some_hash',
        'query_string': 'with_users=true'
    }
    metrics_2 = get_route_metrics(args_2)

    assert len(metrics_2) == 1
    assert metrics_2[0]['count'] == 2

    args_3 = {
        'limit': 10,
        'start_time': before_all_date,
        'path': 'tracks/some_hash',
        'query_string': 'with_users=WRONG'
    }
    metrics_3 = get_route_metrics(args_3)

    assert not metrics_3