Merge branch 'master' of github.com:coreos-inc/quay
This commit is contained in:
commit
36f33e8fd7
2 changed files with 51 additions and 26 deletions
|
@ -19,7 +19,7 @@ from data.database import (User, Repository, Image, AccessToken, Role, Repositor
|
|||
db, BUILD_PHASE, QuayUserField, ImageStorageSignature, QueueItem,
|
||||
ImageStorageSignatureKind, validate_database_url, db_for_update,
|
||||
AccessTokenKind, Star, get_epoch_timestamp, RepositoryActionCount)
|
||||
from peewee import JOIN_LEFT_OUTER, fn
|
||||
from peewee import JOIN_LEFT_OUTER, fn, SQL
|
||||
from util.validation import (validate_username, validate_email, validate_password,
|
||||
INVALID_PASSWORD_MESSAGE)
|
||||
from util.names import format_robot_username, parse_robot_username
|
||||
|
@ -2777,30 +2777,52 @@ def cancel_repository_build(build, work_queue):
|
|||
build.delete_instance()
|
||||
return True
|
||||
|
||||
def get_repository_pushes(repository, time_delta):
|
||||
def _get_repository_events(repository, time_delta, time_delta_earlier, clause):
|
||||
""" Returns a pair representing the count of the number of events for the given
|
||||
repository in each of the specified time deltas. The date ranges are calculated by
|
||||
taking the current time today and subtracting the time delta given. Since
|
||||
we want to grab *two* ranges, we restrict the second range to be greater
|
||||
than the first (i.e. referring to an earlier time), so we can conduct the
|
||||
lookup in a single query. The clause is used to further filter the kind of
|
||||
events being found.
|
||||
"""
|
||||
since = date.today() - time_delta
|
||||
push_repo = LogEntryKind.get(name = 'push_repo')
|
||||
return (LogEntry.select()
|
||||
.where(LogEntry.repository == repository)
|
||||
.where(LogEntry.kind == push_repo)
|
||||
.where(LogEntry.datetime >= since)
|
||||
.count())
|
||||
since_earlier = date.today() - time_delta_earlier
|
||||
|
||||
def get_repository_pulls(repository, time_delta):
|
||||
since = date.today() - time_delta
|
||||
repo_pull = LogEntryKind.get(name = 'pull_repo')
|
||||
repo_verb = LogEntryKind.get(name = 'repo_verb')
|
||||
return (LogEntry.select()
|
||||
.where(LogEntry.repository == repository)
|
||||
.where((LogEntry.kind == repo_pull) | (LogEntry.kind == repo_verb))
|
||||
.where(LogEntry.datetime >= since)
|
||||
.count())
|
||||
if since_earlier >= since:
|
||||
raise ValueError('time_delta_earlier must be greater than time_delta')
|
||||
|
||||
# This uses a CASE WHEN inner clause to further filter the count.
|
||||
formatted = since.strftime('%Y-%m-%d')
|
||||
case_query = 'CASE WHEN datetime >= \'%s\' THEN 1 ELSE 0 END' % formatted
|
||||
|
||||
result = (LogEntry.select(fn.Sum(SQL(case_query)), fn.Count(SQL('*')))
|
||||
.where(LogEntry.repository == repository)
|
||||
.where(clause)
|
||||
.where(LogEntry.datetime >= since_earlier)
|
||||
.tuples()
|
||||
.get())
|
||||
|
||||
return (result[0] or 0, result[1] or 0)
|
||||
|
||||
|
||||
def get_repository_pushes(repository, time_delta, time_delta_earlier):
|
||||
push_repo = LogEntryKind.get(name='push_repo')
|
||||
clauses = (LogEntry.kind == push_repo)
|
||||
return _get_repository_events(repository, time_delta, time_delta_earlier, clauses)
|
||||
|
||||
|
||||
def get_repository_pulls(repository, time_delta, time_delta_earlier):
|
||||
repo_pull = LogEntryKind.get(name='pull_repo')
|
||||
repo_verb = LogEntryKind.get(name='repo_verb')
|
||||
clauses = ((LogEntry.kind == repo_pull) | (LogEntry.kind == repo_verb))
|
||||
return _get_repository_events(repository, time_delta, time_delta_earlier, clauses)
|
||||
|
||||
|
||||
def get_repository_usage():
|
||||
one_month_ago = date.today() - timedelta(weeks=4)
|
||||
repo_pull = LogEntryKind.get(name = 'pull_repo')
|
||||
repo_verb = LogEntryKind.get(name = 'repo_verb')
|
||||
repo_pull = LogEntryKind.get(name='pull_repo')
|
||||
repo_verb = LogEntryKind.get(name='repo_verb')
|
||||
return (LogEntry.select(LogEntry.ip, LogEntry.repository)
|
||||
.where((LogEntry.kind == repo_pull) | (LogEntry.kind == repo_verb))
|
||||
.where(~(LogEntry.repository >> None))
|
||||
|
|
Reference in a new issue