{"action":"created","comment":{"url":"https://api.github.com/repos/unicef/carrotjuice2/pulls/comments/55932389","id":55932389,"diff_hunk":"@@ -0,0 +1,143 @@\n+/**\n+ * Stores epidemiological/case data.\n+ *\n+ * NOTE! This is a super hyper rough sketch. Data is *not* fetched from the\n+ * backend, but just hardcoded for now. We still need to decide on a reasonable\n+ * schema to account for temporal and spatial spottiness and inconsistencies.\n+ */\n+\n+var _ = require('lodash');\n+var P = require('pjs').P;\n+var Q = require('q');\n+\n+var DateUtil = require('./date-util.js');\n+\n+var utc_date = function(year, month, day) {\n+  // Note: months are zero-indexed.\n+  return new Date(Date.UTC(year, month - 1, day));\n+};\n+\n+// TODO(jetpack): screaming for unit tests.\n+\n+var EpiDataStore = P({\n+  init: function(on_update) {\n+    this.on_update = on_update;\n+\n+    // `data_by_date_interval_and_region` format is an array of objects that looks\n+    // like this:\n+    // {start_time: <Date>, end_time: <Date>, region_case_data: <region case data>}\n+    //\n+    // The array is sorted by `start_time`. The times are all at midnight UTC.\n+    // The time interval is half-open: [start_time, end_time). So, with\n+    // start_time = 2016-01-01 and end_time = 2016-01-08, the interval contains\n+    // data for 2016-01-01 up to and including 2016-01-07.\n+    //\n+    // Region case data is a mapping from region code -> condition ->\n+    // culmulative case count. For example:\n+    // {br1: {dengue: 120, malaria: 110, zika: 100},\n+    //  br2: {dengue: 220, malaria: 210},\n+    //  br3: {malaria: 310, zika: 300}}\n+    this.data_by_date_interval_and_region = [];\n+    this.most_recent_start_time = null;\n+\n+    this.initial_load_promise = Q.delay(10).then((function() {\n+      // 4589 is Iguape, 4611 is Itanhaém, and 4877 is São Paulo.\n+      this.data_by_date_interval_and_region = [\n+        // Feb 1 - Feb 8: Missing malaria data for 4589.\n+        {start_time: utc_date(2016, 2, 1), end_time: utc_date(2016, 2, 8),\n+         region_case_data: {4589: {fake_dengue: 111},\n+                            4611: {fake_dengue: 121, fake_zika: 122},\n+                            4877: {fake_dengue: 1131, fake_zika: 1132}}\n+        },\n+        // Feb 8 - Feb 15: Missing dengue data for 4611. Also, 4877 has chikungunya data.\n+        {start_time: utc_date(2016, 2, 8), end_time: utc_date(2016, 2, 15),\n+         region_case_data: {4589: {fake_dengue: 211, fake_zika: 212},\n+                            4611: {fake_zika: 222},\n+                            4877: {fake_dengue: 1231, fake_zika: 1232, fake_chikungunya: 1233}}\n+        },\n+        // Feb 15 - Feb 29: Only has data for 4611. 2 weeks.\n+        {start_time: utc_date(2016, 2, 15), end_time: utc_date(2016, 2, 29),\n+         region_case_data: {4611: {fake_dengue: 321, fake_zika: 322}}\n+        },\n+        // Feb 15 - Mar 8: Only has data fro 4589 and 4877. Overlaps with previous time span,\n+        // but longer.\n+        {start_time: utc_date(2016, 2, 15), end_time: utc_date(2016, 3, 8),\n+         region_case_data: {4589: {fake_dengue: 311, fake_zika: 312},\n+                            4877: {fake_dengue: 1331, fake_zika: 1332}}\n+        }\n+      ];\n+      this.most_recent_start_time = _.last(this.data_by_date_interval_and_region).start_time;\n+    }).bind(this));\n+  },\n+\n+  // Return a recent epi data record. If there are multiple records that include\n+  // the given `date`, returns the one that has data on the most regions. If\n+  // there are no records that include `date`, returns the most recent record\n+  // that occurred before the date.\n+  get_best_recent_epi_data: function(date) {\n+    // TODO(jetpack): binary search instead of linear scan.\n+    var matching_records = _.filter(this.data_by_date_interval_and_region, function(record) {\n+      return record.start_time <= date && date < record.end_time;\n+    });\n+    if (matching_records.length) {\n+      console.log('EDS GBRED matching records:', matching_records);\n+      var best_record = _.reduce(matching_records, function(best_record, record) {\n+        return (_.size(record.region_case_data) > _.size(best_record.region_case_data)) ?\n+          record : best_record;\n+      });\n+      return best_record;\n+    }\n+    // No epi records contain `date`. Find the record with the most recent\n+    // `start_time` that occurred before the date.\n+    // TODO(jetpack): binary search instead of linear scan.\n+    console.log('EpiDataStore doesnt have matching data for', date,\n+                ', just looking for most recent data now..');\n+    return _.findLast(this.data_by_date_interval_and_region, function(record) {\n+      console.log('testing if', record.start_time, 'older than', date, 'for record', record);\n+      return record.start_time < date;\n+    });\n+  },\n+\n+  get_recent_epi_data_for_region: function(date, region_code) {\n+    // TODO(jetpack): binary search instead of linear scan.","path":"public/model/epi-data-store.js","position":102,"original_position":102,"commit_id":"8d1a20e437097eaa8712dba4b62ab8db0e121f9c","original_commit_id":"8d1a20e437097eaa8712dba4b62ab8db0e121f9c","user":{"login":"jetpack","id":8691736,"avatar_url":"https://avatars.githubusercontent.com/u/8691736?v=3","gravatar_id":"","url":"https://api.github.com/users/jetpack","html_url":"https://github.com/jetpack","followers_url":"https://api.github.com/users/jetpack/followers","following_url":"https://api.github.com/users/jetpack/following{/other_user}","gists_url":"https://api.github.com/users/jetpack/gists{/gist_id}","starred_url":"https://api.github.com/users/jetpack/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jetpack/subscriptions","organizations_url":"https://api.github.com/users/jetpack/orgs","repos_url":"https://api.github.com/users/jetpack/repos","events_url":"https://api.github.com/users/jetpack/events{/privacy}","received_events_url":"https://api.github.com/users/jetpack/received_events","type":"User","site_admin":false},"body":"This will probably hold for quite a while, since data will almost certainly be less frequent than 1/week. But good point.","created_at":"2016-03-13T05:27:46Z","updated_at":"2016-03-13T05:27:46Z","html_url":"https://github.com/unicef/carrotjuice2/pull/35#discussion_r55932389","pull_request_url":"https://api.github.com/repos/unicef/carrotjuice2/pulls/35","_links":{"self":{"href":"https://api.github.com/repos/unicef/carrotjuice2/pulls/comments/55932389"},"html":{"href":"https://github.com/unicef/carrotjuice2/pull/35#discussion_r55932389"},"pull_request":{"href":"https://api.github.com/repos/unicef/carrotjuice2/pulls/35"}}},"pull_request":{"url":"https://api.github.com/repos/unicef/carrotjuice2/pulls/35","id":62659691,"html_url":"https://github.com/unicef/carrotjuice2/pull/35","diff_url":"https://github.com/unicef/carrotjuice2/pull/35.diff","patch_url":"https://github.com/unicef/carrotjuice2/pull/35.patch","issue_url":"https://api.github.com/repos/unicef/carrotjuice2/issues/35","number":35,"state":"open","locked":false,"title":"Add epi data store and display.","user":{"login":"jetpack","id":8691736,"avatar_url":"https://avatars.githubusercontent.com/u/8691736?v=3","gravatar_id":"","url":"https://api.github.com/users/jetpack","html_url":"https://github.com/jetpack","followers_url":"https://api.github.com/users/jetpack/followers","following_url":"https://api.github.com/users/jetpack/following{/other_user}","gists_url":"https://api.github.com/users/jetpack/gists{/gist_id}","starred_url":"https://api.github.com/users/jetpack/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jetpack/subscriptions","organizations_url":"https://api.github.com/users/jetpack/orgs","repos_url":"https://api.github.com/users/jetpack/repos","events_url":"https://api.github.com/users/jetpack/events{/privacy}","received_events_url":"https://api.github.com/users/jetpack/received_events","type":"User","site_admin":false},"body":"Currently, just has hardcoded test data.","created_at":"2016-03-13T02:45:04Z","updated_at":"2016-03-13T05:27:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"07fe78cf8d19827c99a072369442216fd9fb9f56","assignee":{"login":"gatoatigrado","id":95724,"avatar_url":"https://avatars.githubusercontent.com/u/95724?v=3","gravatar_id":"","url":"https://api.github.com/users/gatoatigrado","html_url":"https://github.com/gatoatigrado","followers_url":"https://api.github.com/users/gatoatigrado/followers","following_url":"https://api.github.com/users/gatoatigrado/following{/other_user}","gists_url":"https://api.github.com/users/gatoatigrado/gists{/gist_id}","starred_url":"https://api.github.com/users/gatoatigrado/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gatoatigrado/subscriptions","organizations_url":"https://api.github.com/users/gatoatigrado/orgs","repos_url":"https://api.github.com/users/gatoatigrado/repos","events_url":"https://api.github.com/users/gatoatigrado/events{/privacy}","received_events_url":"https://api.github.com/users/gatoatigrado/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/unicef/carrotjuice2/pulls/35/commits","review_comments_url":"https://api.github.com/repos/unicef/carrotjuice2/pulls/35/comments","review_comment_url":"https://api.github.com/repos/unicef/carrotjuice2/pulls/comments{/number}","comments_url":"https://api.github.com/repos/unicef/carrotjuice2/issues/35/comments","statuses_url":"https://api.github.com/repos/unicef/carrotjuice2/statuses/8d1a20e437097eaa8712dba4b62ab8db0e121f9c","head":{"label":"unicef:jetpack-epihack2","ref":"jetpack-epihack2","sha":"8d1a20e437097eaa8712dba4b62ab8db0e121f9c","user":{"login":"unicef","id":175849,"avatar_url":"https://avatars.githubusercontent.com/u/175849?v=3","gravatar_id":"","url":"https://api.github.com/users/unicef","html_url":"https://github.com/unicef","followers_url":"https://api.github.com/users/unicef/followers","following_url":"https://api.github.com/users/unicef/following{/other_user}","gists_url":"https://api.github.com/users/unicef/gists{/gist_id}","starred_url":"https://api.github.com/users/unicef/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/unicef/subscriptions","organizations_url":"https://api.github.com/users/unicef/orgs","repos_url":"https://api.github.com/users/unicef/repos","events_url":"https://api.github.com/users/unicef/events{/privacy}","received_events_url":"https://api.github.com/users/unicef/received_events","type":"Organization","site_admin":false},"repo":{"id":52188236,"name":"carrotjuice2","full_name":"unicef/carrotjuice2","owner":{"login":"unicef","id":175849,"avatar_url":"https://avatars.githubusercontent.com/u/175849?v=3","gravatar_id":"","url":"https://api.github.com/users/unicef","html_url":"https://github.com/unicef","followers_url":"https://api.github.com/users/unicef/followers","following_url":"https://api.github.com/users/unicef/following{/other_user}","gists_url":"https://api.github.com/users/unicef/gists{/gist_id}","starred_url":"https://api.github.com/users/unicef/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/unicef/subscriptions","organizations_url":"https://api.github.com/users/unicef/orgs","repos_url":"https://api.github.com/users/unicef/repos","events_url":"https://api.github.com/users/unicef/events{/privacy}","received_events_url":"https://api.github.com/users/unicef/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/unicef/carrotjuice2","description":"Front end to resources repo. Defaults to Brazil but will eventually be virus and country agnostic.","fork":false,"url":"https://api.github.com/repos/unicef/carrotjuice2","forks_url":"https://api.github.com/repos/unicef/carrotjuice2/forks","keys_url":"https://api.github.com/repos/unicef/carrotjuice2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/unicef/carrotjuice2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/unicef/carrotjuice2/teams","hooks_url":"https://api.github.com/repos/unicef/carrotjuice2/hooks","issue_events_url":"https://api.github.com/repos/unicef/carrotjuice2/issues/events{/number}","events_url":"https://api.github.com/repos/unicef/carrotjuice2/events","assignees_url":"https://api.github.com/repos/unicef/carrotjuice2/assignees{/user}","branches_url":"https://api.github.com/repos/unicef/carrotjuice2/branches{/branch}","tags_url":"https://api.github.com/repos/unicef/carrotjuice2/tags","blobs_url":"https://api.github.com/repos/unicef/carrotjuice2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/unicef/carrotjuice2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/unicef/carrotjuice2/git/refs{/sha}","trees_url":"https://api.github.com/repos/unicef/carrotjuice2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/unicef/carrotjuice2/statuses/{sha}","languages_url":"https://api.github.com/repos/unicef/carrotjuice2/languages","stargazers_url":"https://api.github.com/repos/unicef/carrotjuice2/stargazers","contributors_url":"https://api.github.com/repos/unicef/carrotjuice2/contributors","subscribers_url":"https://api.github.com/repos/unicef/carrotjuice2/subscribers","subscription_url":"https://api.github.com/repos/unicef/carrotjuice2/subscription","commits_url":"https://api.github.com/repos/unicef/carrotjuice2/commits{/sha}","git_commits_url":"https://api.github.com/repos/unicef/carrotjuice2/git/commits{/sha}","comments_url":"https://api.github.com/repos/unicef/carrotjuice2/comments{/number}","issue_comment_url":"https://api.github.com/repos/unicef/carrotjuice2/issues/comments{/number}","contents_url":"https://api.github.com/repos/unicef/carrotjuice2/contents/{+path}","compare_url":"https://api.github.com/repos/unicef/carrotjuice2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/unicef/carrotjuice2/merges","archive_url":"https://api.github.com/repos/unicef/carrotjuice2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/unicef/carrotjuice2/downloads","issues_url":"https://api.github.com/repos/unicef/carrotjuice2/issues{/number}","pulls_url":"https://api.github.com/repos/unicef/carrotjuice2/pulls{/number}","milestones_url":"https://api.github.com/repos/unicef/carrotjuice2/milestones{/number}","notifications_url":"https://api.github.com/repos/unicef/carrotjuice2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/unicef/carrotjuice2/labels{/name}","releases_url":"https://api.github.com/repos/unicef/carrotjuice2/releases{/id}","deployments_url":"https://api.github.com/repos/unicef/carrotjuice2/deployments","created_at":"2016-02-21T03:08:24Z","updated_at":"2016-03-11T22:59:42Z","pushed_at":"2016-03-13T04:50:32Z","git_url":"git://github.com/unicef/carrotjuice2.git","ssh_url":"git@github.com:unicef/carrotjuice2.git","clone_url":"https://github.com/unicef/carrotjuice2.git","svn_url":"https://github.com/unicef/carrotjuice2","homepage":null,"size":348,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":19,"forks":0,"open_issues":19,"watchers":0,"default_branch":"master"}},"base":{"label":"unicef:master","ref":"master","sha":"329e00c9ac0303cabd7090335f5289ea72020214","user":{"login":"unicef","id":175849,"avatar_url":"https://avatars.githubusercontent.com/u/175849?v=3","gravatar_id":"","url":"https://api.github.com/users/unicef","html_url":"https://github.com/unicef","followers_url":"https://api.github.com/users/unicef/followers","following_url":"https://api.github.com/users/unicef/following{/other_user}","gists_url":"https://api.github.com/users/unicef/gists{/gist_id}","starred_url":"https://api.github.com/users/unicef/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/unicef/subscriptions","organizations_url":"https://api.github.com/users/unicef/orgs","repos_url":"https://api.github.com/users/unicef/repos","events_url":"https://api.github.com/users/unicef/events{/privacy}","received_events_url":"https://api.github.com/users/unicef/received_events","type":"Organization","site_admin":false},"repo":{"id":52188236,"name":"carrotjuice2","full_name":"unicef/carrotjuice2","owner":{"login":"unicef","id":175849,"avatar_url":"https://avatars.githubusercontent.com/u/175849?v=3","gravatar_id":"","url":"https://api.github.com/users/unicef","html_url":"https://github.com/unicef","followers_url":"https://api.github.com/users/unicef/followers","following_url":"https://api.github.com/users/unicef/following{/other_user}","gists_url":"https://api.github.com/users/unicef/gists{/gist_id}","starred_url":"https://api.github.com/users/unicef/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/unicef/subscriptions","organizations_url":"https://api.github.com/users/unicef/orgs","repos_url":"https://api.github.com/users/unicef/repos","events_url":"https://api.github.com/users/unicef/events{/privacy}","received_events_url":"https://api.github.com/users/unicef/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/unicef/carrotjuice2","description":"Front end to resources repo. Defaults to Brazil but will eventually be virus and country agnostic.","fork":false,"url":"https://api.github.com/repos/unicef/carrotjuice2","forks_url":"https://api.github.com/repos/unicef/carrotjuice2/forks","keys_url":"https://api.github.com/repos/unicef/carrotjuice2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/unicef/carrotjuice2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/unicef/carrotjuice2/teams","hooks_url":"https://api.github.com/repos/unicef/carrotjuice2/hooks","issue_events_url":"https://api.github.com/repos/unicef/carrotjuice2/issues/events{/number}","events_url":"https://api.github.com/repos/unicef/carrotjuice2/events","assignees_url":"https://api.github.com/repos/unicef/carrotjuice2/assignees{/user}","branches_url":"https://api.github.com/repos/unicef/carrotjuice2/branches{/branch}","tags_url":"https://api.github.com/repos/unicef/carrotjuice2/tags","blobs_url":"https://api.github.com/repos/unicef/carrotjuice2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/unicef/carrotjuice2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/unicef/carrotjuice2/git/refs{/sha}","trees_url":"https://api.github.com/repos/unicef/carrotjuice2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/unicef/carrotjuice2/statuses/{sha}","languages_url":"https://api.github.com/repos/unicef/carrotjuice2/languages","stargazers_url":"https://api.github.com/repos/unicef/carrotjuice2/stargazers","contributors_url":"https://api.github.com/repos/unicef/carrotjuice2/contributors","subscribers_url":"https://api.github.com/repos/unicef/carrotjuice2/subscribers","subscription_url":"https://api.github.com/repos/unicef/carrotjuice2/subscription","commits_url":"https://api.github.com/repos/unicef/carrotjuice2/commits{/sha}","git_commits_url":"https://api.github.com/repos/unicef/carrotjuice2/git/commits{/sha}","comments_url":"https://api.github.com/repos/unicef/carrotjuice2/comments{/number}","issue_comment_url":"https://api.github.com/repos/unicef/carrotjuice2/issues/comments{/number}","contents_url":"https://api.github.com/repos/unicef/carrotjuice2/contents/{+path}","compare_url":"https://api.github.com/repos/unicef/carrotjuice2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/unicef/carrotjuice2/merges","archive_url":"https://api.github.com/repos/unicef/carrotjuice2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/unicef/carrotjuice2/downloads","issues_url":"https://api.github.com/repos/unicef/carrotjuice2/issues{/number}","pulls_url":"https://api.github.com/repos/unicef/carrotjuice2/pulls{/number}","milestones_url":"https://api.github.com/repos/unicef/carrotjuice2/milestones{/number}","notifications_url":"https://api.github.com/repos/unicef/carrotjuice2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/unicef/carrotjuice2/labels{/name}","releases_url":"https://api.github.com/repos/unicef/carrotjuice2/releases{/id}","deployments_url":"https://api.github.com/repos/unicef/carrotjuice2/deployments","created_at":"2016-02-21T03:08:24Z","updated_at":"2016-03-11T22:59:42Z","pushed_at":"2016-03-13T04:50:32Z","git_url":"git://github.com/unicef/carrotjuice2.git","ssh_url":"git@github.com:unicef/carrotjuice2.git","clone_url":"https://github.com/unicef/carrotjuice2.git","svn_url":"https://github.com/unicef/carrotjuice2","homepage":null,"size":348,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":19,"forks":0,"open_issues":19,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/unicef/carrotjuice2/pulls/35"},"html":{"href":"https://github.com/unicef/carrotjuice2/pull/35"},"issue":{"href":"https://api.github.com/repos/unicef/carrotjuice2/issues/35"},"comments":{"href":"https://api.github.com/repos/unicef/carrotjuice2/issues/35/comments"},"review_comments":{"href":"https://api.github.com/repos/unicef/carrotjuice2/pulls/35/comments"},"review_comment":{"href":"https://api.github.com/repos/unicef/carrotjuice2/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/unicef/carrotjuice2/pulls/35/commits"},"statuses":{"href":"https://api.github.com/repos/unicef/carrotjuice2/statuses/8d1a20e437097eaa8712dba4b62ab8db0e121f9c"}}}}