{"action":"created","comment":{"url":"https://api.github.com/repos/CanalTP/kirin/pulls/comments/43142878","id":43142878,"diff_hunk":"@@ -70,36 +70,97 @@ def handle(real_time_update, trip_updates, contributor):\n     return real_time_update\n \n \n-def merge(trip_update, old_trip_update):\n-    if old_trip_update:\n-        old_trip_update.merge(trip_update)\n-        current = old_trip_update\n-    else:\n-        current = trip_update\n-        merge_realtime_theoric(current, trip_update.vj.navitia_vj)\n-    if current.status == 'delete':\n-        current.stop_time_updates = []\n-    return current\n+def merge(navitia_vj, db_trip_update, new_trip_update):\n+    \"\"\"\n+    We need to merge the info from 3 sources:\n+        * the navitia base schedule\n+        * the trip update already in the bd (potentially not existent)\n+        * the incoming trip update\n+\n+    The result is either the db_trip_update if it exists, or the new_trip_update (it is updated as a side\n+    effect)\n+\n+    The mechanism is quite simple:\n+        * the result trip status is the new_trip_update's status\n+            (ie in the db the trip was cancelled, and a new update is only an update, the trip update is\n+            not cancelled anymore, only updated)\n+\n+        * for each navitia's stop_time and for departure|arrival:\n+            - if there is an update on this stoptime (in new_trip_update):\n+                we compute the new datetime based on the new information and the navitia's base schedule\n+            - else if there if the stoptime in the db:\n+                we keep this db stoptime\n+            - else we keep the navitia's base schedule\n+\n+    ** Important Note **:\n+    we DO NOT HANDLE changes in navitia's schedule for the moment\n+    it will need to be handled, but it will be done after\n+    \"\"\"\n+    res = db_trip_update if db_trip_update else new_trip_update\n+    res_stoptime_updates = []\n+\n+    res.status = new_trip_update.status\n+    if new_trip_update.message:\n+        res.message = new_trip_update.message\n+    res.contributor = res.contributor\n \n+    if res.status == 'delete':\n+        # for trip cancellation, we delete all stoptimes update\n+        res.stop_time_updates = []\n+        return res\n \n-def merge_realtime_theoric(trip_update, navitia_vj):\n-    for idx, navitia_stop in enumerate(navitia_vj.get('stop_times', [])):\n+    for navitia_stop in navitia_vj.get('stop_times', []):\n         stop_id = navitia_stop.get('stop_point', {}).get('id')\n-        stop = trip_update.find_stop(stop_id)\n-        #TODO: order is important...\n-        if not stop:\n-            departure_time = navitia_stop.get('departure_time')\n-            arrival_time = navitia_stop.get('arrival_time')\n-            departure = arrival = None\n-            #TODO handle past midnigth\n-            if departure_time:\n-                departure = datetime.datetime.combine(trip_update.vj.circulation_date, departure_time)\n-            if arrival_time:\n-                arrival = datetime.datetime.combine(trip_update.vj.circulation_date, arrival_time)\n-\n-            st = StopTimeUpdate(navitia_stop['stop_point'], departure, arrival)\n-            #we want to keep the order\n-            trip_update.stop_time_updates.insert(idx, st)\n+        new_st = new_trip_update.find_stop(stop_id)\n+        db_st = db_trip_update.find_stop(stop_id) if db_trip_update else None\n+\n+        # TODO handle forbidden pickup/dropoff (in those case set departure/arrival at None)\n+        nav_departure_time = navitia_stop.get('departure_time')\n+        nav_arrival_time = navitia_stop.get('arrival_time')\n+\n+        departure = arrival = None\n+        if nav_departure_time:\n+            departure = datetime.datetime.combine(new_trip_update.vj.circulation_date, nav_departure_time)\n+        if nav_arrival_time:\n+            arrival = datetime.datetime.combine(new_trip_update.vj.circulation_date, nav_arrival_time)\n+\n+        #TODO handle past midnight","path":"kirin/core/handler.py","position":94,"original_position":94,"commit_id":"7ad56513a4872baa4c3252c29658a8e4645a46cc","original_commit_id":"7ad56513a4872baa4c3252c29658a8e4645a46cc","user":{"login":"TeXitoi","id":5787066,"avatar_url":"https://avatars.githubusercontent.com/u/5787066?v=3","gravatar_id":"","url":"https://api.github.com/users/TeXitoi","html_url":"https://github.com/TeXitoi","followers_url":"https://api.github.com/users/TeXitoi/followers","following_url":"https://api.github.com/users/TeXitoi/following{/other_user}","gists_url":"https://api.github.com/users/TeXitoi/gists{/gist_id}","starred_url":"https://api.github.com/users/TeXitoi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TeXitoi/subscriptions","organizations_url":"https://api.github.com/users/TeXitoi/orgs","repos_url":"https://api.github.com/users/TeXitoi/repos","events_url":"https://api.github.com/users/TeXitoi/events{/privacy}","received_events_url":"https://api.github.com/users/TeXitoi/received_events","type":"User","site_admin":false},"body":"Doesn't it works out of the box ?","created_at":"2015-10-27T16:01:44Z","updated_at":"2015-10-27T16:01:44Z","html_url":"https://github.com/CanalTP/kirin/pull/46#discussion_r43142878","pull_request_url":"https://api.github.com/repos/CanalTP/kirin/pulls/46","_links":{"self":{"href":"https://api.github.com/repos/CanalTP/kirin/pulls/comments/43142878"},"html":{"href":"https://github.com/CanalTP/kirin/pull/46#discussion_r43142878"},"pull_request":{"href":"https://api.github.com/repos/CanalTP/kirin/pulls/46"}}},"pull_request":{"url":"https://api.github.com/repos/CanalTP/kirin/pulls/46","id":48771696,"html_url":"https://github.com/CanalTP/kirin/pull/46","diff_url":"https://github.com/CanalTP/kirin/pull/46.diff","patch_url":"https://github.com/CanalTP/kirin/pull/46.patch","issue_url":"https://api.github.com/repos/CanalTP/kirin/issues/46","number":46,"state":"open","locked":false,"title":"Delay handling","user":{"login":"antoine-de","id":3987698,"avatar_url":"https://avatars.githubusercontent.com/u/3987698?v=3","gravatar_id":"","url":"https://api.github.com/users/antoine-de","html_url":"https://github.com/antoine-de","followers_url":"https://api.github.com/users/antoine-de/followers","following_url":"https://api.github.com/users/antoine-de/following{/other_user}","gists_url":"https://api.github.com/users/antoine-de/gists{/gist_id}","starred_url":"https://api.github.com/users/antoine-de/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/antoine-de/subscriptions","organizations_url":"https://api.github.com/users/antoine-de/orgs","repos_url":"https://api.github.com/users/antoine-de/repos","events_url":"https://api.github.com/users/antoine-de/events{/privacy}","received_events_url":"https://api.github.com/users/antoine-de/received_events","type":"User","site_admin":false},"body":"Handle delay on StopTimeUpdates\r\n\r\nrefactoring of the handle.merge to make it works.\r\n\r\nWe need to merge the info from 3 sources:\r\n* the navitia base schedule\r\n* the trip update already in the bd (potentially not existent)\r\n* the incoming trip update\r\nThe result is either the db_trip_update if it exists, or the new_trip_update (it is updated as a side\r\neffect)\r\nThe mechanism is quite simple:\r\n* the result trip status is the new_trip_update's status\r\n        (ie in the db the trip was cancelled, and a new update is only an update, the trip update is\r\n        not cancelled anymore, only updated)\r\n* for each navitia's stop_time and for departure|arrival:\r\n    - if there is an update on this stoptime (in new_trip_update):\r\n            we compute the new datetime based on the new information and the navitia's base schedule\r\n    - else if there if the stoptime in the db:\r\n            we keep this db stoptime\r\n    - else we keep the navitia's base schedule\r\n\r\n**Important Note**:\r\nwe DO NOT HANDLE changes in navitia's schedule for the moment\r\nit will need to be handled, but it will be done after\r\n\r\nfix http://jira.canaltp.fr/browse/NAVP-354","created_at":"2015-10-26T17:35:57Z","updated_at":"2015-10-27T16:01:44Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ee46f2e1372e12971734ac7415b5bb160b0f8d41","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/CanalTP/kirin/pulls/46/commits","review_comments_url":"https://api.github.com/repos/CanalTP/kirin/pulls/46/comments","review_comment_url":"https://api.github.com/repos/CanalTP/kirin/pulls/comments{/number}","comments_url":"https://api.github.com/repos/CanalTP/kirin/issues/46/comments","statuses_url":"https://api.github.com/repos/CanalTP/kirin/statuses/7ad56513a4872baa4c3252c29658a8e4645a46cc","head":{"label":"antoine-de:merge_delay","ref":"merge_delay","sha":"7ad56513a4872baa4c3252c29658a8e4645a46cc","user":{"login":"antoine-de","id":3987698,"avatar_url":"https://avatars.githubusercontent.com/u/3987698?v=3","gravatar_id":"","url":"https://api.github.com/users/antoine-de","html_url":"https://github.com/antoine-de","followers_url":"https://api.github.com/users/antoine-de/followers","following_url":"https://api.github.com/users/antoine-de/following{/other_user}","gists_url":"https://api.github.com/users/antoine-de/gists{/gist_id}","starred_url":"https://api.github.com/users/antoine-de/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/antoine-de/subscriptions","organizations_url":"https://api.github.com/users/antoine-de/orgs","repos_url":"https://api.github.com/users/antoine-de/repos","events_url":"https://api.github.com/users/antoine-de/events{/privacy}","received_events_url":"https://api.github.com/users/antoine-de/received_events","type":"User","site_admin":false},"repo":{"id":43301704,"name":"kirin","full_name":"antoine-de/kirin","owner":{"login":"antoine-de","id":3987698,"avatar_url":"https://avatars.githubusercontent.com/u/3987698?v=3","gravatar_id":"","url":"https://api.github.com/users/antoine-de","html_url":"https://github.com/antoine-de","followers_url":"https://api.github.com/users/antoine-de/followers","following_url":"https://api.github.com/users/antoine-de/following{/other_user}","gists_url":"https://api.github.com/users/antoine-de/gists{/gist_id}","starred_url":"https://api.github.com/users/antoine-de/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/antoine-de/subscriptions","organizations_url":"https://api.github.com/users/antoine-de/orgs","repos_url":"https://api.github.com/users/antoine-de/repos","events_url":"https://api.github.com/users/antoine-de/events{/privacy}","received_events_url":"https://api.github.com/users/antoine-de/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/antoine-de/kirin","description":"The magical unicorn","fork":true,"url":"https://api.github.com/repos/antoine-de/kirin","forks_url":"https://api.github.com/repos/antoine-de/kirin/forks","keys_url":"https://api.github.com/repos/antoine-de/kirin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/antoine-de/kirin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/antoine-de/kirin/teams","hooks_url":"https://api.github.com/repos/antoine-de/kirin/hooks","issue_events_url":"https://api.github.com/repos/antoine-de/kirin/issues/events{/number}","events_url":"https://api.github.com/repos/antoine-de/kirin/events","assignees_url":"https://api.github.com/repos/antoine-de/kirin/assignees{/user}","branches_url":"https://api.github.com/repos/antoine-de/kirin/branches{/branch}","tags_url":"https://api.github.com/repos/antoine-de/kirin/tags","blobs_url":"https://api.github.com/repos/antoine-de/kirin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/antoine-de/kirin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/antoine-de/kirin/git/refs{/sha}","trees_url":"https://api.github.com/repos/antoine-de/kirin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/antoine-de/kirin/statuses/{sha}","languages_url":"https://api.github.com/repos/antoine-de/kirin/languages","stargazers_url":"https://api.github.com/repos/antoine-de/kirin/stargazers","contributors_url":"https://api.github.com/repos/antoine-de/kirin/contributors","subscribers_url":"https://api.github.com/repos/antoine-de/kirin/subscribers","subscription_url":"https://api.github.com/repos/antoine-de/kirin/subscription","commits_url":"https://api.github.com/repos/antoine-de/kirin/commits{/sha}","git_commits_url":"https://api.github.com/repos/antoine-de/kirin/git/commits{/sha}","comments_url":"https://api.github.com/repos/antoine-de/kirin/comments{/number}","issue_comment_url":"https://api.github.com/repos/antoine-de/kirin/issues/comments{/number}","contents_url":"https://api.github.com/repos/antoine-de/kirin/contents/{+path}","compare_url":"https://api.github.com/repos/antoine-de/kirin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/antoine-de/kirin/merges","archive_url":"https://api.github.com/repos/antoine-de/kirin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/antoine-de/kirin/downloads","issues_url":"https://api.github.com/repos/antoine-de/kirin/issues{/number}","pulls_url":"https://api.github.com/repos/antoine-de/kirin/pulls{/number}","milestones_url":"https://api.github.com/repos/antoine-de/kirin/milestones{/number}","notifications_url":"https://api.github.com/repos/antoine-de/kirin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/antoine-de/kirin/labels{/name}","releases_url":"https://api.github.com/repos/antoine-de/kirin/releases{/id}","created_at":"2015-09-28T13:12:08Z","updated_at":"2015-09-28T13:12:09Z","pushed_at":"2015-10-27T14:25:24Z","git_url":"git://github.com/antoine-de/kirin.git","ssh_url":"git@github.com:antoine-de/kirin.git","clone_url":"https://github.com/antoine-de/kirin.git","svn_url":"https://github.com/antoine-de/kirin","homepage":null,"size":319,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"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":"CanalTP:master","ref":"master","sha":"366c2c75a8024c26650d68189f238d0c40b0bd42","user":{"login":"CanalTP","id":2743985,"avatar_url":"https://avatars.githubusercontent.com/u/2743985?v=3","gravatar_id":"","url":"https://api.github.com/users/CanalTP","html_url":"https://github.com/CanalTP","followers_url":"https://api.github.com/users/CanalTP/followers","following_url":"https://api.github.com/users/CanalTP/following{/other_user}","gists_url":"https://api.github.com/users/CanalTP/gists{/gist_id}","starred_url":"https://api.github.com/users/CanalTP/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CanalTP/subscriptions","organizations_url":"https://api.github.com/users/CanalTP/orgs","repos_url":"https://api.github.com/users/CanalTP/repos","events_url":"https://api.github.com/users/CanalTP/events{/privacy}","received_events_url":"https://api.github.com/users/CanalTP/received_events","type":"Organization","site_admin":false},"repo":{"id":39950201,"name":"kirin","full_name":"CanalTP/kirin","owner":{"login":"CanalTP","id":2743985,"avatar_url":"https://avatars.githubusercontent.com/u/2743985?v=3","gravatar_id":"","url":"https://api.github.com/users/CanalTP","html_url":"https://github.com/CanalTP","followers_url":"https://api.github.com/users/CanalTP/followers","following_url":"https://api.github.com/users/CanalTP/following{/other_user}","gists_url":"https://api.github.com/users/CanalTP/gists{/gist_id}","starred_url":"https://api.github.com/users/CanalTP/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CanalTP/subscriptions","organizations_url":"https://api.github.com/users/CanalTP/orgs","repos_url":"https://api.github.com/users/CanalTP/repos","events_url":"https://api.github.com/users/CanalTP/events{/privacy}","received_events_url":"https://api.github.com/users/CanalTP/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/CanalTP/kirin","description":"The magical unicorn","fork":false,"url":"https://api.github.com/repos/CanalTP/kirin","forks_url":"https://api.github.com/repos/CanalTP/kirin/forks","keys_url":"https://api.github.com/repos/CanalTP/kirin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CanalTP/kirin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CanalTP/kirin/teams","hooks_url":"https://api.github.com/repos/CanalTP/kirin/hooks","issue_events_url":"https://api.github.com/repos/CanalTP/kirin/issues/events{/number}","events_url":"https://api.github.com/repos/CanalTP/kirin/events","assignees_url":"https://api.github.com/repos/CanalTP/kirin/assignees{/user}","branches_url":"https://api.github.com/repos/CanalTP/kirin/branches{/branch}","tags_url":"https://api.github.com/repos/CanalTP/kirin/tags","blobs_url":"https://api.github.com/repos/CanalTP/kirin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CanalTP/kirin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CanalTP/kirin/git/refs{/sha}","trees_url":"https://api.github.com/repos/CanalTP/kirin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CanalTP/kirin/statuses/{sha}","languages_url":"https://api.github.com/repos/CanalTP/kirin/languages","stargazers_url":"https://api.github.com/repos/CanalTP/kirin/stargazers","contributors_url":"https://api.github.com/repos/CanalTP/kirin/contributors","subscribers_url":"https://api.github.com/repos/CanalTP/kirin/subscribers","subscription_url":"https://api.github.com/repos/CanalTP/kirin/subscription","commits_url":"https://api.github.com/repos/CanalTP/kirin/commits{/sha}","git_commits_url":"https://api.github.com/repos/CanalTP/kirin/git/commits{/sha}","comments_url":"https://api.github.com/repos/CanalTP/kirin/comments{/number}","issue_comment_url":"https://api.github.com/repos/CanalTP/kirin/issues/comments{/number}","contents_url":"https://api.github.com/repos/CanalTP/kirin/contents/{+path}","compare_url":"https://api.github.com/repos/CanalTP/kirin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CanalTP/kirin/merges","archive_url":"https://api.github.com/repos/CanalTP/kirin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CanalTP/kirin/downloads","issues_url":"https://api.github.com/repos/CanalTP/kirin/issues{/number}","pulls_url":"https://api.github.com/repos/CanalTP/kirin/pulls{/number}","milestones_url":"https://api.github.com/repos/CanalTP/kirin/milestones{/number}","notifications_url":"https://api.github.com/repos/CanalTP/kirin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CanalTP/kirin/labels{/name}","releases_url":"https://api.github.com/repos/CanalTP/kirin/releases{/id}","created_at":"2015-07-30T12:48:49Z","updated_at":"2015-09-28T12:43:43Z","pushed_at":"2015-10-27T14:25:24Z","git_url":"git://github.com/CanalTP/kirin.git","ssh_url":"git@github.com:CanalTP/kirin.git","clone_url":"https://github.com/CanalTP/kirin.git","svn_url":"https://github.com/CanalTP/kirin","homepage":null,"size":736,"stargazers_count":2,"watchers_count":2,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5,"mirror_url":null,"open_issues_count":2,"forks":5,"open_issues":2,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/CanalTP/kirin/pulls/46"},"html":{"href":"https://github.com/CanalTP/kirin/pull/46"},"issue":{"href":"https://api.github.com/repos/CanalTP/kirin/issues/46"},"comments":{"href":"https://api.github.com/repos/CanalTP/kirin/issues/46/comments"},"review_comments":{"href":"https://api.github.com/repos/CanalTP/kirin/pulls/46/comments"},"review_comment":{"href":"https://api.github.com/repos/CanalTP/kirin/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/CanalTP/kirin/pulls/46/commits"},"statuses":{"href":"https://api.github.com/repos/CanalTP/kirin/statuses/7ad56513a4872baa4c3252c29658a8e4645a46cc"}}}}