Skip to content

Commit

Permalink
[Sahara] Add support for event logs
Browse files Browse the repository at this point in the history
Sahara API provides a log of events happening while the cluster is being
set up or deleted. The tab in the 'cluster details' view was added to
expose these events to a user.

Implements blueprint: sahara-event-log

Change-Id: Ie8b0d895b0b5af4b3cf2fffe7ac490eac633d97a
  • Loading branch information
Konovalov-Nik authored and dklyle committed Aug 27, 2015
1 parent d7016d6 commit e0cd466
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 4 deletions.
6 changes: 4 additions & 2 deletions openstack_dashboard/contrib/sahara/api/sahara.py
Expand Up @@ -275,8 +275,10 @@ def cluster_list(request, search_opts=None):
return client(request).clusters.list(search_opts=search_opts)


def cluster_get(request, cluster_id):
return client(request).clusters.get(cluster_id=cluster_id)
def cluster_get(request, cluster_id, show_progress=False):
return client(request).clusters.get(
cluster_id=cluster_id,
show_progress=show_progress)


def cluster_delete(request, cluster_id):
Expand Down
Expand Up @@ -176,7 +176,20 @@ def get_cluster_instances_data(self):
return instances


class EventLogTab(tabs.Tab):
name = _("Cluster Events")
slug = "cluster_event_log"
template_name = "project/data_processing.clusters/_event_log.html"

def get_context_data(self, request, **kwargs):
cluster_id = self.tab_group.kwargs['cluster_id']
kwargs["cluster_id"] = cluster_id
kwargs['data_update_url'] = request.get_full_path()

return kwargs


class ClusterDetailsTabs(tabs.TabGroup):
slug = "cluster_details"
tabs = (GeneralTab, NodeGroupsTab, InstancesTab, )
tabs = (GeneralTab, NodeGroupsTab, InstancesTab, EventLogTab)
sticky = True
@@ -0,0 +1,62 @@
{% load i18n %}

<h4>{% trans "Cluster provision steps" %}</h4>
<table id="steps_table" class="table table-bordered datatable">
<thead>
<tr>
<th>{% trans "Step Description" %}</th>
<th>{% trans "Started at" %}</th>
<th>{% trans "Duration" %}</th>
<th>{% trans "Progress" %}</th>
<th>{% trans "Status" %}</th>
</tr>
</thead>
<tbody id="steps_body">
</tbody>
</table>

<div id="events_modal" class="modal fade">
<div class="modal-dialog" style="width: 85%">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 id="events_modal_header"></h4>
<span id="modal_status_marker"></span>
</div>
<div class="modal-body">
<table id="events_table" class="table table-bordered datatable">
<thead>
<tr>
<th>{% trans "Node Group" %}</th>
<th>{% trans "Instance" %}</th>
<th>{% trans "Event time" %}</th>
<th>{% trans "Info" %}</th>
<th>{% trans "Status" %}</th>
</tr>
</thead>
<tbody id="events_body">
</tbody>
</table>
</div>
</div>
</div>
</div>

<script type="text/javascript">

$(function () {
// Initialize everything.
horizon.event_log.cluster_id = "{{ cluster_id }}";
horizon.event_log.data_update_url = "{{ data_update_url }}";
horizon.event_log.fetch_update_events();
});

$(".show_events_btn").live("click", function () {
// Bind "show events" buttons to modals.
horizon.event_log.modal_step_id = $(this).data("step-id");
horizon.event_log.clear_events();
horizon.event_log.clear_modal_status();
horizon.event_log.update_events_rows(horizon.event_log.cached_data);
});

</script>
Expand Up @@ -10,6 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.

import json

from django.core.urlresolvers import reverse
from django import http

Expand Down Expand Up @@ -49,6 +51,31 @@ def test_launch_cluster_get_nodata(self):
self.assertContains(res, "No Images Available")
self.assertContains(res, "No Templates Available")

@test.create_stubs({api.sahara: ('cluster_get',)})
def test_event_log_tab(self):
cluster = self.clusters.list()[-1]
api.sahara.cluster_get(IsA(http.HttpRequest),
"cl2", show_progress=True).AndReturn(cluster)
self.mox.ReplayAll()

