Last active
July 19, 2018 14:11
-
-
Save veryhappythings/6c74dce3660f605aebcb3280d6c5dcad to your computer and use it in GitHub Desktop.
Python 3 gist to grab the number of currently running builds in our CircleCI
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
from collections import defaultdict | |
import datetime | |
from circleci.api import Api | |
TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ' | |
circleci = Api("<your api key>") | |
build_summaries = circleci.get_project_build_summary( | |
'<project username>', | |
'<project>', | |
vcs_type='bitbucket', | |
limit=100, | |
) | |
counts = defaultdict(int) | |
queue_times = [] | |
for build in build_summaries: | |
if build['status'] in ('running', 'not_running'): | |
counts[build['status']] = counts[build['status']] + 1 | |
if build['status'] != 'canceled': | |
if build['usage_queued_at'] and build['start_time']: | |
queued_at = datetime.datetime.strptime( | |
build['usage_queued_at'], | |
TIMESTAMP_FORMAT, | |
) | |
start_time = datetime.datetime.strptime( | |
build['start_time'], | |
TIMESTAMP_FORMAT, | |
) | |
queue_times.append(start_time - queued_at) | |
average_queue_time = sum(queue_times, datetime.timedelta()) / len(queue_times) | |
print(f'Average Queue Time: {average_queue_time}') | |
print(f'Running: {counts["running"]}') | |
print(f'Queued: {counts["not_running"]}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment