Commit cb38473f authored by Felipe Fonseca's avatar Felipe Fonseca
Browse files

Added page to modify/run tests

parent c9ff35ce
......@@ -3,19 +3,16 @@ from tools.communication_handler import CommunicationHandler as CH
import portal.portal_constants as const
import os
import json
class Test(object):
"""
This class handles the Clearing House's (CH) Project Authority (PA)
and also implements supplementary methods.
"""
def __init__(self, **kwargs):
"""
The initialization method.
"""
self.tests = []
self.tests_config = []
# Iterate over keyword arguments.
for key, value in kwargs.items():
......@@ -26,10 +23,21 @@ class Test(object):
#tests_location = '/home/rnp/fibrechecker/output/'
#tests_location = '/home/felipefonseca/Documents/FIBRE/portal-interface/portal/static/output/'
tests_location = '/var/www/portal/portal/static/output'
#tests_location = '/Users/felipefonseca/Documents/FIBRE/portal-interface/portal/static/output'
test_types = ['Ping', 'GetResources', 'ResourceControl', 'RabbitMQ']
islands = ['CPQD', 'NOC', 'PUCRS', 'RNP', 'UFBA', 'UFF', 'UFG', 'UFMG', 'UFPA', 'UFPE', 'UFRGS', 'UFRJ', 'UFSCAR', 'UFU', 'UNIFACS', 'USP']
for test_type in test_types:
config_file = open('{}/{}-config.json'.format(tests_location, test_type), 'r')
config_data = json.load(config_file)
periodicity = config_data['periodicity']
self.tests_config.append({
'test_type': test_type,
'periodicity': periodicity.capitalize()
})
config_file.close()
for island in islands:
if test_type == "ResourceControl" and island != "NOC":
continue
......@@ -71,4 +79,4 @@ class Test(object):
self.tests.append(test)
pass
return self.tests
return {'tests': self.tests, 'tests_config': self.tests_config}
$('.test-periodicity').on('change', function() {
($(this).parent().next().find('button').prop('disabled', false));
});
function updateTest(testType) {
periodicity = $('#' + testType + '-periodicity').children("option:selected").val();
$.ajax({
type: "POST",
url: '/tests/update',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify({
test_type: testType,
periodicity: periodicity
}),
success: function (data) {
if(data) {
toastr.success('The test was successfully updated')
$('#update' + testType + 'Button').prop('disabled', true)
} else {
}
}
});
}
function runTest(testType) {
//alert('Hello!')
$.ajax({
type: "POST",
url: '/tests/run',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify({
test_type: testType
}),
success: function (data) {
if(data) {
toastr.success('The test is being executed. Results should be updated in 5 minutes.')
} else {
}
}
});
}
\ No newline at end of file
{
"periodicity": "Daily"
}
\ No newline at end of file
{
"periodicity": "Hourly"
}
\ No newline at end of file
{
"periodicity": "Daily"
}
\ No newline at end of file
{
"periodicity": "Daily"
}
\ No newline at end of file
......@@ -4,8 +4,11 @@
{% block content %}
<!-- Toastr -->
<script type="text/javascript" src="{% static 'js/toastr/toastr.min.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'css/toastr/toastr.min.css' %}"/>
<h2 class="text-left">Manage Tests</h2>
<h2 class="text-left">Tests</h2>
{% if project_status == 'error' %}
......@@ -34,42 +37,94 @@
{% csrf_token %}
<table class="table" id="testList">
<thead>
<tr>
<th>{{ test_headers.test_island }}</th>
<th>{{ test_headers.test_date }}</th>
<th>Type</th>
<th>{{ test_headers.test_status }}</th>
<th></th>
</tr>
</thead>
<tbody>
{% for test in tests %}
{% if test.status == 'Success' %}
<tr class="bg-success">
<td>{{ test.island}}</td>
<td>{{ test.date }} {{ test.hour }}</td>
<td>{{ test.test_type }}</td>
<td>{{ test.status }}</td>
<td>
<button type="button" class="btn btn-primary pull-right" onclick="showLog('{% static test.log_url %}')">See log</button>
</td>
</tr>
{% else %}
<tr class="bg-danger">
<td>{{ test.island}}</td>
<td>{{ test.date }} {{ test.hour }}</td>
<td>{{ test.test_type }}</td>
<td>{{ test.status }}</td>
<td>
<button type="button" class="btn btn-primary pull-right" onclick="showLog('{% static test.log_url %}')">See log</button>
</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
<ul class="nav nav-tabs">
<li role="presentation" class="active">
<a href="#list-tests" data-toggle="tab">
<span class="glyphicon glyphicon-th-list"></span>
<span>List tests</span>
</a>
</li>
<li role="presentation">
<a href="#manage-tests" data-toggle="tab">
<span class="glyphicon glyphicon glyphicon-cog"></span>
<span>Manage tests</span>
</a>
</li>
</ul>
<div class="tab-content">
<div id="list-tests" class="tab-pane fade in active">
<table class="table" id="testList">
<thead>
<tr>
<th>{{ test_headers.test_island }}</th>
<th>{{ test_headers.test_date }}</th>
<th>Type</th>
<th>{{ test_headers.test_status }}</th>
<th></th>
</tr>
</thead>
<tbody>
{% for test in tests %}
{% if test.status == 'Success' %}
<tr class="bg-success">
<td>{{ test.island}}</td>
<td>{{ test.date }} {{ test.hour }}</td>
<td>{{ test.test_type }}</td>
<td>{{ test.status }}</td>
<td>
<button type="button" class="btn btn-primary pull-right" onclick="showLog('{% static test.log_url %}')">See log</button>
</td>
</tr>
{% else %}
<tr class="bg-danger">
<td>{{ test.island}}</td>
<td>{{ test.date }} {{ test.hour }}</td>
<td>{{ test.test_type }}</td>
<td>{{ test.status }}</td>
<td>
<button type="button" class="btn btn-primary pull-right" onclick="showLog('{% static test.log_url %}')">See log</button>
</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
<div id="manage-tests" class="tab-pane fade">
<br>
{% for config in tests_config %}
<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">Type</label>
<div class="col-sm-2">
<p class="form-control-static"> {{ config.test_type }}</p>
</div>
<div class="col-sm-2">
<button type="button" class="btn btn-default" onclick="runTest('{{ config.test_type }}')">Run now</button>
</div>
</div>
<div class="form-group">
<label for="{{ config.test_type }}-periodicity" class="control-label col-sm-2">Frequency</label>
<div class="col-sm-2">
<select id="{{ config.test_type }}-periodicity" class="form-control test-periodicity">
<option value="Weekly" {% if config.periodicity == 'Weekly'%} selected {% endif %}>Weekly</option>
<option value="Daily" {% if config.periodicity == 'Daily'%} selected {% endif %}>Daily</option>
<option value="Hourly" {% if config.periodicity == 'Hourly'%} selected {% endif %}>Hourly</option>
<option value="Every 30 minutes" {% if config.periodicity == 'Every 30 minutes'%} selected {% endif %}>Every 30 minutes</option>
<option value="Every 15 minutes" {% if config.periodicity == 'Every 15 minutes'%} selected {% endif %}>Every 15 minutes</option>
</select>
</div>
<div class="col-sm-2">
<button type="button" id="update{{ config.test_type}}Button" class="btn btn-primary" onclick="updateTest('{{ config.test_type }}')" disabled>Update</button>
</div>
</div>
</form>
<br><br>
{% endfor %}
</div>
</div>
<!--Modal: Name-->
<div class="modal fade" id="modalLog" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
......@@ -90,7 +145,8 @@
</div>
</div>
</div>
<script type="text/javascript" src="{% static 'js/pages/tests/test_list.js' %}"></script>
<script type="text/javascript">
$(document).ready( function () {
$('#testList').DataTable();
......
......@@ -125,6 +125,14 @@ urlpatterns = [
url(r'^tests/$',
views_tests.list_tests,
name='list_tests'),
url(r'^tests/update$',
views_tests.update_test,
name='update_test'),
url(r'^tests/run$',
views_tests.run_test,
name='run_test'),
# #############################################
......
......@@ -7,6 +7,9 @@ from django.http import Http404
from django.shortcuts import render, redirect
from django.utils.translation import gettext as _
from django.contrib import messages
from django.http import JsonResponse, HttpResponse
from crontab import CronTab
import portal.util as util
from portal.forms.forms_projects import CreateProjectForm, UpdateProjectForm
......@@ -20,6 +23,10 @@ from portal.models.request import Request
import portal.views.views_requests as v_req
from portal.decorators import ch_login_required, is_full_member, project_exist
import json
from base64 import b64encode, b64decode
import os
# Set log config.
from portal.models.slice_resource import SliceResource
......@@ -39,20 +46,113 @@ def list_tests(request):
context = {}
if request.session['member_privilege'] < 3:
messages.error(request, _('You are not a member of this project, request access.'))
messages.error(request, _('You are not authorized to view or manage tests.'))
return redirect('portal:home')
tests = Test().get_all_tests()
# Set context dictionary as an empty dictionary.
print(tests)
context['tests'] = tests
# print(tests)
context['tests'] = tests['tests']
context['tests_config'] = tests['tests_config']
context['test_headers'] = const.VIEW_TEST_FIELD_DETAILS
# Render html.
return render(request, 'tests/test_list.html', context)
@ch_login_required
def update_test(request):
""" Method that load member's project data and renders a template which
friendly presents those data.
"""
scripts_folder = "/home/rnp"
configs_folder = "/var/www/portal/portal/static/output"
#configs_folder = '/Users/felipefonseca/Documents/FIBRE/portal-interface/portal/static/output'
if request.session['member_privilege'] < 3:
messages.error(request, _('You are not authorized to view or manage tests.'))
return redirect('portal:home')
if request.method != 'POST':
return JsonResponse({'error': 'Forbidden!'}, json_dumps_params={'indent': 2})
try:
json_data = json.loads(request.body.decode('utf-8'))
test_type = json_data['test_type']
periodicity = json_data['periodicity']
except Exception as e:
test_type = ""
periodicity = ""
print(e)
response = {'error': 'Could not update test'}
try:
cron = CronTab(user='rnp')
jobs = cron.find_command(test_type.lower())
for job in jobs:
job.clear()
if periodicity == "Weekly":
job.setall('@weekly')
if periodicity == "Daily":
job.every().dom()
if periodicity == "Hourly":
job.every().hours()
if periodicity == "Every 30 minutes":
job.minute.every(30)
if periodicity == "Every 15 minutes":
job.minute.every(15)
cron.write()
with open('{}/{}-config.json'.format(configs_folder, test_type), 'w') as fp:
json.dump({'periodicity': periodicity}, fp, indent=2)
response = {'success': True}
except Exception as e:
print(e)
return JsonResponse(response, json_dumps_params={'indent': 2})
@ch_login_required
def run_test(request):
""" Method that load member's project data and renders a template which
friendly presents those data.
"""
scripts_folder = "/home/rnp"
if request.session['member_privilege'] < 3:
messages.error(request, _('You are not authorized to view or manage tests.'))
return redirect('portal:home')
if request.method != 'POST':
return JsonResponse({'error': 'Forbidden!'}, json_dumps_params={'indent': 2})
try:
json_data = json.loads(request.body.decode('utf-8'))
test_type = json_data['test_type']
except Exception as e:
test_type = ""
print(e)
response = {'error': 'Could not run test'}
try:
script_file = '{}/start_checker_{}.sh'.format(scripts_folder, test_type.lower())
os.system(script_file)
response = {'success': True}
except Exception as e:
print(e)
return JsonResponse(response, json_dumps_params={'indent': 2})
@ch_login_required
@is_full_member
def create_project_form(request):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment