Skip to content

Instantly share code, notes, and snippets.

@joyeecheung
Last active January 9, 2025 13:07
Show Gist options
  • Select an option

  • Save joyeecheung/6006b7ee781a2a619021767e0c8f2c01 to your computer and use it in GitHub Desktop.

Select an option

Save joyeecheung/6006b7ee781a2a619021767e0c8f2c01 to your computer and use it in GitHub Desktop.
A Python script to generate a chart from Jenkins JSON API of Node.js CI
import json
from datetime import datetime
import matplotlib.pyplot as plt
# Function to convert timestamps to CET
def convert_to_cet(unix_timestamp_ms):
utc_dt = datetime.utcfromtimestamp(unix_timestamp_ms / 1000)
return utc_dt
# Load the JSON file
file_path = './job_data.json' # Replace with your file path
with open(file_path, 'r') as file:
job_data_with_results = json.load(file)
# Initialize counters for each result type per hour
results_by_hour = {
"SUCCESS": [0] * 24,
"FAILURE": [0] * 24,
"UNSTABLE": [0] * 24,
"ABORTED": [0] * 24,
"PENDING": [0] * 24, # For builds with null results
}
# Process the builds
for build in job_data_with_results.get("builds", []):
timestamp = build.get("timestamp")
result = build.get("result", "PENDING") # Default to "PENDING" if result is null
if timestamp:
hour = convert_to_cet(timestamp).hour
if result in results_by_hour:
results_by_hour[result][hour] += 1
else:
results_by_hour["PENDING"][hour] += 1
# Prepare data for plotting
hours = list(range(24))
success_counts = results_by_hour["SUCCESS"]
failure_counts = results_by_hour["FAILURE"]
unstable_counts = results_by_hour["UNSTABLE"]
aborted_counts = results_by_hour["ABORTED"]
pending_counts = results_by_hour["PENDING"]
# Create the stacked bar chart
plt.figure(figsize=(12, 8))
plt.bar(hours, success_counts, color='#66c2a4', label="SUCCESS") # Medium green
plt.bar(hours, failure_counts, bottom=success_counts, color='#fc8d62', label="FAILURE") # Medium red
plt.bar(hours, unstable_counts, bottom=[s + f for s, f in zip(success_counts, failure_counts)], color='#ffd92f', label="UNSTABLE") # Medium yellow
plt.bar(
hours,
aborted_counts,
bottom=[s + f + u for s, f, u in zip(success_counts, failure_counts, unstable_counts)],
color='#b3b3b3',
label="ABORTED", # Medium grey
)
plt.bar(
hours,
pending_counts,
bottom=[s + f + u + a for s, f, u, a in zip(success_counts, failure_counts, unstable_counts, aborted_counts)],
color='#80b1d3', # Medium light blue
label="PENDING",
)
# Add labels and legend
plt.xlabel("Hour (CET)", fontsize=12)
plt.ylabel("Number of Builds", fontsize=12)
plt.title("Hourly Distribution of Build Results (CET)", fontsize=14)
plt.xticks(hours, fontsize=10)
plt.yticks(fontsize=10)
plt.legend(title="Build Result", fontsize=10)
plt.grid(axis="y", linestyle="--", alpha=0.7)
# Display the chart
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment