{"action":"created","comment":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/comments/31473870","id":31473870,"diff_hunk":"@@ -0,0 +1,120 @@\n+\"\"\"\n+Get call stacks of Model Class\n+in three cases-\n+1. QuerySet API\n+2. save()\n+3. delete()\n+\n+classes:\n+Globals - contains global stack_book dictionary , also regular expression filters\n+CallStackManager -  stores all stacks in global dictionary and logs\n+\n+How to use-\n+1. Import following in the file where class to be tracked resides\n+    from openedx.core.lib.call_stack_manager import CallStackManager\n+2. Override objects of default manager by writing following in any model class which you want to track-\n+    objects = CallStackManager()\n+\n+Note -\n+1.Format for stack_book\n+{\n+    \"modelclass1\":\n+        [[(frame 1),(frame 2)],\n+         [(frame 11),(frame21)]]\n+    \"modelclass2\":\n+        [[(frame 3),(frame 4)],\n+         [(frame 6),(frame 5)]]\n+\n+}\n+where frame is a tuple of\n+(file path, Line Number, Context)\n+\"\"\"\n+\n+import logging\n+import traceback\n+import re\n+import threading\n+\n+from django.db.models import Manager\n+\n+\n+log = logging.getLogger(__name__)\n+\n+\n+class Globals(threading.local):\n+    \"\"\"\n+    stores necessary global data structures for Call Stacks and runs a method necessary for\n+    \"\"\"\n+    # dictionary which stores call stacks.\n+    # { \"ModelClasses\" : [ListOfFrames]}\n+    # Frames - ('FilePath','LineNumber','Context')\n+    # ex. {\"<class 'courseware.models.StudentModule'>\" : [[(file,line number,context),(---,---,---)],\n+    #                                                     [(file,line number,context),(---,---,---)]]}\n+    stack_book = {}\n+\n+    # filter to trickle down call stacks.\n+    exclude = ['^.*python2.7.*$', '^.*call_stack_manager.*$']\n+    regular_expressions = [re.compile(x) for x in exclude]\n+\n+# Instantiate Globals Class.\n+GLOBAL_DICT = Globals()\n+\n+\n+class CallStackManager(Manager):\n+    \"\"\"\n+    gets call stacks of model classes\n+    \"\"\"\n+    def __init__(self):\n+        super(CallStackManager, self).__init__()\n+\n+    def get_call_stack(self):\n+        \"\"\"\n+        stores customised call stacks in global dictionary `stack_book`, and logs it.\n+        \"\"\"\n+        # get name of current model class\n+        current_model = str(self.model)\n+        current_model = current_model[current_model.find('\\'') + 1: current_model.rfind('\\'')]\n+\n+        # holds temporary callstack\n+        temp_call_stack = []\n+\n+        for line in traceback.format_stack():\n+            line = line.replace(\"\\n\", \"\")\n+            temp_call_stack.append(line.split(',')[0].strip() + \"+\" + line.split(',')[1].strip() + \"+\" +\n+                                   line.split(',')[2].strip())\n+\n+        filtered_call_stack = [frame for frame in temp_call_stack\n+                               if not len([reg for reg in GLOBAL_DICT.regular_expressions if reg.match(frame)])]\n+        call_stack = [(x[0][6:-1], x[1][6:], x[2][3:]) for x in [tuple(x.split(\"+\")) for x in filtered_call_stack]]","path":"openedx/core/lib/call_stack_manager.py","position":null,"original_position":88,"commit_id":"746b38eb9759c30caa9285aca847e58b185665c6","original_commit_id":"465e017c06b8bc883be32c0e7115348d9af52b2b","user":{"login":"utkjad","id":5477150,"avatar_url":"https://avatars.githubusercontent.com/u/5477150?v=3","gravatar_id":"","url":"https://api.github.com/users/utkjad","html_url":"https://github.com/utkjad","followers_url":"https://api.github.com/users/utkjad/followers","following_url":"https://api.github.com/users/utkjad/following{/other_user}","gists_url":"https://api.github.com/users/utkjad/gists{/gist_id}","starred_url":"https://api.github.com/users/utkjad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/utkjad/subscriptions","organizations_url":"https://api.github.com/users/utkjad/orgs","repos_url":"https://api.github.com/users/utkjad/repos","events_url":"https://api.github.com/users/utkjad/events{/privacy}","received_events_url":"https://api.github.com/users/utkjad/received_events","type":"User","site_admin":false},"body":"Done. ","created_at":"2015-06-01T21:43:56Z","updated_at":"2015-06-01T21:43:56Z","html_url":"https://github.com/edx/edx-platform/pull/8280#discussion_r31473870","pull_request_url":"https://api.github.com/repos/edx/edx-platform/pulls/8280","_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/comments/31473870"},"html":{"href":"https://github.com/edx/edx-platform/pull/8280#discussion_r31473870"},"pull_request":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8280"}}},"pull_request":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/8280","id":36545247,"html_url":"https://github.com/edx/edx-platform/pull/8280","diff_url":"https://github.com/edx/edx-platform/pull/8280.diff","patch_url":"https://github.com/edx/edx-platform/pull/8280.patch","issue_url":"https://api.github.com/repos/edx/edx-platform/issues/8280","number":8280,"state":"open","locked":false,"title":"adding call stack manager","user":{"login":"utkjad","id":5477150,"avatar_url":"https://avatars.githubusercontent.com/u/5477150?v=3","gravatar_id":"","url":"https://api.github.com/users/utkjad","html_url":"https://github.com/utkjad","followers_url":"https://api.github.com/users/utkjad/followers","following_url":"https://api.github.com/users/utkjad/following{/other_user}","gists_url":"https://api.github.com/users/utkjad/gists{/gist_id}","starred_url":"https://api.github.com/users/utkjad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/utkjad/subscriptions","organizations_url":"https://api.github.com/users/utkjad/orgs","repos_url":"https://api.github.com/users/utkjad/repos","events_url":"https://api.github.com/users/utkjad/events{/privacy}","received_events_url":"https://api.github.com/users/utkjad/received_events","type":"User","site_admin":false},"body":"added call stack manager to trace classes defined in Model classes.\r\n\r\n@doctoryes  @cpennington  - can you please review?","created_at":"2015-05-29T21:09:18Z","updated_at":"2015-06-01T21:43:56Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/edx/edx-platform/pulls/8280/commits","review_comments_url":"https://api.github.com/repos/edx/edx-platform/pulls/8280/comments","review_comment_url":"https://api.github.com/repos/edx/edx-platform/pulls/comments{/number}","comments_url":"https://api.github.com/repos/edx/edx-platform/issues/8280/comments","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/746b38eb9759c30caa9285aca847e58b185665c6","head":{"label":"edx:utkjad/callstackmanager","ref":"utkjad/callstackmanager","sha":"746b38eb9759c30caa9285aca847e58b185665c6","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"repo":{"id":10391073,"name":"edx-platform","full_name":"edx/edx-platform","owner":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx/edx-platform","description":"Open edX, the edX platform that powers http://edx.org","fork":false,"url":"https://api.github.com/repos/edx/edx-platform","forks_url":"https://api.github.com/repos/edx/edx-platform/forks","keys_url":"https://api.github.com/repos/edx/edx-platform/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/edx-platform/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/edx-platform/teams","hooks_url":"https://api.github.com/repos/edx/edx-platform/hooks","issue_events_url":"https://api.github.com/repos/edx/edx-platform/issues/events{/number}","events_url":"https://api.github.com/repos/edx/edx-platform/events","assignees_url":"https://api.github.com/repos/edx/edx-platform/assignees{/user}","branches_url":"https://api.github.com/repos/edx/edx-platform/branches{/branch}","tags_url":"https://api.github.com/repos/edx/edx-platform/tags","blobs_url":"https://api.github.com/repos/edx/edx-platform/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/edx-platform/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/edx-platform/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/edx-platform/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/edx-platform/languages","stargazers_url":"https://api.github.com/repos/edx/edx-platform/stargazers","contributors_url":"https://api.github.com/repos/edx/edx-platform/contributors","subscribers_url":"https://api.github.com/repos/edx/edx-platform/subscribers","subscription_url":"https://api.github.com/repos/edx/edx-platform/subscription","commits_url":"https://api.github.com/repos/edx/edx-platform/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/edx-platform/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/edx-platform/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/edx-platform/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/edx-platform/contents/{+path}","compare_url":"https://api.github.com/repos/edx/edx-platform/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/edx-platform/merges","archive_url":"https://api.github.com/repos/edx/edx-platform/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/edx-platform/downloads","issues_url":"https://api.github.com/repos/edx/edx-platform/issues{/number}","pulls_url":"https://api.github.com/repos/edx/edx-platform/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/edx-platform/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/edx-platform/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/edx-platform/labels{/name}","releases_url":"https://api.github.com/repos/edx/edx-platform/releases{/id}","created_at":"2013-05-30T20:20:38Z","updated_at":"2015-06-01T19:21:03Z","pushed_at":"2015-06-01T21:43:45Z","git_url":"git://github.com/edx/edx-platform.git","ssh_url":"git@github.com:edx/edx-platform.git","clone_url":"https://github.com/edx/edx-platform.git","svn_url":"https://github.com/edx/edx-platform","homepage":"http://openedx.org/","size":1966142,"stargazers_count":1953,"watchers_count":1953,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1122,"mirror_url":null,"open_issues_count":196,"forks":1122,"open_issues":196,"watchers":1953,"default_branch":"master"}},"base":{"label":"edx:master","ref":"master","sha":"acbc2113a283e7f037183bebee13850377b7767e","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"repo":{"id":10391073,"name":"edx-platform","full_name":"edx/edx-platform","owner":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx/edx-platform","description":"Open edX, the edX platform that powers http://edx.org","fork":false,"url":"https://api.github.com/repos/edx/edx-platform","forks_url":"https://api.github.com/repos/edx/edx-platform/forks","keys_url":"https://api.github.com/repos/edx/edx-platform/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/edx-platform/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/edx-platform/teams","hooks_url":"https://api.github.com/repos/edx/edx-platform/hooks","issue_events_url":"https://api.github.com/repos/edx/edx-platform/issues/events{/number}","events_url":"https://api.github.com/repos/edx/edx-platform/events","assignees_url":"https://api.github.com/repos/edx/edx-platform/assignees{/user}","branches_url":"https://api.github.com/repos/edx/edx-platform/branches{/branch}","tags_url":"https://api.github.com/repos/edx/edx-platform/tags","blobs_url":"https://api.github.com/repos/edx/edx-platform/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/edx-platform/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/edx-platform/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/edx-platform/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/edx-platform/languages","stargazers_url":"https://api.github.com/repos/edx/edx-platform/stargazers","contributors_url":"https://api.github.com/repos/edx/edx-platform/contributors","subscribers_url":"https://api.github.com/repos/edx/edx-platform/subscribers","subscription_url":"https://api.github.com/repos/edx/edx-platform/subscription","commits_url":"https://api.github.com/repos/edx/edx-platform/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/edx-platform/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/edx-platform/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/edx-platform/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/edx-platform/contents/{+path}","compare_url":"https://api.github.com/repos/edx/edx-platform/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/edx-platform/merges","archive_url":"https://api.github.com/repos/edx/edx-platform/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/edx-platform/downloads","issues_url":"https://api.github.com/repos/edx/edx-platform/issues{/number}","pulls_url":"https://api.github.com/repos/edx/edx-platform/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/edx-platform/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/edx-platform/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/edx-platform/labels{/name}","releases_url":"https://api.github.com/repos/edx/edx-platform/releases{/id}","created_at":"2013-05-30T20:20:38Z","updated_at":"2015-06-01T19:21:03Z","pushed_at":"2015-06-01T21:43:45Z","git_url":"git://github.com/edx/edx-platform.git","ssh_url":"git@github.com:edx/edx-platform.git","clone_url":"https://github.com/edx/edx-platform.git","svn_url":"https://github.com/edx/edx-platform","homepage":"http://openedx.org/","size":1966142,"stargazers_count":1953,"watchers_count":1953,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1122,"mirror_url":null,"open_issues_count":196,"forks":1122,"open_issues":196,"watchers":1953,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8280"},"html":{"href":"https://github.com/edx/edx-platform/pull/8280"},"issue":{"href":"https://api.github.com/repos/edx/edx-platform/issues/8280"},"comments":{"href":"https://api.github.com/repos/edx/edx-platform/issues/8280/comments"},"review_comments":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8280/comments"},"review_comment":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8280/commits"},"statuses":{"href":"https://api.github.com/repos/edx/edx-platform/statuses/746b38eb9759c30caa9285aca847e58b185665c6"}}}}