2015-03-31 12:39:08 +00:00
|
|
|
import datetime
|
2015-04-01 15:49:21 +00:00
|
|
|
import decimal
|
2015-03-31 12:39:08 +00:00
|
|
|
|
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
from orchestra import plugins
|
|
|
|
|
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
class DataMethod(plugins.Plugin, metaclass=plugins.PluginMount):
|
2015-03-31 12:39:08 +00:00
|
|
|
""" filters and computes dataset usage """
|
|
|
|
def filter(self, dataset):
|
|
|
|
""" Filter the dataset to get the relevant data according to the period """
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def compute_usage(self, dataset):
|
|
|
|
""" given a dataset computes its usage according to the method (avg, sum, ...) """
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class Last(DataMethod):
|
|
|
|
name = 'last'
|
2015-04-01 15:49:21 +00:00
|
|
|
verbose_name = _("Last value")
|
2015-03-31 12:39:08 +00:00
|
|
|
|
|
|
|
def filter(self, dataset):
|
|
|
|
try:
|
|
|
|
return dataset.order_by('object_id', '-id').distinct('object_id')
|
|
|
|
except MonitorData.DoesNotExist:
|
|
|
|
return dataset.none()
|
|
|
|
|
|
|
|
def compute_usage(self, dataset):
|
|
|
|
# FIXME Aggregation of 0s returns None! django bug?
|
|
|
|
# value = dataset.aggregate(models.Sum('value'))['value__sum']
|
|
|
|
values = dataset.values_list('value', flat=True)
|
|
|
|
if values:
|
|
|
|
return sum(values)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class MonthlySum(Last):
|
|
|
|
name = 'monthly-sum'
|
|
|
|
verbose_name = _("Monthly Sum")
|
|
|
|
|
|
|
|
def filter(self, dataset):
|
|
|
|
today = timezone.now()
|
|
|
|
return dataset.filter(
|
|
|
|
created_at__year=today.year,
|
|
|
|
created_at__month=today.month
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class MonthlyAvg(MonthlySum):
|
|
|
|
name = 'monthly-avg'
|
|
|
|
verbose_name = _("Monthly AVG")
|
|
|
|
|
|
|
|
def get_epoch(self):
|
|
|
|
today = timezone.now()
|
|
|
|
return datetime(
|
|
|
|
year=today.year,
|
|
|
|
month=today.month,
|
|
|
|
day=1,
|
|
|
|
tzinfo=timezone.utc
|
|
|
|
)
|
|
|
|
|
|
|
|
def compute_usage(self, dataset):
|
|
|
|
last = dataset.latest()
|
|
|
|
epoch = self.get_epoch()
|
|
|
|
total = (last.created_at-epoch).total_seconds()
|
|
|
|
ini = epoch
|
|
|
|
result = 0
|
|
|
|
for data in dataset:
|
|
|
|
slot = (data.created_at-ini).total_seconds()
|
2015-04-01 15:49:21 +00:00
|
|
|
result += data.value * decimal.Decimal(str(slot/total))
|
2015-03-31 12:39:08 +00:00
|
|
|
ini = data.created_at
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class Last10DaysAvg(MonthlyAvg):
|
|
|
|
name = 'last-10-days-avg'
|
|
|
|
verbose_name = _("Last 10 days AVG")
|
|
|
|
days = 10
|
|
|
|
|
|
|
|
def get_epoch(self):
|
|
|
|
today = timezone.now()
|
|
|
|
return today - datetime.timedelta(days=self.days)
|
|
|
|
|
|
|
|
def filter(self, dataset):
|
|
|
|
return dataset.filter(created_at__gt=self.get_epoch())
|