Esempio n. 1
0
def test_monthly_report_queried_daily_returns_correct_length():
    class Sum(Report):
        config_database = database_name()
        config_collection = 'report.sum'
        config_period = MONTH
        config_intervals = [MONTH, HOUR]

    now = pytool.time.utcnow()
    earlier = report._relative_period(MONTH, now, -1)

    event = 'monthly_as_daily'
    with DBTest:
        Sum.record(event)
        Sum.record(event, stamp=earlier)
        days = Sum.daily[-65:]

    days = days.get(event, [])
    # Check we get the right number of days
    eq_(len(days), 65)
    # Check we get the correct total
    eq_(sum(days), 2)

    # Ensure dates come back in correct order
    date = report._relative_period(DAY, days[0].timestamp, -1)
    for day in days:
        ok_(day.timestamp > date)
        date = day.timestamp
Esempio n. 2
0
def test_monthly_report_queried_daily_returns_correct_length():
    class Sum(Report):
        config_database = database_name()
        config_collection = 'report.sum'
        config_period = MONTH
        config_intervals = [MONTH, HOUR]

    now = pytool.time.utcnow()
    earlier = report._relative_period(MONTH, now, -1)

    event = 'monthly_as_daily'
    with DBTest:
        Sum.record(event)
        Sum.record(event, stamp=earlier)
        days = Sum.daily[-65:]

    days = days.get(event, [])
    # Check we get the right number of days
    eq_(len(days), 65)
    # Check we get the correct total
    eq_(sum(days), 2)

    # Ensure dates come back in correct order
    date = report._relative_period(DAY, days[0].timestamp, -1)
    for day in days:
        ok_(day.timestamp > date)
        date = day.timestamp
Esempio n. 3
0
def test_relative_period_MONTH_across_end_of_year_and_beginning():
    stamp = datetime.datetime(2013, 1, 1, tzinfo=pytool.time.UTC())
    eq_(report._relative_period(MONTH, stamp, -1),
        datetime.datetime(2012, 12, 1, tzinfo=pytool.time.UTC()))

    stamp = datetime.datetime(2013, 12, 1, tzinfo=pytool.time.UTC())
    eq_(report._relative_period(MONTH, stamp, 1),
        datetime.datetime(2014, 1, 1, tzinfo=pytool.time.UTC()))
Esempio n. 4
0
def test_relative_period_MONTH_across_end_of_year_and_beginning():
    stamp = datetime.datetime(2013, 1, 1, tzinfo=pytool.time.UTC())
    eq_(report._relative_period(MONTH, stamp, -1), datetime.datetime(2012, 12,
        1, tzinfo=pytool.time.UTC()))

    stamp = datetime.datetime(2013, 12, 1, tzinfo=pytool.time.UTC())
    eq_(report._relative_period(MONTH, stamp, 1), datetime.datetime(2014, 1,
        1, tzinfo=pytool.time.UTC()))
Esempio n. 5
0
def test_report_query_yearly_by_monthly():
    stamp = pytool.time.utcnow()
    stamp = report._relative_period(YEAR, stamp, -1)
    stamp = stamp.replace(hour=1, minute=0, second=0, microsecond=0)
    hour = datetime.timedelta(seconds=60 * 60)
    event = 'event_report_query_yearly_by_monthly'
    with DBTest:
        Yearly.record(event, stamp)
        Yearly.record(event, stamp + hour)
        Yearly.record(event, stamp + hour + hour)

        counts = Yearly.monthly(event)[stamp - hour:stamp + hour]
        eq_(counts, [3])
        count = counts[0]
        eq_(count.timestamp.timetuple()[:2], stamp.timetuple()[:2])
        eq_(count.day, 1)
        eq_(count.hour, 0)
Esempio n. 6
0
def test_report_query_yearly_by_monthly():
    stamp = pytool.time.utcnow()
    stamp = report._relative_period(YEAR, stamp, -1)
    stamp = stamp.replace(hour=1, minute=0, second=0, microsecond=0)
    hour = datetime.timedelta(seconds=60*60)
    event = 'event_report_query_yearly_by_monthly'
    with DBTest:
        Yearly.record(event, stamp)
        Yearly.record(event, stamp + hour)
        Yearly.record(event, stamp + hour + hour)

        counts = Yearly.monthly(event)[stamp - hour:stamp + hour]
        eq_(counts, [3])
        count = counts[0]
        eq_(count.timestamp.timetuple()[:2], stamp.timetuple()[:2])
        eq_(count.day, 1)
        eq_(count.hour, 0)
Esempio n. 7
0
def test_report_query_end_index():
    stamp = pytool.time.utcnow()
    this_year = stamp.year
    last_year = stamp.year - 1

    stamp = datetime.datetime(this_year,
                              12,
                              31,
                              23,
                              59,
                              59,
                              tzinfo=pytool.time.UTC())

    event = 'event_report_query_end_index'
    with DBTest:
        Daily.record(event, stamp, safe=True)
        eq_(Daily.yearly(event)[last_year + 1:this_year + 1][-1], 1)
        eq_(Daily.monthly(event)[1:13][-1], 1)

    stamp = pytool.time.utcnow()
    stamp = report._relative_period(MONTH, stamp, 1)
    stamp -= datetime.timedelta(seconds=1)
    event = 'event_report_query_end_index_daily'
    with DBTest:
        Daily.record(event, stamp)
        _, end_of_month = calendar.monthrange(stamp.year, stamp.month)
        eq_(Daily.daily(event)[1:end_of_month + 1][-1], 1)

    stamp = pytool.time.utcnow()
    stamp = stamp.replace(hour=23, minute=59, second=59)
    event = 'event_report_query_end_index_daily_day'
    with DBTest:
        Daily.record(event, stamp)
        eq_(Daily.hourly(event)[0:24][-1], 1)

    stamp = pytool.time.utcnow()
    stamp = stamp.replace(minute=59, second=59)
    event = 'event_query_end_index_per_minute'
    with DBTest:
        Daily.record(event, stamp)
        eq_(Daily.per_minute(event)[0:60][-1], 1)
Esempio n. 8
0
def test_report_query_end_index():
    stamp = pytool.time.utcnow()
    this_year = stamp.year
    last_year = stamp.year-1

    stamp = datetime.datetime(this_year, 12, 31, 23, 59, 59,
            tzinfo=pytool.time.UTC())

    event = 'event_report_query_end_index'
    with DBTest:
        Daily.record(event, stamp, safe=True)
        eq_(Daily.yearly(event)[last_year+1:this_year+1][-1], 1)
        eq_(Daily.monthly(event)[1:13][-1], 1)

    stamp = pytool.time.utcnow()
    stamp = report._relative_period(MONTH, stamp, 1)
    stamp -= datetime.timedelta(seconds=1)
    event = 'event_report_query_end_index_daily'
    with DBTest:
        Daily.record(event, stamp)
        _, end_of_month = calendar.monthrange(stamp.year, stamp.month)
        eq_(Daily.daily(event)[1:end_of_month+1][-1], 1)

    stamp = pytool.time.utcnow()
    stamp = stamp.replace(hour=23, minute=59, second=59)
    event = 'event_report_query_end_index_daily_day'
    with DBTest:
        Daily.record(event, stamp)
        eq_(Daily.hourly(event)[0:24][-1], 1)

    stamp = pytool.time.utcnow()
    stamp = stamp.replace(minute=59, second=59)
    event = 'event_query_end_index_per_minute'
    with DBTest:
        Daily.record(event, stamp)
        eq_(Daily.per_minute(event)[0:60][-1], 1)