Ejemplo n.º 1
0
def update_resources_metrics_from_backend():
    '''
    Update resource's metrics from backend.
    Get a sum of all metrics for `resource_views` on the backend and
    attach them to `resource.metrics`.
    '''
    log.info('Updating resources metrics from backend...')
    client = metrics_client_factory()
    result = client.get_previous_day_measurements(collection='resource',
                                                  tag='resource')
    for (_, keys), _values in result.items():
        values = next(_values)
        values.pop('time')
        resource_id = keys['resource']

        specific_result = client.sum_views_from_specific_model(
            collection='resource', tag='resource', model_id=resource_id)
        specific_values = next(specific_result.get_points())

        try:
            resource_id = uuid.UUID(resource_id)
            resource = get_resource(resource_id)
        except Exception as e:
            log.exception(e)
            continue
        if resource:
            log.debug(f'Found resource {resource.id}: {specific_values}')
            resource.metrics['views'] = specific_values['sum_nb_visits']
            try:
                resource.save(signal_kwargs={'ignores': ['post_save']})
            except Exception as e:
                log.exception(e)
                continue
        else:
            log.error('Resource not found', extra={'id': resource_id})
Ejemplo n.º 2
0
def write_object_metrics(document):
    dt = datetime.datetime.now()
    client = metrics_client_factory()
    metrics = document.get_metrics()
    body = {
        'time': dt,
        'measurement': 'site_metrics',
        'fields': metrics
    }
    client.insert(body)
Ejemplo n.º 3
0
def update_community_resources_metrics_from_backend():
    '''
    Update community resource's metrics from backend.
    Get a sum of all metrics for `community_resource_views` on the backend and
    attach them to `community_resource.metrics`.
    '''
    log.info('Updating community resources metrics from backend...')
    client = metrics_client_factory()
    result = client.get_previous_day_measurements(
        collection='community_resource', tag='communityresource')
    process_metrics_result(client, result, CommunityResource)
Ejemplo n.º 4
0
def update_users_metrics_from_backend():
    '''
    Update users' metrics from backend.

    Get a sum of all metrics for `user_views` on the backend and
    attach them to `user.metrics`.
    '''
    log.info('Updating users metrics from backend...')
    client = metrics_client_factory()
    result = client.get_previous_day_measurements(collection='user',
                                                  tag='user_view')
    process_metrics_result(client, result, User)
Ejemplo n.º 5
0
def update_organizations_metrics_from_backend():
    '''
    Update organizations' metrics from backend.

    Get a sum of all metrics for `organization_views` on the backend and
    attach them to `organization.metrics`.
    '''
    log.info('Updating organizations metrics from backend...')
    client = metrics_client_factory()
    result = client.get_previous_day_measurements(collection='organization',
                                                  tag='organization')
    process_metrics_result(client, result, Organization)
Ejemplo n.º 6
0
def update_reuses_metrics_from_backend():
    '''
    Update reuses' metrics from backend.

    Get a sum of all metrics for `reuse_views` on the backend and
    attach them to `reuse.metrics`.
    '''
    log.info('Updating reuses metrics from backend...')
    client = metrics_client_factory()
    result = client.get_previous_day_measurements(collection='reuse',
                                                  tag='reuse')
    process_metrics_result(client, result, Reuse)
Ejemplo n.º 7
0
def update_datasets_metrics_from_backend():
    '''
    Update datasets' metrics from backend.

    Get a sum of all metrics for `dataset_views` on the backend and
    attach them to `dataset.metrics`.
    '''
    log.info('Updating datasets metrics from backend...')
    client = metrics_client_factory()
    result = client.get_previous_day_measurements(collection='dataset',
                                                  tag='dataset')
    process_metrics_result(client, result, Dataset)
Ejemplo n.º 8
0
def test_dataset_metric(fixtures):
    metrics_client = metrics_client_factory()
    result = metrics_client.sum_views_from_specific_model(
        'dataset', 'dataset', fixtures['dataset'].id)
    values = next(result.get_points())

    assert values['sum_nb_hits'] == 2
    assert values['sum_nb_uniq_visitors'] == 1
    assert values['sum_nb_visits'] == 1

    update_datasets_metrics_from_backend()
    fixtures['dataset'].reload()
    assert fixtures['dataset'].get_metrics()['views'] == 1
Ejemplo n.º 9
0
def upsert_in_metrics_backend(day=None, metric=None, tags={}, data={}):
    if isinstance(day, str):
        day = date.fromisoformat(day)
    # we're on a daily basis, but backend is not
    dt = datetime.combine(day or date.today(), time())
    client = metrics_client_factory()
    body = {
        'time': dt,
        'measurement': metric,
        'tags': tags,
        'fields': dict((k, data[k]) for k in KEYS)
    }
    client.insert(body)
Ejemplo n.º 10
0
def test_community_resource_metric(fixtures):
    metrics_client = metrics_client_factory()
    result = metrics_client.sum_views_from_specific_model(
        collection='community_resource',
        tag='communityresource',
        model_id=fixtures['community_resource'].id)

    values = next(result.get_points())
    assert values['sum_nb_hits'] == 2
    assert values['sum_nb_uniq_visitors'] == 2
    assert values['sum_nb_visits'] == 2

    update_community_resources_metrics_from_backend()
    fixtures['community_resource'].reload()
    assert fixtures['community_resource'].get_metrics()['views'] == 2
Ejemplo n.º 11
0
def test_resource_metric(fixtures):
    metrics_client = metrics_client_factory()
    result = metrics_client.sum_views_from_specific_model(
        'resource', 'resource', fixtures['resource'].id)

    values = next(result.get_points())
    # 1 hit on permalink, 1 on url
    assert values['sum_nb_hits'] == 2
    assert values['sum_nb_uniq_visitors'] == 2
    assert values['sum_nb_visits'] == 2

    update_resources_metrics_from_backend()
    fixtures['dataset'].reload()
    resource = fixtures['dataset'].resources[0]
    assert resource.get_metrics()['views'] == 2
Ejemplo n.º 12
0
def test_two_datasets_one_resource_url(fixtures):
    (d1, d2), (r1, r2) = fixtures['two_datasets_one_resource_url']

    metrics_client = metrics_client_factory()

    result_r1 = metrics_client.sum_views_from_specific_model(
        collection='resource', tag='resource', model_id=r1.id)
    values_r1 = next(result_r1.get_points())

    result_r2 = metrics_client.sum_views_from_specific_model(
        collection='resource', tag='resource', model_id=r2.id)
    values_r2 = next(result_r2.get_points())

    assert values_r1['sum_nb_hits'] == 1
    assert values_r1['sum_nb_uniq_visitors'] == 1
    assert values_r1['sum_nb_visits'] == 1

    assert values_r2['sum_nb_hits'] == 2
    assert values_r2['sum_nb_uniq_visitors'] == 2
    assert values_r2['sum_nb_visits'] == 2