{"action":"created","comment":{"url":"https://api.github.com/repos/shoopio/shoop/pulls/comments/33542651","id":33542651,"diff_hunk":"@@ -0,0 +1,196 @@\n+# -*- coding: utf-8 -*-\n+# This file is part of Shoop.\n+#\n+# Copyright (c) 2012-2015, Shoop Ltd. All rights reserved.\n+#\n+# This source code is licensed under the AGPLv3 license found in the\n+# LICENSE file in the root directory of this source tree.\n+from django.conf import settings\n+from django.core.exceptions import ObjectDoesNotExist\n+from django.core.serializers.json import DjangoJSONEncoder\n+\n+from django.utils.crypto import get_random_string\n+from django.utils.encoding import force_text\n+from django.utils.timezone import now\n+from shoop.core.models.persistent_cache import PersistentCacheEntry\n+import json\n+import platform\n+import requests\n+import shoop\n+import sys\n+\n+OPT_OUT_KWARGS = dict(module=\"telemetry\", key=\"opt_out\")\n+INSTALLATION_KEY_KWARGS = dict(module=\"telemetry\", key=\"installation_key\")\n+LAST_DATA_KWARGS = dict(module=\"telemetry\", key=\"last_data\")\n+\n+\n+def safe_json(data_dict, indent=None):\n+    return json.dumps(data_dict, cls=DjangoJSONEncoder, sort_keys=True, indent=indent)\n+\n+\n+def get_installation_key():\n+    \"\"\"\n+    Get the unique installation ID for this Shoop instance.\n+\n+    If one doesn't exist, it's generated and saved at this point.\n+\n+    :return: Installation key string\n+    :rtype: str\n+    \"\"\"\n+    try:\n+        return PersistentCacheEntry.objects.get(**INSTALLATION_KEY_KWARGS).data\n+    except ObjectDoesNotExist:\n+        key = get_random_string(48)\n+        PersistentCacheEntry.objects.create(data=key, **INSTALLATION_KEY_KWARGS)\n+        return key\n+\n+\n+def is_opt_out():\n+    return PersistentCacheEntry.objects.filter(**OPT_OUT_KWARGS).exists()\n+\n+\n+def is_in_grace_period():\n+    \"\"\"\n+    Return True if the telemetry module is within the 24-hours-from-installation\n+    grace period where no stats are sent.  This is to \"safely\" allow opting out\n+    of telemetry without leaving a trace.\n+\n+    :return: Graceness flag.\n+    :rtype: bool\n+    \"\"\"\n+    get_installation_key()  # Need to initialize here\n+    installation_time = PersistentCacheEntry.objects.get(**INSTALLATION_KEY_KWARGS).time\n+    return ((now() - installation_time).total_seconds() < 60 * 60 * 24)\n+\n+\n+def is_telemetry_enabled():\n+    return bool(settings.SHOOP_TELEMETRY_ENABLED)\n+\n+\n+def set_opt_out(flag):\n+    \"\"\"\n+    Set whether this installation is opted-out from telemetry submissions.\n+\n+    :param flag: Opt-out flag. True for opt-out, false for opt-in (default)\n+    :type flag: bool\n+    :return: New flag state\n+    :rtype: bool\n+    \"\"\"\n+    if flag and not is_opt_out():\n+        PersistentCacheEntry.objects.create(data=True, **OPT_OUT_KWARGS)\n+        return True\n+    else:\n+        PersistentCacheEntry.objects.filter(**OPT_OUT_KWARGS).delete()\n+        return False\n+\n+\n+def get_last_submission_time():\n+    try:\n+        return PersistentCacheEntry.objects.get(**LAST_DATA_KWARGS).time\n+    except ObjectDoesNotExist:\n+        return None\n+\n+\n+def get_last_submission_data():\n+    try:\n+        return safe_json(PersistentCacheEntry.objects.get(**LAST_DATA_KWARGS).data, indent=4)\n+    except ObjectDoesNotExist:\n+        return None\n+\n+\n+def save_telemetry_submission(data):\n+    \"\"\"\n+    Save a blob of data as the latest telemetry submission.\n+\n+    Naturally updates the latest submission time.\n+\n+    :param data: A blob of data.\n+    :type data: dict\n+    \"\"\"\n+    pce, _ = PersistentCacheEntry.objects.get_or_create(defaults={\"data\": None}, **LAST_DATA_KWARGS)\n+    pce.data = data\n+    pce.save()\n+\n+\n+def get_telemetry_data(request, indent=None):\n+    \"\"\"\n+    Get the telemetry data that would be sent.\n+\n+    :param request: HTTP request. Optional.\n+    :type request: django.http.HttpRequest|None\n+    :return: Data blob.\n+    :rtype: str\n+    \"\"\"\n+    data_dict = {\n+        \"apps\": settings.INSTALLED_APPS,\n+        \"host\": (request.get_host() if request else None),\n+        \"key\": get_installation_key(),\n+        \"machine\": platform.machine(),\n+        \"platform\": platform.platform(),\n+        \"python_version\": sys.version,\n+        \"shoop_version\": shoop.__version__,\n+    }\n+    return safe_json(data_dict, indent)\n+\n+\n+class TelemetryNotSent(Exception):\n+    def __init__(self, message, code):\n+        self.message = message\n+        self.code = code\n+        super(TelemetryNotSent, self).__init__(message, code)\n+\n+\n+def _send_telemetry(request, max_age_hours):\n+    if not is_telemetry_enabled():\n+        raise TelemetryNotSent(\"Telemetry not enabled\", \"disabled\")\n+\n+    if is_opt_out():\n+        raise TelemetryNotSent(\"Telemetry is opted-out\", \"optout\")\n+\n+    if is_in_grace_period():\n+        raise TelemetryNotSent(\"Telemetry in grace period\", \"grace\")\n+\n+    if max_age_hours is not None:\n+        last_send_time = get_last_submission_time()\n+        if last_send_time:\n+            if (now() - last_send_time).total_seconds() <= max_age_hours * 60 * 60:\n+                raise TelemetryNotSent(\"Trying to resend too soon\", \"age\")\n+\n+    data = get_telemetry_data(request)\n+    try:\n+        resp = requests.post(url=settings.SHOOP_TELEMETRY_URL, data=data, timeout=5)\n+    except Exception as exc:\n+        data = {\n+            \"data\": data,\n+            \"error\": force_text(exc),\n+        }\n+    else:\n+        data = {\n+            \"data\": data,\n+            \"response\": force_text(resp.content, errors=\"replace\"),\n+            \"status\": resp.status_code,\n+        }\n+    save_telemetry_submission(data)\n+    return data\n+\n+\n+def send_telemetry(request=None, max_age_hours=72, raise_on_error=False):\n+    \"\"\"\n+    Send telemetry information (unless opted-out, in grace period or disabled).\n+\n+    :param request: HTTP request. Optional.\n+    :type request: django.http.HttpRequest|None\n+    :param max_age_hours: How many hours must have passed since the\n+                          last submission to be able to resend. If None, not checked.\n+    :type max_age_hours: int|None\n+    :param raise_on_error: Raise exceptions when telemetry is not sent instead of quietly returning False.\n+    :type raise_on_error: bool\n+    :return: Sent data (possibly with response information) or False if not sent.\n+    :rtype: dict|bool\n+    \"\"\"\n+    try:\n+        return _send_telemetry(request=request, max_age_hours=max_age_hours)\n+    except TelemetryNotSent:\n+        if raise_on_error:\n+            raise\n+        return False  # pragma: no cover","path":"shoop/core/telemetry.py","position":196,"original_position":196,"commit_id":"5844d01602d8d97818545f6d727946068e6c9f30","original_commit_id":"5844d01602d8d97818545f6d727946068e6c9f30","user":{"login":"suutari-ai","id":3531281,"avatar_url":"https://avatars.githubusercontent.com/u/3531281?v=3","gravatar_id":"","url":"https://api.github.com/users/suutari-ai","html_url":"https://github.com/suutari-ai","followers_url":"https://api.github.com/users/suutari-ai/followers","following_url":"https://api.github.com/users/suutari-ai/following{/other_user}","gists_url":"https://api.github.com/users/suutari-ai/gists{/gist_id}","starred_url":"https://api.github.com/users/suutari-ai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/suutari-ai/subscriptions","organizations_url":"https://api.github.com/users/suutari-ai/orgs","repos_url":"https://api.github.com/users/suutari-ai/repos","events_url":"https://api.github.com/users/suutari-ai/events{/privacy}","received_events_url":"https://api.github.com/users/suutari-ai/received_events","type":"User","site_admin":false},"body":"Please don't add \"no cover\" pragmas for code that can easily be tested.","created_at":"2015-06-30T06:06:10Z","updated_at":"2015-06-30T06:06:10Z","html_url":"https://github.com/shoopio/shoop/pull/26#discussion_r33542651","pull_request_url":"https://api.github.com/repos/shoopio/shoop/pulls/26","_links":{"self":{"href":"https://api.github.com/repos/shoopio/shoop/pulls/comments/33542651"},"html":{"href":"https://github.com/shoopio/shoop/pull/26#discussion_r33542651"},"pull_request":{"href":"https://api.github.com/repos/shoopio/shoop/pulls/26"}}},"pull_request":{"url":"https://api.github.com/repos/shoopio/shoop/pulls/26","id":38328514,"html_url":"https://github.com/shoopio/shoop/pull/26","diff_url":"https://github.com/shoopio/shoop/pull/26.diff","patch_url":"https://github.com/shoopio/shoop/pull/26.patch","issue_url":"https://api.github.com/repos/shoopio/shoop/issues/26","number":26,"state":"open","locked":false,"title":"Telemetry Sender","user":{"login":"akx","id":58669,"avatar_url":"https://avatars.githubusercontent.com/u/58669?v=3","gravatar_id":"","url":"https://api.github.com/users/akx","html_url":"https://github.com/akx","followers_url":"https://api.github.com/users/akx/followers","following_url":"https://api.github.com/users/akx/following{/other_user}","gists_url":"https://api.github.com/users/akx/gists{/gist_id}","starred_url":"https://api.github.com/users/akx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akx/subscriptions","organizations_url":"https://api.github.com/users/akx/orgs","repos_url":"https://api.github.com/users/akx/repos","events_url":"https://api.github.com/users/akx/events{/privacy}","received_events_url":"https://api.github.com/users/akx/received_events","type":"User","site_admin":false},"body":"Implements an **opt-in-by-default** telemetry sender.\r\n\r\nThe receiving side is <del>still missing</del>currently in PR review, so this doesn't do much except slow down loading of the admin dashboard for a few seconds every 72 hours.","created_at":"2015-06-23T11:59:49Z","updated_at":"2015-06-30T06:06:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a890b2622403709e779dd1cc2ed15b4625589440","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/shoopio/shoop/pulls/26/commits","review_comments_url":"https://api.github.com/repos/shoopio/shoop/pulls/26/comments","review_comment_url":"https://api.github.com/repos/shoopio/shoop/pulls/comments{/number}","comments_url":"https://api.github.com/repos/shoopio/shoop/issues/26/comments","statuses_url":"https://api.github.com/repos/shoopio/shoop/statuses/5844d01602d8d97818545f6d727946068e6c9f30","head":{"label":"akx:telemetry","ref":"telemetry","sha":"5844d01602d8d97818545f6d727946068e6c9f30","user":{"login":"akx","id":58669,"avatar_url":"https://avatars.githubusercontent.com/u/58669?v=3","gravatar_id":"","url":"https://api.github.com/users/akx","html_url":"https://github.com/akx","followers_url":"https://api.github.com/users/akx/followers","following_url":"https://api.github.com/users/akx/following{/other_user}","gists_url":"https://api.github.com/users/akx/gists{/gist_id}","starred_url":"https://api.github.com/users/akx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akx/subscriptions","organizations_url":"https://api.github.com/users/akx/orgs","repos_url":"https://api.github.com/users/akx/repos","events_url":"https://api.github.com/users/akx/events{/privacy}","received_events_url":"https://api.github.com/users/akx/received_events","type":"User","site_admin":false},"repo":{"id":37119193,"name":"shoop","full_name":"akx/shoop","owner":{"login":"akx","id":58669,"avatar_url":"https://avatars.githubusercontent.com/u/58669?v=3","gravatar_id":"","url":"https://api.github.com/users/akx","html_url":"https://github.com/akx","followers_url":"https://api.github.com/users/akx/followers","following_url":"https://api.github.com/users/akx/following{/other_user}","gists_url":"https://api.github.com/users/akx/gists{/gist_id}","starred_url":"https://api.github.com/users/akx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akx/subscriptions","organizations_url":"https://api.github.com/users/akx/orgs","repos_url":"https://api.github.com/users/akx/repos","events_url":"https://api.github.com/users/akx/events{/privacy}","received_events_url":"https://api.github.com/users/akx/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/akx/shoop","description":"","fork":true,"url":"https://api.github.com/repos/akx/shoop","forks_url":"https://api.github.com/repos/akx/shoop/forks","keys_url":"https://api.github.com/repos/akx/shoop/keys{/key_id}","collaborators_url":"https://api.github.com/repos/akx/shoop/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/akx/shoop/teams","hooks_url":"https://api.github.com/repos/akx/shoop/hooks","issue_events_url":"https://api.github.com/repos/akx/shoop/issues/events{/number}","events_url":"https://api.github.com/repos/akx/shoop/events","assignees_url":"https://api.github.com/repos/akx/shoop/assignees{/user}","branches_url":"https://api.github.com/repos/akx/shoop/branches{/branch}","tags_url":"https://api.github.com/repos/akx/shoop/tags","blobs_url":"https://api.github.com/repos/akx/shoop/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/akx/shoop/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/akx/shoop/git/refs{/sha}","trees_url":"https://api.github.com/repos/akx/shoop/git/trees{/sha}","statuses_url":"https://api.github.com/repos/akx/shoop/statuses/{sha}","languages_url":"https://api.github.com/repos/akx/shoop/languages","stargazers_url":"https://api.github.com/repos/akx/shoop/stargazers","contributors_url":"https://api.github.com/repos/akx/shoop/contributors","subscribers_url":"https://api.github.com/repos/akx/shoop/subscribers","subscription_url":"https://api.github.com/repos/akx/shoop/subscription","commits_url":"https://api.github.com/repos/akx/shoop/commits{/sha}","git_commits_url":"https://api.github.com/repos/akx/shoop/git/commits{/sha}","comments_url":"https://api.github.com/repos/akx/shoop/comments{/number}","issue_comment_url":"https://api.github.com/repos/akx/shoop/issues/comments{/number}","contents_url":"https://api.github.com/repos/akx/shoop/contents/{+path}","compare_url":"https://api.github.com/repos/akx/shoop/compare/{base}...{head}","merges_url":"https://api.github.com/repos/akx/shoop/merges","archive_url":"https://api.github.com/repos/akx/shoop/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/akx/shoop/downloads","issues_url":"https://api.github.com/repos/akx/shoop/issues{/number}","pulls_url":"https://api.github.com/repos/akx/shoop/pulls{/number}","milestones_url":"https://api.github.com/repos/akx/shoop/milestones{/number}","notifications_url":"https://api.github.com/repos/akx/shoop/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/akx/shoop/labels{/name}","releases_url":"https://api.github.com/repos/akx/shoop/releases{/id}","created_at":"2015-06-09T08:11:10Z","updated_at":"2015-06-09T08:11:11Z","pushed_at":"2015-06-29T11:37:32Z","git_url":"git://github.com/akx/shoop.git","ssh_url":"git@github.com:akx/shoop.git","clone_url":"https://github.com/akx/shoop.git","svn_url":"https://github.com/akx/shoop","homepage":null,"size":1681,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"shoopio:master","ref":"master","sha":"787a8964990c069dcc765149a9fad0e5ca64f859","user":{"login":"shoopio","id":12714852,"avatar_url":"https://avatars.githubusercontent.com/u/12714852?v=3","gravatar_id":"","url":"https://api.github.com/users/shoopio","html_url":"https://github.com/shoopio","followers_url":"https://api.github.com/users/shoopio/followers","following_url":"https://api.github.com/users/shoopio/following{/other_user}","gists_url":"https://api.github.com/users/shoopio/gists{/gist_id}","starred_url":"https://api.github.com/users/shoopio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shoopio/subscriptions","organizations_url":"https://api.github.com/users/shoopio/orgs","repos_url":"https://api.github.com/users/shoopio/repos","events_url":"https://api.github.com/users/shoopio/events{/privacy}","received_events_url":"https://api.github.com/users/shoopio/received_events","type":"Organization","site_admin":false},"repo":{"id":36803364,"name":"shoop","full_name":"shoopio/shoop","owner":{"login":"shoopio","id":12714852,"avatar_url":"https://avatars.githubusercontent.com/u/12714852?v=3","gravatar_id":"","url":"https://api.github.com/users/shoopio","html_url":"https://github.com/shoopio","followers_url":"https://api.github.com/users/shoopio/followers","following_url":"https://api.github.com/users/shoopio/following{/other_user}","gists_url":"https://api.github.com/users/shoopio/gists{/gist_id}","starred_url":"https://api.github.com/users/shoopio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shoopio/subscriptions","organizations_url":"https://api.github.com/users/shoopio/orgs","repos_url":"https://api.github.com/users/shoopio/repos","events_url":"https://api.github.com/users/shoopio/events{/privacy}","received_events_url":"https://api.github.com/users/shoopio/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/shoopio/shoop","description":"E-Commerce Platform","fork":false,"url":"https://api.github.com/repos/shoopio/shoop","forks_url":"https://api.github.com/repos/shoopio/shoop/forks","keys_url":"https://api.github.com/repos/shoopio/shoop/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shoopio/shoop/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shoopio/shoop/teams","hooks_url":"https://api.github.com/repos/shoopio/shoop/hooks","issue_events_url":"https://api.github.com/repos/shoopio/shoop/issues/events{/number}","events_url":"https://api.github.com/repos/shoopio/shoop/events","assignees_url":"https://api.github.com/repos/shoopio/shoop/assignees{/user}","branches_url":"https://api.github.com/repos/shoopio/shoop/branches{/branch}","tags_url":"https://api.github.com/repos/shoopio/shoop/tags","blobs_url":"https://api.github.com/repos/shoopio/shoop/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shoopio/shoop/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shoopio/shoop/git/refs{/sha}","trees_url":"https://api.github.com/repos/shoopio/shoop/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shoopio/shoop/statuses/{sha}","languages_url":"https://api.github.com/repos/shoopio/shoop/languages","stargazers_url":"https://api.github.com/repos/shoopio/shoop/stargazers","contributors_url":"https://api.github.com/repos/shoopio/shoop/contributors","subscribers_url":"https://api.github.com/repos/shoopio/shoop/subscribers","subscription_url":"https://api.github.com/repos/shoopio/shoop/subscription","commits_url":"https://api.github.com/repos/shoopio/shoop/commits{/sha}","git_commits_url":"https://api.github.com/repos/shoopio/shoop/git/commits{/sha}","comments_url":"https://api.github.com/repos/shoopio/shoop/comments{/number}","issue_comment_url":"https://api.github.com/repos/shoopio/shoop/issues/comments{/number}","contents_url":"https://api.github.com/repos/shoopio/shoop/contents/{+path}","compare_url":"https://api.github.com/repos/shoopio/shoop/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shoopio/shoop/merges","archive_url":"https://api.github.com/repos/shoopio/shoop/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shoopio/shoop/downloads","issues_url":"https://api.github.com/repos/shoopio/shoop/issues{/number}","pulls_url":"https://api.github.com/repos/shoopio/shoop/pulls{/number}","milestones_url":"https://api.github.com/repos/shoopio/shoop/milestones{/number}","notifications_url":"https://api.github.com/repos/shoopio/shoop/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shoopio/shoop/labels{/name}","releases_url":"https://api.github.com/repos/shoopio/shoop/releases{/id}","created_at":"2015-06-03T12:53:21Z","updated_at":"2015-06-25T16:44:28Z","pushed_at":"2015-06-29T11:37:33Z","git_url":"git://github.com/shoopio/shoop.git","ssh_url":"git@github.com:shoopio/shoop.git","clone_url":"https://github.com/shoopio/shoop.git","svn_url":"https://github.com/shoopio/shoop","homepage":"https://shoop.io","size":2190,"stargazers_count":24,"watchers_count":24,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":10,"mirror_url":null,"open_issues_count":7,"forks":10,"open_issues":7,"watchers":24,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/shoopio/shoop/pulls/26"},"html":{"href":"https://github.com/shoopio/shoop/pull/26"},"issue":{"href":"https://api.github.com/repos/shoopio/shoop/issues/26"},"comments":{"href":"https://api.github.com/repos/shoopio/shoop/issues/26/comments"},"review_comments":{"href":"https://api.github.com/repos/shoopio/shoop/pulls/26/comments"},"review_comment":{"href":"https://api.github.com/repos/shoopio/shoop/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/shoopio/shoop/pulls/26/commits"},"statuses":{"href":"https://api.github.com/repos/shoopio/shoop/statuses/5844d01602d8d97818545f6d727946068e6c9f30"}}}}