Beispiel #1
0
def test_metric_empty_source_handling():
    config = get_config()

    metric = Metric(spec={
        'name': 'foo/var',
    },
                    feature_name='foo',
                    config=config)

    with pytest.raises(MycroftMetricError):
        metric.fetch_value()
Beispiel #2
0
def test_metric_missing_source_handling():
    config = get_config()

    metric = Metric(spec={
        'name': 'foo/var',
        'source': 'foo/missing-source'
    },
                    feature_name='foo',
                    config=config)

    with pytest.raises(MycroftSourceError):
        metric.fetch_value()
Beispiel #3
0
def test_fetch_value_ignore_weight():
    config = Config(config_file=get_fixtures_directory() + '/config.yaml')

    metric = Metric(spec={
        'name': 'foo/weighted-bar',
        'source': 'common/const',
        'weight': 1000,
    }, feature_name='foo', config=config)

    print(metric)
    assert metric.get_weight() == 1000
    assert metric.fetch_value() == 1  # value should not be affected by metric's weight
Beispiel #4
0
def test_fetch_value():
    config = Config(config_file=get_fixtures_directory() + '/config.yaml')

    metric = Metric(spec={
        'name': 'foo/bar',
        'source': 'common/const',
    }, feature_name='foo', config=config)

    print(metric)
    assert metric.get_source_name() == ConstSource.NAME
    assert metric.fetch_value() == 1
    assert metric.value == 1, 'We should not need to connect to database to get this'
Beispiel #5
0
def test_float_value():
    config = get_config()
    spec = dict(label='This is a metric: %d')

    metric = Metric('feature_foo', config, spec)

    metric.set_value(345.11)
    assert metric.get_label() == 'This is a metric'
    assert metric.get_label_with_value() == 'This is a metric: 345.11'

    metric.set_value(312)
    assert metric.get_label() == 'This is a metric'
    assert metric.get_label_with_value() == 'This is a metric: 312'
Beispiel #6
0
def test_metric():
    config = get_config(
        env={
            'JIRA_URL': 'https://foo.atlasian.net',
            'JIRA_USER': '******',
            'JIRA_PASSWORD': '******',
        })
    metric = Metric(spec={
        'name': 'jira/p2-tickets',
        'source': 'wikia/jira',
        'template': {
            'project': 'DynamicPageList',
            'tag': 'dpl'
        }
    },
                    feature_name='foo',
                    config=config)

    print(metric)

    assert metric.get_name() == 'jira/p2-tickets'
    assert metric.get_source_name() == 'wikia/jira'
    assert metric.get_spec() == {
        'name': 'jira/p2-tickets',
        'source': 'wikia/jira',
        'template': {
            'project': 'DynamicPageList',
            'tag': 'dpl'
        }
    }

    source = SourceBase.new_for_metric(metric=metric, config=config)

    print(source)

    assert isinstance(
        source, JiraSource
    ), 'get_source_from_metric should return an instance of JiraSource'
    assert source._server == 'https://foo.atlasian.net'
    assert source._basic_auth == ('MrFoo',
                                  '9bec73487c01653ad7830c25e4b1dc926d17e518')
Beispiel #7
0
def test_empty_metric_and_label():
    config = get_config()

    metric = Metric(spec={
        'name': 'foo/var',
    },
                    feature_name='foo',
                    config=config)

    # the value is empty, storage read seems to fail for it
    metric.set_value(None)

    assert metric.get_label() is None
    assert metric.value is None
    assert metric.get_formatted_value() is None
    assert metric.get_label_with_value() is None
    assert metric.get_more_link() is None
Beispiel #8
0
def test_metric_get_weight():
    config = get_config()

    metric = Metric(spec={
        'name': 'foo/var',
        'weight': 2.5
    },
                    feature_name='foo',
                    config=config)

    print(metric)
    assert metric.get_weight() == 2.5

    metric = Metric(spec={'name': 'foo/var'},
                    feature_name='foo',
                    config=config)

    print(metric)
    assert metric.get_weight() == 1  # a default value
