django-orchestra/orchestra/contrib/resources/actions.py
2015-07-16 13:07:15 +00:00

67 lines
2.7 KiB
Python

from django.core.urlresolvers import reverse
from django.shortcuts import redirect, render
from django.utils.safestring import mark_safe
from django.utils.translation import ungettext, ugettext_lazy as _
def run_monitor(modeladmin, request, queryset):
""" Resource and ResourceData run monitors """
referer = request.META.get('HTTP_REFERER')
async = modeladmin.model.monitor.__defaults__[0]
logs = set()
for resource in queryset:
rlogs = resource.monitor()
if not async:
logs = logs.union(set([str(log.pk) for log in rlogs]))
modeladmin.log_change(request, resource, _("Run monitors"))
if async:
num = len(queryset)
# TODO listfilter by uuid: task.request.id + ?task_id__in=ids
link = reverse('admin:djcelery_taskstate_changelist')
msg = ungettext(
_("One selected resource has been <a href='%s'>scheduled for monitoring</a>.") % link,
_("%s selected resource have been <a href='%s'>scheduled for monitoring</a>.") % (num, link),
num)
else:
num = len(logs)
if num == 1:
log_pk = int(logs.pop())
link = reverse('admin:orchestration_backendlog_change', args=(log_pk,))
msg = _("One related monitor has <a href='%s'>been executed</a>.") % link
elif num >= 1:
link = reverse('admin:orchestration_backendlog_changelist')
link += '?id__in=%s' % ','.join(logs)
msg = _("%s related monitors have <a href='%s'>been executed</a>.") % (num, link)
else:
msg = _("No related monitors have been executed.")
modeladmin.message_user(request, mark_safe(msg))
if referer:
return redirect(referer)
run_monitor.url_name = 'monitor'
def history(modeladmin, request, queryset):
resources = {}
for data in queryset:
resource = data.resource
total = 0
totals = {}
for dataset in data.get_monitor_datasets():
for date, dataset in resource.aggregation_instance.historic_filter(dataset):
usage = resource.aggregation_instance.compute_usage(dataset)
if usage is not None:
try:
totals[date] += usage
except KeyError:
totals[date] = usage
scale = resource.get_scale()
for date, total in totals.items():
totals[date] = float(total)/scale
totals = list(sorted(totals.items()))
resources[data] = totals
context = {
'resources': resources,
}
return render(request, 'admin/resources/resourcedata/report.html', context)
history.url_name = 'history'