def test_should_by_able_to_get_template_count_from_notifications_history_with_day_limit(
        notify_db,
        notify_db_session,
        sample_service):
    sms = sample_template(notify_db, notify_db_session)

    email = sample_email_template(notify_db, notify_db_session)

    today = datetime.now()
    yesterday = datetime.now() - timedelta(days=1)
    one_month_ago = datetime.now() - timedelta(days=30)

    sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=sms)

    sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=sms)

    sample_notification(notify_db, notify_db_session, created_at=one_month_ago, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, created_at=one_month_ago, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, created_at=one_month_ago, service=sample_service, template=sms)

    results_day_one = dao_get_template_usage(sample_service.id, limit_days=0)
    assert len(results_day_one) == 2

    results_day_two = dao_get_template_usage(sample_service.id, limit_days=1)
    assert len(results_day_two) == 2

    results_day_30 = dao_get_template_usage(sample_service.id, limit_days=31)
    assert len(results_day_30) == 2

    assert [(row.name, row.template_type, row.count) for row in results_day_one] == [
        ('Email Template Name', 'email', 2),
        ('Template Name', 'sms', 1)
    ]

    assert [(row.name, row.template_type, row.count) for row in results_day_two] == [
        ('Email Template Name', 'email', 5),
        ('Template Name', 'sms', 2),
    ]

    assert [(row.name, row.template_type, row.count) for row in results_day_30] == [
        ('Email Template Name', 'email', 5),
        ('Template Name', 'sms', 5),
    ]
Exemplo n.º 2
0
def get_template_statistics_for_service_by_day(service_id):
    if request.args.get('limit_days'):
        try:
            limit_days = int(request.args['limit_days'])
        except ValueError as e:
            error = '{} is not an integer'.format(request.args['limit_days'])
            message = {'limit_days': [error]}
            raise InvalidRequest(message, status_code=400)
    else:
        limit_days = None

    if limit_days == 7:
        stats = get_template_statistics_for_7_days(limit_days, service_id)
    else:
        stats = dao_get_template_usage(service_id, limit_days=limit_days)

    def serialize(data):
        return {
            'count': data.count,
            'template_id': str(data.template_id),
            'template_name': data.name,
            'template_type': data.template_type,
            'is_precompiled_letter': data.is_precompiled_letter
        }

    return jsonify(data=[serialize(row) for row in stats])
def test_should_by_able_to_get_template_count_from_notifications_history_for_service(
        notify_db,
        notify_db_session):
    service_1 = sample_service(notify_db, notify_db_session, service_name="test1", email_from="test1")
    service_2 = sample_service(notify_db, notify_db_session, service_name="test2", email_from="test2")
    service_3 = sample_service(notify_db, notify_db_session, service_name="test3", email_from="test3")

    sms = sample_template(notify_db, notify_db_session)

    sample_notification(notify_db, notify_db_session, service=service_1, template=sms)
    sample_notification(notify_db, notify_db_session, service=service_1, template=sms)
    sample_notification(notify_db, notify_db_session, service=service_2, template=sms)

    assert dao_get_template_usage(service_1.id)[0].count == 2
    assert dao_get_template_usage(service_2.id)[0].count == 1
    assert len(dao_get_template_usage(service_3.id)) == 0
Exemplo n.º 4
0
def test_template_usage_should_by_able_to_get_template_count_for_specific_timezone_boundary(
        sample_template):
    # too early
    create_notification(sample_template,
                        created_at=datetime(2018, 3, 24, 23, 59, 0))
    # just right
    create_notification(sample_template,
                        created_at=datetime(2018, 3, 25, 0, 0, 0))
    create_notification(sample_template,
                        created_at=datetime(2018, 3, 25, 0, 0, 0))
    create_notification(sample_template,
                        created_at=datetime(2018, 3, 25, 22, 59, 0))
    create_notification(sample_template,
                        created_at=datetime(2018, 3, 25, 22, 59, 0))
    create_notification(sample_template,
                        created_at=datetime(2018, 3, 25, 22, 59, 0))
    # too late
    create_notification(sample_template,
                        created_at=datetime(2018, 3, 25, 23, 0, 0))

    results = dao_get_template_usage(sample_template.service_id,
                                     day=date(2018, 3, 25))

    assert len(results) == 1
    assert results[0].count == 5
Exemplo n.º 5
0
def get_template_statistics_for_7_days(limit_days, service_id):
    cache_key = cache_key_for_service_template_counter(service_id)
    template_stats_by_id = redis_store.get_all_from_hash(cache_key)
    if not template_stats_by_id:
        stats = dao_get_template_usage(service_id, limit_days=limit_days)
        cache_values = dict([(x.template_id, x.count) for x in stats])
        if cache_values:
            redis_store.set_hash_and_expire(
                cache_key, cache_values,
                current_app.config['EXPIRE_CACHE_TEN_MINUTES'])
    else:
        stats = dao_get_templates_for_cache(template_stats_by_id.items())
    return stats
