2019-02-12 06:16:10 +01:00
|
|
|
import logging
|
2019-01-31 00:44:02 +01:00
|
|
|
import requests
|
|
|
|
import ujson
|
|
|
|
import urllib
|
2019-01-31 00:39:02 +01:00
|
|
|
from typing import Any, Dict, List, Optional, Tuple, Union
|
2019-01-31 00:44:02 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
2019-01-31 00:39:02 +01:00
|
|
|
from django.forms.models import model_to_dict
|
2019-01-31 00:44:02 +01:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2019-01-31 00:39:02 +01:00
|
|
|
from analytics.models import InstallationCount, RealmCount
|
2019-01-31 00:44:02 +01:00
|
|
|
from version import ZULIP_VERSION
|
|
|
|
from zerver.lib.exceptions import JsonableError
|
2019-01-31 00:39:02 +01:00
|
|
|
from zerver.lib.export import floatify_datetime_fields
|
2019-10-03 02:01:36 +02:00
|
|
|
from zerver.models import RealmAuditLog
|
2019-01-31 00:44:02 +01:00
|
|
|
|
|
|
|
class PushNotificationBouncerException(Exception):
|
|
|
|
pass
|
|
|
|
|
2019-12-02 19:46:11 +01:00
|
|
|
class PushNotificationBouncerRetryLaterError(JsonableError):
|
2019-12-03 20:19:38 +01:00
|
|
|
http_status_code = 502
|
2019-12-02 19:46:11 +01:00
|
|
|
|
2019-01-31 00:44:02 +01:00
|
|
|
def send_to_push_bouncer(method: str,
|
|
|
|
endpoint: str,
|
|
|
|
post_data: Union[str, Dict[str, Any]],
|
2019-12-03 20:19:38 +01:00
|
|
|
extra_headers: Optional[Dict[str, Any]]=None) -> Dict[str, Any]:
|
2019-01-31 00:44:02 +01:00
|
|
|
"""While it does actually send the notice, this function has a lot of
|
|
|
|
code and comments around error handling for the push notifications
|
|
|
|
bouncer. There are several classes of failures, each with its own
|
|
|
|
potential solution:
|
|
|
|
|
2019-12-03 20:19:38 +01:00
|
|
|
* Network errors with requests.request. We raise an exception to signal
|
|
|
|
it to the callers.
|
2019-01-31 00:44:02 +01:00
|
|
|
|
|
|
|
* 500 errors from the push bouncer or other unexpected responses;
|
|
|
|
we don't try to parse the response, but do make clear the cause.
|
|
|
|
|
|
|
|
* 400 errors from the push bouncer. Here there are 2 categories:
|
|
|
|
Our server failed to connect to the push bouncer (should throw)
|
|
|
|
vs. client-side errors like and invalid token.
|
|
|
|
|
|
|
|
"""
|
|
|
|
url = urllib.parse.urljoin(settings.PUSH_NOTIFICATION_BOUNCER_URL,
|
2019-01-31 01:36:18 +01:00
|
|
|
'/api/v1/remotes/' + endpoint)
|
2019-01-31 00:44:02 +01:00
|
|
|
api_auth = requests.auth.HTTPBasicAuth(settings.ZULIP_ORG_ID,
|
|
|
|
settings.ZULIP_ORG_KEY)
|
|
|
|
|
|
|
|
headers = {"User-agent": "ZulipServer/%s" % (ZULIP_VERSION,)}
|
|
|
|
if extra_headers is not None:
|
|
|
|
headers.update(extra_headers)
|
|
|
|
|
2019-12-03 20:19:38 +01:00
|
|
|
try:
|
|
|
|
res = requests.request(method,
|
|
|
|
url,
|
|
|
|
data=post_data,
|
|
|
|
auth=api_auth,
|
|
|
|
timeout=30,
|
|
|
|
verify=True,
|
|
|
|
headers=headers)
|
2019-12-03 20:33:47 +01:00
|
|
|
except (requests.exceptions.Timeout, requests.exceptions.SSLError,
|
|
|
|
requests.exceptions.ConnectionError) as e:
|
2019-12-03 20:19:38 +01:00
|
|
|
raise PushNotificationBouncerRetryLaterError(
|
2019-12-03 20:33:47 +01:00
|
|
|
"{} while trying to connect to push notification bouncer".format(e.__class__.__name__))
|
2019-01-31 00:44:02 +01:00
|
|
|
|
|
|
|
if res.status_code >= 500:
|
|
|
|
# 500s should be resolved by the people who run the push
|
2019-02-12 06:16:10 +01:00
|
|
|
# notification bouncer service, and they'll get an appropriate
|
2019-12-03 20:19:38 +01:00
|
|
|
# error notification from the server. We raise an exception to signal
|
|
|
|
# to the callers that the attempt failed and they can retry.
|
|
|
|
error_msg = "Received 500 from push notification bouncer"
|
|
|
|
logging.warning(error_msg)
|
|
|
|
raise PushNotificationBouncerRetryLaterError(error_msg)
|
2019-01-31 00:44:02 +01:00
|
|
|
elif res.status_code >= 400:
|
|
|
|
# If JSON parsing errors, just let that exception happen
|
|
|
|
result_dict = ujson.loads(res.content)
|
|
|
|
msg = result_dict['msg']
|
|
|
|
if 'code' in result_dict and result_dict['code'] == 'INVALID_ZULIP_SERVER':
|
|
|
|
# Invalid Zulip server credentials should email this server's admins
|
|
|
|
raise PushNotificationBouncerException(
|
|
|
|
_("Push notifications bouncer error: %s") % (msg,))
|
|
|
|
else:
|
|
|
|
# But most other errors coming from the push bouncer
|
|
|
|
# server are client errors (e.g. never-registered token)
|
|
|
|
# and should be handled as such.
|
|
|
|
raise JsonableError(msg)
|
|
|
|
elif res.status_code != 200:
|
|
|
|
# Anything else is unexpected and likely suggests a bug in
|
|
|
|
# this version of Zulip, so we throw an exception that will
|
|
|
|
# email the server admins.
|
|
|
|
raise PushNotificationBouncerException(
|
|
|
|
"Push notification bouncer returned unexpected status code %s" % (res.status_code,))
|
|
|
|
|
|
|
|
# If we don't throw an exception, it's a successful bounce!
|
2019-12-03 20:19:38 +01:00
|
|
|
return ujson.loads(res.content)
|
2019-01-31 00:44:02 +01:00
|
|
|
|
|
|
|
def send_json_to_push_bouncer(method: str, endpoint: str, post_data: Dict[str, Any]) -> None:
|
|
|
|
send_to_push_bouncer(
|
|
|
|
method,
|
|
|
|
endpoint,
|
|
|
|
ujson.dumps(post_data),
|
|
|
|
extra_headers={"Content-type": "application/json"},
|
|
|
|
)
|
2019-01-31 00:39:02 +01:00
|
|
|
|
2019-10-03 02:01:36 +02:00
|
|
|
REALMAUDITLOG_PUSHED_FIELDS = ['id', 'realm', 'event_time', 'backfilled', 'extra_data', 'event_type']
|
|
|
|
|
2019-01-31 00:39:02 +01:00
|
|
|
def build_analytics_data(realm_count_query: Any,
|
2019-10-03 02:01:36 +02:00
|
|
|
installation_count_query: Any,
|
|
|
|
realmauditlog_query: Any) -> Tuple[List[Dict[str, Any]],
|
|
|
|
List[Dict[str, Any]],
|
|
|
|
List[Dict[str, Any]]]:
|
2019-02-02 20:48:16 +01:00
|
|
|
# We limit the batch size on the client side to avoid OOM kills timeouts, etc.
|
|
|
|
MAX_CLIENT_BATCH_SIZE = 10000
|
2019-01-31 00:39:02 +01:00
|
|
|
data = {}
|
|
|
|
data['analytics_realmcount'] = [
|
2019-10-03 02:01:36 +02:00
|
|
|
model_to_dict(row) for row in
|
2019-02-02 20:48:16 +01:00
|
|
|
realm_count_query.order_by("id")[0:MAX_CLIENT_BATCH_SIZE]
|
2019-01-31 00:39:02 +01:00
|
|
|
]
|
|
|
|
data['analytics_installationcount'] = [
|
2019-10-03 02:01:36 +02:00
|
|
|
model_to_dict(row) for row in
|
2019-02-02 20:48:16 +01:00
|
|
|
installation_count_query.order_by("id")[0:MAX_CLIENT_BATCH_SIZE]
|
2019-01-31 00:39:02 +01:00
|
|
|
]
|
2019-10-03 02:01:36 +02:00
|
|
|
data['zerver_realmauditlog'] = [
|
|
|
|
model_to_dict(row, fields=REALMAUDITLOG_PUSHED_FIELDS) for row in
|
|
|
|
realmauditlog_query.order_by("id")[0:MAX_CLIENT_BATCH_SIZE]
|
|
|
|
]
|
2019-01-31 00:39:02 +01:00
|
|
|
|
|
|
|
floatify_datetime_fields(data, 'analytics_realmcount')
|
|
|
|
floatify_datetime_fields(data, 'analytics_installationcount')
|
2019-10-03 02:01:36 +02:00
|
|
|
floatify_datetime_fields(data, 'zerver_realmauditlog')
|
|
|
|
return (data['analytics_realmcount'], data['analytics_installationcount'],
|
|
|
|
data['zerver_realmauditlog'])
|
2019-01-31 00:39:02 +01:00
|
|
|
|
|
|
|
def send_analytics_to_remote_server() -> None:
|
|
|
|
# first, check what's latest
|
2019-12-03 20:19:38 +01:00
|
|
|
try:
|
|
|
|
result = send_to_push_bouncer("GET", "server/analytics/status", {})
|
|
|
|
except PushNotificationBouncerRetryLaterError as e:
|
|
|
|
logging.warning(e.msg)
|
2019-02-12 06:16:10 +01:00
|
|
|
return
|
|
|
|
|
2019-01-31 00:39:02 +01:00
|
|
|
last_acked_realm_count_id = result['last_realm_count_id']
|
|
|
|
last_acked_installation_count_id = result['last_installation_count_id']
|
2019-10-03 02:01:36 +02:00
|
|
|
last_acked_realmauditlog_id = result['last_realmauditlog_id']
|
2019-01-31 00:39:02 +01:00
|
|
|
|
2019-10-03 02:01:36 +02:00
|
|
|
(realm_count_data, installation_count_data, realmauditlog_data) = build_analytics_data(
|
2019-01-31 00:39:02 +01:00
|
|
|
realm_count_query=RealmCount.objects.filter(
|
|
|
|
id__gt=last_acked_realm_count_id),
|
|
|
|
installation_count_query=InstallationCount.objects.filter(
|
2019-10-03 02:01:36 +02:00
|
|
|
id__gt=last_acked_installation_count_id),
|
|
|
|
realmauditlog_query=RealmAuditLog.objects.filter(
|
|
|
|
event_type__in=RealmAuditLog.SYNCED_BILLING_EVENTS,
|
|
|
|
id__gt=last_acked_realmauditlog_id))
|
2019-01-31 00:39:02 +01:00
|
|
|
|
2019-10-03 02:01:36 +02:00
|
|
|
if len(realm_count_data) + len(installation_count_data) + len(realmauditlog_data) == 0:
|
2019-01-31 00:39:02 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
request = {
|
|
|
|
'realm_counts': ujson.dumps(realm_count_data),
|
|
|
|
'installation_counts': ujson.dumps(installation_count_data),
|
2019-10-03 02:01:36 +02:00
|
|
|
'realmauditlog_rows': ujson.dumps(realmauditlog_data),
|
2019-02-12 05:54:17 +01:00
|
|
|
'version': ujson.dumps(ZULIP_VERSION),
|
2019-01-31 00:39:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Gather only entries with an ID greater than last_realm_count_id
|
2019-09-03 03:47:10 +02:00
|
|
|
try:
|
|
|
|
send_to_push_bouncer("POST", "server/analytics", request)
|
|
|
|
except JsonableError as e:
|
|
|
|
logging.warning(e.msg)
|