{"action":"created","comment":{"url":"https://api.github.com/repos/qadium-memex/linkalytics/pulls/comments/40092922","id":40092922,"diff_hunk":"@@ -0,0 +1,117 @@\n+#Investigative Case Management without Pre-Processing -- Sample Code\n+#In a terminal window, type 'python CaseBuilderSansPP.py [indexname]' where you choose the indexname\n+#In a second terminal window, type 'curl -H \"Content-type: application/json\" -XPOST http://127.0.0.1:5000/search -d '{\"search\":[searchterm]}' where you choose the searchterm\n+\n+import json\n+from elasticsearch import Elasticsearch\n+import pymysql\n+import time\n+from environment import cfg\n+from flask import Flask, request  # jsonify\n+\n+app = Flask(__name__)\n+\n+\n+def query_docs(searchTerm, hostIndex, es, size, final):\n+    \"\"\"\n+    Here's a function to query Elastic Search and return either all the documents or a list of ids\n+    \"\"\"\n+    payload = {\n+        \"size\": size,\n+        \"query\": {\n+            \"match\": {\n+                \"_all\": searchTerm\n+                }\n+            }\n+        }\n+    res = es.search(index=hostIndex, body=payload)\n+    groups = set()\n+    for i in res['hits']['hits']:\n+        if final is True:\n+            groups.add(i)\n+        else:\n+            groups.add(str(i[\"_id\"]))\n+    return set(groups)\n+\n+\n+def look_up(docIds, CDRDocIds):\n+    \"\"\"\n+    Here's a function to use initially queried document ids to identify other related document ids\n+    \"\"\"\n+    newIds = list(set(CDRDocIds) - set(docIds))\n+    if newIds:\n+        conn = pymysql.connect(host=cfg.SQL.HOST, port=3306, user=cfg.SQL.USER, passwd=cfg.SQL.PASS, db=cfg.SQL.DB)\n+        cur = conn.cursor(pymysql.cursors.DictCursor)\n+\n+        sqlStatement = (\"SELECT %(docIdName)s, %(groupName)s from %(tableName)s where %(groupIdName)s in (SELECT %(groupIdName)s FROM %(tableName)s where %(docIdName)s in %(docId)s)\")\n+\n+        cur.execute(sqlStatement, {'docIdName': \"ad_id\", 'groupIdName': \"phone_id\", 'tableName': \"phone_link\", 'docId': \"(\" + \", \".join(newIds) + \")\"})\n+        group = {}\n+        for row in cur:\n+            if row[1] in group:\n+                group[str(row[1])].append(str(row[0]))\n+            else:\n+                group[str(row[1])] = []\n+                group[str(row[1])].append(str(row[0]))\n+        conn.close()\n+        return group\n+    else:\n+        return None\n+\n+\n+def post_new(groups, MirrorHost, MirrorEs, CDRHost, CDREs):\n+    \"\"\"\n+    Here's a function to post grouped documents to Elastic Search\n+    \"\"\"\n+    for i in groups.keys():\n+        docIds = groups[i]\n+        payload = {\n+            \"query\": {\n+                \"match\": {\n+                    \"_all\": \" \".join(docIds)\n+                    }\n+                }\n+            }\n+        res = CDREs.search(index=CDRHost, body=payload)\n+        newGroup = {}\n+        newGroup[\"docs\"] = []\n+        for result in res['hits']['hits']:\n+            del result[u\"_score\"]\n+            del result[u\"_type\"]\n+            newGroup[\"docs\"].append(result)\n+        res = MirrorEs.index(index=MirrorHost, doc_type=\"group\", id=i, body=json.dumps(newGroup))\n+\n+\n+@app.route(\"/search\", methods=['POST'])\n+def doc_to_group():\n+    \"\"\"\n+    Here's a server that takes a search term as an input and provides a list of grouped documents as an output\n+    Step 1 -- Query Mirror Elastic\n+    Step 2 -- Query CDR Elastic\n+    Step 3 -- Check Lookup Table\n+    Step 4 -- Post to Mirror Elastic if not yet contained in Mirror Elastic\n+    Step 5 -- Query newly updated Mirror Elastic\n+    \"\"\"\n+    data = request.get_json(force=True)[\"search\"]\n+    print(\"You search for: \" + data)\n+    docIds = query_docs(data, cfg.MIRROR_ELASTIC.INDEX, esMirror, 50, False)\n+    CDRDocIds = query_docs(data, cfg.CDR_ELASTIC.INDEX, esCDR, 50, False)\n+    newGroups = look_up(docIds, CDRDocIds)\n+    post_new(newGroups, cfg.MIRROR_ELASTIC.INDEX, esMirror, cfg.CDR_ELASTIC.INDEX, esCDR)\n+    results = query_docs(data, cfg.MIRROR_ELASTIC.INDEX, esMirror, 100, True)\n+    if results:\n+        return json.dumps(results)\n+    else:\n+        return \"No results\"","path":"src/server.py","position":105,"original_position":105,"commit_id":"f10b6a97592d246044ebe8f96b1e782603149cc8","original_commit_id":"f10b6a97592d246044ebe8f96b1e782603149cc8","user":{"login":"mattgroh","id":6493919,"avatar_url":"https://avatars.githubusercontent.com/u/6493919?v=3","gravatar_id":"","url":"https://api.github.com/users/mattgroh","html_url":"https://github.com/mattgroh","followers_url":"https://api.github.com/users/mattgroh/followers","following_url":"https://api.github.com/users/mattgroh/following{/other_user}","gists_url":"https://api.github.com/users/mattgroh/gists{/gist_id}","starred_url":"https://api.github.com/users/mattgroh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mattgroh/subscriptions","organizations_url":"https://api.github.com/users/mattgroh/orgs","repos_url":"https://api.github.com/users/mattgroh/repos","events_url":"https://api.github.com/users/mattgroh/events{/privacy}","received_events_url":"https://api.github.com/users/mattgroh/received_events","type":"User","site_admin":false},"body":"Good point, done.","created_at":"2015-09-22T14:32:04Z","updated_at":"2015-09-22T14:32:04Z","html_url":"https://github.com/qadium-memex/linkalytics/pull/1#discussion_r40092922","pull_request_url":"https://api.github.com/repos/qadium-memex/linkalytics/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/qadium-memex/linkalytics/pulls/comments/40092922"},"html":{"href":"https://github.com/qadium-memex/linkalytics/pull/1#discussion_r40092922"},"pull_request":{"href":"https://api.github.com/repos/qadium-memex/linkalytics/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/qadium-memex/linkalytics/pulls/1","id":45316300,"html_url":"https://github.com/qadium-memex/linkalytics/pull/1","diff_url":"https://github.com/qadium-memex/linkalytics/pull/1.diff","patch_url":"https://github.com/qadium-memex/linkalytics/pull/1.patch","issue_url":"https://api.github.com/repos/qadium-memex/linkalytics/issues/1","number":1,"state":"open","locked":false,"title":"Develop","user":{"login":"echu","id":52689,"avatar_url":"https://avatars.githubusercontent.com/u/52689?v=3","gravatar_id":"","url":"https://api.github.com/users/echu","html_url":"https://github.com/echu","followers_url":"https://api.github.com/users/echu/followers","following_url":"https://api.github.com/users/echu/following{/other_user}","gists_url":"https://api.github.com/users/echu/gists{/gist_id}","starred_url":"https://api.github.com/users/echu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/echu/subscriptions","organizations_url":"https://api.github.com/users/echu/orgs","repos_url":"https://api.github.com/users/echu/repos","events_url":"https://api.github.com/users/echu/events{/privacy}","received_events_url":"https://api.github.com/users/echu/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-09-17T17:11:31Z","updated_at":"2015-09-22T14:32:04Z","closed_at":null,"merged_at":null,"merge_commit_sha":"aa20bfb8b24735dc28b2606b5ef1d8a6dec267ba","assignee":{"login":"mattgroh","id":6493919,"avatar_url":"https://avatars.githubusercontent.com/u/6493919?v=3","gravatar_id":"","url":"https://api.github.com/users/mattgroh","html_url":"https://github.com/mattgroh","followers_url":"https://api.github.com/users/mattgroh/followers","following_url":"https://api.github.com/users/mattgroh/following{/other_user}","gists_url":"https://api.github.com/users/mattgroh/gists{/gist_id}","starred_url":"https://api.github.com/users/mattgroh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mattgroh/subscriptions","organizations_url":"https://api.github.com/users/mattgroh/orgs","repos_url":"https://api.github.com/users/mattgroh/repos","events_url":"https://api.github.com/users/mattgroh/events{/privacy}","received_events_url":"https://api.github.com/users/mattgroh/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/qadium-memex/linkalytics/pulls/1/commits","review_comments_url":"https://api.github.com/repos/qadium-memex/linkalytics/pulls/1/comments","review_comment_url":"https://api.github.com/repos/qadium-memex/linkalytics/pulls/comments{/number}","comments_url":"https://api.github.com/repos/qadium-memex/linkalytics/issues/1/comments","statuses_url":"https://api.github.com/repos/qadium-memex/linkalytics/statuses/f10b6a97592d246044ebe8f96b1e782603149cc8","head":{"label":"qadium-memex:develop","ref":"develop","sha":"f10b6a97592d246044ebe8f96b1e782603149cc8","user":{"login":"qadium-memex","id":14283725,"avatar_url":"https://avatars.githubusercontent.com/u/14283725?v=3","gravatar_id":"","url":"https://api.github.com/users/qadium-memex","html_url":"https://github.com/qadium-memex","followers_url":"https://api.github.com/users/qadium-memex/followers","following_url":"https://api.github.com/users/qadium-memex/following{/other_user}","gists_url":"https://api.github.com/users/qadium-memex/gists{/gist_id}","starred_url":"https://api.github.com/users/qadium-memex/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qadium-memex/subscriptions","organizations_url":"https://api.github.com/users/qadium-memex/orgs","repos_url":"https://api.github.com/users/qadium-memex/repos","events_url":"https://api.github.com/users/qadium-memex/events{/privacy}","received_events_url":"https://api.github.com/users/qadium-memex/received_events","type":"Organization","site_admin":false},"repo":{"id":42481423,"name":"linkalytics","full_name":"qadium-memex/linkalytics","owner":{"login":"qadium-memex","id":14283725,"avatar_url":"https://avatars.githubusercontent.com/u/14283725?v=3","gravatar_id":"","url":"https://api.github.com/users/qadium-memex","html_url":"https://github.com/qadium-memex","followers_url":"https://api.github.com/users/qadium-memex/followers","following_url":"https://api.github.com/users/qadium-memex/following{/other_user}","gists_url":"https://api.github.com/users/qadium-memex/gists{/gist_id}","starred_url":"https://api.github.com/users/qadium-memex/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qadium-memex/subscriptions","organizations_url":"https://api.github.com/users/qadium-memex/orgs","repos_url":"https://api.github.com/users/qadium-memex/repos","events_url":"https://api.github.com/users/qadium-memex/events{/privacy}","received_events_url":"https://api.github.com/users/qadium-memex/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/qadium-memex/linkalytics","description":"Source code for various analytics","fork":false,"url":"https://api.github.com/repos/qadium-memex/linkalytics","forks_url":"https://api.github.com/repos/qadium-memex/linkalytics/forks","keys_url":"https://api.github.com/repos/qadium-memex/linkalytics/keys{/key_id}","collaborators_url":"https://api.github.com/repos/qadium-memex/linkalytics/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/qadium-memex/linkalytics/teams","hooks_url":"https://api.github.com/repos/qadium-memex/linkalytics/hooks","issue_events_url":"https://api.github.com/repos/qadium-memex/linkalytics/issues/events{/number}","events_url":"https://api.github.com/repos/qadium-memex/linkalytics/events","assignees_url":"https://api.github.com/repos/qadium-memex/linkalytics/assignees{/user}","branches_url":"https://api.github.com/repos/qadium-memex/linkalytics/branches{/branch}","tags_url":"https://api.github.com/repos/qadium-memex/linkalytics/tags","blobs_url":"https://api.github.com/repos/qadium-memex/linkalytics/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/qadium-memex/linkalytics/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/qadium-memex/linkalytics/git/refs{/sha}","trees_url":"https://api.github.com/repos/qadium-memex/linkalytics/git/trees{/sha}","statuses_url":"https://api.github.com/repos/qadium-memex/linkalytics/statuses/{sha}","languages_url":"https://api.github.com/repos/qadium-memex/linkalytics/languages","stargazers_url":"https://api.github.com/repos/qadium-memex/linkalytics/stargazers","contributors_url":"https://api.github.com/repos/qadium-memex/linkalytics/contributors","subscribers_url":"https://api.github.com/repos/qadium-memex/linkalytics/subscribers","subscription_url":"https://api.github.com/repos/qadium-memex/linkalytics/subscription","commits_url":"https://api.github.com/repos/qadium-memex/linkalytics/commits{/sha}","git_commits_url":"https://api.github.com/repos/qadium-memex/linkalytics/git/commits{/sha}","comments_url":"https://api.github.com/repos/qadium-memex/linkalytics/comments{/number}","issue_comment_url":"https://api.github.com/repos/qadium-memex/linkalytics/issues/comments{/number}","contents_url":"https://api.github.com/repos/qadium-memex/linkalytics/contents/{+path}","compare_url":"https://api.github.com/repos/qadium-memex/linkalytics/compare/{base}...{head}","merges_url":"https://api.github.com/repos/qadium-memex/linkalytics/merges","archive_url":"https://api.github.com/repos/qadium-memex/linkalytics/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/qadium-memex/linkalytics/downloads","issues_url":"https://api.github.com/repos/qadium-memex/linkalytics/issues{/number}","pulls_url":"https://api.github.com/repos/qadium-memex/linkalytics/pulls{/number}","milestones_url":"https://api.github.com/repos/qadium-memex/linkalytics/milestones{/number}","notifications_url":"https://api.github.com/repos/qadium-memex/linkalytics/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/qadium-memex/linkalytics/labels{/name}","releases_url":"https://api.github.com/repos/qadium-memex/linkalytics/releases{/id}","created_at":"2015-09-14T22:49:38Z","updated_at":"2015-09-17T02:41:17Z","pushed_at":"2015-09-21T23:56:52Z","git_url":"git://github.com/qadium-memex/linkalytics.git","ssh_url":"git@github.com:qadium-memex/linkalytics.git","clone_url":"https://github.com/qadium-memex/linkalytics.git","svn_url":"https://github.com/qadium-memex/linkalytics","homepage":null,"size":264,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":1,"default_branch":"master"}},"base":{"label":"qadium-memex:master","ref":"master","sha":"3c25b15c33b0a9c777b6f59512cd2ca09d6dc454","user":{"login":"qadium-memex","id":14283725,"avatar_url":"https://avatars.githubusercontent.com/u/14283725?v=3","gravatar_id":"","url":"https://api.github.com/users/qadium-memex","html_url":"https://github.com/qadium-memex","followers_url":"https://api.github.com/users/qadium-memex/followers","following_url":"https://api.github.com/users/qadium-memex/following{/other_user}","gists_url":"https://api.github.com/users/qadium-memex/gists{/gist_id}","starred_url":"https://api.github.com/users/qadium-memex/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qadium-memex/subscriptions","organizations_url":"https://api.github.com/users/qadium-memex/orgs","repos_url":"https://api.github.com/users/qadium-memex/repos","events_url":"https://api.github.com/users/qadium-memex/events{/privacy}","received_events_url":"https://api.github.com/users/qadium-memex/received_events","type":"Organization","site_admin":false},"repo":{"id":42481423,"name":"linkalytics","full_name":"qadium-memex/linkalytics","owner":{"login":"qadium-memex","id":14283725,"avatar_url":"https://avatars.githubusercontent.com/u/14283725?v=3","gravatar_id":"","url":"https://api.github.com/users/qadium-memex","html_url":"https://github.com/qadium-memex","followers_url":"https://api.github.com/users/qadium-memex/followers","following_url":"https://api.github.com/users/qadium-memex/following{/other_user}","gists_url":"https://api.github.com/users/qadium-memex/gists{/gist_id}","starred_url":"https://api.github.com/users/qadium-memex/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qadium-memex/subscriptions","organizations_url":"https://api.github.com/users/qadium-memex/orgs","repos_url":"https://api.github.com/users/qadium-memex/repos","events_url":"https://api.github.com/users/qadium-memex/events{/privacy}","received_events_url":"https://api.github.com/users/qadium-memex/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/qadium-memex/linkalytics","description":"Source code for various analytics","fork":false,"url":"https://api.github.com/repos/qadium-memex/linkalytics","forks_url":"https://api.github.com/repos/qadium-memex/linkalytics/forks","keys_url":"https://api.github.com/repos/qadium-memex/linkalytics/keys{/key_id}","collaborators_url":"https://api.github.com/repos/qadium-memex/linkalytics/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/qadium-memex/linkalytics/teams","hooks_url":"https://api.github.com/repos/qadium-memex/linkalytics/hooks","issue_events_url":"https://api.github.com/repos/qadium-memex/linkalytics/issues/events{/number}","events_url":"https://api.github.com/repos/qadium-memex/linkalytics/events","assignees_url":"https://api.github.com/repos/qadium-memex/linkalytics/assignees{/user}","branches_url":"https://api.github.com/repos/qadium-memex/linkalytics/branches{/branch}","tags_url":"https://api.github.com/repos/qadium-memex/linkalytics/tags","blobs_url":"https://api.github.com/repos/qadium-memex/linkalytics/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/qadium-memex/linkalytics/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/qadium-memex/linkalytics/git/refs{/sha}","trees_url":"https://api.github.com/repos/qadium-memex/linkalytics/git/trees{/sha}","statuses_url":"https://api.github.com/repos/qadium-memex/linkalytics/statuses/{sha}","languages_url":"https://api.github.com/repos/qadium-memex/linkalytics/languages","stargazers_url":"https://api.github.com/repos/qadium-memex/linkalytics/stargazers","contributors_url":"https://api.github.com/repos/qadium-memex/linkalytics/contributors","subscribers_url":"https://api.github.com/repos/qadium-memex/linkalytics/subscribers","subscription_url":"https://api.github.com/repos/qadium-memex/linkalytics/subscription","commits_url":"https://api.github.com/repos/qadium-memex/linkalytics/commits{/sha}","git_commits_url":"https://api.github.com/repos/qadium-memex/linkalytics/git/commits{/sha}","comments_url":"https://api.github.com/repos/qadium-memex/linkalytics/comments{/number}","issue_comment_url":"https://api.github.com/repos/qadium-memex/linkalytics/issues/comments{/number}","contents_url":"https://api.github.com/repos/qadium-memex/linkalytics/contents/{+path}","compare_url":"https://api.github.com/repos/qadium-memex/linkalytics/compare/{base}...{head}","merges_url":"https://api.github.com/repos/qadium-memex/linkalytics/merges","archive_url":"https://api.github.com/repos/qadium-memex/linkalytics/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/qadium-memex/linkalytics/downloads","issues_url":"https://api.github.com/repos/qadium-memex/linkalytics/issues{/number}","pulls_url":"https://api.github.com/repos/qadium-memex/linkalytics/pulls{/number}","milestones_url":"https://api.github.com/repos/qadium-memex/linkalytics/milestones{/number}","notifications_url":"https://api.github.com/repos/qadium-memex/linkalytics/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/qadium-memex/linkalytics/labels{/name}","releases_url":"https://api.github.com/repos/qadium-memex/linkalytics/releases{/id}","created_at":"2015-09-14T22:49:38Z","updated_at":"2015-09-17T02:41:17Z","pushed_at":"2015-09-21T23:56:52Z","git_url":"git://github.com/qadium-memex/linkalytics.git","ssh_url":"git@github.com:qadium-memex/linkalytics.git","clone_url":"https://github.com/qadium-memex/linkalytics.git","svn_url":"https://github.com/qadium-memex/linkalytics","homepage":null,"size":264,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/qadium-memex/linkalytics/pulls/1"},"html":{"href":"https://github.com/qadium-memex/linkalytics/pull/1"},"issue":{"href":"https://api.github.com/repos/qadium-memex/linkalytics/issues/1"},"comments":{"href":"https://api.github.com/repos/qadium-memex/linkalytics/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/qadium-memex/linkalytics/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/qadium-memex/linkalytics/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/qadium-memex/linkalytics/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/qadium-memex/linkalytics/statuses/f10b6a97592d246044ebe8f96b1e782603149cc8"}}}}