def test_get_aggregate_route_metrics_all_time_weekly_bucket(app):
    with app.app_context():
        db_mock = get_db()

    with db_mock.scoped_session() as session:
        session.bulk_save_objects([
            AggregateDailyUniqueUsersMetrics(count=1,
                                             summed_count=2,
                                             timestamp=today -
                                             timedelta(days=367)),
            AggregateDailyUniqueUsersMetrics(count=2,
                                             summed_count=3,
                                             timestamp=yesterday),
            AggregateDailyUniqueUsersMetrics(count=3,
                                             summed_count=4,
                                             timestamp=today),
            AggregateDailyTotalUsersMetrics(count=2,
                                            timestamp=today -
                                            timedelta(days=367)),
            AggregateDailyTotalUsersMetrics(count=4, timestamp=yesterday),
            AggregateDailyTotalUsersMetrics(count=6, timestamp=today)
        ])

        aggregate_metrics = _get_aggregate_route_metrics(
            session, 'all_time', 'week')

        assert len(aggregate_metrics) == 2
        assert aggregate_metrics[0]['unique_count'] == 1
        assert aggregate_metrics[0]['summed_unique_count'] == 2
        assert aggregate_metrics[0]['total_count'] == 2
        assert aggregate_metrics[1]['unique_count'] == 2
        assert aggregate_metrics[1]['summed_unique_count'] == 3
        assert aggregate_metrics[1]['total_count'] == 4
def update_historical_daily_route_metrics(db, metrics):
    with db.scoped_session() as session:
        for day, values in metrics.items():
            day_unique_record = (
                session.query(AggregateDailyUniqueUsersMetrics).filter(
                    AggregateDailyUniqueUsersMetrics.timestamp == day).first())
            if day_unique_record:
                day_unique_record.count = values["unique_count"]
                day_unique_record.summed_count = values["summed_unique_count"]
            else:
                day_unique_record = AggregateDailyUniqueUsersMetrics(
                    timestamp=day,
                    count=values["unique_count"],
                    summed_count=values["summed_unique_count"],
                )
            session.add(day_unique_record)

            day_total_record = (
                session.query(AggregateDailyTotalUsersMetrics).filter(
                    AggregateDailyTotalUsersMetrics.timestamp == day).first())
            if day_total_record:
                day_total_record.count = values["total_count"]
            else:
                day_total_record = AggregateDailyTotalUsersMetrics(
                    timestamp=day, count=values["total_count"])
            session.add(day_total_record)
def test_get_aggregate_route_metrics_month_daily_bucket(app):
    with app.app_context():
        db_mock = get_db()

    with db_mock.scoped_session() as session:
        session.bulk_save_objects([
            AggregateDailyUniqueUsersMetrics(count=1,
                                             summed_count=2,
                                             timestamp=today -
                                             timedelta(days=31)),
            AggregateDailyUniqueUsersMetrics(count=2,
                                             summed_count=3,
                                             timestamp=today -
                                             timedelta(days=8)),
            AggregateDailyUniqueUsersMetrics(count=3,
                                             summed_count=4,
                                             timestamp=yesterday),
            AggregateDailyUniqueUsersMetrics(count=4,
                                             summed_count=5,
                                             timestamp=today),
            AggregateDailyTotalUsersMetrics(count=2,
                                            timestamp=today -
                                            timedelta(days=31)),
            AggregateDailyTotalUsersMetrics(count=4,
                                            timestamp=today -
                                            timedelta(days=8)),
            AggregateDailyTotalUsersMetrics(count=6, timestamp=yesterday),
            AggregateDailyTotalUsersMetrics(count=8, timestamp=today),
        ])

        aggregate_metrics = _get_aggregate_route_metrics(
            session, "month", "day")

        assert len(aggregate_metrics) == 2
        assert aggregate_metrics[0]["unique_count"] == 2
        assert aggregate_metrics[0]["summed_unique_count"] == 3
        assert aggregate_metrics[0]["total_count"] == 4
        assert aggregate_metrics[1]["unique_count"] == 3
        assert aggregate_metrics[1]["summed_unique_count"] == 4
        assert aggregate_metrics[1]["total_count"] == 6
