{"action":"created","comment":{"url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls/comments/52078281","id":52078281,"diff_hunk":"@@ -0,0 +1,192 @@\n+from django.conf import settings\n+from django.core.paginator import Paginator\n+from django.db.models import Q\n+\n+from celery import shared_task\n+from celery.app.task import Task\n+from celery.utils.log import get_task_logger\n+\n+from data.models import (DedupeJob, RecordDuplicate)\n+from ashlar.models import Record\n+\n+import datetime\n+from pytz import timezone\n+\n+logger = get_task_logger(__name__)\n+\n+\n+class DedupeTask(Task):\n+    \"\"\"Base class for the deduplication task to handle updating status in database\"\"\"\n+\n+    def on_success(self, retval, task_id, args, kwargs):\n+        job = DedupeJob.objects.get(celery_task=task_id)\n+        job.status = DedupeJob.Status.SUCCESS\n+        job.save()\n+\n+    def on_failure(self, exc, task_id, args, kwargs, einfo):\n+        \"\"\" On failure, delete duplicates found in the unsuccessful run\"\"\"\n+        job = DedupeJob.objects.get(celery_task=task_id)\n+        job.status = DedupeJob.Status.ERROR\n+        job.save()\n+        RecordDuplicate.objects.filter(job=job).delete()\n+\n+\n+def calculate_similarity_score(record1, record2, time_allowance, distance_allowance):\n+    \"\"\"\n+    Set a score representing the likelyhood the two records are duplicated, based\n+    on the criteria.  score is a float between 0 and 1\n+\n+    normalized dimensions are considered equal (1 day equally as important as 100m)\n+\n+    If the records are more than time_allowance or distance_allowance apart, this will\n+    return a negative value\n+    \"\"\"\n+    tdelta = abs(record1.occurred_from - record2.occurred_from)\n+    tscore = 1.0 - (tdelta.total_seconds() / time_allowance.total_seconds())\n+    ddelta = record1.geom.distance(record2.geom)\n+    dscore = 1.0 - (ddelta / distance_allowance)\n+    score = (tscore + dscore) / 2\n+    return score\n+\n+\n+def find_duplicates_for_record(uuid, time_allowance, distance_allowance):\n+    \"\"\" Given a record, find all possible duplicates for it\n+    \"\"\"\n+    record = Record.objects.get(uuid=uuid)\n+    return Record.objects.filter(\n+        schema__record_type=record.schema.record_type,\n+        geom__dwithin=(record.geom, distance_allowance),\n+        occurred_from__range=(\n+            record.occurred_from - time_allowance, record.occurred_from + time_allowance\n+        )\n+    ).exclude(\n+        Q(uuid=record.uuid) | Q(\n+            uuid__in=RecordDuplicate.objects.filter(\n+                Q(record__uuid=record.uuid) | Q(\n+                    duplicate_record__uuid=record.uuid\n+                )\n+            )\n+        )\n+    )\n+\n+\n+def get_time_extent(job):\n+    \"\"\" Get the time range to dedupe\n+    Calculate from X hr before the last dedupe run to the current task run's\n+    start time, where X is the heuristic's time allowance\n+\n+    Returns:\n+        dictionary containing the start and end times for the next dedupe task\n+        {starttime: timestamp, endtime: timestamp}\n+    \"\"\"\n+    last_job = None\n+    if DedupeJob.objects.all().count() > 0:\n+        try:\n+            last_job = DedupeJob.objects.exclude(uuid=job.uuid).latest()\n+        except:","path":"app/data/tasks/deduplicate_records.py","position":86,"original_position":86,"commit_id":"59f3c5f1c0c9d5fa74ec19a2bef58e7cbbf998e8","original_commit_id":"59f3c5f1c0c9d5fa74ec19a2bef58e7cbbf998e8","user":{"login":"Lknechtli","id":4392704,"avatar_url":"https://avatars.githubusercontent.com/u/4392704?v=3","gravatar_id":"","url":"https://api.github.com/users/Lknechtli","html_url":"https://github.com/Lknechtli","followers_url":"https://api.github.com/users/Lknechtli/followers","following_url":"https://api.github.com/users/Lknechtli/following{/other_user}","gists_url":"https://api.github.com/users/Lknechtli/gists{/gist_id}","starred_url":"https://api.github.com/users/Lknechtli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Lknechtli/subscriptions","organizations_url":"https://api.github.com/users/Lknechtli/orgs","repos_url":"https://api.github.com/users/Lknechtli/repos","events_url":"https://api.github.com/users/Lknechtli/events{/privacy}","received_events_url":"https://api.github.com/users/Lknechtli/received_events","type":"User","site_admin":false},"body":"whups. I just need to ignore the case that the job isn't found.","created_at":"2016-02-05T21:57:35Z","updated_at":"2016-02-05T21:57:35Z","html_url":"https://github.com/WorldBank-Transport/DRIVER/pull/391#discussion_r52078281","pull_request_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls/391","_links":{"self":{"href":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls/comments/52078281"},"html":{"href":"https://github.com/WorldBank-Transport/DRIVER/pull/391#discussion_r52078281"},"pull_request":{"href":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls/391"}}},"pull_request":{"url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls/391","id":58209345,"html_url":"https://github.com/WorldBank-Transport/DRIVER/pull/391","diff_url":"https://github.com/WorldBank-Transport/DRIVER/pull/391.diff","patch_url":"https://github.com/WorldBank-Transport/DRIVER/pull/391.patch","issue_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/issues/391","number":391,"state":"open","locked":false,"title":"Add dedupe task","user":{"login":"Lknechtli","id":4392704,"avatar_url":"https://avatars.githubusercontent.com/u/4392704?v=3","gravatar_id":"","url":"https://api.github.com/users/Lknechtli","html_url":"https://github.com/Lknechtli","followers_url":"https://api.github.com/users/Lknechtli/followers","following_url":"https://api.github.com/users/Lknechtli/following{/other_user}","gists_url":"https://api.github.com/users/Lknechtli/gists{/gist_id}","starred_url":"https://api.github.com/users/Lknechtli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Lknechtli/subscriptions","organizations_url":"https://api.github.com/users/Lknechtli/orgs","repos_url":"https://api.github.com/users/Lknechtli/repos","events_url":"https://api.github.com/users/Lknechtli/events{/privacy}","received_events_url":"https://api.github.com/users/Lknechtli/received_events","type":"User","site_admin":false},"body":"Add migration for model change","created_at":"2016-02-03T22:12:53Z","updated_at":"2016-02-05T21:57:35Z","closed_at":null,"merged_at":null,"merge_commit_sha":"be7d8642778ab06317005a82f16083518686116f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls/391/commits","review_comments_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls/391/comments","review_comment_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls/comments{/number}","comments_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/issues/391/comments","statuses_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/statuses/59f3c5f1c0c9d5fa74ec19a2bef58e7cbbf998e8","head":{"label":"WorldBank-Transport:feature/dedupe-celery-task","ref":"feature/dedupe-celery-task","sha":"59f3c5f1c0c9d5fa74ec19a2bef58e7cbbf998e8","user":{"login":"WorldBank-Transport","id":5641611,"avatar_url":"https://avatars.githubusercontent.com/u/5641611?v=3","gravatar_id":"","url":"https://api.github.com/users/WorldBank-Transport","html_url":"https://github.com/WorldBank-Transport","followers_url":"https://api.github.com/users/WorldBank-Transport/followers","following_url":"https://api.github.com/users/WorldBank-Transport/following{/other_user}","gists_url":"https://api.github.com/users/WorldBank-Transport/gists{/gist_id}","starred_url":"https://api.github.com/users/WorldBank-Transport/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WorldBank-Transport/subscriptions","organizations_url":"https://api.github.com/users/WorldBank-Transport/orgs","repos_url":"https://api.github.com/users/WorldBank-Transport/repos","events_url":"https://api.github.com/users/WorldBank-Transport/events{/privacy}","received_events_url":"https://api.github.com/users/WorldBank-Transport/received_events","type":"Organization","site_admin":false},"repo":{"id":35051490,"name":"DRIVER","full_name":"WorldBank-Transport/DRIVER","owner":{"login":"WorldBank-Transport","id":5641611,"avatar_url":"https://avatars.githubusercontent.com/u/5641611?v=3","gravatar_id":"","url":"https://api.github.com/users/WorldBank-Transport","html_url":"https://github.com/WorldBank-Transport","followers_url":"https://api.github.com/users/WorldBank-Transport/followers","following_url":"https://api.github.com/users/WorldBank-Transport/following{/other_user}","gists_url":"https://api.github.com/users/WorldBank-Transport/gists{/gist_id}","starred_url":"https://api.github.com/users/WorldBank-Transport/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WorldBank-Transport/subscriptions","organizations_url":"https://api.github.com/users/WorldBank-Transport/orgs","repos_url":"https://api.github.com/users/WorldBank-Transport/repos","events_url":"https://api.github.com/users/WorldBank-Transport/events{/privacy}","received_events_url":"https://api.github.com/users/WorldBank-Transport/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/WorldBank-Transport/DRIVER","description":"DRIVER - Data for Road Incident Visualization, Evaluation, and Reporting ","fork":false,"url":"https://api.github.com/repos/WorldBank-Transport/DRIVER","forks_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/forks","keys_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/keys{/key_id}","collaborators_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/teams","hooks_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/hooks","issue_events_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/issues/events{/number}","events_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/events","assignees_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/assignees{/user}","branches_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/branches{/branch}","tags_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/tags","blobs_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/git/refs{/sha}","trees_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/git/trees{/sha}","statuses_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/statuses/{sha}","languages_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/languages","stargazers_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/stargazers","contributors_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/contributors","subscribers_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/subscribers","subscription_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/subscription","commits_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/commits{/sha}","git_commits_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/git/commits{/sha}","comments_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/comments{/number}","issue_comment_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/issues/comments{/number}","contents_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/contents/{+path}","compare_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/compare/{base}...{head}","merges_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/merges","archive_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/downloads","issues_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/issues{/number}","pulls_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls{/number}","milestones_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/milestones{/number}","notifications_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/labels{/name}","releases_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/releases{/id}","deployments_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/deployments","created_at":"2015-05-04T18:20:38Z","updated_at":"2016-01-11T15:34:37Z","pushed_at":"2016-02-05T21:50:27Z","git_url":"git://github.com/WorldBank-Transport/DRIVER.git","ssh_url":"git@github.com:WorldBank-Transport/DRIVER.git","clone_url":"https://github.com/WorldBank-Transport/DRIVER.git","svn_url":"https://github.com/WorldBank-Transport/DRIVER","homepage":null,"size":19853,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":26,"forks":3,"open_issues":26,"watchers":1,"default_branch":"develop"}},"base":{"label":"WorldBank-Transport:develop","ref":"develop","sha":"64e111cf550a3538eafba4cf31f7fd0a7e3f67d9","user":{"login":"WorldBank-Transport","id":5641611,"avatar_url":"https://avatars.githubusercontent.com/u/5641611?v=3","gravatar_id":"","url":"https://api.github.com/users/WorldBank-Transport","html_url":"https://github.com/WorldBank-Transport","followers_url":"https://api.github.com/users/WorldBank-Transport/followers","following_url":"https://api.github.com/users/WorldBank-Transport/following{/other_user}","gists_url":"https://api.github.com/users/WorldBank-Transport/gists{/gist_id}","starred_url":"https://api.github.com/users/WorldBank-Transport/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WorldBank-Transport/subscriptions","organizations_url":"https://api.github.com/users/WorldBank-Transport/orgs","repos_url":"https://api.github.com/users/WorldBank-Transport/repos","events_url":"https://api.github.com/users/WorldBank-Transport/events{/privacy}","received_events_url":"https://api.github.com/users/WorldBank-Transport/received_events","type":"Organization","site_admin":false},"repo":{"id":35051490,"name":"DRIVER","full_name":"WorldBank-Transport/DRIVER","owner":{"login":"WorldBank-Transport","id":5641611,"avatar_url":"https://avatars.githubusercontent.com/u/5641611?v=3","gravatar_id":"","url":"https://api.github.com/users/WorldBank-Transport","html_url":"https://github.com/WorldBank-Transport","followers_url":"https://api.github.com/users/WorldBank-Transport/followers","following_url":"https://api.github.com/users/WorldBank-Transport/following{/other_user}","gists_url":"https://api.github.com/users/WorldBank-Transport/gists{/gist_id}","starred_url":"https://api.github.com/users/WorldBank-Transport/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WorldBank-Transport/subscriptions","organizations_url":"https://api.github.com/users/WorldBank-Transport/orgs","repos_url":"https://api.github.com/users/WorldBank-Transport/repos","events_url":"https://api.github.com/users/WorldBank-Transport/events{/privacy}","received_events_url":"https://api.github.com/users/WorldBank-Transport/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/WorldBank-Transport/DRIVER","description":"DRIVER - Data for Road Incident Visualization, Evaluation, and Reporting ","fork":false,"url":"https://api.github.com/repos/WorldBank-Transport/DRIVER","forks_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/forks","keys_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/keys{/key_id}","collaborators_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/teams","hooks_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/hooks","issue_events_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/issues/events{/number}","events_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/events","assignees_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/assignees{/user}","branches_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/branches{/branch}","tags_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/tags","blobs_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/git/refs{/sha}","trees_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/git/trees{/sha}","statuses_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/statuses/{sha}","languages_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/languages","stargazers_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/stargazers","contributors_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/contributors","subscribers_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/subscribers","subscription_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/subscription","commits_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/commits{/sha}","git_commits_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/git/commits{/sha}","comments_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/comments{/number}","issue_comment_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/issues/comments{/number}","contents_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/contents/{+path}","compare_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/compare/{base}...{head}","merges_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/merges","archive_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/downloads","issues_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/issues{/number}","pulls_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls{/number}","milestones_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/milestones{/number}","notifications_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/labels{/name}","releases_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/releases{/id}","deployments_url":"https://api.github.com/repos/WorldBank-Transport/DRIVER/deployments","created_at":"2015-05-04T18:20:38Z","updated_at":"2016-01-11T15:34:37Z","pushed_at":"2016-02-05T21:50:27Z","git_url":"git://github.com/WorldBank-Transport/DRIVER.git","ssh_url":"git@github.com:WorldBank-Transport/DRIVER.git","clone_url":"https://github.com/WorldBank-Transport/DRIVER.git","svn_url":"https://github.com/WorldBank-Transport/DRIVER","homepage":null,"size":19853,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":26,"forks":3,"open_issues":26,"watchers":1,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls/391"},"html":{"href":"https://github.com/WorldBank-Transport/DRIVER/pull/391"},"issue":{"href":"https://api.github.com/repos/WorldBank-Transport/DRIVER/issues/391"},"comments":{"href":"https://api.github.com/repos/WorldBank-Transport/DRIVER/issues/391/comments"},"review_comments":{"href":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls/391/comments"},"review_comment":{"href":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/WorldBank-Transport/DRIVER/pulls/391/commits"},"statuses":{"href":"https://api.github.com/repos/WorldBank-Transport/DRIVER/statuses/59f3c5f1c0c9d5fa74ec19a2bef58e7cbbf998e8"}}}}