<commit_before>#!/usr/bin/python

import argparse
import datetime
import json
import subprocess
import sys

import numpy as np
import matplotlib.pyplot as plt


def exec_cmd(command):
    process = subprocess.Popen(
        command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    output, error = process.communicate()

    return output, error


parser = argparse.ArgumentParser(
    description='Generate a graph decipting how long it took patches to get '
                'merged over time for a given user and project.')
parser.add_argument(
    'owner',
    help='The Gerrit username. For example amuller.')
parser.add_argument(
    'project',
    help='The OpenStack project to query. For example openstack/neutron.')
args = parser.parse_args()


def get_json_data_from_query(query):
    result, error = exec_cmd(
        'ssh -p 29418 review.openstack.org gerrit query %s --format=json' %
        query)

    if error:
        print error
        sys.exit(1)

    lines = result.split('\n')
    data = [json.loads(line) for line in lines[:-2]]

    if not data:
        print 'No patches found!'
        sys.exit(1)

    print 'Found metadata for %s patches' % len(data)
    data = sorted(data, key=lambda x: x['createdOn'])
    return data


def get_points_from_data(data):
    points = []

    for patch in data:
        creation = datetime.date.fromtimestamp(patch['createdOn'])
        updated = datetime.date.fromtimestamp(patch['lastUpdated'])
        x_value = (creation - start).days
        y_value = (updated - creation).days
        # Gerrit has a weird issue where some old patches have a bogus
        # createdOn value
        if y_value > 0:
            points.append((x_value, y_value))

    return points


def filter_above_percentile(points, percentile):
    percentile = np.percentile([point[1] for point in points], percentile)
    return [point for point in points if point[1] < percentile]


data = get_json_data_from_query(
    'status:merged branch:master owner:%(owner)s project:%(project)s' %
    {'owner': args.owner, 'project': args.project})

start = datetime.date.fromtimestamp(data[0]['createdOn'])

points = get_points_from_data(data)

points = filter_above_percentile(points, 95)

x = [point[0] for point in points]
y = [point[1] for point in points]

plt.xlabel('%s - %s' % (data[0]['owner']['name'], args.project))
plt.ylabel('Days to merge patch')
plt.grid()

# Generate a linear regression line
regression_line = np.polyfit(x, y, 1)<commit_msg>Allow a list of owners<commit_after>#!/usr/bin/python

import argparse
import datetime
import json
import subprocess
import sys

import numpy as np
import matplotlib.pyplot as plt


def exec_cmd(command):
    process = subprocess.Popen(
        command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    output, error = process.communicate()

    return output, error


parser = argparse.ArgumentParser(
    description='Generate a graph decipting how long it took patches to get '
                'merged over time for a given user(s) and project.')
parser.add_argument(
    'project',
    help='The OpenStack project to query. For example openstack/neutron.')
parser.add_argument(
    'owner',
    nargs='*',
    help='A list of one or more Gerrit usernames. For example foo bar.')
args = parser.parse_args()


def get_json_data_from_query(query):
    result, error = exec_cmd(
        'ssh -p 29418 review.openstack.org gerrit query %s --format=json' %
        query)

    print query

    if error:
        print error
        sys.exit(1)

    lines = result.split('\n')
    data = [json.loads(line) for line in lines[:-2]]

    if not data:
        print 'No patches found!'
        sys.exit(1)

    print 'Found metadata for %s patches' % len(data)
    data = sorted(data, key=lambda x: x['createdOn'])
    return data


def get_points_from_data(data):
    points = []

    for patch in data:
        creation = datetime.date.fromtimestamp(patch['createdOn'])
        updated = datetime.date.fromtimestamp(patch['lastUpdated'])
        x_value = (creation - start).days
        y_value = (updated - creation).days
        # Gerrit has a weird issue where some old patches have a bogus
        # createdOn value
        if y_value > 0:
            points.append((x_value, y_value))

    return points


def filter_above_percentile(points, percentile):
    percentile = np.percentile([point[1] for point in points], percentile)
    return [point for point in points if point[1] < percentile]


def get_list_of_owners(people):
    people_query = ''
    for person in people:
        people_query += 'owner:%s OR ' % person
    return people_query[:-4]


data = get_json_data_from_query(
    "status:merged branch:master project:%(project)s \(%(owner)s\)" %
    {'project': args.project,
     'owner': get_list_of_owners(args.owner)})

start = datetime.date.fromtimestamp(data[0]['createdOn'])

points = get_points_from_data(data)

points = filter_above_percentile(points, 95)

x = [point[0] for point in points]
y = [point[1] for point in points]

plt.xlabel('%s - %s' % (data[0]['owner']['name'], args.project))
plt.ylabel('Days to merge patch')
plt.grid()

# Generate a linear regression line
regression_line = np.polyfit(x, y, 1)
{'fraction_non_alphanumeric': 0.11383971064153817, 'fraction_numerical': 0.0070435941366838, 'mean_word_length': 3.338563170933113, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '607896', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1614, 'n_words': 481}