diversity: Factor out policy from data gathering.

The diversity script had a single function that gathered a bunch of
stats from stackalytics and also determined whether the diversity
criteria was met.  Split the policy check out.  This is just a bit of
refactoring in preparation for adding some additional stats and policy
checks based on this code.

While we're at it, rename the script to 'teamstats', since this will
soon be looking at more than diversity.

Change-Id: I7993249b14cbb94a4700de10fc31bc0cd0394cf7
Signed-off-by: Russell Bryant <rbryant@redhat.com>
This commit is contained in:
Russell Bryant 2015-07-31 13:55:50 -04:00
parent 1a0e01fce1
commit 2326a2db16
2 changed files with 56 additions and 36 deletions

88
tools/diversity.py → tools/teamstats.py Executable file → Normal file
View File

@ -48,7 +48,8 @@ def get_core_reviews_by_company(group):
return companies
def get_diversity(project):
def get_diversity_stats(project):
team_stats = {}
# commits by company
group = "%s-group" % project.lower()
commits = s.get('http://stackalytics.com/api/'
@ -62,55 +63,73 @@ def get_diversity(project):
% (group, six_months)).json()
core_reviews_by_company = get_core_reviews_by_company(group)
commits_total = sum([company['metric'] for company in commits['stats']])
commits_top = float(commits['stats'][0]['metric']) / commits_total * 100
commits_top2 = ((float(commits['stats'][0]['metric']) +
float(commits['stats'][1]['metric']))
/ commits_total * 100)
team_stats['commits_top'] = (
float(commits['stats'][0]['metric']) / commits_total * 100)
team_stats['commits_top2'] = ((float(commits['stats'][0]['metric']) +
float(commits['stats'][1]['metric']))
/ commits_total * 100)
reviews_total = sum([company['metric'] for company in reviews['stats']])
reviews_top = float(reviews['stats'][0]['metric']) / reviews_total * 100
reviews_top2 = ((float(reviews['stats'][0]['metric']) +
float(reviews['stats'][1]['metric']))
/ reviews_total * 100)
team_stats['reviews_top'] = (
float(reviews['stats'][0]['metric']) / reviews_total * 100)
team_stats['reviews_top2'] = ((float(reviews['stats'][0]['metric']) +
float(reviews['stats'][1]['metric']))
/ reviews_total * 100)
core_review_values = [company['reviews'] for company in
core_reviews_by_company.itervalues()]
if len(core_review_values) == 1:
core_review_values = [core_review_values[0], 0]
core_review_values.sort(reverse=True)
core_reviews_total = sum(core_review_values)
core_reviews_top = (float(core_review_values[0]) /
core_reviews_total * 100)
core_reviews_top2 = ((float(core_review_values[0]) +
float(core_review_values[1])) /
core_reviews_total * 100)
team_stats['core_reviews_top'] = (
float(core_review_values[0]) / core_reviews_total * 100)
team_stats['core_reviews_top2'] = (
(float(core_review_values[0]) + float(core_review_values[1])) /
core_reviews_total * 100)
core_reviewers_values = [company['reviewers'] for company in
core_reviews_by_company.itervalues()]
if len(core_reviewers_values) == 1:
core_reviewers_values = [core_reviewers_values[0], 0]
core_reviewers_values.sort(reverse=True)
core_reviewers_total = sum(core_reviewers_values)
core_reviewers_top = (float(core_reviewers_values[0]) /
core_reviewers_total * 100)
core_reviewers_top2 = ((float(core_reviewers_values[0]) +
float(core_reviewers_values[1])) /
core_reviewers_total * 100)
print '%-18s (%.2f%% | %.2f%% | %.2f%% | %.2f%%)' % (
project, commits_top, reviews_top, core_reviews_top,
core_reviewers_top)
print '%-18s (%.2f%% | %.2f%% | %.2f%% | %.2f%%)' % (
'', commits_top2, reviews_top2, core_reviews_top2,
core_reviewers_top2)
is_diverse = (commits_top <= 50 and reviews_top <= 50 and
core_reviews_top <= 50 and core_reviewers_top <= 50 and
commits_top2 <= 80 and reviews_top2 <= 80 and
core_reviews_top2 <= 80 and core_reviewers_top2 <= 80)
team_stats['core_reviewers_top'] = (
float(core_reviewers_values[0]) / core_reviewers_total * 100)
team_stats['core_reviewers_top2'] = (
(float(core_reviewers_values[0]) + float(core_reviewers_values[1])) /
core_reviewers_total * 100)
return team_stats
def get_diversity(team):
team_stats = get_diversity_stats(team)
is_diverse = all((
(team_stats['commits_top'] <= 50),
(team_stats['reviews_top'] <= 50),
(team_stats['core_reviews_top'] <= 50),
(team_stats['core_reviewers_top'] <= 50),
(team_stats['commits_top2'] <= 80),
(team_stats['reviews_top2'] <= 80),
(team_stats['core_reviews_top2'] <= 80),
(team_stats['core_reviewers_top2'] <= 80),
))
return is_diverse
def print_diversity(team):
team_stats = get_diversity_stats(team)
print '%-18s (%.2f%% | %.2f%% | %.2f%% | %.2f%%)' % (
team, team_stats['commits_top'], team_stats['reviews_top'],
team_stats['core_reviews_top'], team_stats['core_reviewers_top'])
print '%-18s (%.2f%% | %.2f%% | %.2f%% | %.2f%%)' % (
'', team_stats['commits_top2'], team_stats['reviews_top2'],
team_stats['core_reviews_top2'], team_stats['core_reviewers_top2'])
class ValidateDiversity(base.ValidatorBase):
@staticmethod
def validate(team):
"""Return True of team should contain the tag 'team:diverse-affiliation'"""
"""Return True of team should have 'team:diverse-affiliation'"""
return get_diversity(team)
@staticmethod
@ -119,15 +138,16 @@ class ValidateDiversity(base.ValidatorBase):
def main():
filename = os.path.abspath('reference/projects.yaml')
with open(filename, 'r') as f:
projects = [k for k in yaml.load(f.read())]
projects.sort()
print '<Team> (top commit % | top review % | top core review % | ' \
'top core reviewer %)'
print ' (top 2 commit % | top 2 review % | top 2 core review % | ' \
'top 2 core reviewer %)'
filename = os.path.abspath('reference/projects.yaml')
with open(filename, 'r') as f:
projects = yaml.load(f.read())
for project in projects:
get_diversity(project)
print_diversity(project)
if __name__ == '__main__':

View File

@ -21,8 +21,8 @@ and reorder projects.yaml
"""
import diversity
import stable
import teamstats
import requests
import yaml
@ -33,7 +33,7 @@ import urllib
# List of modules to validate team based tags
team_validators = [
diversity.ValidateDiversity,
teamstats.ValidateDiversity,
]
# List of modules to validate repository based tags