1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
import json
from graphite.util import jsonResponse, HttpResponse, HttpError
from graphite.functions import SeriesFunctions, SeriesFunction, PieFunctions, PieFunction, functionInfo
class jsonInfinityEncoder(json.JSONEncoder):
def encode(self, o):
return super(jsonInfinityEncoder, self).encode(o).replace('Infinity,', '1e9999,').replace('Infinity}', '1e9999}')
def default(self, o):
if hasattr(o, 'toJSON'):
return o.toJSON()
return o.__dict__
@jsonResponse(encoder=jsonInfinityEncoder)
def functionList(request, queryParams):
if request.method != 'GET':
return HttpResponse(status=405)
if queryParams.get('type') == 'pie':
funcs = PieFunctions()
else:
funcs = SeriesFunctions()
grouped = queryParams.get('grouped', '').lower() in ['1', 'true']
group = queryParams.get('group')
result = {}
for (name, func) in funcs.items():
info = functionInfo(name, func)
if group is not None and group != info['group']:
continue
if grouped:
if info['group'] not in result:
result[info['group']] = {}
result[info['group']][name] = info
else:
result[name] = info
return result
@jsonResponse(encoder=jsonInfinityEncoder)
def functionDetails(request, queryParams, name):
if request.method != 'GET':
return HttpResponse(status=405)
try:
if queryParams.get('type') == 'pie':
func = PieFunction(name)
else:
func = SeriesFunction(name)
except KeyError:
raise HttpError('Function not found: %s' % name, status=404)
return functionInfo(name, func)
|