From 1984d2fc0a237f522d2da1021e247acc5f9250da Mon Sep 17 00:00:00 2001
From: Brian Moe <brian.moe@ligo.org>
Date: Thu, 15 Apr 2010 13:05:28 -0500
Subject: [PATCH] Added histogram display to web page.

---
 .../management/commands/make_histograms.py    | 42 +++++++++----------
 gracedb/reports.py                            | 15 +++++++
 settings.py                                   |  7 ++++
 settings_dev.py                               |  6 +++
 static/css/admin-nav.css                      |  2 +
 static/css/style.css                          |  2 +
 templates/admin/base_site.html                |  9 +---
 templates/base.html                           |  1 +
 templates/gracedb/histogram.html              | 11 +++++
 urls.py                                       |  6 +++
 10 files changed, 73 insertions(+), 28 deletions(-)
 create mode 100644 gracedb/reports.py
 create mode 100644 templates/gracedb/histogram.html

diff --git a/gracedb/management/commands/make_histograms.py b/gracedb/management/commands/make_histograms.py
index d1413ff60..ccb3e193f 100644
--- a/gracedb/management/commands/make_histograms.py
+++ b/gracedb/management/commands/make_histograms.py
@@ -16,8 +16,11 @@ from datetime import datetime, timedelta
 from subprocess import Popen, PIPE, STDOUT
 
 
-DEST_DIR = "/var/www/html/histo/2"
-MAX_X = 1800
+DEST_DIR = settings.LATENCY_REPORT_DEST_DIR
+MAX_X = settings.LATENCY_MAXIMUM_CHARTED
+
+WEB_PAGE_FILE_PATH = settings.LATENCY_REPORT_WEB_PAGE_FILE_PATH
+
 
 
 class Command(NoArgsCommand):
@@ -31,6 +34,12 @@ class Command(NoArgsCommand):
         start_week = now - timedelta(7)
         start_month = now - timedelta(30)
 
+        past = timedelta(100)
+        now -= past
+        start_day -= past
+        start_week -= past
+        start_month -= past
+
         time_ranges =  [(start_day, "day"), (start_week, "week"), (start_month, "month")]
 
         annotations = {}
@@ -60,22 +69,17 @@ class Command(NoArgsCommand):
                     makePlot(data, atype, maxx=MAX_X).savefig(fname)
                 annotations[atype][time_range] = note
 
-        writeIndex(annotations, os.path.join(DEST_DIR, 'index.html'))
+        writeIndex(annotations, WEB_PAGE_FILE_PATH)
 
 
 def writeIndex(notes, fname):
-    template = """<html>
-<head>
-</head>
-<h1>Gracedb Event Reporting Latency</h1>
-Tables generated: %(time)s<br/>
-Maximum charted latency: %(maxx)s seconds
-<body>%(table)s
-</body>
-</html>
-    """
-
-    table = '<table border="1">'
+
+    createdDate = str(datetime.now())
+    maxx = MAX_X
+
+    table = '<table border="1" bgcolor="white">'
+    table += """<caption>Tables generated: %s<br/>
+                      Maximum charted latency: %s seconds</caption>""" % (createdDate, maxx)
     table += "<tr><th>&nbsp;</th>"
     for time_range in ['day', 'week', 'month']:
         table += "<th>last %s</th>" % time_range
@@ -84,7 +88,7 @@ Maximum charted latency: %(maxx)s seconds
         table += "<tr>"
         table += "<td>%s</td>" % atype_name
         for time_range in ['day', 'week', 'month']:
-            table += "<td>"
+            table += '<td align="center" bgcolor="white">'
             n = notes[atype][time_range]
             extra = ""
             if n['fname'] is not None:
@@ -99,12 +103,8 @@ Maximum charted latency: %(maxx)s seconds
         table += "</tr>"
     table += "</table>"
 
-    values = {}
-    values['table'] = table
-    values['time'] = str(datetime.now())
-    values['maxx'] = MAX_X # XXX ugh.
     f = open(fname, "w")
-    f.write(template % values)
+    f.write(table)
     f.close()
 
 def makePlot(data, title, maxx=1800, facecolor='green'):
diff --git a/gracedb/reports.py b/gracedb/reports.py
new file mode 100644
index 000000000..3bc74476b
--- /dev/null
+++ b/gracedb/reports.py
@@ -0,0 +1,15 @@
+
+from django.http import HttpResponse
+from django.template import RequestContext
+from django.shortcuts import render_to_response
+from django.conf import settings
+
+
+
+def histo(request):
+    table = open(settings.LATENCY_REPORT_WEB_PAGE_FILE_PATH, "r").read()
+    return render_to_response(
+            'gracedb/histogram.html',
+            {'table': table},
+            context_instance=RequestContext(request))
+
diff --git a/settings.py b/settings.py
index 976821b3b..937e8cc4d 100644
--- a/settings.py
+++ b/settings.py
@@ -43,6 +43,13 @@ DATABASE_PASSWORD = 'redrum4x'
 DATABASE_HOST = ''             # Set to empty string for localhost.
 DATABASE_PORT = ''             # Set to empty string for default.
 
+
+# Latency histograms.  Where they go and max latency to bin.
+LATENCY_REPORT_DEST_DIR = "/home/lars/django/data/latency"
+LATENCY_MAXIMUM_CHARTED = 1800
+LATENCY_REPORT_WEB_PAGE_FILE_PATH = LATENCY_REPORT_DEST_DIR + "/latency.inc"
+
+
 # Local time zone for this installation. Choices can be found here:
 # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
 # although not all choices may be available on all operating systems.
diff --git a/settings_dev.py b/settings_dev.py
index 7e4d3d59e..7e093557d 100644
--- a/settings_dev.py
+++ b/settings_dev.py
@@ -34,6 +34,12 @@ DATABASE_PASSWORD = 'redrum4x'
 DATABASE_HOST = ''             # Set to empty string for localhost.
 DATABASE_PORT = ''             # Set to empty string for default.
 
+# Latency histograms.  Where they go and max latency to bin.
+LATENCY_REPORT_DEST_DIR = "/home/lars/django/data/latency"
+LATENCY_MAXIMUM_CHARTED = 1800
+LATENCY_REPORT_WEB_PAGE_FILE_PATH = LATENCY_REPORT_DEST_DIR + "/latency.inc"
+
+
 # Local time zone for this installation. Choices can be found here:
 # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
 # although not all choices may be available on all operating systems.
diff --git a/static/css/admin-nav.css b/static/css/admin-nav.css
index d19d736a5..1a3a77cd4 100644
--- a/static/css/admin-nav.css
+++ b/static/css/admin-nav.css
@@ -56,6 +56,7 @@
 #home #nav-home a,
 #create #nav-create a,
 #search #nav-search a,
+#reports #nav-reports a,
 #feeds #nav-feeds a,
 #about #nav-about a,
 #archive #nav-archive a,
@@ -70,6 +71,7 @@
 #home #nav-home a:hover,
 #create #nav-create a,
 #search #nav-search a,
+#reports #nav-reports a,
 #feeds #nav-feeds a,
 #about #nav-about a:hover,
 #archive #nav-archive a:hover,
diff --git a/static/css/style.css b/static/css/style.css
index 03bdc0a66..508a87950 100644
--- a/static/css/style.css
+++ b/static/css/style.css
@@ -131,6 +131,7 @@ a.link, a, a.active {
 #create #nav-create a,
 #search #nav-search a,
 #doc #nav-doc a,
+#reports #nav-reports a,
 #feeds #nav-feeds a,
 #about #nav-about a,
 #archive #nav-archive a,
@@ -146,6 +147,7 @@ a.link, a, a.active {
 #create #nav-create a,
 #search #nav-search a,
 #doc #nav-doc a,
+#reports #nav-reports a,
 #feeds #nav-feeds a,
 #about #nav-about a:hover,
 #archive #nav-archive a:hover,
diff --git a/templates/admin/base_site.html b/templates/admin/base_site.html
index b4103d0fd..66d5f6d33 100644
--- a/templates/admin/base_site.html
+++ b/templates/admin/base_site.html
@@ -13,18 +13,13 @@
 {% endblock %}
 
 {% block nav-global %}
-<!--
-    <span id="nav-home"><a href="{% url home %}">Home</a></span>
-    <span id="nav-search"><a href="{% url search %}">Search</a></span>
-    <span id="nav-create"><a href="{% url create %}">Create</a></span>
-    <span id="nav-feeds"><a href="{% url feeds %}">RSS</a></span>
-    {% if ligouser %}<div id="nav-user">Authenticated as: {{ ligouser.name }}</div>{% endif %}
--->
 <ul id="nav">
     <li id="nav-home"><a href="{% url home %}">Home</a></li>
     <li id="nav-search"><a href="{% url search %}">Search</a></li>
     <li id="nav-create"><a href="{% url create %}">Create</a></li>
+    <li id="nav-reports"><a href="{% url reports %}">RSS</a></li>
     <li id="nav-feeds"><a href="{% url feeds %}">RSS</a></li>
+    <li id="nav-userprofile"><a href="{% url userprofile-home %}">Options</a></li>
     {% if ligouser %}<li id="nav-user">Authenticated as: {{ ligouser.name }}</li>{% endif %}
 <ul>
 {% endblock %}
diff --git a/templates/base.html b/templates/base.html
index f5b64acd4..ffa7d202a 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -33,6 +33,7 @@ function changeTime(obj, label) {
     <li id="nav-home"><a href="{% url home %}">Home</a></li>
     <li id="nav-search"><a href="{% url search %}">Search</a></li>
     <li id="nav-create"><a href="{% url create %}">Create</a></li>
+    <li id="nav-reports"><a href="{% url reports %}">Reports</a></li>
     <li id="nav-feeds"><a href="{% url feeds %}">RSS</a></li>
     <li id="nav-userprofile"><a href="{% url userprofile-home %}">Options</a></li>
     {% if ligouser %}<li id="nav-user">Authenticated as: {{ ligouser.name }}</li>{% endif %}
diff --git a/templates/gracedb/histogram.html b/templates/gracedb/histogram.html
new file mode 100644
index 000000000..5419b8f4f
--- /dev/null
+++ b/templates/gracedb/histogram.html
@@ -0,0 +1,11 @@
+{% extends "base.html" %}
+
+{% block title %}Reporting Latency{% endblock %}
+{% block heading %}Reporting Latency{% endblock %}
+{% block pageid %}reports{% endblock %}
+
+{% block content %}
+
+{{ table|safe }}
+
+{% endblock %}
diff --git a/urls.py b/urls.py
index 4a8e866b7..fea04f409 100644
--- a/urls.py
+++ b/urls.py
@@ -1,4 +1,6 @@
+
 from django.conf.urls.defaults import *
+from django.conf import settings
 
 # Uncomment the next two lines to enable the admin:
 from django.contrib import admin
@@ -27,6 +29,10 @@ urlpatterns = patterns('',
         {'feed_dict': feeds}),
     url (r'^feeds/$', feedview, name="feeds"),
 
+    url (r'^reports/$', 'gracedb.gracedb.reports.histo', name="reports"),
+    (r'^reports/(?P<path>.+)$', 'django.views.static.serve',
+            {'document_root': settings.LATENCY_REPORT_DEST_DIR}),
+
     # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
     # to INSTALLED_APPS to enable admin documentation:
     # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
-- 
GitLab