{"action":"created","comment":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/comments/27889860","id":27889860,"diff_hunk":"@@ -0,0 +1,192 @@\n+\"\"\" Code to allow module store to interface with courseware index \"\"\"\n+from __future__ import absolute_import\n+\n+from datetime import timedelta\n+import logging\n+\n+from django.conf import settings\n+from django.utils.translation import ugettext as _\n+from eventtracking import tracker\n+from xmodule.modulestore import ModuleStoreEnum\n+from search.search_engine_base import SearchEngine\n+\n+\n+# Use default index and document names for now\n+INDEX_NAME = \"courseware_index\"\n+DOCUMENT_TYPE = \"courseware_content\"\n+REINDEX_AGE = timedelta(0, 60)\n+\n+log = logging.getLogger('edx.modulestore')\n+\n+\n+def indexing_is_enabled():\n+    \"\"\"\n+    Checks to see if the indexing feature is enabled\n+    \"\"\"\n+    return settings.FEATURES.get('ENABLE_COURSEWARE_INDEX', False)\n+\n+\n+class SearchIndexingError(Exception):\n+    \"\"\" Indicates some error(s) occured during indexing \"\"\"\n+\n+    def __init__(self, message, error_list):\n+        super(SearchIndexingError, self).__init__(message)\n+        self.error_list = error_list\n+\n+\n+class CoursewareSearchIndexer(object):\n+    \"\"\"\n+    Class to perform indexing for courseware search from different modulestores\n+    \"\"\"\n+\n+    @staticmethod\n+    def index_course(modulestore, course_key, triggered_at=None, reindex_age=REINDEX_AGE):\n+        \"\"\"\n+        Process course for indexing\n+\n+        Arguments:\n+        course_key (CourseKey) - course identifier\n+\n+        triggered_at (datetime) - provides time at which indexing was triggered;\n+            useful for index updates - only things changed recently from that date\n+            (within REINDEX_AGE above ^^) will have their index updated, others skip\n+            updating their index but are still walked through in order to identify\n+            which items may need to be removed from the index\n+            If None, then a full reindex takes place\n+\n+        Returns:\n+        Number of items that have been added to the index\n+        \"\"\"\n+        error_list = []\n+        searcher = SearchEngine.get_search_engine(INDEX_NAME)\n+        if not searcher:\n+            return\n+\n+        location_info = {\n+            \"course\": unicode(course_key),\n+        }\n+\n+        # Wrap counter in dictionary - otherwise we seem to lose scope inside the embedded function `index_item`\n+        indexed_count = {\n+            \"count\": 0\n+        }\n+        indexed_items = set()\n+\n+        def index_item(item, current_start_date, skip_index=False):\n+            \"\"\"\n+            Add this item to the search index and indexed_items list\n+\n+            Arguments:\n+            item - item to add to index, its children will be processed recursively\n+\n+            current_start_date - the startdate with which to associate the item,\n+                this will apply downwards until overridden while recursively processing\n+                the children\n+\n+            skip_index - simply walk the children in the tree, the content change is\n+                older than the REINDEX_AGE window and would have been already indexed.\n+                This should really only be passed from the recursive child calls when\n+                this method has determined that it is safe to do so\n+            \"\"\"\n+            is_indexable = hasattr(item, \"index_dictionary\")\n+            item_index_dictionary = item.index_dictionary() if is_indexable else None\n+            # if it's not indexable and it does not have children, then ignore\n+            if not item_index_dictionary and not item.has_children:\n+                return\n+\n+            item_id = unicode(item.scope_ids.usage_id)\n+            indexed_items.add(item_id)\n+\n+            # if it has a defined start, then apply it and to it's children\n+            if item.start and (not current_start_date or item.start > current_start_date):\n+                current_start_date = item.start\n+\n+            if item.has_children:\n+                # determine if it's okay to skip adding the children herein based upon how recently any may have changed\n+                skip_child_index = skip_index or \\\n+                    (triggered_at is not None and (triggered_at - item.subtree_edited_on) > reindex_age)\n+                for child_item in item.get_children():\n+                    index_item(child_item, current_start_date, skip_index=skip_child_index)\n+\n+            if skip_index or not item_index_dictionary:\n+                return\n+\n+            item_index = {}\n+            # if it has something to add to the index, then add it\n+            try:\n+                item_index.update(location_info)\n+                item_index.update(item_index_dictionary)\n+                item_index['id'] = item_id\n+                if current_start_date:\n+                    item_index['start_date'] = current_start_date\n+\n+                searcher.index(DOCUMENT_TYPE, item_index)\n+                indexed_count[\"count\"] += 1\n+            except Exception as err:  # pylint: disable=broad-except\n+                # broad exception so that index operation does not fail on one item of many\n+                log.warning('Could not index item: %s - %r', item.location, err)\n+                error_list.append(_('Could not index item: {}').format(item.location))\n+\n+        def remove_deleted_items():\n+            \"\"\"\n+            remove any item that is present in the search index that is not present in updated list of indexed items\n+            as we find items we can shorten the set of items to keep\n+            \"\"\"\n+            response = searcher.search(\n+                doc_type=DOCUMENT_TYPE,\n+                field_dictionary={\"course\": unicode(course_key)},\n+                exclude_ids=indexed_items","path":"cms/djangoapps/contentstore/courseware_index.py","position":149,"original_position":138,"commit_id":"0af22556a43df707ec21e64f4613b21f16d971ee","original_commit_id":"1b17a1eaebce2c935af459c49885246a5bb0aca8","user":{"login":"martynjames","id":675165,"avatar_url":"https://avatars.githubusercontent.com/u/675165?v=3","gravatar_id":"","url":"https://api.github.com/users/martynjames","html_url":"https://github.com/martynjames","followers_url":"https://api.github.com/users/martynjames/followers","following_url":"https://api.github.com/users/martynjames/following{/other_user}","gists_url":"https://api.github.com/users/martynjames/gists{/gist_id}","starred_url":"https://api.github.com/users/martynjames/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/martynjames/subscriptions","organizations_url":"https://api.github.com/users/martynjames/orgs","repos_url":"https://api.github.com/users/martynjames/repos","events_url":"https://api.github.com/users/martynjames/events{/privacy}","received_events_url":"https://api.github.com/users/martynjames/received_events","type":"User","site_admin":false},"body":"I added a test to investigate when it would start to error (however it is disabled for running on Jenkins because it takes time to build a course that is very large). I ran last night with ever-increasing sizes of courses for 10-12 hours with no failures. \r\n\r\nTo get an idea of how far I got I ran the test with approx. 4096 items in the `exclude_ids` list (this took about 3 hours with the course creation and tear down). I added a note to SOL-641 to enhance the performance tests to see if we can get an idea on when it will break-down, but I think this is a fine first step.","created_at":"2015-04-07T15:40:12Z","updated_at":"2015-04-07T15:40:12Z","html_url":"https://github.com/edx/edx-platform/pull/7313#discussion_r27889860","pull_request_url":"https://api.github.com/repos/edx/edx-platform/pulls/7313","_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/comments/27889860"},"html":{"href":"https://github.com/edx/edx-platform/pull/7313#discussion_r27889860"},"pull_request":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/7313"}}},"pull_request":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/7313","id":30982304,"html_url":"https://github.com/edx/edx-platform/pull/7313","diff_url":"https://github.com/edx/edx-platform/pull/7313.diff","patch_url":"https://github.com/edx/edx-platform/pull/7313.patch","issue_url":"https://api.github.com/repos/edx/edx-platform/issues/7313","number":7313,"state":"open","locked":false,"title":"Mjames/sol 174","user":{"login":"martynjames","id":675165,"avatar_url":"https://avatars.githubusercontent.com/u/675165?v=3","gravatar_id":"","url":"https://api.github.com/users/martynjames","html_url":"https://github.com/martynjames","followers_url":"https://api.github.com/users/martynjames/followers","following_url":"https://api.github.com/users/martynjames/following{/other_user}","gists_url":"https://api.github.com/users/martynjames/gists{/gist_id}","starred_url":"https://api.github.com/users/martynjames/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/martynjames/subscriptions","organizations_url":"https://api.github.com/users/martynjames/orgs","repos_url":"https://api.github.com/users/martynjames/repos","events_url":"https://api.github.com/users/martynjames/events{/privacy}","received_events_url":"https://api.github.com/users/martynjames/received_events","type":"User","site_admin":false},"body":"@dino-cikatic - can you do a first-pass review for these changes\r\n@dsego - It would be nice if you can review too - but seeing as you performed some of the code, you can't be the official reviewer","created_at":"2015-03-11T20:33:06Z","updated_at":"2015-04-07T15:40:12Z","closed_at":null,"merged_at":null,"merge_commit_sha":"891736740f0b64d35fa7eeeef79f536dc7ba76cc","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/edx/edx-platform/pulls/7313/commits","review_comments_url":"https://api.github.com/repos/edx/edx-platform/pulls/7313/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/7313/comments","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/0af22556a43df707ec21e64f4613b21f16d971ee","head":{"label":"edx:mjames/SOL-174","ref":"mjames/SOL-174","sha":"0af22556a43df707ec21e64f4613b21f16d971ee","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-04-07T14:40:07Z","pushed_at":"2015-04-07T15:37:11Z","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":1876728,"stargazers_count":1850,"watchers_count":1850,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1046,"mirror_url":null,"open_issues_count":153,"forks":1046,"open_issues":153,"watchers":1850,"default_branch":"master"}},"base":{"label":"edx:master","ref":"master","sha":"037a189828c12b6062c1f33bd8157a4912827f43","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-04-07T14:40:07Z","pushed_at":"2015-04-07T15:37:11Z","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":1876728,"stargazers_count":1850,"watchers_count":1850,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1046,"mirror_url":null,"open_issues_count":153,"forks":1046,"open_issues":153,"watchers":1850,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/7313"},"html":{"href":"https://github.com/edx/edx-platform/pull/7313"},"issue":{"href":"https://api.github.com/repos/edx/edx-platform/issues/7313"},"comments":{"href":"https://api.github.com/repos/edx/edx-platform/issues/7313/comments"},"review_comments":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/7313/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/7313/commits"},"statuses":{"href":"https://api.github.com/repos/edx/edx-platform/statuses/0af22556a43df707ec21e64f4613b21f16d971ee"}}}}