Beispiel #9
0
def test_format_value():
    assert Metric.format_value(1) == '1'
    assert Metric.format_value(12) == '12'
    assert Metric.format_value(123) == '123'
    assert Metric.format_value(1213) == '1.21k'
    assert Metric.format_value(22213) == '22.2k'
    assert Metric.format_value(222130) == '222k'
    assert Metric.format_value(222930) == '223k'

    assert Metric.format_value(10556844.0) == '10.56M'
    assert Metric.format_value(22832412) == '22.83M'

    # floats (#63)
    assert Metric.format_value(20.56) == '20.56'
    assert Metric.format_value(
        120.5675
    ) == '120.57'  # we always assume two digits precision + rounding
    assert Metric.format_value(1120.56) == '1.12k'
    assert Metric.format_value(222930.56) == '223k'
Beispiel #10
0
def test_storage():
    if environ.get('TEST_DATABASE') is None:
        raise SkipTest(
            'TEST_DATABASE env variable needs to be set to run this test.')

    storage = MetricsStorage(config=ConfigForMetricsStorage(), use_slave=False)

    # clean up the storage
    cursor = storage.storage.cursor()
    cursor.execute('TRUNCATE TABLE features_metrics')

    # push some metrics and later on try to get them
    storage.push('foo', {'score': 123, 'bar/metric': 42.458})
    storage.push('bar', {'score': 1, 'bar/metric': -3})
    storage.commit(timestamp=TIMESTAMP)

    # multiple values in the same day - test values aggregation
    storage.push('bar', {'score': 1, 'bar/metric': 6})
    storage.commit(timestamp=TIMESTAMP)

    storage.push('bar', {'score': 5, 'bar/metric': -4})
    storage.commit(timestamp=TIMESTAMP_LATER)

    assert storage.get(feature_id='foo', feature_metric='score') == 123
    assert storage.get(feature_id='foo', feature_metric='bar/metric'
                       ) == 42.46, 'Storage keeps floats with scale of 2'

    assert storage.get(
        feature_id='bar',
        feature_metric='score') == 5, 'The most recent value should be taken'
    assert storage.get(
        feature_id='bar',
        feature_metric='bar/metric') == -4, 'Negative values are accepted'

    assert storage.get(
        feature_id='not_existing',
        feature_metric='bar/metric') is None, 'Not existing metric'

    # now check if we can get the metric value
    metric = Metric(feature_name='Bar',
                    config=ConfigForMetricsStorage(),
                    spec={'name': 'bar/metric'})
    assert metric.value == -4, 'Get the most recent value from the storage'
    assert isinstance(metric.value, int), 'The value is returned as an int'

    metric = Metric(feature_name='Foo',
                    config=ConfigForMetricsStorage(),
                    spec={'name': 'bar/metric'})
    assert metric.value == 42.46, 'Get the most recent value from the storage'
    assert isinstance(metric.value, float), 'The value is returned as a float'

    # check the latest timestamp
    assert storage.get_the_latest_timestamp() == TIMESTAMP_LATER

    # get metrics history
    assert list(storage.get_feature_metrics_history(
        feature__id='not_existing')) == [], 'Not existing feature'

    assert list(storage.get_feature_metrics_history(feature__id='foo')) == [
        {
            'date': '2019-03-02',
            'metric': 'bar/metric',
            'value': 42.46
        },
        {
            'date': '2019-03-02',
            'metric': 'score',
            'value': 123
        },
    ]

    assert list(storage.get_feature_metrics_history(feature__id='bar')) == [
        {
            'date': '2019-03-02',
            'metric': 'bar/metric',
            'value': 6.0
        },  # get max value for the day
        {
            'date': '2019-03-02',
            'metric': 'score',
            'value': 1.0
        },
        {
            'date': '2019-03-04',
            'metric': 'bar/metric',
            'value': -4.0
        },
        {
            'date': '2019-03-04',
            'metric': 'score',
            'value': 5.0
        }
    ]