chendl/compositional_test
1
1import argparse2import math3import traceback4 5import dateutil.parser as date_parser6import requests7 8 9def extract_time_from_single_job(job):10 """Extract time info from a single job in a GitHub Actions workflow run"""11 12 job_info = {}13 14 start = job["started_at"]15 end = job["completed_at"]16 17 start_datetime = date_parser.parse(start)18 end_datetime = date_parser.parse(end)19 20 duration_in_min = round((end_datetime - start_datetime).total_seconds() / 60.0)21 22 job_info["started_at"] = start23 job_info["completed_at"] = end24 job_info["duration"] = duration_in_min25 26 return job_info27 28 29def get_job_time(workflow_run_id, token=None):30 """Extract time info for all jobs in a GitHub Actions workflow run"""31 32 headers = None33 if token is not None:34 headers = {"Accept": "application/vnd.github+json", "Authorization": f"Bearer {token}"}35 36 url = f"https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100"37 result = requests.get(url, headers=headers).json()38 job_time = {}39 40 try:41 job_time.update({job["name"]: extract_time_from_single_job(job) for job in result["jobs"]})42 pages_to_iterate_over = math.ceil((result["total_count"] - 100) / 100)43 44 for i in range(pages_to_iterate_over):45 result = requests.get(url + f"&page={i + 2}", headers=headers).json()46 job_time.update({job["name"]: extract_time_from_single_job(job) for job in result["jobs"]})47 48 return job_time49 except Exception:50 print(f"Unknown error, could not fetch links:\n{traceback.format_exc()}")51 52 return {}53 54 55if __name__ == "__main__":56 r"""57 Example:58 59 python get_github_job_time.py --workflow_run_id 294560951760 """61 62 parser = argparse.ArgumentParser()63 # Required parameters64 parser.add_argument("--workflow_run_id", type=str, required=True, help="A GitHub Actions workflow run id.")65 args = parser.parse_args()66 67 job_time = get_job_time(args.workflow_run_id)68 job_time = dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True))69 70 for k, v in job_time.items():71 print(f'{k}: {v["duration"]}')72 