Change repo stats to use the RAC table and a nice UI
This commit is contained in:
parent
dbe14fe729
commit
853cca35f3
10 changed files with 184 additions and 125 deletions
|
@ -4,12 +4,11 @@ import logging
|
|||
import datetime
|
||||
import features
|
||||
|
||||
from datetime import timedelta
|
||||
from datetime import timedelta, datetime
|
||||
|
||||
from flask import request, abort
|
||||
|
||||
from data import model
|
||||
from data.database import Repository as RepositoryTable
|
||||
from endpoints.api import (truthy_bool, format_date, nickname, log_action, validate_json_request,
|
||||
require_repo_read, require_repo_write, require_repo_admin,
|
||||
RepositoryParamResource, resource, query_param, parse_args, ApiResource,
|
||||
|
@ -29,6 +28,7 @@ from util.names import REPOSITORY_NAME_REGEX
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
REPOS_PER_PAGE = 100
|
||||
MAX_DAYS_IN_3_MONTHS = 92
|
||||
|
||||
def check_allowed_private_repos(namespace):
|
||||
""" Checks to see if the given namespace has reached its private repository limit. If so,
|
||||
|
@ -180,7 +180,7 @@ class RepositoryList(ApiResource):
|
|||
last_modified_map = model.repository.get_when_last_modified(repository_ids)
|
||||
|
||||
if parsed_args['popularity']:
|
||||
action_count_map = model.repository.get_action_counts(repository_ids)
|
||||
action_sum_map = model.log.get_repositories_action_sums(repository_ids)
|
||||
|
||||
# Collect the IDs of the repositories that are starred for the user, so we can mark them
|
||||
# in the returned results.
|
||||
|
@ -203,7 +203,7 @@ class RepositoryList(ApiResource):
|
|||
repo['last_modified'] = last_modified_map.get(repo_id)
|
||||
|
||||
if parsed_args['popularity']:
|
||||
repo['popularity'] = action_count_map.get(repo_id, 0)
|
||||
repo['popularity'] = action_sum_map.get(repo_id, 0)
|
||||
|
||||
if username:
|
||||
repo['is_starred'] = repo_id in star_set
|
||||
|
@ -236,7 +236,7 @@ class Repository(RepositoryParamResource):
|
|||
}
|
||||
|
||||
@parse_args()
|
||||
@query_param('includeStats', 'Whether to include pull and push statistics', type=truthy_bool,
|
||||
@query_param('includeStats', 'Whether to include action statistics', type=truthy_bool,
|
||||
default=False)
|
||||
@require_repo_read
|
||||
@nickname('getRepo')
|
||||
|
@ -252,7 +252,7 @@ class Repository(RepositoryParamResource):
|
|||
}
|
||||
|
||||
if tag.lifetime_start_ts > 0:
|
||||
last_modified = format_date(datetime.datetime.fromtimestamp(tag.lifetime_start_ts))
|
||||
last_modified = format_date(datetime.fromtimestamp(tag.lifetime_start_ts))
|
||||
tag_info['last_modified'] = last_modified
|
||||
|
||||
return tag_info
|
||||
|
@ -270,22 +270,28 @@ class Repository(RepositoryParamResource):
|
|||
is_public = model.repository.is_repository_public(repo)
|
||||
|
||||
if parsed_args['includeStats']:
|
||||
(pull_today, pull_thirty_day) = model.log.get_repository_pulls(repo, timedelta(days=1),
|
||||
timedelta(days=30))
|
||||
stats = []
|
||||
found_dates = {}
|
||||
|
||||
(push_today, push_thirty_day) = model.log.get_repository_pushes(repo, timedelta(days=1),
|
||||
timedelta(days=30))
|
||||
start_date = datetime.now() - timedelta(days=MAX_DAYS_IN_3_MONTHS)
|
||||
counts = model.log.get_repository_action_counts(repo, start_date)
|
||||
for count in counts:
|
||||
stats.append({
|
||||
'date': count.date.isoformat(),
|
||||
'count': count.count,
|
||||
})
|
||||
|
||||
stats = {
|
||||
'pulls': {
|
||||
'today': pull_today,
|
||||
'thirty_day': pull_thirty_day
|
||||
},
|
||||
'pushes': {
|
||||
'today': push_today,
|
||||
'thirty_day': push_thirty_day
|
||||
}
|
||||
}
|
||||
found_dates['%s/%s' % (count.date.month, count.date.day)] = True
|
||||
|
||||
# Fill in any missing stats with zeros.
|
||||
for day in range(-31, MAX_DAYS_IN_3_MONTHS):
|
||||
day_date = datetime.now() - timedelta(days=day)
|
||||
key = '%s/%s' % (day_date.month, day_date.day)
|
||||
if not key in found_dates:
|
||||
stats.append({
|
||||
'date': day_date.date().isoformat(),
|
||||
'count': 0,
|
||||
})
|
||||
|
||||
repo_data = {
|
||||
'namespace': namespace,
|
||||
|
|
Reference in a new issue