Created
February 8, 2023 20:09
-
-
Save makkes/8311598a2b65fe1bf3612f202ed148ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import datetime | |
import numpy as np | |
import os | |
import json | |
import sys | |
repo = "weaveworks/weave-gitops" # Replace with the name of the repository | |
access_token = "<access_token>" # Replace with your GitHub access token | |
def get_issues(repo, state): | |
filename = f"{repo.replace('/', '_')}_{state}_issues.json" | |
if os.path.exists(filename): | |
with open(filename, "rb") as f: | |
return json.load(f) | |
issues = [] | |
page = 1 | |
while True: | |
response = requests.get( | |
f"https://api.github.com/repos/{repo}/issues?state={state}&page={page}", | |
headers={"Authorization": f"Token {access_token}"} | |
) | |
if response.status_code != 200: | |
raise Exception("Failed to fetch issues") | |
data = response.json() | |
if not data: | |
break | |
issues += data | |
page += 1 | |
with open(filename, "w") as f: | |
json.dump(issues, f) | |
return issues | |
def calculate_lead_time(issue): | |
created_at = datetime.datetime.strptime(issue["created_at"], "%Y-%m-%dT%H:%M:%SZ") | |
if issue["state"] == "closed": | |
closed_at = datetime.datetime.strptime(issue["closed_at"], "%Y-%m-%dT%H:%M:%SZ") | |
return (closed_at - created_at).total_seconds() | |
return None | |
closed_issues = get_issues(repo, "closed") | |
def label_filter(issue): | |
if len(sys.argv) >= 2: | |
return all(label in map(lambda label: label["name"], issue["labels"]) for label in sys.argv[1:]) | |
return True | |
after_date = datetime.datetime(2022, 10, 1) | |
lead_times = [ | |
calculate_lead_time(issue) | |
for issue in closed_issues | |
if "closed_at" in issue | |
and datetime.datetime.strptime(issue["closed_at"], "%Y-%m-%dT%H:%M:%SZ") >= after_date | |
and label_filter(issue) | |
] | |
lead_times = [lead_time for lead_time in lead_times if lead_time is not None] | |
percentiles = [50, 75, 90, 95, 99] | |
percentile_values = np.percentile(lead_times, percentiles) | |
def human_readable_time(seconds): | |
minutes = int(seconds / 60) % 60 | |
hours = int(seconds / 3600) % 24 | |
days = int(seconds / 86400) | |
return f'{days}d {hours}h {minutes}m {seconds%60:.0f}s' | |
print(f"lead time stats from {len(lead_times)} issues closed after {after_date}\n") | |
for i, percentile in enumerate(percentiles): | |
print(f"{percentile}th percentile: {human_readable_time(percentile_values[i])}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment