Commit 0003f774 by Kálmán Viktor

dashboard: rework activity ajax

parent ad85f38e
$(function() {
if($('.timeline .activity:first i:first').hasClass('icon-spin'))
checkNewActivity();
/* do we need to check for new activities */
if(decideActivityRefresh()) {
checkNewActivity(false, 1);
}
/* save resources */
$('#vm-details-resources-save').click(function() {
......@@ -172,48 +174,41 @@ function removePort(data) {
});
}
function checkNewActivity() {
var latest = $('.activity:first').data('activity-id');
var latest_sub = $('div[data-activity-id="' + latest + '"] .sub-timeline .sub-activity:first').data('activity-id');
function decideActivityRefresh() {
var check = false;
/* if something is still spinning */
if($('.timeline .activity:first i:first').hasClass('icon-spin'))
check = true;
/* if there is only one activity */
if($('#activity-timeline div[class="activity"]').length < 2)
check = true;
return check;
}
function checkNewActivity(only_state, runs) {
// set default only_state to false
only_state = typeof only_state !== 'undefined' ? only_state : false;
var instance = location.href.split('/'); instance = instance[instance.length - 2];
$.ajax({
type: 'POST',
type: 'GET',
url: '/dashboard/vm/' + instance + '/activity/',
headers: {"X-CSRFToken": getCookie('csrftoken')},
data: {'latest': latest, 'latest_sub': latest_sub},
data: {'only_state': only_state},
success: function(data) {
if(data['new_sub_activities'].length > 0) {
d = data['new_sub_activities'];
html = ""
for(var i=0; i<d.length; i++) {
html += '<div data-activity-id="' + d[i].id + '" class="sub-activity">' + d[i].name + ' - ';
if(d[i].finished != null) {
html += d[i].finished
} else {
html += '<i class="icon-refresh icon-spin" class="sub-activity-loading-icon"></i>';
}
html += '</div>';
}
$('div[data-activity-id="' + latest_sub + '"] .sub-activity .sub-activity-loading-icon').remove();
$('div[data-activity-id="' + latest + '"] .sub-timeline').prepend(html);
if(!only_state) {
$("#activity-timeline").html(data['activities']);
}
if(data['is_parent_finished']) {
var c = "icon-plus"
$('div[data-activity-id="' + latest + '"] .icon-refresh.icon-spin:first').removeClass('icon-refresh').removeClass('icon-spin').addClass(c);
}
$("#vm-details-state").html(data['state']);
if(data['latest_sub_finished'] != null) {
s = $('div[data-activity-id="' + latest_sub + '"]')
$('.icon-refresh.icon-spin', s).remove();
$(s).append(data['latest_sub_finished']);
if(decideActivityRefresh()) {
console.log("szia");
setTimeout(
function() {checkNewActivity(true, runs + 1)},
1000 + runs * 250
);
}
if(data['is_parent_finished'])
return;
else
setTimeout(checkNewActivity, 1000);
},
error: function() {
......
......@@ -51,7 +51,7 @@
<div class="row">
<div class="col-md-4" id="vm-info-pane">
<div class="big">
<span class="label label-success">{{ instance.state }}</span>
<span id="vm-details-state" class="label label-success">{{ instance.state }}</span>
<div class="btn-group">
<button type="button" class="btn btn-warning dropdown-toggle" data-toggle="dropdown">Action <i class="icon-caret-down"></i></button>
<ul class="dropdown-menu" role="menu">
......
{% for a in activities %}
<div class="activity" data-activity-id="{{ a.pk }}">
<span class="timeline-icon">
<i class="{% if not a.finished %} icon-refresh icon-spin {% else %}icon-plus{% endif %}"></i>
</span>
<strong>{{ a.get_readable_name }}</strong>
{{ a.started|date:"Y-m-d H:i" }}, {{ a.user }}
{% if a.children.count > 0 %}
<div class="sub-timeline">
{% for s in a.children.all %}
<div data-activity-id="{{ s.pk }}" class="sub-activity">
{{ s.get_readable_name }} -
{% if s.finished %}
{{ s.finished|time:"H:i:s" }}
{% else %}
<i class="icon-refresh icon-spin" class="sub-activity-loading-icon"></i>
{% endif %}
</div>
{% endfor %}
</div>
{% endif %}
</div>
{% endfor %}
......@@ -8,30 +8,8 @@
}
</style>
<div class="timeline">
{% for a in activity %}
<div class="activity" data-activity-id="{{ a.pk }}">
<span class="timeline-icon">
<i class="{% if not a.finished %} icon-refresh icon-spin {% else %}icon-plus{% endif %}"></i>
</span>
<strong>{{ a.get_readable_name }}</strong>
{{ a.started|date:"Y-m-d H:i" }}, {{ a.user }}
{% if a.children.count > 0 %}
<div class="sub-timeline">
{% for s in a.children.all %}
<div data-activity-id="{{ s.pk }}" class="sub-activity">
{{ s.get_readable_name }} -
{% if s.finished %}
{{ s.finished|time:"H:i:s" }}
{% else %}
<i class="icon-refresh icon-spin" class="sub-activity-loading-icon"></i>
{% endif %}
</div>
{% endfor %}
</div>
{% endif %}
</div>
{% endfor %}
<div id="activity-timeline" class="timeline">
{% include "dashboard/vm-detail/_activity-timeline.html" %}
</div>
{% block extra_js %}
......
......@@ -14,13 +14,14 @@ from django.core import signing
from django.core.urlresolvers import reverse, reverse_lazy
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import redirect, render
from django.views.decorators.http import require_POST
from django.views.decorators.http import require_GET
from django.views.generic.detail import SingleObjectMixin
from django.views.generic import (TemplateView, DetailView, View, DeleteView,
UpdateView, CreateView)
from django.contrib import messages
from django.utils.translation import ugettext as _
from django.template.defaultfilters import title
from django.template.loader import render_to_string
from django.forms.models import inlineformset_factory
from django_tables2 import SingleTableView
......@@ -151,7 +152,7 @@ class VmDetailView(CheckedDetailView):
ia = InstanceActivity.objects.filter(
instance=self.object, parent=None
).order_by('-started').select_related()
context['activity'] = ia
context['activities'] = ia
context['vlans'] = Vlan.get_objects_with_level(
'user', self.request.user
......@@ -1186,40 +1187,25 @@ class LeaseDelete(LoginRequiredMixin, SuperuserRequiredMixin, DeleteView):
return HttpResponseRedirect(success_url)
@require_POST
@require_GET
def vm_activity(request, pk):
object = Instance.objects.get(pk=pk)
if not object.has_level(request.user, 'owner'):
instance = Instance.objects.get(pk=pk)
if not instance.has_level(request.user, 'owner'):
raise PermissionDenied()
latest = request.POST.get('latest')
latest_sub = request.POST.get('latest_sub')
instance = Instance.objects.get(pk=pk)
new_sub_activities = InstanceActivity.objects.filter(
parent=latest, pk__gt=latest_sub,
instance=instance)
# new_activities = InstanceActivity.objects.filter(
# parent=None, instance=instance, pk__gt=latest).values('finished')
latest_sub_finished = InstanceActivity.objects.get(pk=latest_sub).finished
time_string = "%H:%M:%S"
new_sub_activities = [
{'name': a.get_readable_name(), 'id': a.pk,
'finished': None if a.finished is None else a.finished.strftime(
time_string
)
} for a in new_sub_activities
]
response = {
'new_sub_activities': new_sub_activities,
# TODO 'new_acitivites': new_activities,
'is_parent_finished': True if InstanceActivity.objects.get(
pk=latest).finished is not None else False,
'latest_sub_finished': None if latest_sub_finished is None else
latest_sub_finished.strftime(time_string)
}
response = {}
only_state = request.GET.get("only_state")
response['state'] = instance.state
if only_state is not None and only_state == "false": # instance activity
print "Sdsa"
activities = render_to_string(
"dashboard/vm-detail/_activity-timeline.html",
{'activities': InstanceActivity.objects.filter(
instance=instance, parent=None
).order_by('-started').select_related()}
)
response['activities'] = activities
return HttpResponse(
json.dumps(response),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment