-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircletimes.py
77 lines (65 loc) · 2.42 KB
/
circletimes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#
# Depends on a patch to the python circleci tool:
# https://github.com/qba73/circleclient/pull/4
#
# Run on command line like so:
# CIRCLECI_API_TOKEN="" CIRCLECI_REPO_ORG="" CIRCLECI_REPO_NAME="" CIRCLECI_BRANCH="" python circletimes.py
#
from circleclient import circleclient
import datetime
from matplotlib import pyplot, ticker
import numpy
import os
def get_builds():
TOKEN = os.environ['CIRCLECI_API_TOKEN']
REPO_ORG = os.environ['CIRCLECI_REPO_ORG']
REPO_NAME = os.environ['CIRCLECI_REPO_NAME']
BRANCH = os.environ['CIRCLECI_BRANCH']
client = circleclient.CircleClient(TOKEN)
builds = []
for x in range(0, 40):
b = get_builds_(client, REPO_ORG, REPO_NAME, BRANCH, x*100)
if not b:
break
builds.extend(b)
return builds
def get_builds_(client, repo_org, repo_name, branch_name, offset):
builds = client.build.recent(repo_org, repo_name,
branch=branch_name,
limit=100,
filter="successful",
offset=offset)
return builds
def create_dataset(builds):
xseries = []
buildtimes = []
for build in builds:
buildtimes.append(build["build_time_millis"]/1000.0/60.0)
#2015-01-08T23:45:35.452Z
format = "%Y-%m-%dT%H:%M:%S.%fZ"
date_object = datetime.datetime.strptime(build["start_time"], format)
xseries.append(date_object)
return numpy.array(buildtimes), numpy.array(xseries)
def moving_average(values, window):
weigths = numpy.repeat(1.0, window)/window
# numpy array
return numpy.convolve(values, weigths, 'valid')
def plot(buildtimes, xseries, moving_avg):
pyplot.plot(xseries, buildtimes, 'k.')
pyplot.plot(xseries[:len(moving_avg)], moving_avg, 'r')
pyplot.xlim(min(xseries), max(xseries))
pyplot.ylim(0, max(buildtimes)+1)
pyplot.ylabel("Build time in minutes")
pyplot.grid(True)
pyplot.gcf().autofmt_xdate()
import matplotlib.dates as mdates
pyplot.gca().fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
pyplot.title("Time in minutes of successful builds with moving average")
pyplot.show()
def create_xseries(dataset):
return range(1, len(dataset))
if __name__ == "__main__":
builds = get_builds()
buildtimes, xseries = create_dataset(builds)
moving_avg = moving_average(buildtimes, 10)
plot(buildtimes, xseries, moving_avg)