#!/usr/bin/env bash
# Measures where a repository's GitHub Actions time goes: billed minutes, the
# wait from push to the last job, queue time, the slowest steps across many
# runs, and the work thrown away. Needs gh (signed in, read access) and jq.
#
#   measure.sh OWNER/REPO [--days 14] [--max-runs 300] [--workflow ci.yml] [--branch main] [--out .ci-measure]
#
# Run it once before changing anything and again after, with the same flags,
# and compare the two reports.
set -euo pipefail

usage() { sed -n '2,9p' "$0" | sed 's/^# \{0,1\}//'; exit 2; }

[ $# -ge 1 ] || usage
repo=$1
shift
days=14
max_runs=300
workflow=""
branch=""
out=".ci-measure"
while [ $# -gt 0 ]; do
    case $1 in
        --days) days=$2; shift 2 ;;
        --max-runs) max_runs=$2; shift 2 ;;
        --workflow) workflow=$2; shift 2 ;;
        --branch) branch=$2; shift 2 ;;
        --out) out=$2; shift 2 ;;
        *) usage ;;
    esac
done

for tool in gh jq; do
    command -v "$tool" >/dev/null || { echo "measure.sh needs $tool" >&2; exit 1; }
done

# GNU date first, then BSD's (macOS).
since=$(date -u -d "-$days days" +%F 2>/dev/null || date -u -v-"$days"d +%F)
mkdir -p "$out"

echo "Reading runs of $repo since $since…" >&2
args=(-R "$repo" --created ">=$since" --limit 1000 --json databaseId,workflowName,event,status,conclusion,createdAt,headBranch,attempt)
[ -n "$workflow" ] && args+=(--workflow "$workflow")
[ -n "$branch" ] && args+=(--branch "$branch")
gh run list "${args[@]}" >"$out/runs.json"

total=$(jq '[.[] | select(.status == "completed")] | length' "$out/runs.json")
sampled=$(( total < max_runs ? total : max_runs ))
echo "Reading the jobs of $sampled of $total completed runs…" >&2

# Every attempt's jobs (filter=all), so re-runs count towards what was billed.
jq -r --argjson n "$sampled" '[.[] | select(.status == "completed")] | .[:$n] | .[].databaseId' "$out/runs.json" |
    while read -r id; do
        gh api "repos/$repo/actions/runs/$id/jobs?filter=all&per_page=100" --paginate -q '.jobs[]' || true
    done | jq -s '.' >"$out/jobs.json"

gh cache list -R "$repo" --limit 1000 --json key,ref,sizeInBytes,lastAccessedAt >"$out/caches.json" 2>/dev/null || echo '[]' >"$out/caches.json"

jq -r --argjson days "$days" --argjson total "$total" --argjson sampled "$sampled" \
    --slurpfile runs "$out/runs.json" --slurpfile caches "$out/caches.json" '
def secs(a; b): if a and b then ((b | fromdate) - (a | fromdate)) else 0 end;
def pct(p): sort | if length == 0 then 0 else .[((length - 1) * p | floor)] end;
def mins: . / 60 | . * 10 | round / 10;
def fmt: if . >= 60 then "\(. / 60 | floor)m\(. % 60 | floor)s" else "\(. | floor)s" end;
# GitHub bills each job rounded up to the minute, Windows twice and macOS ten
# times. Self-hosted and third-party runners bill their own way: the figure is
# then runner time, not GitHub money.
def multiplier: (.labels | join(" ") | ascii_downcase) as $l
    | if ($l | test("macos")) then 10 elif ($l | test("windows")) then 2 else 1 end;
def rounded: secs(.started_at; .completed_at) as $s | if $s <= 0 then 0 else ($s / 60) | ceil end;
def billed: rounded * multiplier;
def table: (.[0] | keys_unsorted | join("\t")), (.[] | [.[] | tostring] | join("\t"));
# Dependabot and Copilot name every run after what it updates or reviews: one
# row for all of them reads better.
def wfname(event): if event == "dynamic" then "Dependabot and Copilot" else . end;
def short: if length > 56 then .[:53] + "..." else . end;

($runs[0] | map({key: (.databaseId | tostring), value: .}) | from_entries) as $byRun
| ($runs[0] | map(select(.status == "completed") | .event as $e | "\(.workflowName | wfname($e)) [\($e)]")
    | group_by(.) | map({key: .[0], value: length}) | from_entries) as $runsPerWorkflow
