Browse Source

Ajout de l'url pour les graphes VPN

Mathieu 10 years ago
parent
commit
c90803f1f4
2 changed files with 23 additions and 1 deletions
  1. 3 1
      coin/vpn/urls.py
  2. 20 0
      coin/vpn/views.py

+ 3 - 1
coin/vpn/urls.py

@@ -1,5 +1,5 @@
 from django.conf.urls import patterns, url
-from coin.vpn.views import VPNView, VPNGeneratePasswordView
+from coin.vpn.views import VPNView, VPNGeneratePasswordView, get_graph
 
 urlpatterns = patterns(
     '',
@@ -7,4 +7,6 @@ urlpatterns = patterns(
     # the same as the "backend_name" of the model).
     url(r'^([0-9]+)$', VPNView.as_view(template_name="vpn/vpn.html"), name="openvpn_ldap"),
     url(r'^password/([0-9]+)$', VPNGeneratePasswordView.as_view(template_name="vpn/password.html"), name="generate_password"),
+    url(r'^graph/(?P<vpn_id>.+)/(?P<period>[a-z]+)$', get_graph, name="get_graph"),
+    url(r'^graph/(?P<vpn_id>.+)$', get_graph, name="get_graph"),
 )

+ 20 - 0
coin/vpn/views.py

@@ -4,6 +4,8 @@ from django.views.generic.detail import DetailView
 
 from coin.vpn.models import VPNSubscription
 
+from urllib2 import urlopen
+
 
 class VPNView(DetailView):
 
@@ -27,3 +29,21 @@ class VPNGeneratePasswordView(VPNView):
         self.object.save()
         context['password'] = password
         return context
+
+
+def get_graph(request, vpn_id, period="daily"):
+    """ This get the graph for the associated vpn_id and time period
+    """
+    vpn = get_object_or_404(VPNSubscription, pk=vpn_id,
+                            administrative_subscription__member__user=request.user)
+    
+    time_periods = { 'daily': '-24hours', 'weekly': '-8days', 'monthly': '-32days', 'yearly': '-13months', }
+    if period not in time_periods:
+        period = 'daily'
+
+    graph_url = "http://graphite-dev.illyse.org/render/?width=586&height=308&from=%(period)s&" \
+                "target=alias%28scaleToSeconds%28vpn1.%(login)s.downrxbytes%2C1%29%2C%20%22Download%22%29&" \
+                "target=alias%28scaleToSeconds%28vpn1.%(login)s.uptxbytes%2C1%29%2C%20%22Upload%22%29&" \
+                "title=VPN%20Usage%20%(login)s" % \
+                    { 'period': time_periods[period], 'login': vpn.login }
+    return HttpResponse(urlopen(graph_url), mimetype="image/png")