Fix popularity metrics on list repos API

This commit is contained in:
Joseph Schorr 2016-07-06 16:15:54 -04:00
parent 6bde6406c9
commit e252ee07cb
4 changed files with 18 additions and 11 deletions

View file

@ -15,15 +15,15 @@ def paginate(query, model, descending=False, page_token=None, limit=50, id_field
start_id = page_token.get('start_id')
if start_id is not None:
if descending:
query = query.where(model.id <= start_id)
query = query.where(getattr(model, id_field) <= start_id)
else:
query = query.where(model.id >= start_id)
query = query.where(getattr(model, id_field) >= start_id)
results = list(query)
page_token = None
if len(results) > limit:
page_token = {
'start_id': results[limit].id
'start_id': getattr(results[limit], id_field)
}
return results[0:limit], page_token

View file

@ -187,7 +187,7 @@ def unstar_repository(user, repository):
def get_user_starred_repositories(user):
""" Retrieves all of the repositories a user has starred. """
query = (Repository
.select(Repository, User, Visibility)
.select(Repository, User, Visibility, Repository.id.alias('rid'))
.join(Star)
.switch(Repository)
.join(User)
@ -233,11 +233,11 @@ def get_visible_repositories(username, namespace=None, include_public=False):
if not include_public and not username:
# Short circuit by returning a query that will find no repositories. We need to return a query
# here, as it will be modified by other queries later on.
return Repository.select().where(Repository.id == -1)
return Repository.select(Repository.id.alias('rid')).where(Repository.id == -1)
query = (Repository
.select(Repository.name, Repository.id.alias('rid'), Repository.description, Namespace.username,
Repository.visibility)
.select(Repository.name, Repository.id.alias('rid'),
Repository.description, Namespace.username, Repository.visibility)
.distinct()
.switch(Repository)
.join(Namespace, on=(Repository.namespace_user == Namespace.id))

View file

@ -175,7 +175,7 @@ class RepositoryList(ApiResource):
# Collect the IDs of the repositories found for subequent lookup of popularity
# and/or last modified.
if parsed_args['last_modified'] or parsed_args['popularity']:
repository_ids = [repo.id for repo in repos]
repository_ids = [repo.rid for repo in repos]
if parsed_args['last_modified']:
last_modified_map = model.repository.get_when_last_modified(repository_ids)
@ -198,7 +198,7 @@ class RepositoryList(ApiResource):
'is_public': repo_obj.visibility_id == model.repository.get_public_repo_visibility().id,
}
repo_id = repo_obj.id
repo_id = repo_obj.rid
if parsed_args['last_modified']:
repo['last_modified'] = last_modified_map.get(repo_id)

View file

@ -1479,6 +1479,10 @@ class TestListRepos(ApiTestCase):
self.assertEquals(ORGANIZATION, repo['namespace'])
def test_listrepos_allparams(self):
# Add a repository action count entry for one of the org repos.
repo = model.repository.get_repository(ORGANIZATION, ORG_REPO)
RepositoryActionCount.create(repository=repo, count=10, date=datetime.datetime.utcnow())
self.login(ADMIN_ACCESS_USER)
# Queries: Base + the list query + the popularity and last modified queries + full perms load
@ -1491,8 +1495,11 @@ class TestListRepos(ApiTestCase):
self.assertGreater(len(json['repositories']), 0)
for repo in json['repositories']:
self.assertEquals(ORGANIZATION, repo['namespace'])
for repository in json['repositories']:
self.assertEquals(ORGANIZATION, repository['namespace'])
if repository['name'] == ORG_REPO:
self.assertGreater(repository['popularity'], 0)
def test_listrepos_starred_nouser(self):
self.getResponse(RepositoryList,