def test_calc_aggregation_periods_day(self): periods = dateperiods.calc_aggregation_periods( self.start_date, self.end_date, dateperiods.DAY) self.assertEqual(periods[0][0], self.start_date) self.assertEqual(periods[0][1], datetime.datetime(1979, 5, 26, tzinfo=pytz.UTC)) self.assertEqual(periods[-1][0], datetime.datetime(1979, 7, 14, tzinfo=pytz.UTC)) self.assertEqual(periods[-1][1], self.end_date)
def test_calc_aggregation_periods_quarter(self): end_date = datetime.datetime(1980, 4, 15, tzinfo=pytz.UTC) periods = dateperiods.calc_aggregation_periods( self.start_date, end_date, dateperiods.QUARTER) self.assertEqual(periods[0][0], self.start_date) self.assertEqual(periods[0][1], datetime.datetime(1979, 7, 1, tzinfo=pytz.UTC)) self.assertEqual(periods[-1][0], datetime.datetime(1980, 4, 1, tzinfo=pytz.UTC)) self.assertEqual(periods[-1][1], end_date)
def test_calc_aggregation_periods_year(self): end_date = datetime.datetime(1980, 4, 15, tzinfo=pytz.UTC) periods = dateperiods.calc_aggregation_periods( self.start_date, end_date, dateperiods.YEAR) self.assertEqual(periods[0][0], self.start_date) self.assertEqual(periods[0][1], datetime.datetime(1980, 1, 1, tzinfo=pytz.UTC)) self.assertEqual(periods[1][0], datetime.datetime(1980, 1, 1, tzinfo=pytz.UTC)) self.assertEqual(periods[1][1], end_date)
def test_calc_aggregation_periods_week(self): monday_after_startdate = datetime.datetime(1979, 5, 28, tzinfo=pytz.UTC) monday_before_enddate = datetime.datetime(1979, 7, 9, tzinfo=pytz.UTC) periods = dateperiods.calc_aggregation_periods( self.start_date, self.end_date, dateperiods.WEEK) self.assertEqual(periods[0][0], self.start_date) self.assertEqual(periods[0][1], monday_after_startdate) self.assertEqual(periods[-1][0], monday_before_enddate) self.assertEqual(periods[-1][1], self.end_date)
def test_calc_aggregation_periods_month(self): end_date = datetime.datetime(1980, 4, 15, tzinfo=pytz.UTC) first_day_of_new_month_after_startdate = datetime.datetime( 1979, 6, 1, tzinfo=pytz.UTC) first_day_of_end_month = datetime.datetime( 1980, 4, 1, tzinfo=pytz.UTC) periods = dateperiods.calc_aggregation_periods( self.start_date, end_date, dateperiods.MONTH) self.assertEqual(periods[0][0], self.start_date) self.assertEqual(periods[0][1], first_day_of_new_month_after_startdate) self.assertEqual(periods[-1][0], first_day_of_end_month) self.assertEqual(periods[-1][1], end_date)
def statistics(self, start_date, end_date): """From collage snippet group. Brings statistics and collage properties together. """ adapter = self.adapter if adapter is None: return [] # Calc periods based on aggregation period setting. periods = dateperiods.calc_aggregation_periods(start_date, end_date, self.aggregation_period) statistics = [] for period_start_date, period_end_date in periods: if not self.restrict_to_month or ( self.aggregation_period != dateperiods.MONTH) or ( self.aggregation_period == dateperiods.MONTH and self.restrict_to_month == period_start_date.month): # Base statistics for each period. statistics_row = adapter.value_aggregate( self.identifier, {'min': None, 'max': None, 'avg': None, 'count_lt': self.boundary_value, 'count_gte': self.boundary_value, 'percentile': self.percentile_value}, start_date=period_start_date, end_date=period_end_date) # Add name. if statistics_row: statistics_row['name'] = self.name statistics_row['period'] = dateperiods.fancy_period( period_start_date, period_end_date, self.aggregation_period) statistics_row['boundary_value'] = self.boundary_value statistics_row['percentile_value'] = self.percentile_value statistics.append(statistics_row) return statistics
def test_calc_aggregation_periods_all(self): periods = dateperiods.calc_aggregation_periods( self.start_date, self.end_date, dateperiods.ALL) self.assertEqual(periods[0][0], self.start_date) self.assertEqual(periods[0][1], self.end_date)