From 99d4bc114b5e10f9f49dbbfbbdfdab064dbf97d1 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 13 Mar 2013 15:02:57 -0400 Subject: [PATCH] Move Zephyr mirroring liveness check to the UserPresence queries. It's closer to a presence query than an update, and more importantly this moves this out of Tornado -- previously Tornado was spending at least 3ms per recipient on messages sent to the MIT realm fetching all this data to return back to users. This should save around 100ms per message sent to a popular stream the MIT realm -- but more importantly, each such event is 100ms during which Tornado is not processing other messages. (imported from commit 134169f0fdcd9f6640fda957edc4a28b07783d8e) --- zephyr/static/js/activity.js | 7 +++++++ zephyr/static/js/zephyr.js | 6 ------ zephyr/tornadoviews.py | 10 ---------- zephyr/views.py | 20 ++++++++++++++++---- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/zephyr/static/js/activity.js b/zephyr/static/js/activity.js index 8bb5f1a4f4..99708c6bbf 100644 --- a/zephyr/static/js/activity.js +++ b/zephyr/static/js/activity.js @@ -75,6 +75,13 @@ function focus_ping() { var user_info = {}; var users = []; + // Update Zephyr mirror activity warning + if (data.zephyr_mirror_active === false) { + $('#zephyr-mirror-error').show(); + } else { + $('#zephyr-mirror-error').hide(); + } + // Ping returns the active peer list $.each(data.presences, function (this_email, presence) { var age = -1; diff --git a/zephyr/static/js/zephyr.js b/zephyr/static/js/zephyr.js index d439666b9b..65a87b93ee 100644 --- a/zephyr/static/js/zephyr.js +++ b/zephyr/static/js/zephyr.js @@ -594,12 +594,6 @@ function get_updates(options) { compose.update_faded_messages(); } - if (data.zephyr_mirror_active === false) { - $('#zephyr-mirror-error').show(); - } else { - $('#zephyr-mirror-error').hide(); - } - if (data.new_pointer !== undefined && data.new_pointer > furthest_read) { diff --git a/zephyr/tornadoviews.py b/zephyr/tornadoviews.py index 41e618d447..9911fc6fbb 100644 --- a/zephyr/tornadoviews.py +++ b/zephyr/tornadoviews.py @@ -289,16 +289,6 @@ def format_updates_response(messages=[], apply_markdown=True, ret['server_generation'] = SERVER_GENERATION if new_pointer is not None: ret['new_pointer'] = new_pointer - if user_profile.realm.domain == "mit.edu": - try: - activity = UserActivity.objects.get(user_profile = user_profile, - query="/api/v1/get_messages", - client__name="zephyr_mirror") - ret['zephyr_mirror_active'] = \ - (activity.last_visit.replace(tzinfo=None) > - datetime.datetime.utcnow() - datetime.timedelta(minutes=5)) - except UserActivity.DoesNotExist: - ret['zephyr_mirror_active'] = False return ret diff --git a/zephyr/views.py b/zephyr/views.py index a83ecf5316..eaf6b8e1eb 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -1400,7 +1400,7 @@ def get_status_list(requesting_user_profile): # Return no status info for MIT if requesting_user_profile.realm.domain == 'mit.edu': - return json_success({'presences': user_statuses}) + return {'presences': user_statuses} for presence in UserPresence.objects.filter( user_profile__realm=requesting_user_profile.realm).select_related( @@ -1409,7 +1409,7 @@ def get_status_list(requesting_user_profile): user_statuses[presence.user_profile.user.email][presence.client.name] = \ presence_to_dict(presence) - return json_success({'presences': user_statuses}) + return {'presences': user_statuses} @authenticated_json_post_view @has_request_variables @@ -1424,11 +1424,23 @@ def json_update_active_status(request, user_profile, update_user_presence(user_profile, request._client, now(), status_val) - return get_status_list(user_profile) + ret = get_status_list(user_profile) + if user_profile.realm.domain == "mit.edu": + try: + activity = UserActivity.objects.get(user_profile = user_profile, + query="/api/v1/get_messages", + client__name="zephyr_mirror") + ret['zephyr_mirror_active'] = \ + (activity.last_visit.replace(tzinfo=None) > + datetime.datetime.utcnow() - datetime.timedelta(minutes=5)) + except UserActivity.DoesNotExist: + ret['zephyr_mirror_active'] = False + + return json_success(ret) @authenticated_json_post_view def json_get_active_statuses(request, user_profile): - return get_status_list(user_profile) + return json_success(get_status_list(user_profile)) @authenticated_json_post_view @has_request_variables