← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~ilasc/launchpad:top50-repos-for-repack into launchpad:master

 

Ioana Lasc has proposed merging ~ilasc/launchpad:top50-repos-for-repack into launchpad:master.

Commit message:
Add a getTop50RepositoriesForRepack endpoint

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~ilasc/launchpad/+git/launchpad/+merge/402973
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~ilasc/launchpad:top50-repos-for-repack into launchpad:master.
diff --git a/lib/lp/code/interfaces/gitrepository.py b/lib/lp/code/interfaces/gitrepository.py
index 294cc35..cb3e15f 100644
--- a/lib/lp/code/interfaces/gitrepository.py
+++ b/lib/lp/code/interfaces/gitrepository.py
@@ -1214,6 +1214,14 @@ class IGitRepositorySet(Interface):
             Projects that do not have default repositories are omitted.
         """
 
+    @export_read_operation()
+    @operation_for_version("devel")
+    def getTop50RepositoriesForRepack():
+        """Get the first 50 badly packed repositories.
+
+        :return: A list of the top 50 badly packed repositories.
+        """
+
 
 class IGitRepositoryDelta(Interface):
     """The quantitative changes made to a Git repository that was edited or
diff --git a/lib/lp/code/model/gitrepository.py b/lib/lp/code/model/gitrepository.py
index d447abb..d4b37e4 100644
--- a/lib/lp/code/model/gitrepository.py
+++ b/lib/lp/code/model/gitrepository.py
@@ -1982,6 +1982,21 @@ class GitRepositorySet:
         return {
             repository.project_id: repository for repository in repositories}
 
+    def getTop50RepositoriesForRepack(self):
+        """See `IGitRepositorySet`."""
+        repos = IStore(GitRepository).find(
+            GitRepository,
+            Or(
+                GitRepository.loose_object_count >=
+                    config.codehosting.loose_objects_threshold,
+                GitRepository.pack_count >=
+                    config.codehosting.packs_threshold,
+                ),
+            GitRepository.status == GitRepositoryStatus.AVAILABLE,
+        ).order_by(GitRepository.loose_object_count).config(limit=50)
+
+        return list(repos)
+
 
 @implementer(IMacaroonIssuer)
 class GitRepositoryMacaroonIssuer(MacaroonIssuerBase):
diff --git a/lib/lp/code/model/tests/test_gitrepository.py b/lib/lp/code/model/tests/test_gitrepository.py
index 008c640..88f0de0 100644
--- a/lib/lp/code/model/tests/test_gitrepository.py
+++ b/lib/lp/code/model/tests/test_gitrepository.py
@@ -4069,6 +4069,34 @@ class TestGitRepositoryWebservice(TestCaseWithFactory):
     def test_getRepositories_personal(self):
         self.assertGetRepositoriesWorks(self.factory.makePerson())
 
+    def test_getTop50RepositoriesForRepack(self):
+        person = self.factory.makePerson()
+        webservice = webservice_for_person(
+            person, permission=OAuthPermission.WRITE_PUBLIC)
+        webservice.default_api_version = "devel"
+        response = webservice.named_get(
+            "/+git", "getTop50RepositoriesForRepack")
+        self.assertEqual(200, response.status)
+        self.assertEqual([], response.jsonBody())
+        with person_logged_in(person):
+            repo = []
+            for i in range(5):
+                repo.append(self.factory.makeGitRepository())
+            for i in range(3):
+                repo.append(self.factory.makeGitRepository())
+                repo[i] = removeSecurityProxy(repo[i])
+                repo[i].loose_object_count = 7000 + i
+                repo[i].pack_count = 43
+
+        # We have a total of 3 candidates now
+        response = webservice.named_get(
+            "/+git", "getTop50RepositoriesForRepack")
+        self.assertEqual(200, response.status)
+        self.assertContentEqual(
+            [7000, 7001, 7002],
+            [entry['loose_object_count']
+             for entry in response.jsonBody()])
+
     def test_get_without_default_branch(self):
         # Ensure we're not getting an error when calling
         # GET on the Webservice when a Git Repo exists in the DB