{"action":"created","comment":{"url":"https://api.github.com/repos/mozilla/redash/pulls/comments/176841619","pull_request_review_id":106618636,"id":176841619,"diff_hunk":"@@ -1000,6 +1006,32 @@ def outdated_queries(cls):\n \n         return outdated_queries.values()\n \n+    @classmethod\n+    def delete_stale_resultsets(cls):\n+        delete_count = 0\n+        queries = Query.query.filter(Query.schedule_resultset_size != None).order_by(Query.schedule_resultset_size.desc())\n+        # Multiple queries with the same text may request multiple result sets\n+        # be kept. We start with the one that keeps the most, and delete both\n+        # the unneeded bridge rows and result sets.\n+        first_query = queries.first()\n+        if first_query is not None and first_query.schedule_resultset_size:\n+            resultsets = QueryResultSet.query.filter(QueryResultSet.query_rel == first_query).order_by(QueryResultSet.result_id)\n+            resultset_count = resultsets.count()\n+            if resultset_count > first_query.schedule_resultset_size:\n+                n_to_delete = resultset_count - first_query.schedule_resultset_size\n+                r_ids = [r.result_id for r in resultsets][:n_to_delete]\n+                delete_count = QueryResultSet.query.filter(QueryResultSet.result_id.in_(r_ids)).delete(synchronize_session=False)\n+                QueryResult.query.filter(QueryResult.id.in_(r_ids)).delete(synchronize_session=False)\n+        # Delete unneeded bridge rows for the remaining queries.\n+        for q in queries[1:]:","path":"redash/models.py","position":80,"original_position":80,"commit_id":"49e6c5d96224105684ecbd9370ffc8c0f0e687c5","original_commit_id":"49e6c5d96224105684ecbd9370ffc8c0f0e687c5","user":{"login":"emtwo","id":784781,"avatar_url":"https://avatars3.githubusercontent.com/u/784781?v=4","gravatar_id":"","url":"https://api.github.com/users/emtwo","html_url":"https://github.com/emtwo","followers_url":"https://api.github.com/users/emtwo/followers","following_url":"https://api.github.com/users/emtwo/following{/other_user}","gists_url":"https://api.github.com/users/emtwo/gists{/gist_id}","starred_url":"https://api.github.com/users/emtwo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/emtwo/subscriptions","organizations_url":"https://api.github.com/users/emtwo/orgs","repos_url":"https://api.github.com/users/emtwo/repos","events_url":"https://api.github.com/users/emtwo/events{/privacy}","received_events_url":"https://api.github.com/users/emtwo/received_events","type":"User","site_admin":false},"body":"I'm not sure I follow what's happening in this loop. My understanding is that we look at the query with the largest allowable query results and delete its stale results and bridge rows like you said. That is what happens in the code above.\r\n\r\nSince other queries may have pointed to the stale results that were deleted, the bridge rows should be deleted for those too? That's what I would have expected this loop to be doing. But it seems to be looking at whether queries that have more stale results, similar to the check that was done for the `first_query`?\r\n\r\nI think in general there is some code in this loop that seems to be similar to what is done to the `first_query` and I'm wondering if factoring some of it out into a separate function might make it clearer or if it's possible include the operations on `first_query` in this loop?","created_at":"2018-03-23T19:24:55Z","updated_at":"2018-03-23T19:25:42Z","html_url":"https://github.com/mozilla/redash/pull/339#discussion_r176841619","pull_request_url":"https://api.github.com/repos/mozilla/redash/pulls/339","author_association":"NONE","_links":{"self":{"href":"https://api.github.com/repos/mozilla/redash/pulls/comments/176841619"},"html":{"href":"https://github.com/mozilla/redash/pull/339#discussion_r176841619"},"pull_request":{"href":"https://api.github.com/repos/mozilla/redash/pulls/339"}}},"pull_request":{"url":"https://api.github.com/repos/mozilla/redash/pulls/339","id":170323864,"html_url":"https://github.com/mozilla/redash/pull/339","diff_url":"https://github.com/mozilla/redash/pull/339.diff","patch_url":"https://github.com/mozilla/redash/pull/339.patch","issue_url":"https://api.github.com/repos/mozilla/redash/issues/339","number":339,"state":"open","locked":false,"title":"Aggregate query results (re #35)","user":{"login":"washort","id":947487,"avatar_url":"https://avatars2.githubusercontent.com/u/947487?v=4","gravatar_id":"","url":"https://api.github.com/users/washort","html_url":"https://github.com/washort","followers_url":"https://api.github.com/users/washort/followers","following_url":"https://api.github.com/users/washort/following{/other_user}","gists_url":"https://api.github.com/users/washort/gists{/gist_id}","starred_url":"https://api.github.com/users/washort/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/washort/subscriptions","organizations_url":"https://api.github.com/users/washort/orgs","repos_url":"https://api.github.com/users/washort/repos","events_url":"https://api.github.com/users/washort/events{/privacy}","received_events_url":"https://api.github.com/users/washort/received_events","type":"User","site_admin":false},"body":"This provides UI for keeping multiple query results and backend support for storing and aggregating them (and deleting them when no longer needed).\n\nStill needs frontend work for choosing to display aggregated results.\n\nfixes #35","created_at":"2018-02-20T22:14:21Z","updated_at":"2018-03-23T19:25:42Z","closed_at":null,"merged_at":null,"merge_commit_sha":"02be77c6bc9ede5e3416faec20aaac2dc698b9bb","assignee":{"login":"emtwo","id":784781,"avatar_url":"https://avatars3.githubusercontent.com/u/784781?v=4","gravatar_id":"","url":"https://api.github.com/users/emtwo","html_url":"https://github.com/emtwo","followers_url":"https://api.github.com/users/emtwo/followers","following_url":"https://api.github.com/users/emtwo/following{/other_user}","gists_url":"https://api.github.com/users/emtwo/gists{/gist_id}","starred_url":"https://api.github.com/users/emtwo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/emtwo/subscriptions","organizations_url":"https://api.github.com/users/emtwo/orgs","repos_url":"https://api.github.com/users/emtwo/repos","events_url":"https://api.github.com/users/emtwo/events{/privacy}","received_events_url":"https://api.github.com/users/emtwo/received_events","type":"User","site_admin":false},"assignees":[{"login":"emtwo","id":784781,"avatar_url":"https://avatars3.githubusercontent.com/u/784781?v=4","gravatar_id":"","url":"https://api.github.com/users/emtwo","html_url":"https://github.com/emtwo","followers_url":"https://api.github.com/users/emtwo/followers","following_url":"https://api.github.com/users/emtwo/following{/other_user}","gists_url":"https://api.github.com/users/emtwo/gists{/gist_id}","starred_url":"https://api.github.com/users/emtwo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/emtwo/subscriptions","organizations_url":"https://api.github.com/users/emtwo/orgs","repos_url":"https://api.github.com/users/emtwo/repos","events_url":"https://api.github.com/users/emtwo/events{/privacy}","received_events_url":"https://api.github.com/users/emtwo/received_events","type":"User","site_admin":false}],"requested_reviewers":[],"requested_teams":[],"labels":[{"id":627506462,"url":"https://api.github.com/repos/mozilla/redash/labels/in%20review","name":"in review","color":"00ff00","default":false}],"milestone":{"url":"https://api.github.com/repos/mozilla/redash/milestones/20","html_url":"https://github.com/mozilla/redash/milestone/20","labels_url":"https://api.github.com/repos/mozilla/redash/milestones/20/labels","id":2947245,"number":20,"title":"13","description":"","creator":{"login":"rafrombrc","id":151364,"avatar_url":"https://avatars0.githubusercontent.com/u/151364?v=4","gravatar_id":"","url":"https://api.github.com/users/rafrombrc","html_url":"https://github.com/rafrombrc","followers_url":"https://api.github.com/users/rafrombrc/followers","following_url":"https://api.github.com/users/rafrombrc/following{/other_user}","gists_url":"https://api.github.com/users/rafrombrc/gists{/gist_id}","starred_url":"https://api.github.com/users/rafrombrc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rafrombrc/subscriptions","organizations_url":"https://api.github.com/users/rafrombrc/orgs","repos_url":"https://api.github.com/users/rafrombrc/repos","events_url":"https://api.github.com/users/rafrombrc/events{/privacy}","received_events_url":"https://api.github.com/users/rafrombrc/received_events","type":"User","site_admin":false},"open_issues":7,"closed_issues":28,"state":"open","created_at":"2017-11-29T17:45:34Z","updated_at":"2018-03-23T01:04:47Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/mozilla/redash/pulls/339/commits","review_comments_url":"https://api.github.com/repos/mozilla/redash/pulls/339/comments","review_comment_url":"https://api.github.com/repos/mozilla/redash/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mozilla/redash/issues/339/comments","statuses_url":"https://api.github.com/repos/mozilla/redash/statuses/49e6c5d96224105684ecbd9370ffc8c0f0e687c5","head":{"label":"mozilla:incremental-jobs-35","ref":"incremental-jobs-35","sha":"49e6c5d96224105684ecbd9370ffc8c0f0e687c5","user":{"login":"mozilla","id":131524,"avatar_url":"https://avatars2.githubusercontent.com/u/131524?v=4","gravatar_id":"","url":"https://api.github.com/users/mozilla","html_url":"https://github.com/mozilla","followers_url":"https://api.github.com/users/mozilla/followers","following_url":"https://api.github.com/users/mozilla/following{/other_user}","gists_url":"https://api.github.com/users/mozilla/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla/subscriptions","organizations_url":"https://api.github.com/users/mozilla/orgs","repos_url":"https://api.github.com/users/mozilla/repos","events_url":"https://api.github.com/users/mozilla/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla/received_events","type":"Organization","site_admin":false},"repo":{"id":69394887,"name":"redash","full_name":"mozilla/redash","owner":{"login":"mozilla","id":131524,"avatar_url":"https://avatars2.githubusercontent.com/u/131524?v=4","gravatar_id":"","url":"https://api.github.com/users/mozilla","html_url":"https://github.com/mozilla","followers_url":"https://api.github.com/users/mozilla/followers","following_url":"https://api.github.com/users/mozilla/following{/other_user}","gists_url":"https://api.github.com/users/mozilla/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla/subscriptions","organizations_url":"https://api.github.com/users/mozilla/orgs","repos_url":"https://api.github.com/users/mozilla/repos","events_url":"https://api.github.com/users/mozilla/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mozilla/redash","description":"This is a Mozilla fork of the re:dash project (https://redash.io/), where we do work to be contributed back to the upstream project and for our own custom needs.","fork":true,"url":"https://api.github.com/repos/mozilla/redash","forks_url":"https://api.github.com/repos/mozilla/redash/forks","keys_url":"https://api.github.com/repos/mozilla/redash/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mozilla/redash/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mozilla/redash/teams","hooks_url":"https://api.github.com/repos/mozilla/redash/hooks","issue_events_url":"https://api.github.com/repos/mozilla/redash/issues/events{/number}","events_url":"https://api.github.com/repos/mozilla/redash/events","assignees_url":"https://api.github.com/repos/mozilla/redash/assignees{/user}","branches_url":"https://api.github.com/repos/mozilla/redash/branches{/branch}","tags_url":"https://api.github.com/repos/mozilla/redash/tags","blobs_url":"https://api.github.com/repos/mozilla/redash/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mozilla/redash/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mozilla/redash/git/refs{/sha}","trees_url":"https://api.github.com/repos/mozilla/redash/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mozilla/redash/statuses/{sha}","languages_url":"https://api.github.com/repos/mozilla/redash/languages","stargazers_url":"https://api.github.com/repos/mozilla/redash/stargazers","contributors_url":"https://api.github.com/repos/mozilla/redash/contributors","subscribers_url":"https://api.github.com/repos/mozilla/redash/subscribers","subscription_url":"https://api.github.com/repos/mozilla/redash/subscription","commits_url":"https://api.github.com/repos/mozilla/redash/commits{/sha}","git_commits_url":"https://api.github.com/repos/mozilla/redash/git/commits{/sha}","comments_url":"https://api.github.com/repos/mozilla/redash/comments{/number}","issue_comment_url":"https://api.github.com/repos/mozilla/redash/issues/comments{/number}","contents_url":"https://api.github.com/repos/mozilla/redash/contents/{+path}","compare_url":"https://api.github.com/repos/mozilla/redash/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mozilla/redash/merges","archive_url":"https://api.github.com/repos/mozilla/redash/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mozilla/redash/downloads","issues_url":"https://api.github.com/repos/mozilla/redash/issues{/number}","pulls_url":"https://api.github.com/repos/mozilla/redash/pulls{/number}","milestones_url":"https://api.github.com/repos/mozilla/redash/milestones{/number}","notifications_url":"https://api.github.com/repos/mozilla/redash/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mozilla/redash/labels{/name}","releases_url":"https://api.github.com/repos/mozilla/redash/releases{/id}","deployments_url":"https://api.github.com/repos/mozilla/redash/deployments","created_at":"2016-09-27T20:16:23Z","updated_at":"2018-03-23T04:45:46Z","pushed_at":"2018-03-23T18:47:34Z","git_url":"git://github.com/mozilla/redash.git","ssh_url":"git@github.com:mozilla/redash.git","clone_url":"https://github.com/mozilla/redash.git","svn_url":"https://github.com/mozilla/redash","homepage":"","size":11168,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":7,"mirror_url":null,"archived":false,"open_issues_count":44,"license":{"key":"bsd-2-clause","name":"BSD 2-Clause \"Simplified\" License","spdx_id":"BSD-2-Clause","url":"https://api.github.com/licenses/bsd-2-clause"},"forks":7,"open_issues":44,"watchers":1,"default_branch":"master"}},"base":{"label":"mozilla:master","ref":"master","sha":"5f501f5df20cee3de8eaa74cca43d6862afc196b","user":{"login":"mozilla","id":131524,"avatar_url":"https://avatars2.githubusercontent.com/u/131524?v=4","gravatar_id":"","url":"https://api.github.com/users/mozilla","html_url":"https://github.com/mozilla","followers_url":"https://api.github.com/users/mozilla/followers","following_url":"https://api.github.com/users/mozilla/following{/other_user}","gists_url":"https://api.github.com/users/mozilla/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla/subscriptions","organizations_url":"https://api.github.com/users/mozilla/orgs","repos_url":"https://api.github.com/users/mozilla/repos","events_url":"https://api.github.com/users/mozilla/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla/received_events","type":"Organization","site_admin":false},"repo":{"id":69394887,"name":"redash","full_name":"mozilla/redash","owner":{"login":"mozilla","id":131524,"avatar_url":"https://avatars2.githubusercontent.com/u/131524?v=4","gravatar_id":"","url":"https://api.github.com/users/mozilla","html_url":"https://github.com/mozilla","followers_url":"https://api.github.com/users/mozilla/followers","following_url":"https://api.github.com/users/mozilla/following{/other_user}","gists_url":"https://api.github.com/users/mozilla/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla/subscriptions","organizations_url":"https://api.github.com/users/mozilla/orgs","repos_url":"https://api.github.com/users/mozilla/repos","events_url":"https://api.github.com/users/mozilla/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mozilla/redash","description":"This is a Mozilla fork of the re:dash project (https://redash.io/), where we do work to be contributed back to the upstream project and for our own custom needs.","fork":true,"url":"https://api.github.com/repos/mozilla/redash","forks_url":"https://api.github.com/repos/mozilla/redash/forks","keys_url":"https://api.github.com/repos/mozilla/redash/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mozilla/redash/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mozilla/redash/teams","hooks_url":"https://api.github.com/repos/mozilla/redash/hooks","issue_events_url":"https://api.github.com/repos/mozilla/redash/issues/events{/number}","events_url":"https://api.github.com/repos/mozilla/redash/events","assignees_url":"https://api.github.com/repos/mozilla/redash/assignees{/user}","branches_url":"https://api.github.com/repos/mozilla/redash/branches{/branch}","tags_url":"https://api.github.com/repos/mozilla/redash/tags","blobs_url":"https://api.github.com/repos/mozilla/redash/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mozilla/redash/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mozilla/redash/git/refs{/sha}","trees_url":"https://api.github.com/repos/mozilla/redash/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mozilla/redash/statuses/{sha}","languages_url":"https://api.github.com/repos/mozilla/redash/languages","stargazers_url":"https://api.github.com/repos/mozilla/redash/stargazers","contributors_url":"https://api.github.com/repos/mozilla/redash/contributors","subscribers_url":"https://api.github.com/repos/mozilla/redash/subscribers","subscription_url":"https://api.github.com/repos/mozilla/redash/subscription","commits_url":"https://api.github.com/repos/mozilla/redash/commits{/sha}","git_commits_url":"https://api.github.com/repos/mozilla/redash/git/commits{/sha}","comments_url":"https://api.github.com/repos/mozilla/redash/comments{/number}","issue_comment_url":"https://api.github.com/repos/mozilla/redash/issues/comments{/number}","contents_url":"https://api.github.com/repos/mozilla/redash/contents/{+path}","compare_url":"https://api.github.com/repos/mozilla/redash/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mozilla/redash/merges","archive_url":"https://api.github.com/repos/mozilla/redash/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mozilla/redash/downloads","issues_url":"https://api.github.com/repos/mozilla/redash/issues{/number}","pulls_url":"https://api.github.com/repos/mozilla/redash/pulls{/number}","milestones_url":"https://api.github.com/repos/mozilla/redash/milestones{/number}","notifications_url":"https://api.github.com/repos/mozilla/redash/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mozilla/redash/labels{/name}","releases_url":"https://api.github.com/repos/mozilla/redash/releases{/id}","deployments_url":"https://api.github.com/repos/mozilla/redash/deployments","created_at":"2016-09-27T20:16:23Z","updated_at":"2018-03-23T04:45:46Z","pushed_at":"2018-03-23T18:47:34Z","git_url":"git://github.com/mozilla/redash.git","ssh_url":"git@github.com:mozilla/redash.git","clone_url":"https://github.com/mozilla/redash.git","svn_url":"https://github.com/mozilla/redash","homepage":"","size":11168,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":7,"mirror_url":null,"archived":false,"open_issues_count":44,"license":{"key":"bsd-2-clause","name":"BSD 2-Clause \"Simplified\" License","spdx_id":"BSD-2-Clause","url":"https://api.github.com/licenses/bsd-2-clause"},"forks":7,"open_issues":44,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mozilla/redash/pulls/339"},"html":{"href":"https://github.com/mozilla/redash/pull/339"},"issue":{"href":"https://api.github.com/repos/mozilla/redash/issues/339"},"comments":{"href":"https://api.github.com/repos/mozilla/redash/issues/339/comments"},"review_comments":{"href":"https://api.github.com/repos/mozilla/redash/pulls/339/comments"},"review_comment":{"href":"https://api.github.com/repos/mozilla/redash/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mozilla/redash/pulls/339/commits"},"statuses":{"href":"https://api.github.com/repos/mozilla/redash/statuses/49e6c5d96224105684ecbd9370ffc8c0f0e687c5"}},"author_association":"NONE"}}
{"actor":{"display_login":"emtwo"}}