DRY resources.aggregations

This commit is contained in:
Marc Aymerich 2015-07-16 13:12:50 +00:00
parent 48a19d8be9
commit 63c9f381bc
2 changed files with 7 additions and 21 deletions

View file

@ -423,5 +423,3 @@ Colaesce('total', 'computed_total')
Case Case
# case on payment transaction state ? case when trans.amount > # case on payment transaction state ? case when trans.amount >
# Yield multiple values on historic filter on all aggregators

View file

@ -35,8 +35,7 @@ class Last(Aggregation):
dataset = dataset.filter(created_at__lte=date) dataset = dataset.filter(created_at__lte=date)
return dataset return dataset
def historic_filter(self, dataset): def monthly_historic_filter(self, dataset):
yield (timezone.now(), self.filter(dataset))
now = timezone.now() now = timezone.now()
date = datetime.datetime( date = datetime.datetime(
year=now.year, year=now.year,
@ -54,6 +53,10 @@ class Last(Aggregation):
yield (date, dataset_copy) yield (date, dataset_copy)
date -= relativedelta(months=1) date -= relativedelta(months=1)
def historic_filter(self, dataset):
yield (timezone.now(), self.filter(dataset))
yield from self.monthly_historic_filter(dataset)
def compute_usage(self, dataset): def compute_usage(self, dataset):
values = dataset.values_list('value', flat=True) values = dataset.values_list('value', flat=True)
if values: if values:
@ -75,22 +78,7 @@ class MonthlySum(Last):
) )
def historic_filter(self, dataset): def historic_filter(self, dataset):
now = timezone.now() yield from self.monthly_historic_filter(dataset)
date = datetime.datetime(
year=now.year,
month=now.month,
day=1,
tzinfo=timezone.utc,
)
while True:
dataset_copy = copy.copy(dataset)
dataset_copy = self.filter(dataset_copy, date=date)
try:
dataset_copy[0]
except IndexError:
raise StopIteration
yield (date, dataset_copy)
date -= relativedelta(months=1)
class MonthlyAvg(MonthlySum): class MonthlyAvg(MonthlySum):