{"action":"created","comment":{"url":"https://api.github.com/repos/alphagov/publishing-api/pulls/comments/59879244","id":59879244,"diff_hunk":"@@ -0,0 +1,26 @@\n+module Queries\n+  module GetLatest\n+    class << self\n+      # Returns a new scope for the content items with the highest user-facing\n+      # version number per (content_id, locale) group of the given scope.","path":"app/queries/get_latest.rb","position":5,"original_position":5,"commit_id":"aff568f9c65eecfee34b6b1888b56c32e8e360e9","original_commit_id":"aff568f9c65eecfee34b6b1888b56c32e8e360e9","user":{"login":"elliotcm","id":109225,"avatar_url":"https://avatars.githubusercontent.com/u/109225?v=3","gravatar_id":"","url":"https://api.github.com/users/elliotcm","html_url":"https://github.com/elliotcm","followers_url":"https://api.github.com/users/elliotcm/followers","following_url":"https://api.github.com/users/elliotcm/following{/other_user}","gists_url":"https://api.github.com/users/elliotcm/gists{/gist_id}","starred_url":"https://api.github.com/users/elliotcm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elliotcm/subscriptions","organizations_url":"https://api.github.com/users/elliotcm/orgs","repos_url":"https://api.github.com/users/elliotcm/repos","events_url":"https://api.github.com/users/elliotcm/events{/privacy}","received_events_url":"https://api.github.com/users/elliotcm/received_events","type":"User","site_admin":false},"body":"Might be clearer as \"per content_id and locale of the given scope\".","created_at":"2016-04-15T14:08:20Z","updated_at":"2016-04-15T14:08:20Z","html_url":"https://github.com/alphagov/publishing-api/pull/268#discussion_r59879244","pull_request_url":"https://api.github.com/repos/alphagov/publishing-api/pulls/268","_links":{"self":{"href":"https://api.github.com/repos/alphagov/publishing-api/pulls/comments/59879244"},"html":{"href":"https://github.com/alphagov/publishing-api/pull/268#discussion_r59879244"},"pull_request":{"href":"https://api.github.com/repos/alphagov/publishing-api/pulls/268"}}},"pull_request":{"url":"https://api.github.com/repos/alphagov/publishing-api/pulls/268","id":66175097,"html_url":"https://github.com/alphagov/publishing-api/pull/268","diff_url":"https://github.com/alphagov/publishing-api/pull/268.diff","patch_url":"https://github.com/alphagov/publishing-api/pull/268.patch","issue_url":"https://api.github.com/repos/alphagov/publishing-api/issues/268","number":268,"state":"open","locked":false,"title":"Make the ContentItemPresenter much faster","user":{"login":"tuzz","id":892251,"avatar_url":"https://avatars.githubusercontent.com/u/892251?v=3","gravatar_id":"","url":"https://api.github.com/users/tuzz","html_url":"https://github.com/tuzz","followers_url":"https://api.github.com/users/tuzz/followers","following_url":"https://api.github.com/users/tuzz/following{/other_user}","gists_url":"https://api.github.com/users/tuzz/gists{/gist_id}","starred_url":"https://api.github.com/users/tuzz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tuzz/subscriptions","organizations_url":"https://api.github.com/users/tuzz/orgs","repos_url":"https://api.github.com/users/tuzz/repos","events_url":"https://api.github.com/users/tuzz/events{/privacy}","received_events_url":"https://api.github.com/users/tuzz/received_events","type":"User","site_admin":false},"body":"https://trello.com/c/1wLclYR2/668-improve-efficiency-of-content-item-presenter\r\n\r\n**Note:** Tests won't pass until this is merged: https://github.com/alphagov/gds-api-adapters/pull/488\r\n\r\nPreviously, the ContentItemPresenter could take a long time,\r\neven with pagination and this was causing timeouts across a\r\nnumber of projects and general instability. It was also\r\nslowing down put content requests because the response goes\r\nthrough the presenter.\r\n\r\nThis work effectively rewrites this presenter to work much\r\nmore quickly. It relies on `Queries::GetLatest` which is an\r\nexample of a \"get rows with max value per group\" query.\r\nThere are further improvements that could be made, perhaps\r\nwith how the count is computed, but these changes are\r\nprobably good enough for now.\r\n\r\nThe old presenter made it so that public_updated_at was\r\nalways output, regardless of whether this field was\r\nrequested or not. I have fixed this. It should now be\r\nexplicitly requested, just like any other field. In\r\naddition to this, I made it so that the presenter defaults\r\nto return all fields. The presenter is now quick enough to\r\ndo this and I think it serves as good documentation as to\r\nwhich fields are available.\r\n\r\nFinally, here are the results of some quick benchmarking\r\nin development. Times are in milliseconds:\r\n\r\n```\r\n/v2/linkables?document_type=organisation\r\nnew: 95, 100, 96, 97, 95 (avg 97)\r\nold: 553, 302, 535, 519, 563 (avg 494)\r\naverage: 509% as fast\r\n\r\n/v2/linkables?document_type=policy\r\nnew: 39, 44, 37, 44, 38 (avg 40)\r\nold: 109, 91, 92, 197, 210 (avg 140)\r\naverage: 350% as fast\r\n\r\n/v2/content?document_type=organisation&fields[]=title&fields[]=base_path\r\nnew: 84, 87, 83, 90, 87 (avg 86)\r\nold: 320, 361, 374, 351, 368 (avg 355)\r\naverage: 416% as fast\r\n\r\n/v2/content?document_type=cma_case&fields[]=description&per_page=1000\r\nnew: 116, 111, 109, 114, 117 (avg 113)\r\nold: 985, 678, 964, 953, 730 (avg 862)\r\naverage: 763% as fast\r\n\r\n/v2/content?document_type=travel_advice&fields[]=details&per_page=3\r\nnew: 98, 99, 98, 105, 107 (avg 101)\r\nold: 1363, 1048, 1020, 1025, 1030 (avg 1097)\r\naverage: 1086% as fast\r\n\r\nPUT /v2/content/08d48cdd-6b50-43ff-a53b-beab47f4aab0 (travel advice index)\r\nnew: 904, 780, 804, 660, 883 (avg 806)\r\nold: 3601, 2483, 2683, 2542, 2592 (avg 2780)\r\naverage: 345% as fast\r\n```\r\n\r\nNote: This last case was particularly problematic because the default\r\ngds-api-adapters timeout is 4 seconds, which the old implementation\r\nwas close to hitting.\r\n\r\n## Benchmarks graph\r\n\r\n![benchmark](https://cloud.githubusercontent.com/assets/892251/14531374/fc9ee7c0-0254-11e6-817f-aec7c95c7f86.png)\r\n","created_at":"2016-04-12T15:12:14Z","updated_at":"2016-04-15T14:08:20Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5a274574636ceec81da5973a89ccccc0c6a52f1b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/alphagov/publishing-api/pulls/268/commits","review_comments_url":"https://api.github.com/repos/alphagov/publishing-api/pulls/268/comments","review_comment_url":"https://api.github.com/repos/alphagov/publishing-api/pulls/comments{/number}","comments_url":"https://api.github.com/repos/alphagov/publishing-api/issues/268/comments","statuses_url":"https://api.github.com/repos/alphagov/publishing-api/statuses/aff568f9c65eecfee34b6b1888b56c32e8e360e9","head":{"label":"alphagov:content-item-presenter","ref":"content-item-presenter","sha":"aff568f9c65eecfee34b6b1888b56c32e8e360e9","user":{"login":"alphagov","id":596977,"avatar_url":"https://avatars.githubusercontent.com/u/596977?v=3","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"repo":{"id":27546098,"name":"publishing-api","full_name":"alphagov/publishing-api","owner":{"login":"alphagov","id":596977,"avatar_url":"https://avatars.githubusercontent.com/u/596977?v=3","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/alphagov/publishing-api","description":"Central store for all content on GOV.UK.","fork":false,"url":"https://api.github.com/repos/alphagov/publishing-api","forks_url":"https://api.github.com/repos/alphagov/publishing-api/forks","keys_url":"https://api.github.com/repos/alphagov/publishing-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/alphagov/publishing-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/alphagov/publishing-api/teams","hooks_url":"https://api.github.com/repos/alphagov/publishing-api/hooks","issue_events_url":"https://api.github.com/repos/alphagov/publishing-api/issues/events{/number}","events_url":"https://api.github.com/repos/alphagov/publishing-api/events","assignees_url":"https://api.github.com/repos/alphagov/publishing-api/assignees{/user}","branches_url":"https://api.github.com/repos/alphagov/publishing-api/branches{/branch}","tags_url":"https://api.github.com/repos/alphagov/publishing-api/tags","blobs_url":"https://api.github.com/repos/alphagov/publishing-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/alphagov/publishing-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/alphagov/publishing-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/alphagov/publishing-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/alphagov/publishing-api/statuses/{sha}","languages_url":"https://api.github.com/repos/alphagov/publishing-api/languages","stargazers_url":"https://api.github.com/repos/alphagov/publishing-api/stargazers","contributors_url":"https://api.github.com/repos/alphagov/publishing-api/contributors","subscribers_url":"https://api.github.com/repos/alphagov/publishing-api/subscribers","subscription_url":"https://api.github.com/repos/alphagov/publishing-api/subscription","commits_url":"https://api.github.com/repos/alphagov/publishing-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/alphagov/publishing-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/alphagov/publishing-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/alphagov/publishing-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/alphagov/publishing-api/contents/{+path}","compare_url":"https://api.github.com/repos/alphagov/publishing-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/alphagov/publishing-api/merges","archive_url":"https://api.github.com/repos/alphagov/publishing-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/alphagov/publishing-api/downloads","issues_url":"https://api.github.com/repos/alphagov/publishing-api/issues{/number}","pulls_url":"https://api.github.com/repos/alphagov/publishing-api/pulls{/number}","milestones_url":"https://api.github.com/repos/alphagov/publishing-api/milestones{/number}","notifications_url":"https://api.github.com/repos/alphagov/publishing-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/alphagov/publishing-api/labels{/name}","releases_url":"https://api.github.com/repos/alphagov/publishing-api/releases{/id}","deployments_url":"https://api.github.com/repos/alphagov/publishing-api/deployments","created_at":"2014-12-04T15:15:43Z","updated_at":"2016-04-08T12:43:42Z","pushed_at":"2016-04-15T12:58:43Z","git_url":"git://github.com/alphagov/publishing-api.git","ssh_url":"git@github.com:alphagov/publishing-api.git","clone_url":"https://github.com/alphagov/publishing-api.git","svn_url":"https://github.com/alphagov/publishing-api","homepage":"","size":1630,"stargazers_count":6,"watchers_count":6,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":2,"forks":3,"open_issues":2,"watchers":6,"default_branch":"master"}},"base":{"label":"alphagov:master","ref":"master","sha":"2eeffa19fe2fe86583fce883e4872f099e2e2ae6","user":{"login":"alphagov","id":596977,"avatar_url":"https://avatars.githubusercontent.com/u/596977?v=3","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"repo":{"id":27546098,"name":"publishing-api","full_name":"alphagov/publishing-api","owner":{"login":"alphagov","id":596977,"avatar_url":"https://avatars.githubusercontent.com/u/596977?v=3","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/alphagov/publishing-api","description":"Central store for all content on GOV.UK.","fork":false,"url":"https://api.github.com/repos/alphagov/publishing-api","forks_url":"https://api.github.com/repos/alphagov/publishing-api/forks","keys_url":"https://api.github.com/repos/alphagov/publishing-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/alphagov/publishing-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/alphagov/publishing-api/teams","hooks_url":"https://api.github.com/repos/alphagov/publishing-api/hooks","issue_events_url":"https://api.github.com/repos/alphagov/publishing-api/issues/events{/number}","events_url":"https://api.github.com/repos/alphagov/publishing-api/events","assignees_url":"https://api.github.com/repos/alphagov/publishing-api/assignees{/user}","branches_url":"https://api.github.com/repos/alphagov/publishing-api/branches{/branch}","tags_url":"https://api.github.com/repos/alphagov/publishing-api/tags","blobs_url":"https://api.github.com/repos/alphagov/publishing-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/alphagov/publishing-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/alphagov/publishing-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/alphagov/publishing-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/alphagov/publishing-api/statuses/{sha}","languages_url":"https://api.github.com/repos/alphagov/publishing-api/languages","stargazers_url":"https://api.github.com/repos/alphagov/publishing-api/stargazers","contributors_url":"https://api.github.com/repos/alphagov/publishing-api/contributors","subscribers_url":"https://api.github.com/repos/alphagov/publishing-api/subscribers","subscription_url":"https://api.github.com/repos/alphagov/publishing-api/subscription","commits_url":"https://api.github.com/repos/alphagov/publishing-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/alphagov/publishing-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/alphagov/publishing-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/alphagov/publishing-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/alphagov/publishing-api/contents/{+path}","compare_url":"https://api.github.com/repos/alphagov/publishing-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/alphagov/publishing-api/merges","archive_url":"https://api.github.com/repos/alphagov/publishing-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/alphagov/publishing-api/downloads","issues_url":"https://api.github.com/repos/alphagov/publishing-api/issues{/number}","pulls_url":"https://api.github.com/repos/alphagov/publishing-api/pulls{/number}","milestones_url":"https://api.github.com/repos/alphagov/publishing-api/milestones{/number}","notifications_url":"https://api.github.com/repos/alphagov/publishing-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/alphagov/publishing-api/labels{/name}","releases_url":"https://api.github.com/repos/alphagov/publishing-api/releases{/id}","deployments_url":"https://api.github.com/repos/alphagov/publishing-api/deployments","created_at":"2014-12-04T15:15:43Z","updated_at":"2016-04-08T12:43:42Z","pushed_at":"2016-04-15T12:58:43Z","git_url":"git://github.com/alphagov/publishing-api.git","ssh_url":"git@github.com:alphagov/publishing-api.git","clone_url":"https://github.com/alphagov/publishing-api.git","svn_url":"https://github.com/alphagov/publishing-api","homepage":"","size":1630,"stargazers_count":6,"watchers_count":6,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":2,"forks":3,"open_issues":2,"watchers":6,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/alphagov/publishing-api/pulls/268"},"html":{"href":"https://github.com/alphagov/publishing-api/pull/268"},"issue":{"href":"https://api.github.com/repos/alphagov/publishing-api/issues/268"},"comments":{"href":"https://api.github.com/repos/alphagov/publishing-api/issues/268/comments"},"review_comments":{"href":"https://api.github.com/repos/alphagov/publishing-api/pulls/268/comments"},"review_comment":{"href":"https://api.github.com/repos/alphagov/publishing-api/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/alphagov/publishing-api/pulls/268/commits"},"statuses":{"href":"https://api.github.com/repos/alphagov/publishing-api/statuses/aff568f9c65eecfee34b6b1888b56c32e8e360e9"}}}}