From d22b8adaf6764736026be31023bc174d01ed8d93 Mon Sep 17 00:00:00 2001
From: Bach Dániel <bd@ik.bme.hu>
Date: Tue, 11 Feb 2014 02:49:08 +0100
Subject: [PATCH] dashboard: fix graphite errors

fixes #58
fixes #56
---
 circle/dashboard/urls.py  |  2 +-
 circle/dashboard/views.py | 31 ++++++++++++++++---------------
 2 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/circle/dashboard/urls.py b/circle/dashboard/urls.py
index 5c3a67b..35083c1 100644
--- a/circle/dashboard/urls.py
+++ b/circle/dashboard/urls.py
@@ -66,7 +66,7 @@ urlpatterns = patterns(
     url(r'^group/list/$', GroupList.as_view(),
         name='dashboard.views.group-list'),
     url((r'^vm/(?P<pk>\d+)/graph/(?P<metric>cpu|memory|network)/'
-         r'(?P<time>[0-9]+[hdwy])$'),
+         r'(?P<time>[0-9]{1,2}[hdwy])$'),
         VmGraphView.as_view(),
         name='dashboard.views.vm-graph'),
 )
diff --git a/circle/dashboard/views.py b/circle/dashboard/views.py
index 2a24920..a75cb9a 100644
--- a/circle/dashboard/views.py
+++ b/circle/dashboard/views.py
@@ -3,6 +3,7 @@ import json
 import logging
 import re
 from datetime import datetime
+import requests
 
 from django.contrib.auth.models import User, Group
 from django.contrib.messages import warning
@@ -1345,8 +1346,12 @@ class TransferOwnershipConfirmView(LoginRequiredMixin, View):
 
 class VmGraphView(LoginRequiredMixin, View):
     def get(self, request, pk, metric, time, *args, **kwargs):
-        from urllib import urlencode
-        import urllib2
+        graphite_host = getenv("GRAPHITE_HOST", None)
+        graphite_port = getenv("GRAPHITE_PORT", None)
+
+        if (graphite_host in ['', None] or graphite_port in ['', None]):
+            logger.debug('GRAPHITE_HOST is empty.')
+            raise Http404()
 
         if metric not in ['cpu', 'memory', 'network']:
             raise SuspiciousOperation()
@@ -1371,17 +1376,13 @@ class VmGraphView(LoginRequiredMixin, View):
 
         prefix = 'vm.%s' % instance.vm_name
         target = targets[metric] % prefix
-
         title = '%s (%s) - %s' % (instance.name, instance.vm_name, metric)
-
-        params = urlencode({'target': target,
-                            'from': '-%s' % time,
-                            'title': title.encode('UTF-8')})
-        url = ('http://%s:%s/render/?width=500&height=200&%s'
-               % (getenv("GRAPHITE_HOST"),
-                  getenv("GRAPHITE_PORT"),
-                  params))
-        print url
-        response = urllib2.urlopen(urllib2.Request(url))
-        return HttpResponse(response.read(), mimetype="image/png")
-        print pk, metric
+        params = {'target': target,
+                  'from': '-%s' % time,
+                  'title': title.encode('UTF-8'),
+                  'width': '500',
+                  'height': '200'}
+        url = ('http://%s:%s/render/?%s' % (graphite_host, graphite_port,
+                                            params))
+        response = requests.post(url, data=params)
+        return HttpResponse(response.content, mimetype="image/png")
--
libgit2 0.26.0