django-orchestra/orchestra/contrib/resources/aggregations.py

104 lines
3.0 KiB
Python
Raw Normal View History

import datetime
2015-04-01 15:49:21 +00:00
import decimal
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from orchestra import plugins
2015-04-08 14:41:09 +00:00
class Aggregation(plugins.Plugin, metaclass=plugins.PluginMount):
""" 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
2015-04-08 14:41:09 +00:00
class Last(Aggregation):
2015-04-09 14:32:10 +00:00
""" Sum of the last value of all monitors """
name = 'last'
2015-04-01 15:49:21 +00:00
verbose_name = _("Last value")
def filter(self, dataset):
try:
2015-04-09 14:32:10 +00:00
return dataset.order_by('object_id', '-id').distinct('monitor')
2015-04-04 17:44:07 +00:00
except dataset.model.DoesNotExist:
return dataset.none()
def compute_usage(self, dataset):
values = dataset.values_list('value', flat=True)
if values:
return sum(values)
return None
class MonthlySum(Last):
2015-04-09 14:32:10 +00:00
""" Monthly sum the values of all monitors """
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):
2015-04-09 14:32:10 +00:00
""" sum of the monthly averages of each monitor """
name = 'monthly-avg'
verbose_name = _("Monthly AVG")
2015-04-09 14:32:10 +00:00
def filter(self, dataset):
qs = super(MonthlyAvg, self).filter(dataset)
return qs.order_by('created_at')
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):
2015-04-03 10:14:45 +00:00
result = 0
2015-04-09 14:32:10 +00:00
has_result = False
for monitor, dataset in dataset.group_by('monitor').items():
try:
last = dataset[-1]
except IndexError:
continue
epoch = self.get_epoch()
total = (last.created_at-epoch).total_seconds()
ini = epoch
for data in dataset:
has_result = True
slot = (data.created_at-ini).total_seconds()
result += data.value * decimal.Decimal(str(slot/total))
ini = data.created_at
if has_result:
2015-04-03 10:14:45 +00:00
return result
2015-04-09 14:32:10 +00:00
return None
class Last10DaysAvg(MonthlyAvg):
2015-04-09 14:32:10 +00:00
""" sum of the last 10 days averages of each monitor """
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):
2015-04-09 14:32:10 +00:00
epoch = self.get_epoch()
return dataset.filter(created_at__gt=epoch).order_by('created_at')