Make the repository API faster by only checking the log entries table once for each kind of entry, rather than twice. We make use of a special subquery-like syntax, which allows us to count those entries that are both 30 days only and 1 day old in the same query. This was tested successfully on MySQL, Postgres and Sqlite.

This commit is contained in:
Joseph Schorr 2015-05-07 22:49:11 -04:00
parent 469f25b64c
commit c767aafcd6
2 changed files with 39 additions and 22 deletions

View file

@ -194,13 +194,17 @@ class Repository(RepositoryParamResource):
tag_dict = {tag.name: tag_view(tag) for tag in tags}
can_write = ModifyRepositoryPermission(namespace, repository).can()
can_admin = AdministerRepositoryPermission(namespace, repository).can()
active_builds = model.list_repository_builds(namespace, repository, 1,
include_inactive=False)
is_starred = (model.repository_is_starred(get_authenticated_user(), repo)
if get_authenticated_user() else False)
is_public = model.is_repository_public(repo)
(pull_today, pull_thirty_day) = model.get_repository_pulls(repo, timedelta(days=1),
timedelta(days=30))
(push_today, push_thirty_day) = model.get_repository_pushes(repo, timedelta(days=1),
timedelta(days=30))
return {
'namespace': namespace,
'name': repository,
@ -209,18 +213,17 @@ class Repository(RepositoryParamResource):
'can_write': can_write,
'can_admin': can_admin,
'is_public': is_public,
'is_building': len(list(active_builds)) > 0,
'is_organization': repo.namespace_user.organization,
'is_starred': is_starred,
'status_token': repo.badge_token if not is_public else '',
'stats': {
'pulls': {
'today': model.get_repository_pulls(repo, timedelta(days=1)),
'thirty_day': model.get_repository_pulls(repo, timedelta(days=30))
'today': pull_today,
'thirty_day': pull_thirty_day
},
'pushes': {
'today': model.get_repository_pushes(repo, timedelta(days=1)),
'thirty_day': model.get_repository_pushes(repo, timedelta(days=30))
'today': push_today,
'thirty_day': push_thirty_day
}
}
}