def test_get_aggregate_route_metrics_trailing_month(app):
    with app.app_context():
        db_mock = get_db()

    with db_mock.scoped_session() as session:
        session.bulk_save_objects([
            AggregateDailyUniqueUsersMetrics(count=1,
                                             summed_count=2,
                                             timestamp=today -
                                             timedelta(days=31)),
            AggregateDailyUniqueUsersMetrics(count=2,
                                             summed_count=3,
                                             timestamp=today -
                                             timedelta(days=30)),
            AggregateDailyUniqueUsersMetrics(count=3,
                                             summed_count=4,
                                             timestamp=yesterday),
            AggregateDailyUniqueUsersMetrics(count=4,
                                             summed_count=5,
                                             timestamp=today),
            AggregateDailyTotalUsersMetrics(count=2,
                                            timestamp=today -
                                            timedelta(days=31)),
            AggregateDailyTotalUsersMetrics(count=4,
                                            timestamp=today -
                                            timedelta(days=30)),
            AggregateDailyTotalUsersMetrics(count=6, timestamp=yesterday),
            AggregateDailyTotalUsersMetrics(count=8, timestamp=today)
        ])

        aggregate_metrics = _get_aggregate_route_metrics_trailing_month(
            session)

        assert aggregate_metrics['unique_count'] == 5
        assert aggregate_metrics['summed_unique_count'] == 7
        assert aggregate_metrics['total_count'] == 10
예제 #5
0
def test_get_historical_route_metrics(app):
    with app.app_context():
        db_mock = get_db()

    with db_mock.scoped_session() as session:
        session.bulk_save_objects([
            AggregateDailyUniqueUsersMetrics(
                count=1,
                summed_count=2,
                timestamp=thirty_days_ago - timedelta(days=1)
            ),
            AggregateDailyUniqueUsersMetrics(
                count=2,
                summed_count=3,
                timestamp=thirty_days_ago
            ),
            AggregateDailyUniqueUsersMetrics(
                count=3,
                summed_count=4,
                timestamp=yesterday
            ),
            AggregateDailyUniqueUsersMetrics(
                count=4,
                summed_count=5,
                timestamp=today
            ),
            AggregateDailyTotalUsersMetrics(
                count=2,
                timestamp=thirty_days_ago - timedelta(days=1)
            ),
            AggregateDailyTotalUsersMetrics(
                count=4,
                timestamp=thirty_days_ago
            ),
            AggregateDailyTotalUsersMetrics(
                count=6,
                timestamp=yesterday
            ),
            AggregateDailyTotalUsersMetrics(
                count=8,
                timestamp=today
            ),
            AggregateMonthlyUniqueUsersMetrics(
                count=1,
                summed_count=2,
                timestamp=today - timedelta(days=367)
            ),
            AggregateMonthlyUniqueUsersMetrics(
                count=2,
                summed_count=3,
                timestamp=today - timedelta(days=100)
            ),
            AggregateMonthlyUniqueUsersMetrics(
                count=3,
                summed_count=4,
                timestamp=today
            ),
            AggregateMonthlyTotalUsersMetrics(
                count=2,
                timestamp=today - timedelta(days=367)
            ),
            AggregateMonthlyTotalUsersMetrics(
                count=4,
                timestamp=today - timedelta(days=100)
            ),
            AggregateMonthlyTotalUsersMetrics(
                count=6,
                timestamp=today
            )
        ])

        aggregate_metrics = _get_historical_route_metrics(session)
        daily_aggregate_metrics = aggregate_metrics['daily']
        monthly_aggregate_metrics = aggregate_metrics['monthly']

        assert len(daily_aggregate_metrics.items()) == 2
        assert daily_aggregate_metrics[str(thirty_days_ago)]['unique_count'] == 2
        assert daily_aggregate_metrics[str(thirty_days_ago)]['summed_unique_count'] == 3
        assert daily_aggregate_metrics[str(thirty_days_ago)]['total_count'] == 4
        assert daily_aggregate_metrics[str(yesterday)]['unique_count'] == 3
        assert daily_aggregate_metrics[str(yesterday)]['summed_unique_count'] == 4
        assert daily_aggregate_metrics[str(yesterday)]['total_count'] == 6

        assert len(monthly_aggregate_metrics.items()) == 2
        assert monthly_aggregate_metrics[str(today - timedelta(days=367))]['unique_count'] == 1
        assert monthly_aggregate_metrics[str(today - timedelta(days=367))]['summed_unique_count'] == 2
        assert monthly_aggregate_metrics[str(today - timedelta(days=367))]['total_count'] == 2
        assert monthly_aggregate_metrics[str(today - timedelta(days=100))]['unique_count'] == 2
        assert monthly_aggregate_metrics[str(today - timedelta(days=100))]['summed_unique_count'] == 3
        assert monthly_aggregate_metrics[str(today - timedelta(days=100))]['total_count'] == 4
