← Back to team overview

graphite-dev team mailing list archive

[Question #151814]: Render view for json

 

New question #151814 on Graphite:
https://answers.launchpad.net/graphite/+question/151814

The following patch adds support for JSON output to the render view. It was developed and tested against 0.9.8. My apologies if the Python is atrocious, I'm primarily a Perl hacker.

--- views.py        2011-04-05 22:36:37.000000000 +0000
+++ views.py    2011-04-06 03:27:26.000000000 +0000
@@ -12,6 +12,10 @@
 See the License for the specific language governing permissions and
 limitations under the License."""
 import csv
+try:
+  import json
+except ImportError:
+  import simplejson as json
 from time import time, strftime, localtime
 from datetime import datetime, timedelta
 from random import shuffle
@@ -125,6 +129,26 @@
 
       return response
 
+    if requestOptions.get('format') == 'json':
+      json_data = []
+      for series in data:
+        a = {}
+        a['target'] = series.name
+        metrics = []
+        for i, value in enumerate(series):
+          b = {}
+          b['timestamp'] = series.start + (i * series.step)
+          b['value'] = value
+          metrics.append( b )
+        a['metrics'] = metrics
+        json_data.append( a )
+      json_serialized = json.dumps(json_data)
+      response = HttpResponse(json_serialized,mimetype='application/json')
+      response['Pragma'] = 'no-cache'
+      response['Cache-Control'] = 'no-cache'
+
+      return response
+
     if 'rawData' in requestOptions:
       response = HttpResponse(mimetype='text/plain')
       for series in data:

You received this question notification because you are a member of
graphite-dev, which is an answer contact for Graphite.