Skip to content

Instantly share code, notes, and snippets.

@jprinet
Last active February 21, 2023 15:24
Show Gist options
  • Save jprinet/79734d18bc93fbc00499f36fbfcab88a to your computer and use it in GitHub Desktop.
Save jprinet/79734d18bc93fbc00499f36fbfcab88a to your computer and use it in GitHub Desktop.
#!/bin/bash
outputFile=$0.out.csv
rm -f $outputFile
while getopts ":u:t:s:k:v:p:" opt; do
case $opt in
u) geUrl="$OPTARG"
;;
t) bearerToken="$OPTARG"
;;
s) since="$OPTARG"
;;
k) customKey="$OPTARG"
;;
v) customValue="$OPTARG"
;;
p) taskPath="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
exit 1
;;
esac
case $OPTARG in
-*) echo "Option $opt needs a valid argument"
exit 1
;;
esac
done
startDate=$(date -d @${since})
echo "Looking for builds with task ${taskPath} since ${startDate}"
if [ ! -z "${customKey}" ] && [ ! -z "${customValue}" ]
then
echo "Filtering builds matching custom value (${customKey}=${customValue})"
fi
function processBuilds() {
local buildType=$1
for buildScanId in $buildScanIds; do
echo "Processing $buildType build $buildScanId"
processBuild $buildType $buildScanId
done
}
function processBuild() {
local buildType=$1
local buildScanId=$2
isMatching=false
isMatchingCustomValue $buildType $buildScanId
if [ "$isMatching" == "true" ]
then
local taskExecutionData=""
echo "matching filter!"
if [ "${buildType}" == "gradle" ]
then
buildPerformanceData=$(curl -s ${geUrl}/api/builds/${buildScanId}/${buildType}-build-cache-performance --header "authorization: Bearer ${bearerToken}" |
jq --arg taskPath "$taskPath" '.taskExecution[] | select(.taskPath | contains($taskPath))'
)
if [ ! -z "${buildPerformanceData}" ]
then
taskExecutionData=$(echo ${buildPerformanceData} | jq -r "[.taskPath,.duration,.avoidanceOutcome] | @csv")
fi
else
buildPerformanceData=$(curl -s ${geUrl}/api/builds/${buildScanId}/${buildType}-build-cache-performance --header "authorization: Bearer ${bearerToken}" |
jq --arg taskPath "$taskPath" '.goalExecution[] | select(.goalName | contains($taskPath))'
)
if [ ! -z "${buildPerformanceData}" ]
then
taskExecutionData=$(echo ${buildPerformanceData} | jq -r "[.goalName,.duration,.avoidanceOutcome] | @csv")
fi
fi
if [ ! -z "${taskExecutionData}" ]
then
echo "task found!"
echo "$buildScanId,$taskExecutionData" >> $outputFile
fi
fi
}
function isMatchingCustomValue() {
local buildType=$1
local buildScanId=$2
if [ ! -z "${customKey}" ] && [ ! -z "${customValue}" ]
then
buildData=$(curl -s ${geUrl}/api/builds/${buildScanId}/${buildType}-attributes --header "authorization: Bearer ${bearerToken}" |
jq --arg customKey "$customKey" --arg customValue "$customValue" 'select(contains({values:[{name:$customKey,value:$customValue}]}))'
)
if [ ! -z "${buildData}" ]
then
isMatching=true
fi
else
isMatching=true
fi
}
function getTotal() {
local column=$1
awk -F "," "{ sum += \$$column } END { print sum }" $outputFile
}
function countOccurences() {
local key=$1
grep "$key" $outputFile | wc -l
}
# Process Gradle builds
buildScanIds=$(curl -s ${geUrl}/api/builds?fromInstant=${since}&reverse=false --header "authorization: Bearer ${bearerToken}" |
jq -r ".[] | select( .buildToolType == \"gradle\") | .id"
)
processBuilds gradle $gradleBuildScanIds
# Process Maven builds
buildScanIds=$(curl -s ${geUrl}/api/builds?fromInstant=${since}&reverse=false --header "authorization: Bearer ${bearerToken}" |
jq -r ".[] | select( .buildToolType == \"maven\") | .id"
)
processBuilds maven $gradleBuildScanIds
# Display Results
if [ -f "$outputFile" ]; then
totalExecutionTime=$(getTotal 2)
countExecuted=$(countOccurences executed_)
countUpToDate=$(countOccurences avoided_up_to_date)
countLocalCacheHit=$(countOccurences avoided_from_local_cache)
countRemoteCacheHit=$(countOccurences avoided_from_remote_cache)
echo "Total execution time = $totalExecutionTime"
echo "Executed count = $countExecuted"
echo "Up-to-date count = $countUpToDate"
echo "Local cache hit count = $countLocalCacheHit"
echo "Remote cache hit count = $countRemoteCacheHit"
else
echo "No data found"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment