3
from django.shortcuts import render_to_response
4
from django.http import HttpResponse, Http404
5
from django.conf import settings
8
from graphite.render.views import parseOptions
9
from graphite.render.evaluator import evaluateTarget
12
def graphlot_render(request):
13
"""Render the main graphlot view."""
15
for target in request.GET.getlist('target'):
16
metrics.append(dict(name=target, yaxis="one"))
17
for target in request.GET.getlist('y2target'):
18
metrics.append(dict(name=target, yaxis="two"))
20
untiltime = request.GET.get('until', "-0hour")
21
fromtime = request.GET.get('from', "-24hour")
22
context = dict(metric_list=metrics, fromtime=fromtime, untiltime=untiltime)
23
return render_to_response("graphlot.html", context)
25
def get_data(request):
26
"""Get the data for one series."""
27
(graphOptions, requestOptions) = parseOptions(request)
29
'startTime' : requestOptions['startTime'],
30
'endTime' : requestOptions['endTime'],
33
target = requestOptions['targets'][0]
34
seriesList = evaluateTarget(requestContext, target)
37
data=[ x for x in timeseries ],
38
start=timeseries.start,
41
) for timeseries in seriesList ]
44
return HttpResponse(simplejson.dumps(result), mimetype="application/json")
46
def find_metric(request):
47
"""Autocomplete helper on metric names."""
49
query = str( request.REQUEST['q'] )
51
return HttpResponseBadRequest(
52
content="Missing required parameter 'q'", mimetype="text/plain")
54
store = settings.LOCAL_STORE
56
matches = list( store.find(query+"*") )
57
content = "\n".join([node.metric_path for node in matches ])
58
response = HttpResponse(content, mimetype='text/plain')
63
"View for the header frame of the browser UI"
65
context['user'] = request.user
66
context['profile'] = getProfile(request)
67
context['documentation_url'] = settings.DOCUMENTATION_URL
68
return render_to_response("browserHeader.html", context)
72
"View for the top-level frame of the browser UI"
74
'queryString' : request.GET.urlencode(),
75
'target' : request.GET.get('target')
77
if context['queryString']:
78
context['queryString'] = context['queryString'].replace('#','%23')
80
context['target'] = context['target'].replace('#','%23') #js libs terminate a querystring on #
81
return render_to_response("browser.html", context)
85
query = request.POST['query']
87
return HttpResponse("")
89
patterns = query.split()
90
regexes = [re.compile(p,re.I) for p in patterns]
99
index_file = open(settings.INDEX_FILE)
100
for line in index_file:
102
results.append( line.strip() )
103
if len(results) >= 100:
107
result_string = ','.join(results)
108
return HttpResponse(result_string, mimetype='text/plain')
111
def myGraphLookup(request):
112
"View for My Graphs navigation"
113
profile = getProfile(request,allowDefault=False)
129
path = str( request.GET['path'] )
132
if path.endswith('.'):
133
userpath_prefix = path
136
userpath_prefix = path + '.'
141
matches = [ graph for graph in profile.mygraph_set.all().order_by('name') if graph.name.startswith(userpath_prefix) ]
143
log.info( "myGraphLookup: username=%s, path=%s, userpath_prefix=%s, %ld graph to process" % (profile.user.username, path, userpath_prefix, len(matches)) )
144
branch_inserted = set()
145
leaf_inserted = set()
147
for graph in matches: #Now let's add the matching graph
149
dotPos = graph.name.find( '.', len(userpath_prefix) )
153
name = graph.name[ len(userpath_prefix) : dotPos ]
154
if name in branch_inserted: continue
155
branch_inserted.add(name)
158
name = graph.name[ len(userpath_prefix): ]
159
if name in leaf_inserted: continue
160
leaf_inserted.add(name)
162
node = {'text' : str(name) }
165
node.update( { 'id' : str(userpath_prefix + name + '.') } )
166
node.update(branchNode)
169
node.update( { 'id' : str(userpath_prefix + name), 'graphUrl' : str(graph.url) } )
170
node.update(leafNode)
175
log.exception("browser.views.myGraphLookup(): could not complete request.")
178
no_graphs = { 'text' : "No saved graphs", 'id' : 'no-click' }
179
no_graphs.update(leafNode)
180
nodes.append(no_graphs)
182
return json_response(nodes)
184
def userGraphLookup(request):
185
"View for User Graphs navigation"
186
username = request.GET['path']
203
profiles = Profile.objects.exclude(user=defaultUser)
205
for profile in profiles:
206
if profile.mygraph_set.count():
208
'text' : str(profile.user.username),
209
'id' : str(profile.user.username)
212
node.update(branchNode)
216
profile = getProfileByUsername(username)
217
assert profile, "No profile for username '%s'" % username
219
for graph in profile.mygraph_set.all().order_by('name'):
221
'text' : str(graph.name),
222
'id' : str(graph.name),
223
'graphUrl' : str(graph.url)
225
node.update(leafNode)
229
log.exception("browser.views.userLookup(): could not complete request for %s" % username)
232
no_graphs = { 'text' : "No saved graphs", 'id' : 'no-click' }
233
no_graphs.update(leafNode)
234
nodes.append(no_graphs)
236
return json_response(nodes)
239
def json_response(nodes):
240
#json = str(nodes) #poor man's json encoder for simple types
241
json_data = json.dumps(nodes)
242
response = HttpResponse(json_data,mimetype="application/json")
243
response['Pragma'] = 'no-cache'
244
response['Cache-Control'] = 'no-cache'
248
def any(iterable): #python2.4 compatibility