summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcoderick142017-06-30 20:14:49 +0530
committercoderick142017-06-30 20:44:30 +0530
commit93b771d4dcb515a8662611b135d13abe27c767ed (patch)
tree8bbd5fd9da5f9e340a96d4de294307bfa9120e0d
parentb53254f75b79fecf97a33f685de8d3ef8a261dc7 (diff)
downloadSBHS-2018-Rpi-93b771d4dcb515a8662611b135d13abe27c767ed.tar.gz
SBHS-2018-Rpi-93b771d4dcb515a8662611b135d13abe27c767ed.tar.bz2
SBHS-2018-Rpi-93b771d4dcb515a8662611b135d13abe27c767ed.zip
Add feature to fetch logs based on date-time
-rw-r--r--myadmin/urls.py5
-rw-r--r--myadmin/views.py45
-rw-r--r--templates/admin/sub_nav.html1
-rw-r--r--templates/admin/user_logs.html167
4 files changed, 216 insertions, 2 deletions
diff --git a/myadmin/urls.py b/myadmin/urls.py
index 20e6311..7272f79 100644
--- a/myadmin/urls.py
+++ b/myadmin/urls.py
@@ -6,6 +6,7 @@ urlpatterns = [
url(r'^admin/?$', views.index, name='admin_index'),
url(r'^admin/bookings/?$', views.booking_index, name='admin_bookings'),
url(r'^admin/webcam/?$', views.webcam_index, name='admin_webcam'),
+ url(r'^admin/viewlogs/?$', views.logs_index, name='admin_viewlogs'),
url(r'^admin/profile/([0-9]+)/?$', views.profile, name='admin_profile'),
url(r'^admin/testing/?$', views.testing, name='admin_testing'),
url(r'^admin/resetdevice/?$', views.reset_device, name='admin_reset_device'),
@@ -17,6 +18,8 @@ urlpatterns = [
url(r'^admin/changemid/?$', views.get_allocated_mids, name='admin_getmids'),
url(r'^admin/getusers/?$', views.get_users, name='admin_users'),
url(r'^admin/toggledevice', views.toggle_device_status, name='toggle_status'),
+ url(r'^admin/rangelogs/?$', views.range_logs, name='admin_range_logs'),
+ url(r'^admin/explogs/([0-9]+)/?$', views.download_experiment_log, name='admin_explogs'),
url(r'^admin/toggle_allotment_mode/?$', views.toggle_allotment_mode, name='admin_toggle_allotment_mode'),
-] \ No newline at end of file
+]
diff --git a/myadmin/views.py b/myadmin/views.py
index f0c811a..8e1372d 100644
--- a/myadmin/views.py
+++ b/myadmin/views.py
@@ -46,6 +46,12 @@ def webcam_index(req):
return render(req, 'admin/webcam_index.html', {"boards": boards})
@login_required(redirect_field_name=None)
+def logs_index(req):
+ checkadmin(req)
+ date = (datetime.datetime.now()).strftime("%Y-%m-%d")
+ return render(req, 'admin/user_logs.html', {"nowdate" : date})
+
+@login_required(redirect_field_name=None)
def profile(req, mid):
checkadmin(req)
try:
@@ -174,7 +180,7 @@ def toggle_device_status(req):
return HttpResponse(json.dumps({"status_code":200, "message":"Toggle successful"}), content_type="application/json")
except Exception as e:
- return HttpResponse(json.dumps({"status_code":400, "message":"Unsuccessful"}), content_type="application/json")
+ return HttpResponse(json.dumps({"status_code":500, "message":"Unsuccessful"}), content_type="application/json")
def user_exists(username):
@@ -214,6 +220,43 @@ def download_log(req, mid):
except:
return HttpResponse("Requested log file doesn't exist.Please Try in the next hour after your slot ends.")
+@login_required(redirect_field_name=None)
+@csrf_exempt
+def range_logs(req):
+ checkadmin(req)
+ try:
+ start_date = req.POST.get("start_date")
+ end_date = req.POST.get("end_date")
+ start_time = req.POST.get("start_time")
+ end_time = req.POST.get("end_time")
+ except:
+ return HttpResponse(json.dumps({"status_code":400, "message":"Invalid parameters"}), content_type="application/json")
+
+ try:
+ start = start_date + " " + start_time
+ end = end_date + " " + end_time
+ log_files = Experiment.objects.filter(created_at__range=[start, end]).values("id", "log")
+
+ return HttpResponse(json.dumps({"status_code":200, "message":list(log_files)}), content_type="application/json")
+ except Exception as e:
+ return HttpResponse(json.dumps({"status_code": 500, "message": "Some error occured" + str(e)}), content_type="application/json")
+
+@login_required(redirect_field_name=None)
+@csrf_exempt
+def download_experiment_log(req, experiment_id):
+ """ Downloads the experimental log file.
+ Input: req: request object, experiment_id: experimental id
+ Output: HttpResponse object
+ """
+ checkadmin(req)
+ try:
+ experiment_data = Experiment.objects.select_related("booking", "booking__account").get(id=experiment_id)
+ f = open(os.path.join(settings.EXPERIMENT_LOGS_DIR, experiment_data.log), "r")
+ data = f.read()
+ f.close()
+ return HttpResponse(data, content_type='text/text')
+ except:
+ return HttpResponse("Requested log file doesn't exist.")
@csrf_exempt
def reset_device(req):
diff --git a/templates/admin/sub_nav.html b/templates/admin/sub_nav.html
index b493274..c77205b 100644
--- a/templates/admin/sub_nav.html
+++ b/templates/admin/sub_nav.html
@@ -3,4 +3,5 @@
<a href="{% url 'admin_webcam' %}" class="btn btn-info">Show all images</a>
<a href="{% url 'admin_testing' %}" class="btn btn-info">Test boards</a>
<a href="{% url 'admin_getmids' %}" class="btn btn-info">Update MID</a>
+<a href="{% url 'admin_viewlogs' %}" class="btn btn-info">Fetch Logs</a>
<br><br> \ No newline at end of file
diff --git a/templates/admin/user_logs.html b/templates/admin/user_logs.html
new file mode 100644
index 0000000..4586b56
--- /dev/null
+++ b/templates/admin/user_logs.html
@@ -0,0 +1,167 @@
+{% extends "layout.html" %}
+{% load staticfiles %}
+
+{% block headerfiles %}
+<script src="{% static "js/bootstrap-datepicker.js" %}"></script>
+<link rel="stylesheet" href="{% static "css/datepicker.css" %}">
+{% endblock %}
+
+{% block content %}
+<div class="container">
+ <div class="row">
+ {% include "account/sub_nav.html" %}
+ <div class="span12">
+ {% include "admin/sub_nav.html" %}
+
+ <h4>Filter log files</h4>
+ <div id="mywrapper">
+ <label for="start-date">Start date</label>
+ <div class="input-append date" id="dp1" data-date="{{ nowdate }}" data-date-format="yyyy-mm-dd">
+ <input id="start-date" class="span2" size="16" type="text" value="{{ nowdate }}" readonly>
+ <span class="add-on"><i class="icon-calendar"></i></span>
+ </div>
+
+ <label for="end-date">End date</label>
+ <div class="input-append date" id="dp2" data-date="{{ nowdate }}" data-date-format="yyyy-mm-dd">
+ <input id="end-date" class="span2" size="16" type="text" value="{{ nowdate }}" readonly>
+ <span class="add-on"><i class="icon-calendar"></i></span>
+ </div>
+
+ <label for="start-time">Start time</label>
+ <select id="start-time">
+ <option>00:00:00</option>
+ <option>01:00:00</option>
+ <option>02:00:00</option>
+ <option>03:00:00</option>
+ <option>04:00:00</option>
+ <option>05:00:00</option>
+ <option>06:00:00</option>
+ <option>07:00:00</option>
+ <option>08:00:00</option>
+ <option>09:00:00</option>
+ <option>10:00:00</option>
+ <option>11:00:00</option>
+ <option>12:00:00</option>
+ <option>13:00:00</option>
+ <option>14:00:00</option>
+ <option>15:00:00</option>
+ <option>16:00:00</option>
+ <option>17:00:00</option>
+ <option>18:00:00</option>
+ <option>19:00:00</option>
+ <option>20:00:00</option>
+ <option>21:00:00</option>
+ <option>22:00:00</option>
+ <option>23:00:00</option>
+ </select>
+
+ <label for="end-time">End time</label>
+ <select id="end-time">
+ <option>00:00:00</option>
+ <option>01:00:00</option>
+ <option>02:00:00</option>
+ <option>03:00:00</option>
+ <option>04:00:00</option>
+ <option>05:00:00</option>
+ <option>06:00:00</option>
+ <option>07:00:00</option>
+ <option>08:00:00</option>
+ <option>09:00:00</option>
+ <option>10:00:00</option>
+ <option>11:00:00</option>
+ <option>12:00:00</option>
+ <option>13:00:00</option>
+ <option>14:00:00</option>
+ <option>15:00:00</option>
+ <option>16:00:00</option>
+ <option>17:00:00</option>
+ <option>18:00:00</option>
+ <option>19:00:00</option>
+ <option>20:00:00</option>
+ <option>21:00:00</option>
+ <option>22:00:00</option>
+ <option>23:00:00</option>
+ </select>
+
+ <br>
+ <button type="button" class="btn btn-primary" onclick="fetchFileNames()">Submit</button>
+ </div>
+ </div>
+ </div>
+ <h4 id="file-count"></h4>
+ <table id="logs-table" class="table table-condensed">
+ <thead>
+ <tr>
+ <th>Sl No</th>
+ <th>User</th>
+ <th>Filename</th>
+ </tr>
+ </thead>
+ <tbody>
+
+ </tbody>
+ </table>
+</div>
+<script type="text/javascript">
+var startDate = "{{ nowdate }}";
+var endDate = "{{ nowdate }}";
+
+(function(){
+ $("#start-date").val("{{ nowdate }}");
+ $("#end-date").val("{{ nowdate }}");
+
+ var nowTemp = new Date();
+ var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
+
+ var DP1 = $('#dp1').datepicker({
+ onRender: function(date) {
+ return date.valueOf() > now.valueOf() ? 'disabled' : '';
+ }
+ }).on('changeDate', function(ev) {
+ var newDate = new Date(ev.date);
+ startDate = newDate.getFullYear() + "-" + (newDate.getMonth() + 1) + "-" + newDate.getDate();
+ }).data('datepicker');
+
+ var DP2 = $('#dp2').datepicker({
+ onRender: function(date) {
+ return date.valueOf() > now.valueOf() ? 'disabled' : '';
+ }
+ }).on('changeDate', function(ev) {
+ var newDate = new Date(ev.date);
+ endDate = newDate.getFullYear() + "-" + (newDate.getMonth() + 1) + "-" + newDate.getDate();
+ }).data('datepicker');
+})();
+
+function fetchFileNames() {
+ var startTime = $("#start-time").val();
+ var endTime = $("#end-time").val();
+ var BASE_URl = window.location.origin + "/sbhs";
+ var request = $.ajax({
+ url : BASE_URl + "/admin/rangelogs",
+ method : "POST",
+ data : {
+ "start_date" : startDate,
+ "end_date" : endDate,
+ "start_time" : startTime,
+ "end_time" : endTime
+ }
+ });
+
+ request.done(function(data){
+ if (data.status_code == 200) {
+ htmlContent = "";
+ ctr=1;
+ for (item of data.message) {
+ htmlContent += "<tr>" + "<td>" + ctr + "</td>" + "<td>" + item.log.split("/")[0] +"</td>" + "<td><a href='" + BASE_URl + "/admin/explogs/" + item.id + "'>" + item.log.split("/")[1] +"</a></td>" + "</tr>";
+ ctr+=1;
+ }
+ $("#file-count").html(data.message.length + " results found");
+ $("#logs-table tbody").html(htmlContent);
+ }
+ else {
+ alert(data.message);
+ }
+ });
+}
+</script>
+{% endblock %} \ No newline at end of file