Move reporting views to their own file.

This commit is contained in:
Tim Abbott
2015-11-23 08:29:37 -08:00
parent 8526d02370
commit e64a3d0fae
3 changed files with 110 additions and 96 deletions

View File

@@ -43,8 +43,7 @@ from django.views.decorators.csrf import csrf_exempt
from django_auth_ldap.backend import LDAPBackend, _LDAPUser
from zerver.lib import bugdown
from zerver.lib.alert_words import user_alert_words
from zerver.lib.validator import check_string, check_list, check_dict, \
check_bool
from zerver.lib.validator import check_string, check_list, check_bool
from zerver.decorator import require_post, authenticated_json_post_view, \
has_request_variables, authenticated_json_view, to_non_negative_int, \
JsonableError, get_user_profile_by_email, REQ, require_realm_admin
@@ -52,9 +51,7 @@ from zerver.lib.avatar import avatar_url, get_avatar_url
from zerver.lib.upload import upload_message_image_through_web_client, upload_avatar_image, \
get_signed_upload_url, get_realm_for_filename
from zerver.lib.response import json_success, json_error
from zerver.lib.unminify import SourceMap
from zerver.lib.queue import queue_json_publish
from zerver.lib.utils import statsd, generate_random_token, statsd_key
from zerver.lib.utils import statsd, generate_random_token
from zproject.backends import password_auth_enabled, dev_auth_enabled
from confirmation.models import Confirmation
@@ -71,7 +68,6 @@ import urllib
import base64
import time
import logging
import os
import jwt
import hashlib
import hmac
@@ -1210,92 +1206,6 @@ def json_update_active_status(request, user_profile):
def json_get_active_statuses(request, user_profile):
return json_success(get_status_list(user_profile))
# Read the source map information for decoding JavaScript backtraces
js_source_map = None
if not (settings.DEBUG or settings.TEST_SUITE):
js_source_map = SourceMap(os.path.join(
settings.DEPLOY_ROOT, 'prod-static/source-map'))
@authenticated_json_post_view
@has_request_variables
def json_report_send_time(request, user_profile,
time=REQ(converter=to_non_negative_int),
received=REQ(converter=to_non_negative_int, default="(unknown)"),
displayed=REQ(converter=to_non_negative_int, default="(unknown)"),
locally_echoed=REQ(validator=check_bool, default=False),
rendered_content_disparity=REQ(validator=check_bool, default=False)):
request._log_data["extra"] = "[%sms/%sms/%sms/echo:%s/diff:%s]" \
% (time, received, displayed, locally_echoed, rendered_content_disparity)
statsd.timing("endtoend.send_time.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), time)
if received != "(unknown)":
statsd.timing("endtoend.receive_time.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), received)
if displayed != "(unknown)":
statsd.timing("endtoend.displayed_time.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), displayed)
if locally_echoed:
statsd.incr('locally_echoed')
if rendered_content_disparity:
statsd.incr('render_disparity')
return json_success()
@authenticated_json_post_view
@has_request_variables
def json_report_narrow_time(request, user_profile,
initial_core=REQ(converter=to_non_negative_int),
initial_free=REQ(converter=to_non_negative_int),
network=REQ(converter=to_non_negative_int)):
request._log_data["extra"] = "[%sms/%sms/%sms]" % (initial_core, initial_free, network)
statsd.timing("narrow.initial_core.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), initial_core)
statsd.timing("narrow.initial_free.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), initial_free)
statsd.timing("narrow.network.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), network)
return json_success()
@authenticated_json_post_view
@has_request_variables
def json_report_unnarrow_time(request, user_profile,
initial_core=REQ(converter=to_non_negative_int),
initial_free=REQ(converter=to_non_negative_int)):
request._log_data["extra"] = "[%sms/%sms]" % (initial_core, initial_free)
statsd.timing("unnarrow.initial_core.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), initial_core)
statsd.timing("unnarrow.initial_free.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), initial_free)
return json_success()
@authenticated_json_post_view
@has_request_variables
def json_report_error(request, user_profile, message=REQ, stacktrace=REQ,
ui_message=REQ(validator=check_bool), user_agent=REQ,
href=REQ, log=REQ,
more_info=REQ(validator=check_dict([]), default=None)):
if not settings.ERROR_REPORTING:
return json_success()
if js_source_map:
stacktrace = js_source_map.annotate_stacktrace(stacktrace)
try:
version = subprocess.check_output(["git", "log", "HEAD^..HEAD", "--oneline"])
except Exception:
version = None
queue_json_publish('error_reports', dict(
type = "browser",
report = dict(
user_email = user_profile.email,
user_full_name = user_profile.full_name,
user_visible = ui_message,
server_path = settings.DEPLOY_ROOT,
version = version,
user_agent = user_agent,
href = href,
message = message,
stacktrace = stacktrace,
log = log,
more_info = more_info,
)
), lambda x: None)
return json_success()
@authenticated_json_post_view
def json_events_register(request, user_profile):
return events_register_backend(request, user_profile)

104
zerver/views/report.py Normal file
View File

@@ -0,0 +1,104 @@
from __future__ import absolute_import
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
from zerver.decorator import authenticated_json_post_view, has_request_variables, REQ, \
to_non_negative_int
from zerver.lib.response import json_success
from zerver.lib.queue import queue_json_publish
from zerver.lib.unminify import SourceMap
from zerver.lib.utils import statsd, statsd_key
from zerver.lib.validator import check_bool, check_dict
import subprocess
import os
from zerver.lib.rest import rest_dispatch as _rest_dispatch
rest_dispatch = csrf_exempt((lambda request, *args, **kwargs: _rest_dispatch(request, globals(), *args, **kwargs)))
# Read the source map information for decoding JavaScript backtraces
js_source_map = None
if not (settings.DEBUG or settings.TEST_SUITE):
js_source_map = SourceMap(os.path.join(
settings.DEPLOY_ROOT, 'prod-static/source-map'))
@authenticated_json_post_view
@has_request_variables
def json_report_send_time(request, user_profile,
time=REQ(converter=to_non_negative_int),
received=REQ(converter=to_non_negative_int, default="(unknown)"),
displayed=REQ(converter=to_non_negative_int, default="(unknown)"),
locally_echoed=REQ(validator=check_bool, default=False),
rendered_content_disparity=REQ(validator=check_bool, default=False)):
request._log_data["extra"] = "[%sms/%sms/%sms/echo:%s/diff:%s]" \
% (time, received, displayed, locally_echoed, rendered_content_disparity)
statsd.timing("endtoend.send_time.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), time)
if received != "(unknown)":
statsd.timing("endtoend.receive_time.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), received)
if displayed != "(unknown)":
statsd.timing("endtoend.displayed_time.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), displayed)
if locally_echoed:
statsd.incr('locally_echoed')
if rendered_content_disparity:
statsd.incr('render_disparity')
return json_success()
@authenticated_json_post_view
@has_request_variables
def json_report_narrow_time(request, user_profile,
initial_core=REQ(converter=to_non_negative_int),
initial_free=REQ(converter=to_non_negative_int),
network=REQ(converter=to_non_negative_int)):
request._log_data["extra"] = "[%sms/%sms/%sms]" % (initial_core, initial_free, network)
statsd.timing("narrow.initial_core.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), initial_core)
statsd.timing("narrow.initial_free.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), initial_free)
statsd.timing("narrow.network.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), network)
return json_success()
@authenticated_json_post_view
@has_request_variables
def json_report_unnarrow_time(request, user_profile,
initial_core=REQ(converter=to_non_negative_int),
initial_free=REQ(converter=to_non_negative_int)):
request._log_data["extra"] = "[%sms/%sms]" % (initial_core, initial_free)
statsd.timing("unnarrow.initial_core.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), initial_core)
statsd.timing("unnarrow.initial_free.%s" % (statsd_key(user_profile.realm.domain, clean_periods=True),), initial_free)
return json_success()
@authenticated_json_post_view
@has_request_variables
def json_report_error(request, user_profile, message=REQ, stacktrace=REQ,
ui_message=REQ(validator=check_bool), user_agent=REQ,
href=REQ, log=REQ,
more_info=REQ(validator=check_dict([]), default=None)):
if not settings.ERROR_REPORTING:
return json_success()
if js_source_map:
stacktrace = js_source_map.annotate_stacktrace(stacktrace)
try:
version = subprocess.check_output(["git", "log", "HEAD^..HEAD", "--oneline"])
except Exception:
version = None
queue_json_publish('error_reports', dict(
type = "browser",
report = dict(
user_email = user_profile.email,
user_full_name = user_profile.full_name,
user_visible = ui_message,
server_path = settings.DEPLOY_ROOT,
version = version,
user_agent = user_agent,
href = href,
message = message,
stacktrace = stacktrace,
log = log,
more_info = more_info,
)
), lambda x: None)
return json_success()

View File

@@ -128,10 +128,10 @@ urlpatterns += patterns('zerver.views',
url(r'^json/tutorial_status$', 'json_tutorial_status'),
url(r'^json/change_enter_sends$', 'user_settings.json_change_enter_sends'),
url(r'^json/get_profile$', 'json_get_profile'),
url(r'^json/report_error$', 'json_report_error'),
url(r'^json/report_send_time$', 'json_report_send_time'),
url(r'^json/report_narrow_time$', 'json_report_narrow_time'),
url(r'^json/report_unnarrow_time$', 'json_report_unnarrow_time'),
url(r'^json/report_error$', 'report.json_report_error'),
url(r'^json/report_send_time$', 'report.json_report_send_time'),
url(r'^json/report_narrow_time$', 'report.json_report_narrow_time'),
url(r'^json/report_unnarrow_time$', 'report.json_report_unnarrow_time'),
url(r'^json/update_message_flags$', 'messages.json_update_flags'),
url(r'^json/register$', 'json_events_register'),
url(r'^json/upload_file$', 'json_upload_file'),