Skip to content

Instantly share code, notes, and snippets.

@gastonfournier
Created October 2, 2024 10:23
Show Gist options
  • Save gastonfournier/70aec9aecf601a33196e98a8862fd34d to your computer and use it in GitHub Desktop.
Save gastonfournier/70aec9aecf601a33196e98a8862fd34d to your computer and use it in GitHub Desktop.
Lead time in hours between deployments avg MoM
#!/bin/bash
# Get the list of tags with their creation dates in ISO 8601 format
tags=$(git for-each-ref --sort=creatordate --format '%(creatordate:iso8601) %(refname:short)' refs/tags | egrep "v[0-9]+\.[0-9]+\.[0-9]+$")
# Convert the list to an array
IFS=$'\n' read -rd '' -a tag_array <<< "$tags"
declare -A month_diffs
declare -A month_counts
prev_date=""
prev_month=""
# Loop through the tags and calculate time differences
for ((i=1; i<${#tag_array[@]}; i++)); do
# Get the current tag's date and the previous tag's date
curr_date=$(echo "${tag_array[i]}" | awk '{print $1}')
curr_month=$(date -d "$curr_date" '+%Y-%m') # Get the year and month (e.g., 2023-09)
if [[ -n "$prev_date" && "$prev_month" == "$curr_month" ]]; then
# Convert dates to Unix timestamps
prev_timestamp=$(date -d "$prev_date" +%s)
curr_timestamp=$(date -d "$curr_date" +%s)
# Calculate the difference in seconds
diff=$((curr_timestamp - prev_timestamp))
# Add the difference to the month_diffs array for the current month
month_diffs["$curr_month"]=$((month_diffs["$curr_month"] + diff))
month_counts["$curr_month"]=$((month_counts["$curr_month"] + 1))
fi
# Set current values as previous for the next iteration
prev_date="$curr_date"
prev_month="$curr_month"
done
# Output the month-over-month average time between releases in hours
echo "Month-by-month average time between releases (in hours):"
for month in "${!month_diffs[@]}"; do
if [[ ${month_counts[$month]} -gt 0 ]]; then
avg_diff=$((month_diffs[$month] / month_counts[$month]))
avg_diff_hours=$((avg_diff / 3600)) # Convert seconds to hours
echo "$month: $avg_diff_hours hours"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment