This commit is contained in:
Bruno Alexandre Rosa 2026-03-14 18:57:18 -03:00
parent 4d1a7698aa
commit 1d57d46210

View file

@ -12,6 +12,16 @@ set -eo pipefail
LIMIT=1000 LIMIT=1000
MARKDOWN=false MARKDOWN=false
# Group cache keys by first segment (before "-" or "/"), sum sizes, output prefix, count, GiB (TSV).
JQ_GROUPED_TSV='
def prefix: (if (.key | test("/")) then (.key | split("/")[0]) else (.key | split("-")[0]) end);
def to_gib: . / 1024 / 1024 / 1024 | . * 100 | floor / 100;
map(. + {prefix: prefix}) | group_by(.prefix) |
map({prefix: .[0].prefix, count: length, totalBytes: (map(.sizeInBytes) | add // 0)}) |
sort_by(-.totalBytes) |
.[] | "\(.prefix)\t\(.count)\t\(.totalBytes | to_gib)"
'
if ! command -v gh &>/dev/null; then if ! command -v gh &>/dev/null; then
echo "Error: gh (GitHub CLI) is required. Install from https://cli.github.com/" >&2 echo "Error: gh (GitHub CLI) is required. Install from https://cli.github.com/" >&2
exit 1 exit 1
@ -29,66 +39,40 @@ while [[ $# -gt 0 ]]; do
done done
if [[ $# -eq 0 ]]; then if [[ $# -eq 0 ]]; then
# Grouped mode: all caches, group by first segment (before - or /) echo "Grouping all caches by prefix..."
json=$(gh cache list --limit "$LIMIT" --json key,sizeInBytes 2>/dev/null) || { json=$(gh cache list --limit "$LIMIT" --json key,sizeInBytes 2>/dev/null) || {
echo "Error: gh cache list failed (check gh auth or repo access)" >&2 echo "Error: gh cache list failed (check gh auth or repo access)" >&2
exit 1 exit 1
} }
# Add prefix field, group by prefix, sum count and size, sort by size desc; output GiB (2 decimals) rows=$(echo "$json" | jq -r "$JQ_GROUPED_TSV")
total_entries=$(echo "$json" | jq 'length')
total_bytes=$(echo "$json" | jq 'map(.sizeInBytes) | add // 0')
else
echo "Reporting caches for prefix: $KEY_PREFIX"
KEY_PREFIX="${1:-buildkit-blob}"
json=$(gh cache list --key "$KEY_PREFIX" --limit "$LIMIT" --json key,sizeInBytes 2>/dev/null) || {
echo "Error: gh cache list failed (check gh auth or repo access)" >&2
exit 1
}
total_entries=$(echo "$json" | jq 'length') total_entries=$(echo "$json" | jq 'length')
total_bytes=$(echo "$json" | jq 'map(.sizeInBytes) | add // 0') total_bytes=$(echo "$json" | jq 'map(.sizeInBytes) | add // 0')
total_gib=$(echo "$total_bytes" | jq -r '. / 1024 / 1024 / 1024 | . * 100 | floor / 100')
if [[ "$MARKDOWN" == true ]]; then
echo "| PREFIX | COUNT | SIZE (GiB) |"
echo "|--------|------:|----------:|"
while IFS=$'\t' read -r prefix count gib; do
echo "| $prefix | $count | $gib |"
done < <(echo "$json" | jq -r '
def prefix: (if (.key | test("/")) then (.key | split("/")[0]) else (.key | split("-")[0]) end);
def to_gib: . / 1024 / 1024 / 1024 | . * 100 | floor / 100;
map(. + {prefix: prefix}) | group_by(.prefix) |
map({prefix: .[0].prefix, count: length, totalBytes: (map(.sizeInBytes) | add // 0)}) |
sort_by(-.totalBytes) |
.[] | "\(.prefix)\t\(.count)\t\(.totalBytes | to_gib)"
')
echo "| **Total** | $total_entries | $total_gib |"
else
printf "%-12s %6s %10s\n" "PREFIX" "COUNT" "SIZE (GiB)"
printf "%-12s %6s %10s\n" "------------" "------" "----------"
while IFS=$'\t' read -r prefix count gib; do
printf "%-12s %6s %10.2f\n" "$prefix" "$count" "$gib"
done < <(echo "$json" | jq -r '
def prefix: (if (.key | test("/")) then (.key | split("/")[0]) else (.key | split("-")[0]) end);
def to_gib: . / 1024 / 1024 / 1024 | . * 100 | floor / 100;
map(. + {prefix: prefix}) | group_by(.prefix) |
map({prefix: .[0].prefix, count: length, totalBytes: (map(.sizeInBytes) | add // 0)}) |
sort_by(-.totalBytes) |
.[] | "\(.prefix)\t\(.count)\t\(.totalBytes | to_gib)"
')
printf "%-12s %6s %10.2f\n" "Total" "$total_entries" "$total_gib"
fi
exit 0
fi fi
# Single-prefix mode
KEY_PREFIX="${1:-buildkit-blob}"
json=$(gh cache list --key "$KEY_PREFIX" --limit "$LIMIT" --json key,sizeInBytes 2>/dev/null) || {
echo "Error: gh cache list failed (check gh auth or repo access)" >&2
exit 1
}
count=$(echo "$json" | jq 'length')
total_bytes=$(echo "$json" | jq 'map(.sizeInBytes) | add // 0')
total_gib=$(echo "$total_bytes" | jq -r '. / 1024 / 1024 / 1024 | . * 100 | floor / 100') total_gib=$(echo "$total_bytes" | jq -r '. / 1024 / 1024 / 1024 | . * 100 | floor / 100')
[[ $# -ne 0 ]] && rows="${KEY_PREFIX}"$'\t'"${total_entries}"$'\t'"${total_gib}"
if [[ "$MARKDOWN" == true ]]; then if [[ "$MARKDOWN" == true ]]; then
echo "| PREFIX | COUNT | SIZE (GiB) |" echo "| PREFIX | COUNT | SIZE (GiB) |"
echo "|--------|------:|----------:|" echo "|--------|------:|----------:|"
echo "| $KEY_PREFIX | $count | $total_gib |" while IFS=$'\t' read -r prefix count gib; do
echo "| **Total** | $count | $total_gib |" echo "| $prefix | $count | $gib |"
done <<< "$rows"
echo "| **Total** | $total_entries | $total_gib |"
else else
printf "%-12s %6s %10s\n" "PREFIX" "COUNT" "SIZE (GiB)" printf "%-12s %6s %10s\n" "PREFIX" "COUNT" "SIZE (GiB)"
printf "%-12s %6s %10s\n" "------------" "------" "----------" printf "%-12s %6s %10s\n" "------------" "------" "----------"
printf "%-12s %6s %10.2f\n" "$KEY_PREFIX" "$count" "$total_gib" while IFS=$'\t' read -r prefix count gib; do
printf "%-12s %6s %10.2f\n" "Total" "$count" "$total_gib" printf "%-12s %6s %10.2f\n" "$prefix" "$count" "$gib"
done <<< "$rows"
printf "%-12s %6s %10.2f\n" "Total" "$total_entries" "$total_gib"
fi fi