2014-10-27 13:29:02 +00:00
|
|
|
from django.contrib import messages
|
2014-11-21 13:53:39 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2014-10-27 13:29:02 +00:00
|
|
|
from django.db import transaction
|
|
|
|
from django.shortcuts import redirect
|
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')
|
|
|
|
if not queryset:
|
|
|
|
modeladmin.message_user(request, _("No resource has been selected,"))
|
|
|
|
return redirect(referer)
|
2014-11-21 13:53:39 +00:00
|
|
|
async = modeladmin.model.monitor.func_defaults[0]
|
|
|
|
results = []
|
2014-10-27 13:29:02 +00:00
|
|
|
for resource in queryset:
|
2014-11-21 13:53:39 +00:00
|
|
|
result = resource.monitor()
|
|
|
|
if not async:
|
|
|
|
results += result
|
2014-10-27 13:29:02 +00:00
|
|
|
modeladmin.log_change(request, resource, _("Run monitors"))
|
|
|
|
num = len(queryset)
|
2014-11-20 16:48:50 +00:00
|
|
|
if async:
|
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-21 13:53:39 +00:00
|
|
|
if len(results) == 1:
|
|
|
|
log = results[0].log
|
|
|
|
link = reverse('admin:orchestration_backendlog_change', args=(log.pk,))
|
|
|
|
msg = _("One selected resource has <a href='%s'>been monitored</a>.") % link
|
|
|
|
elif len(results) >= 1:
|
|
|
|
logs = [str(result.log.pk) for result in results]
|
|
|
|
link = reverse('admin:orchestration_backendlog_changelist')
|
|
|
|
link += '?id__in=%s' % ','.join(logs)
|
|
|
|
msg = _("%s selected resources have <a href='%s'>been monitored</a>.") % (num, link)
|
|
|
|
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'
|