Skip to content

Instantly share code, notes, and snippets.

@glombard
Last active July 5, 2022 02:04
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save glombard/f856659f604593b535c2 to your computer and use it in GitHub Desktop.
Save glombard/f856659f604593b535c2 to your computer and use it in GitHub Desktop.
Plot a graph of Jenkins job build durations over time
import logging
import datetime
import time
from dateutil import tz
import matplotlib.pyplot as plt
import pytz
import requests
logging.captureWarnings(True)
def get_timestamp(ts):
t = datetime.datetime(*time.gmtime(ts / 1000.0)[:6])
return pytz.utc.localize(t).astimezone(tz.tzlocal())
url = 'https://my-jenkins/job/my-job/api/json?tree=allBuilds[timestamp,duration]'
response = requests.get(url)
data = response.json()
builds = data['allBuilds']
x = [get_timestamp(e['timestamp']) for e in builds]
y = [e['duration'] / 60000 for e in builds]
plt.plot(x, y)
plt.gcf().autofmt_xdate()
plt.show()
@glombard
Copy link
Author

The result might look something like:

image

@jhyry-gcpud
Copy link

Works like a charm :) Thank you for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment