2015-07-23 12:41:42 +00:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2014-11-21 13:53:39 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2015-07-16 13:07:15 +00:00
|
|
|
from django.shortcuts import redirect, render
|
2014-11-21 13:53:39 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
2014-10-27 13:29:02 +00:00
|
|
|
from django.utils.translation import ungettext, ugettext_lazy as _
|
|
|
|
|
|
|
|
|
|
|
|
def run_monitor(modeladmin, request, queryset):
|
2014-11-20 16:48:50 +00:00
|
|
|
""" Resource and ResourceData run monitors """
|
|
|
|
referer = request.META.get('HTTP_REFERER')
|
2015-04-02 16:14:55 +00:00
|
|
|
async = modeladmin.model.monitor.__defaults__[0]
|
2014-11-27 19:17:26 +00:00
|
|
|
logs = set()
|
2014-10-27 13:29:02 +00:00
|
|
|
for resource in queryset:
|
2015-04-12 18:18:10 +00:00
|
|
|
rlogs = resource.monitor()
|
2014-11-21 13:53:39 +00:00
|
|
|
if not async:
|
2015-06-16 11:34:46 +00:00
|
|
|
logs = logs.union(set([str(log.pk) for log in rlogs]))
|
2014-10-27 13:29:02 +00:00
|
|
|
modeladmin.log_change(request, resource, _("Run monitors"))
|
2014-11-20 16:48:50 +00:00
|
|
|
if async:
|
2014-11-27 19:17:26 +00:00
|
|
|
num = len(queryset)
|
2015-05-01 18:05:34 +00:00
|
|
|
# TODO listfilter by uuid: task.request.id + ?task_id__in=ids
|
2014-11-21 13:53:39 +00:00
|
|
|
link = reverse('admin:djcelery_taskstate_changelist')
|
2014-11-20 16:48:50 +00:00
|
|
|
msg = ungettext(
|
2014-11-21 13:53:39 +00:00
|
|
|
_("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),
|
2014-11-20 16:48:50 +00:00
|
|
|
num)
|
|
|
|
else:
|
2014-11-27 19:17:26 +00:00
|
|
|
num = len(logs)
|
|
|
|
if num == 1:
|
2015-03-20 15:13:08 +00:00
|
|
|
log_pk = int(logs.pop())
|
|
|
|
link = reverse('admin:orchestration_backendlog_change', args=(log_pk,))
|
2014-11-27 19:17:26 +00:00
|
|
|
msg = _("One related monitor has <a href='%s'>been executed</a>.") % link
|
|
|
|
elif num >= 1:
|
2014-11-21 13:53:39 +00:00
|
|
|
link = reverse('admin:orchestration_backendlog_changelist')
|
|
|
|
link += '?id__in=%s' % ','.join(logs)
|
2014-11-27 19:17:26 +00:00
|
|
|
msg = _("%s related monitors have <a href='%s'>been executed</a>.") % (num, link)
|
2014-11-21 13:53:39 +00:00
|
|
|
else:
|
|
|
|
msg = _("No related monitors have been executed.")
|
|
|
|
modeladmin.message_user(request, mark_safe(msg))
|
2014-10-27 13:29:02 +00:00
|
|
|
if referer:
|
|
|
|
return redirect(referer)
|
|
|
|
run_monitor.url_name = 'monitor'
|
2015-07-16 13:07:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def history(modeladmin, request, queryset):
|
2015-07-23 12:41:42 +00:00
|
|
|
resources = OrderedDict()
|
|
|
|
names = {}
|
2015-07-16 13:07:15 +00:00
|
|
|
for data in queryset:
|
|
|
|
resource = data.resource
|
2015-07-23 12:41:42 +00:00
|
|
|
try:
|
|
|
|
objects, totals, all_dates = resources[resource]
|
|
|
|
except KeyError:
|
|
|
|
objects, totals, all_dates = OrderedDict(), OrderedDict(), set()
|
|
|
|
resources[resource] = (objects, totals, all_dates)
|
|
|
|
scale = resource.get_scale()
|
|
|
|
# Per monitor
|
|
|
|
aggregate = len(resource.monitors) > 1
|
|
|
|
for monitor, dataset in data.get_monitor_datasets():
|
2015-07-16 13:07:15 +00:00
|
|
|
for date, dataset in resource.aggregation_instance.historic_filter(dataset):
|
2015-07-23 12:41:42 +00:00
|
|
|
if dataset is None:
|
|
|
|
break
|
|
|
|
all_dates.add(date)
|
|
|
|
# Per object
|
|
|
|
for mdata, usage in resource.aggregation_instance.compute_historic_usage(dataset):
|
|
|
|
# objects
|
|
|
|
usage = float(usage or 0)/scale
|
|
|
|
key = (monitor, mdata.object_id)
|
|
|
|
names[key] = mdata.content_object_repr
|
2015-07-16 13:07:15 +00:00
|
|
|
try:
|
2015-07-23 12:41:42 +00:00
|
|
|
dates = objects[key]
|
2015-07-16 13:07:15 +00:00
|
|
|
except KeyError:
|
2015-07-23 12:41:42 +00:00
|
|
|
dates = {
|
|
|
|
date: usage
|
|
|
|
}
|
|
|
|
objects[key] = dates
|
|
|
|
dates[date] = usage
|
|
|
|
if aggregate:
|
|
|
|
# Totals
|
|
|
|
key = (data)
|
|
|
|
try:
|
|
|
|
dates = totals[data]
|
|
|
|
except KeyError:
|
|
|
|
dates = {
|
|
|
|
date: usage
|
|
|
|
}
|
|
|
|
totals[data] = dates
|
|
|
|
try:
|
|
|
|
dates[date] += usage
|
|
|
|
except KeyError:
|
|
|
|
dates[date] = usage
|
|
|
|
results = []
|
|
|
|
from .backends import ServiceMonitor
|
|
|
|
import json
|
|
|
|
from django.template.defaultfilters import date as date_filter
|
|
|
|
for resource, content in resources.items():
|
|
|
|
object_result = []
|
|
|
|
total_result = []
|
|
|
|
objects, totals, all_dates = content
|
|
|
|
all_dates = list(sorted(all_dates))
|
|
|
|
for key, dates in objects.items():
|
|
|
|
name = names[key]
|
|
|
|
data = []
|
|
|
|
for date in all_dates:
|
|
|
|
try:
|
|
|
|
data.append(round(dates[date], 2))
|
|
|
|
except KeyError:
|
|
|
|
data.append(0)
|
|
|
|
object_result.append({
|
|
|
|
'name': name,
|
|
|
|
'data': data,
|
|
|
|
})
|
|
|
|
if len(totals) > 1:
|
|
|
|
for rdata, dates in totals.items():
|
|
|
|
name = rdata.content_object_repr
|
|
|
|
data = []
|
|
|
|
for date in all_dates:
|
|
|
|
try:
|
|
|
|
data.append(round(dates[date], 2))
|
|
|
|
except KeyError:
|
|
|
|
data.append(0)
|
|
|
|
total_result.append({
|
|
|
|
'name': name,
|
|
|
|
'data': data,
|
|
|
|
})
|
|
|
|
results.append((resource, object_result, total_result, all_dates))
|
|
|
|
|
|
|
|
|
2015-07-16 13:07:15 +00:00
|
|
|
context = {
|
2015-07-23 12:41:42 +00:00
|
|
|
'resources': results
|
2015-07-16 13:07:15 +00:00
|
|
|
}
|
2015-07-17 13:29:29 +00:00
|
|
|
return render(request, 'admin/resources/resourcedata/history.html', context)
|
2015-07-16 13:07:15 +00:00
|
|
|
history.url_name = 'history'
|