예제 #6
0
def persist_route_metrics(db, day, month, count, unique_daily_count,
                          unique_monthly_count):
    with db.scoped_session() as session:
        day_unique_record = (
            session.query(AggregateDailyUniqueUsersMetrics).filter(
                AggregateDailyUniqueUsersMetrics.timestamp == day).first())
        if day_unique_record:
            logger.info(
                f"unique count record for day {day} before adding new unique count \
                {unique_daily_count}: {day_unique_record.count}")
            day_unique_record.count += unique_daily_count
            logger.info(
                f"unique count record for day {day} after adding new unique count \
                {unique_daily_count}: {day_unique_record.count}")
        else:
            day_unique_record = AggregateDailyUniqueUsersMetrics(
                timestamp=day, count=unique_daily_count)
            logger.info(
                f"new record for daily unique count with day {day} and unique count {unique_daily_count}"
            )
        session.add(day_unique_record)

        day_total_record = (
            session.query(AggregateDailyTotalUsersMetrics).filter(
                AggregateDailyTotalUsersMetrics.timestamp == day).first())
        if day_total_record:
            logger.info(
                f"total count record for day {day} before adding new total count \
                {count}: {day_total_record.count}")
            day_total_record.count += count
            logger.info(
                f"total count record for day {day} after adding new total count \
                {count}: {day_total_record.count}")
        else:
            day_total_record = AggregateDailyTotalUsersMetrics(timestamp=day,
                                                               count=count)
            logger.info(
                f"new record for daily total count with day {day} and total count {count}"
            )
        session.add(day_total_record)

        month_unique_record = (
            session.query(AggregateMonthlyUniqueUsersMetrics).filter(
                AggregateMonthlyUniqueUsersMetrics.timestamp == month).first())
        if month_unique_record:
            logger.info(
                f"unique count record for month {month} before adding new unique count \
                {unique_monthly_count}: {month_unique_record.count}")
            month_unique_record.count += unique_monthly_count
            logger.info(
                f"unique count record for month {month} after adding new unique count \
                {unique_monthly_count}: {month_unique_record.count}")
        else:
            month_unique_record = AggregateMonthlyUniqueUsersMetrics(
                timestamp=month, count=unique_monthly_count)
            logger.info(
                f"new record for monthly unique count with month {month} and unique count \
                {unique_monthly_count}")
        session.add(month_unique_record)

        month_total_record = (
            session.query(AggregateMonthlyTotalUsersMetrics).filter(
                AggregateMonthlyTotalUsersMetrics.timestamp == month).first())
        if month_total_record:
            logger.info(
                f"total count record for month {month} before adding new total count \
                {count}: {month_total_record.count}")
            month_total_record.count += count
            logger.info(
                f"total count record for month {month} after adding new total count \
                {count}: {month_total_record.count}")
        else:
            month_total_record = AggregateMonthlyTotalUsersMetrics(
                timestamp=month, count=count)
            logger.info(
                f"new record for monthly total count with month {month} and total count {count}"
            )
        session.add(month_total_record)