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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Web-based User Interface for appstats.
This is a simple set of webapp-based request handlers that display the
collected statistics and let you drill down on the information in
various ways.
Template files are in the templates/ subdirectory. Static files are
in the static/ subdirectory.
The templates are written to work with either Django 0.96 or Django
1.0, and most likely they also work with Django 1.1.
"""
import cgi
import email.Utils
import logging
import mimetypes
import os
import re
import sys
import time
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.appstats import recording
DEBUG = recording.config.DEBUG
from google.appengine.ext.webapp import template
import django
def render(tmplname, data):
"""Helper function to render a template."""
here = os.path.dirname(__file__)
tmpl = os.path.join(here, 'templates', tmplname)
data['env'] = os.environ
try:
return template.render(tmpl, data)
except Exception, err:
logging.exception('Failed to render %s', tmpl)
return 'Problematic template %s: %s' % (tmplname, err)
class SummaryHandler(webapp.RequestHandler):
"""Request handler for the main stats page (/stats/)."""
def get(self):
recording.dont_record()
if not self.request.path.endswith('stats/'):
self.redirect('stats/')
return
summaries = recording.load_summary_protos()
allstats = {}
pathstats = {}
pivot_path_rpc = {}
pivot_rpc_path = {}
for index, summary in enumerate(summaries):
path_key = recording.config.extract_key(summary)
if path_key not in pathstats:
pathstats[path_key] = [1, index+1]
else:
values = pathstats[path_key]
values[0] += 1
if len(values) >= 11:
if values[-1]:
values.append(0)
else:
values.append(index+1)
if path_key not in pivot_path_rpc:
pivot_path_rpc[path_key] = {}
for x in summary.rpc_stats_list():
rpc_key = x.service_call_name()
value = x.total_amount_of_calls()
if rpc_key in allstats:
allstats[rpc_key] += value
else:
allstats[rpc_key] = value
if rpc_key not in pivot_path_rpc[path_key]:
pivot_path_rpc[path_key][rpc_key] = 0
pivot_path_rpc[path_key][rpc_key] += value
if rpc_key not in pivot_rpc_path:
pivot_rpc_path[rpc_key] = {}
if path_key not in pivot_rpc_path[rpc_key]:
pivot_rpc_path[rpc_key][path_key] = 0
pivot_rpc_path[rpc_key][path_key] += value
allstats_by_count = []
for k, v in allstats.iteritems():
pivot = sorted(pivot_rpc_path[k].iteritems(),
key=lambda x: (-x[1], x[0]))
allstats_by_count.append((k, v, pivot))
allstats_by_count.sort(key=lambda x: (-x[1], x[0]))
pathstats_by_count = []
for path_key, values in pathstats.iteritems():
pivot = sorted(pivot_path_rpc[path_key].iteritems(),
key=lambda x: (-x[1], x[0]))
rpc_count = sum(x[1] for x in pivot)
pathstats_by_count.append((path_key, rpc_count,
values[0], values[1:], pivot))
pathstats_by_count.sort(key=lambda x: (-x[1], -x[2], x[0]))
data = {'requests': summaries,
'allstats_by_count': allstats_by_count,
'pathstats_by_count': pathstats_by_count,
}
self.response.out.write(render('main.html', data))
class DetailsHandler(webapp.RequestHandler):
"""Request handler for the details page (/stats/details)."""
def get(self):
recording.dont_record()
time_key = self.request.get('time')
timestamp = None
record = None
if time_key:
try:
timestamp = int(time_key) * 0.001
except Exception:
pass
if timestamp:
record = recording.load_full_proto(timestamp)
if record is None:
self.response.set_status(404)
self.response.out.write(render('details.html', {}))
return
rpcstats_map = {}
for rpc_stat in record.individual_stats_list():
key = rpc_stat.service_call_name()
count, real, api = rpcstats_map.get(key, (0, 0, 0))
count += 1
real += rpc_stat.duration_milliseconds()
api += rpc_stat.api_mcycles()
rpcstats_map[key] = (count, real, api)
rpcstats_by_count = [
(name, count, real, recording.mcycles_to_msecs(api))
for name, (count, real, api) in rpcstats_map.iteritems()]
rpcstats_by_count.sort(key=lambda x: -x[1])
real_total = 0
api_total_mcycles = 0
for i, rpc_stat in enumerate(record.individual_stats_list()):
real_total += rpc_stat.duration_milliseconds()
api_total_mcycles += rpc_stat.api_mcycles()
api_total = recording.mcycles_to_msecs(api_total_mcycles)
charged_total = recording.mcycles_to_msecs(record.processor_mcycles() +
api_total_mcycles)
data = {'sys': sys,
'record': record,
'rpcstats_by_count': rpcstats_by_count,
'real_total': real_total,
'api_total': api_total,
'charged_total': charged_total,
'file_url': './file',
}
self.response.out.write(render('details.html', data))
class FileHandler(webapp.RequestHandler):
"""Request handler for displaying any text file in the system.
NOTE: This gives any admin of your app full access to your source code.
"""
def get(self):
recording.dont_record()
lineno = self.request.get('n')
try:
lineno = int(lineno)
except:
lineno = 0
filename = self.request.get('f') or ''
orig_filename = filename
match = re.match('<path\[(\d+)\]>(.*)', filename)
if match:
index, tail = match.groups()
index = int(index)
if index < len(sys.path):
filename = sys.path[index] + tail
try:
fp = open(filename)
except IOError, err:
self.response.out.write('<h1>IOError</h1><pre>%s</pre>' %
cgi.escape(str(err)))
self.response.set_status(404)
else:
try:
data = {'fp': fp,
'filename': filename,
'orig_filename': orig_filename,
'lineno': lineno,
}
self.response.out.write(render('file.html', data))
finally:
fp.close()
class StaticHandler(webapp.RequestHandler):
"""Request handler to serve static files.
Only files directory in the static subdirectory are rendered this
way (no subdirectories).
"""
def get(self):
here = os.path.dirname(__file__)
fn = self.request.path
i = fn.rfind('/')
fn = fn[i+1:]
fn = os.path.join(here, 'static', fn)
ctype, encoding = mimetypes.guess_type(fn)
assert ctype and '/' in ctype, repr(ctype)
expiry = 3600
expiration = email.Utils.formatdate(time.time() + expiry, usegmt=True)
fp = open(fn, 'rb')
try:
self.response.out.write(fp.read())
finally:
fp.close()
self.response.headers['Content-type'] = ctype
self.response.headers['Cache-Control'] = 'public, max-age=expiry'
self.response.headers['Expires'] = expiration
if django.VERSION[:2] < (0, 97):
from django.template import defaultfilters
def safe(text, dummy=None):
return text
defaultfilters.register.filter("safe", safe)
URLMAP = [
('.*/details', DetailsHandler),
('.*/file', FileHandler),
('.*/static/.*', StaticHandler),
('.*', SummaryHandler),
]
def main():
"""Main program. Auth check, then create and run the WSGIApplication."""
if not os.getenv('SERVER_SOFTWARE', '').startswith('Dev'):
if not users.is_current_user_admin():
if users.get_current_user() is None:
print 'Status: 302'
print 'Location:', users.create_login_url(os.getenv('PATH_INFO', ''))
else:
print 'Status: 403'
print
print 'Forbidden'
return
app = webapp.WSGIApplication(URLMAP, debug=DEBUG)
util.run_bare_wsgi_app(app)
if __name__ == '__main__':
main()
|