1+ #! /usr/bin/env bash
2+ # filepath: scripts/metrics/ci_metrics.sh
3+ set -euo pipefail
4+ OWNER=" ${OWNER:- owl-sol} "
5+ REPO=" ${REPO:- OWLSOL_CLI} "
6+ WORKFLOW_FILE=" ${WORKFLOW_FILE:- .github/ workflows/ nightly.yml} "
7+
8+ if [ -z " ${GITHUB_TOKEN:- } " ]; then
9+ echo ' {"error":"GITHUB_TOKEN not set"}' >&2
10+ echo ' {}'
11+ exit 0
12+ fi
13+
14+ AUTH=" Authorization: token ${GITHUB_TOKEN} "
15+
16+ # Get workflow ID
17+ workflows=$( curl -sSL -H " $AUTH " " https://api.github.com/repos/${OWNER} /${REPO} /actions/workflows" 2> /dev/null)
18+ if [ -z " $workflows " ] || [ " $workflows " = " null" ]; then
19+ echo ' {"error":"Failed to fetch workflows"}' >&2
20+ echo ' {}'
21+ exit 0
22+ fi
23+
24+ wf=$( echo " $workflows " | jq -r --arg wf " $WORKFLOW_FILE " ' .workflows[]? | select(.path==$wf) | .id' 2> /dev/null || echo " " )
25+
26+ if [ -z " $wf " ] || [ " $wf " = " null" ]; then
27+ echo ' {"error":"Workflow not found","workflow":"' $WORKFLOW_FILE ' ","total_runs":0}' >&2
28+ echo ' {"workflow":"' $WORKFLOW_FILE ' ","total_runs":0}'
29+ exit 0
30+ fi
31+
32+ # Get workflow runs
33+ runs=$( curl -sSL -H " $AUTH " " https://api.github.com/repos/${OWNER} /${REPO} /actions/workflows/${wf} /runs?per_page=100" 2> /dev/null)
34+ if [ -z " $runs " ] || [ " $runs " = " null" ]; then
35+ echo ' {"error":"Failed to fetch runs"}' >&2
36+ echo ' {}'
37+ exit 0
38+ fi
39+
40+ total=$( echo " $runs " | jq ' .total_count // 0' 2> /dev/null || echo 0)
41+
42+ if [ " $total " -eq 0 ]; then
43+ echo ' {"workflow":"' $WORKFLOW_FILE ' ","total_runs":0,"success":0,"fail":0,"success_rate":0}'
44+ exit 0
45+ fi
46+
47+ success_count=$( echo " $runs " | jq ' [.workflow_runs[]? | select(.conclusion=="success")] | length' 2> /dev/null || echo 0)
48+ fail_count=$( echo " $runs " | jq ' [.workflow_runs[]? | select(.conclusion=="failure" or .conclusion=="cancelled" or .conclusion=="timed_out")] | length' 2> /dev/null || echo 0)
49+
50+ # Calculate success rate
51+ success_rate=$( awk -v s=" $success_count " -v t=" $total " ' BEGIN{if(t>0) printf "%.2f", (s/t)*100; else print 0}' )
52+
53+ # Get average duration (in seconds, not ms)
54+ avg_duration_sec=$( echo " $runs " | jq ' [.workflow_runs[]? | (.updated_at | fromdateiso8601) - (.created_at | fromdateiso8601)] | if length > 0 then (add / length) else 0 end' 2> /dev/null || echo 0)
55+
56+ # Get last run status
57+ last_run_status=$( echo " $runs " | jq -r ' .workflow_runs[0]?.conclusion // "unknown"' 2> /dev/null || echo " unknown" )
58+ last_run_time=$( echo " $runs " | jq -r ' .workflow_runs[0]?.created_at // "unknown"' 2> /dev/null || echo " unknown" )
59+
60+ jq -n --arg wf " $WORKFLOW_FILE " \
61+ --argjson total " $total " \
62+ --argjson success " $success_count " \
63+ --argjson fail " $fail_count " \
64+ --argjson rate " $success_rate " \
65+ --argjson avg_sec " $avg_duration_sec " \
66+ --arg last_status " $last_run_status " \
67+ --arg last_time " $last_run_time " \
68+ ' {
69+ workflow:$wf,
70+ total_runs:$total,
71+ success:$success,
72+ fail:$fail,
73+ success_rate:$rate,
74+ avg_duration_seconds:$avg_sec,
75+ last_run_status:$last_status,
76+ last_run_time:$last_time
77+ }'
0 commit comments