url = reverse(
'horizon:project:data_processing.clusters:events', args=["cl2"])
res = self.client.get(url)
data = json.loads(res.content)

self.assertIn("provision_steps", data)
self.assertEqual(data["need_update"], False)

step_0 = data["provision_steps"][0]
self.assertEqual(2, step_0["completed"])
self.assertEqual(2, len(step_0["events"]))
for evt in step_0["events"]:
self.assertEqual(True, evt["successful"])

step_1 = data["provision_steps"][1]
self.assertEqual(3, step_1["completed"])
self.assertEqual(0, len(step_1["events"]))

@test.create_stubs({api.sahara: ('cluster_list',
'cluster_delete')})
def test_delete(self):
Expand Down
Expand Up @@ -33,6 +33,9 @@
url(r'^(?P<cluster_id>[^/]+)$',
views.ClusterDetailsView.as_view(),
name='details'),
url(r'^(?P<cluster_id>[^/]+)/events$',
views.ClusterEventsView.as_view(),
name='events'),
url(r'^(?P<cluster_id>[^/]+)/scale$',
views.ScaleClusterView.as_view(),
name='scale'))
Expand Up @@ -11,9 +11,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from datetime import datetime
import json
import logging

from django.utils.translation import ugettext_lazy as _
from django.http import HttpResponse
from django.utils.translation import ugettext as _
from django.views.generic import base as django_base
import six

from horizon import exceptions
from horizon import tables
Expand All @@ -32,6 +37,7 @@
workflows.create as create_flow
import openstack_dashboard.contrib.sahara.content.data_processing.clusters. \
workflows.scale as scale_flow
from saharaclient.api.base import APIException

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -77,6 +83,94 @@ def get_context_data(self, **kwargs):
return context


class ClusterEventsView(django_base.View):

_date_format = "%Y-%m-%dT%H:%M:%S"

@staticmethod
def _created_at_key(obj):
return datetime.strptime(obj["created_at"],
ClusterEventsView._date_format)

def get(self, request, *args, **kwargs):

cluster_id = kwargs.get("cluster_id")

try:
cluster = saharaclient.cluster_get(request, cluster_id,
show_progress=True)
node_group_mapping = {}
for node_group in cluster.node_groups:
node_group_mapping[node_group["id"]] = node_group["name"]

provision_steps = cluster.provision_progress

# Sort by create time
provision_steps = sorted(provision_steps,
key=ClusterEventsView._created_at_key,
reverse=True)

for step in provision_steps:
# Sort events of the steps also
step["events"] = sorted(step["events"],
key=ClusterEventsView._created_at_key,
reverse=True)

successful_events_count = 0

for event in step["events"]:
if event["node_group_id"]:
event["node_group_name"] = node_group_mapping[
event["node_group_id"]]

event_result = _("Unknown")
if event["successful"] is True:
successful_events_count += 1
event_result = _("Completed Successfully")
elif event["successful"] is False:
event_result = _("Failed")

event["result"] = event_result

if not event["event_info"]:
event["event_info"] = _("No info available")

start_time = datetime.strptime(step["created_at"],
self._date_format)
end_time = datetime.now()
# Clear out microseconds. There is no need for that precision.
end_time = end_time.replace(microsecond=0)
if step["successful"] is not None:
updated_at = step["updated_at"]
end_time = datetime.strptime(updated_at,
self._date_format)
step["duration"] = six.text_type(end_time - start_time)

result = _("In progress")
step["completed"] = successful_events_count

if step["successful"] is True:
step["completed"] = step["total"]
result = _("Completed Successfully")
elif step["successful"] is False:
result = _("Failed")

step["result"] = result

status = cluster.status.lower()
need_update = status not in ("active", "error")
except APIException:
# Cluster is not available. Returning empty event log.
need_update = False
provision_steps = []

context = {"provision_steps": provision_steps,
"need_update": need_update}

return HttpResponse(json.dumps(context),
content_type='application/json')


class CreateClusterView(workflows.WorkflowView):
workflow_class = create_flow.CreateCluster
success_url = \
Expand Down

0 comments on commit e0cd466

Please sign in to comment.