| [.[] | select(.started_at != null)
    | ($byRun[(.run_id | tostring)].event // "?") as $e
    | . + {event: $e, wf: "\(.workflow_name | wfname($e)) [\($e)]", key: "\(.workflow_name | wfname($e)) / \(.name)"}] as $jobs
# What the runs not read would have billed: each workflow scaled by how many
# of its runs were read, so a few long runs do not stand for many short ones.
| ($jobs | group_by(.wf) | map({wf: .[0].wf, billed: (map(billed) | add), read: (map(.run_id) | unique | length)})
    | map(.billed * ($runsPerWorkflow[.wf] // .read) / .read) | add // 0) as $estimate
| ($jobs | map(secs(.started_at; .completed_at)) | add // 0) as $raw
| ($jobs | map(billed) | add // 0) as $billed
| ($jobs | map(rounded) | add // 0) as $rounded
| ($jobs | map(select(.conclusion == "cancelled" or .conclusion == "failure") | billed) | add // 0) as $wasted
| ($jobs | map(select(.labels | join(" ") | test("self-hosted"))) | length) as $selfHosted

| "== Overview (\($days) days, jobs of \($sampled) of \($total) runs read) ==",
  "Billed minutes, runs read:  \($billed)",
  "Billed minutes, window:     about \($estimate | round)  (about \($estimate * 30 / $days | round) a month)",
  "Lost to rounding up:        \(($rounded - ($raw / 60)) | round) of \($rounded) min (\(if $rounded > 0 then (($rounded - $raw / 60) / $rounded * 100 | round) else 0 end)%, before OS multipliers)",
  "Cancelled or failed jobs:   \($wasted) min",
  (if $selfHosted > 0 then "Self-hosted jobs:           \($selfHosted) (runner time, not GitHub billing)" else empty end),
  "",

  "== Wait from push to last job, successful first attempts ==",
  ($jobs | map(select(.run_attempt == 1)) | group_by(.run_id)
    | map({run: $byRun[(.[0].run_id | tostring)], end: (map(.completed_at) | max)})
    | map(select(.run != null and .run.conclusion == "success"))
    | map(.run.event as $e | {wf: "\(.run.workflowName | wfname($e)) [\($e)]", wait: secs(.run.createdAt; .end)})
    | group_by(.wf)
    | map({workflow: .[0].wf, runs: length, p50: (map(.wait) | pct(0.5) | fmt), p90: (map(.wait) | pct(0.9) | fmt)})
    | sort_by(-.runs) | table),
  "",

  "== Billed minutes by workflow and event ==",
  ($jobs | group_by(.wf)
    | map({workflow: .[0].wf, runs_read: (map(.run_id) | unique | length), runs_in_window: ($runsPerWorkflow[.[0].wf] // 0),
        billed_min: (map(billed) | add), share_of_read: "\((map(billed) | add) / ([$billed, 1] | max) * 100 | round)%"})
    | sort_by(-.billed_min) | table),
  "",

  "== Jobs, by billed minutes (queue = created to started) ==",
  ($jobs | group_by(.key)
    | map({job: (.[0].key | short), runs: length,
        p50: (map(secs(.started_at; .completed_at)) | pct(0.5) | fmt),
        p90: (map(secs(.started_at; .completed_at)) | pct(0.9) | fmt),
        max: (map(secs(.started_at; .completed_at)) | max | fmt),
        queue_p50: (map(secs(.created_at; .started_at)) | pct(0.5) | fmt),
        billed_min: (map(billed) | add),
        labels: (.[0].labels | join(","))})
    | sort_by(-.billed_min) | .[:25] | table),
  "",

  "== Slowest steps, median across successful runs ==",
  ($jobs | map(select(.conclusion == "success")) | map(. as $j | .steps[]?
      | select(.started_at != null and .completed_at != null and .conclusion == "success")
      | {key: ("\($j.key) / \(.name)" | short), s: secs(.started_at; .completed_at)})
    | group_by(.key)
    | map({step: .[0].key, runs: length, p50: (map(.s) | pct(0.5)), p90: (map(.s) | pct(0.9))})
    | sort_by(-.p50) | .[:25]
    | map(.p50 |= fmt | .p90 |= fmt) | table),
  "",

  "== Runs per pull request branch ==",
  ($runs[0] | map(select(.event == "pull_request")) | group_by(.headBranch) | map(length) as $c
    | if ($c | length) == 0 then "No pull request runs in the window."
      else "branches: \($c | length)  runs per branch p50: \($c | pct(0.5))  p90: \($c | pct(0.9))  max: \($c | max)" end),
  ($runs[0] | map(select(.status == "completed")) | group_by(.conclusion) | map("\(.[0].conclusion): \(length) runs") | join("  ")),
  "",

  "== Actions cache ==",
  ($caches[0] | if length == 0 then "No cache entries (or no access to read them)."
    else "entries: \(length)  total: \((map(.sizeInBytes) | add) / 1073741824 * 100 | round / 100) GB  on pull request refs: \(map(select(.ref | startswith("refs/pull/"))) | length)",
         (sort_by(-.sizeInBytes) | .[:10] | map({key: .key[:70], ref, mb: (.sizeInBytes / 1048576 | round), last_used: .lastAccessedAt[:10]}) | table) end)
' "$out/jobs.json" |
    # Each section lines up its own columns.
    awk -v RS= '{ print | "column -t -s \"\t\""; close("column -t -s \"\t\""); print "" }' | tee "$out/report.txt"

echo >&2
echo "Raw data in $out/ (runs.json, jobs.json, caches.json); this report in $out/report.txt." >&2
