def main()

in tools/self-hosted-runner-utils/check_runners_state.py [0:0]


def main() -> None:
    options = parse_args()
    if options.token == "":
        raise Exception("GITHUB_TOKEN or --token must be set")
    gh = Github(options.token)
    repo = gh.get_repo(options.repo)
    runners = repo.get_self_hosted_runners()
    include_pattern = re.compile(options.include)
    state = RunnersState()
    for runner in runners:
        if not include_pattern.match(runner.name):
            continue
        state.num_total += 1
        if runner.status == "online":
            state.num_online += 1
            for label in runner.labels():
                if label.get("type") == "custom" and label:
                    state.num_per_label[str(label["name"])] += 1
                    if runner.busy:
                        state.num_busy_per_label[str(label["name"])] += 1
    over_total = lambda num: f"{num}/{state.num_total}"
    percentage_of = lambda num, label: f"{state.num_busy_per_label[label]}/{num}"
    num_queued_workflows = len([_ for _ in repo.get_workflow_runs(status="queued")])
    num_in_progress_workflows = len(
        [_ for _ in repo.get_workflow_runs(status="in_progress")]
    )
    print(f"Self Hosted stats for {options.repo}")
    print(f"{state.num_total:>15} total runners")
    print(f"{over_total(state.num_online):>15} online runners")
    print()
    print("Number of busy/online runners per label")
    for label, num_label in sorted(state.num_per_label.items()):
        print(f"{percentage_of(num_label, label):>15} {label}")
    print()
    print(f"Workflow stats for {options.repo}")
    print(f"{num_queued_workflows:>15} queued workflows")
    print(f"{num_in_progress_workflows:>15} in_progress workflows")