Exemplo n.º 6
0
def test_should_by_able_to_get_template_count(sample_template,
                                              sample_email_template):
    create_notification(sample_template)
    create_notification(sample_template)
    create_notification(sample_template)
    create_notification(sample_email_template)
    create_notification(sample_email_template)

    results = dao_get_template_usage(sample_template.service_id, date.today())
    assert results[0].name == sample_email_template.name
    assert results[0].template_type == sample_email_template.template_type
    assert results[0].count == 2

    assert results[1].name == sample_template.name
    assert results[1].template_type == sample_template.template_type
    assert results[1].count == 3
def test_should_by_able_to_get_template_count_from_notifications_history(notify_db, notify_db_session, sample_service):
    sms = sample_template(notify_db, notify_db_session)
    email = sample_email_template(notify_db, notify_db_session)
    sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, service=sample_service, template=email)

    results = dao_get_template_usage(sample_service.id)
    assert results[0].name == 'Email Template Name'
    assert results[0].template_type == 'email'
    assert results[0].count == 2

    assert results[1].name == 'Template Name'
    assert results[1].template_type == 'sms'
    assert results[1].count == 3
Exemplo n.º 8
0
def test_template_usage_should_ignore_test_keys(sample_team_api_key,
                                                sample_test_api_key,
                                                sample_api_key,
                                                sample_template):

    create_notification(sample_template,
                        api_key=sample_api_key,
                        key_type=KEY_TYPE_NORMAL)
    create_notification(sample_template,
                        api_key=sample_team_api_key,
                        key_type=KEY_TYPE_TEAM)
    create_notification(sample_template,
                        api_key=sample_test_api_key,
                        key_type=KEY_TYPE_TEST)
    create_notification(sample_template)

    results = dao_get_template_usage(sample_template.service_id, date.today())
    assert results[0].name == sample_template.name
    assert results[0].template_type == sample_template.template_type
    assert results[0].count == 3
Exemplo n.º 9
0
def _get_template_statistics_for_last_n_days(service_id, whole_days):
    template_stats_by_id = Counter()

    # 0 whole_days = last 1 days (ie since midnight today) = today.
    # 7 whole days = last 8 days (ie since midnight this day last week) = a week and a bit
    for day in last_n_days(whole_days + 1):
        # "{SERVICE_ID}-template-usage-{YYYY-MM-DD}"
        key = cache_key_for_service_template_usage_per_day(service_id, day)
        stats = redis_store.get_all_from_hash(key)
        if stats:
            stats = {k.decode('utf-8'): int(v) for k, v in stats.items()}
        else:
            # key didn't exist (or redis was down) - lets populate from DB.
            stats = {
                str(row.id): row.count
                for row in dao_get_template_usage(service_id, day=day)
            }
            # if there is data in db, but not in redis - lets put it in redis so we don't have to do
            # this calc again next time. If there isn't any data, we can't put it in redis.
            # Zero length hashes aren't a thing in redis. (There'll only be no data if the service has no templates)
            # Nothing is stored if redis is down.
            if stats:
                redis_store.set_hash_and_expire(
                    key, stats, current_app.config['EXPIRE_CACHE_EIGHT_DAYS'])
        template_stats_by_id += Counter(stats)

    # attach count from stats to name/type/etc from database
    template_details = dao_get_multiple_template_details(
        template_stats_by_id.keys())
    return [
        {
            'count': template_stats_by_id[str(template.id)],
            'template_id': str(template.id),
            'template_name': template.name,
            'template_type': template.template_type,
            'is_precompiled_letter': template.is_precompiled_letter
        } for template in template_details
        # we don't want to return templates with no count to the front-end,
        # but they're returned from the DB and might be put in redis like that (if there was no data that day)
        if template_stats_by_id[str(template.id)] != 0
    ]
def test_template_history_should_ignore_test_keys(
    notify_db,
    notify_db_session,
    sample_team_api_key,
    sample_test_api_key,
    sample_api_key
):
    sms = sample_template(notify_db, notify_db_session)

    sample_notification(
        notify_db, notify_db_session, template=sms, api_key_id=sample_api_key.id, key_type=KEY_TYPE_NORMAL)
    sample_notification(
        notify_db, notify_db_session, template=sms, api_key_id=sample_team_api_key.id, key_type=KEY_TYPE_TEAM)
    sample_notification(
        notify_db, notify_db_session, template=sms, api_key_id=sample_test_api_key.id, key_type=KEY_TYPE_TEST)
    sample_notification(
        notify_db, notify_db_session, template=sms)

    results = dao_get_template_usage(sms.service_id)
    assert results[0].name == 'Template Name'
    assert results[0].template_type == 'sms'
    assert results[0].count == 3
def test_should_by_able_to_get_zero_count_from_notifications_history_if_no_service():
    results = dao_get_template_usage(str(uuid.uuid4()))
    assert len(results) == 0
def test_should_by_able_to_get_zero_count_from_notifications_history_if_no_rows(sample_service):
    results = dao_get_template_usage(sample_service.id)
    assert len(results) == 0
Exemplo n.º 13
0
def test_template_usage_should_by_able_to_get_zero_count_from_notifications_history_if_no_service(
):
    results = dao_get_template_usage(str(uuid.uuid4()), date.today())
    assert len(results) == 0
Exemplo n.º 14
0
def test_template_usage_should_by_able_to_get_zero_count_from_notifications_history_if_no_rows(
        sample_service):
    results = dao_get_template_usage(sample_service.id, date.today())
    assert len(results) == 0