Skip to content

Instantly share code, notes, and snippets.

Created March 3, 2024 23:51
Show Gist options
  • Save fzakaria/b3ca5a1d3700714497925d026bd8105b to your computer and use it in GitHub Desktop.
Save fzakaria/b3ca5a1d3700714497925d026bd8105b to your computer and use it in GitHub Desktop.
Graph a GitHub Workflow using plotnine
import pandas as pd
from plotnine import ggplot, aes, geom_line, theme, labs, scale_x_datetime, element_text, geom_point
import json
from datetime import datetime
# $ gh run list --workflow buildAndTestBazel.yml --repo openxla/stablehlo --json startedAt,updatedAt --status completed --branch main --limit 100 > json_data.json
# Assuming `json_data` is your JSON data loaded into a variable
# If your JSON is in a file, you can load it with:
with open('json_data.json', 'r') as f:
json_data = json.load(f)
# Convert JSON data to DataFrame
df = pd.DataFrame(json_data)
# Convert the startedAt and updatedAt columns to datetime
df['startedAt'] = pd.to_datetime(df['startedAt'])
df['updatedAt'] = pd.to_datetime(df['updatedAt'])
# Calculate the elapsed time in seconds
df['elapsedTime'] = (df['updatedAt'] - df['startedAt']).dt.total_seconds()
# If your datetimes are in UTC, ensure the comparison Timestamp is also in UTC
now = pd.Timestamp.utcnow()
one_month_ago = now - pd.DateOffset(months=1)
# Filter data to the last month, ensuring both datetimes are timezone-aware or both are naive
df_last_month = df[df['startedAt'].dt.tz_localize(None) >= one_month_ago.tz_localize(None)]
# Plot using plotnine
plot = (ggplot(df_last_month, aes('startedAt', 'elapsedTime')) +
geom_line() +
theme(axis_text_x=element_text(rotation=90, hjust=1)) +
labs(x='Date Workflow Started', y='Elapsed Time (seconds)', title='Elapsed Time Series') +
scale_x_datetime(date_breaks='1 day', date_labels='%Y-%m-%d')) + geom_point()
# Display or save the plot
# To display the plot in a Jupyter notebook or similar environment, just use:
# To save the plot to a file, use:"elapsed_time_series.png", width=10, height=6, dpi=300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment