Skip to content

Instantly share code, notes, and snippets.

@hfossli
Last active June 29, 2018 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hfossli/7070f8434448e4696f8015402bd5b53d to your computer and use it in GitHub Desktop.
Save hfossli/7070f8434448e4696f8015402bd5b53d to your computer and use it in GitHub Desktop.
Get time difference between output lines from fastlane
#!/bin/bash
CLEAR='\033[0m'
RED='\033[38;5;160m'
ORANGE='\033[38;5;202m'
YELLOW='\033[38;5;220m'
GREEN='\033[0;32m'
BLUE='\033[0;36m'
GREY='\033[38;5;237m'
# default values
THRESHOLD_LOW=1
THRESHOLD_MEDIUM=5
THRESHOLD_HIGH=10
RESET_MARK=""
ACTIVE_MODE=false
function usage() {
if [ -n "$1" ]; then
echo -e "${RED}👉 $1${CLEAR}\n";
fi
echo "Usage: $0 [-l low] [-m medium] [-h high]"
echo " -l, --low Threshold in seconds for low duration color formatting"
echo " -m, --medium Threshold in seconds for medium duration color formatting"
echo " -h, --high Threshold in seconds for high duration color formatting"
echo " -r, --reset-mark String match to reset total counter"
echo " -[n]a, --[no]-active Don't parse date, but instead use date when reading line"
echo ""
echo "Example: $0 --low 10 --medium 30 --high 60 --reset-mark \"--- Step: \w* ---\""
exit 1
}
# parse params
while [[ "$#" > 0 ]]; do case $1 in
-l|--low) THRESHOLD_LOW="$2";shift;shift;;
-m|--medium) THRESHOLD_MEDIUM="$2";shift;shift;;
-h|--high) THRESHOLD_HIGH="$2"; shift;shift;;
-r|--reset-marks) RESET_MARK="$2"; shift;shift;;
-a|--active-mode) ACTIVE_MODE=true; shift;shift;;
-na|--no-active-mode) ACTIVE_MODE=false; shift;;
*) usage "Unknown parameter passed: $1"; shift; shift;;
esac; done
# verify valid params
if ! [ "$THRESHOLD_LOW" -eq "$THRESHOLD_LOW" ] 2>/dev/null; then usage "Low threshold is not a number"; fi;
if ! [ "$THRESHOLD_MEDIUM" -eq "$THRESHOLD_MEDIUM" ] 2>/dev/null; then usage "Medium threshold is not a number"; fi;
if ! [ "$THRESHOLD_HIGH" -eq "$THRESHOLD_HIGH" ] 2>/dev/null; then usage "High threshold is not a number"; fi;
function seconds() {
echo "$@" | sed 's/^/((/; s/:/)*60+/g' | bc
}
function seconds_between() {
echo "$(seconds $2)-$(seconds $1)" | bc
}
function color_for_duration() {
if [ $1 -ge $THRESHOLD_HIGH ]; then
echo $RED
elif [ $1 -ge $THRESHOLD_MEDIUM ]; then
echo $ORANGE
elif [ $1 -ge $THRESHOLD_LOW ]; then
echo $YELLOW
else
echo $GREY
fi
}
first_date=""
last_date=""
total_diff="0"
while read -s -r -e; do
if $ACTIVE_MODE; then
date=$(gdate +%H:%M:%S)
else
date=$(echo $REPLY | grep -o "\[\d\d:\d\d:\d\d\]: " | grep -o "\d\d:\d\d:\d\d" | head -1)
fi
if [[ ! -z $RESET_MARK ]] && echo "$REPLY" | grep -q "$RESET_MARK"; then
first_date=""
echo -e "${BLUE}Reseting timer -----------$CLEAR"
fi
if [ "$date" != "" ]; then
if [ "$first_date" = "" ]; then
first_date="$date"
fi
if [ "$last_date" = "" ]; then
last_date="$date"
fi
last_diff=$(seconds_between $last_date $date)
total_diff=$(seconds_between $first_date $date)
color=$(color_for_duration $last_diff)
format=$(printf "$color%-14s$CLEAR $GREY= %-12s$CLEAR" "+ $last_diff seconds" "$total_diff seconds")
echo -e "$format $REPLY"
last_date="$date"
else
color=$(color_for_duration 0)
format=$(printf "$GREY%-14s$CLEAR $GREY= %-12s$CLEAR" "+ 0 seconds" "$total_diff seconds")
echo -e "$format $REPLY"
fi
done
+ 0 seconds = 0 seconds [09:06:04]: --- Step: team_name ---
+ 0 seconds = 0 seconds [09:06:04]: -----------------------
+ 0 seconds = 0 seconds [09:06:04]: Setting Team Name to 'Agens AS' for all build steps
+ 0 seconds = 0 seconds [09:06:04]: -----------------------
Reseting timer -----------
+ 0 seconds = 0 seconds [09:06:04]: --- Step: cocoapods ---
+ 0 seconds = 0 seconds [09:06:04]: -----------------------
+ 0 seconds = 0 seconds [09:06:04]: $ pod install
+ 1 seconds = 1 seconds [09:06:05]: ▸ Analyzing dependencies
+ 14 seconds = 15 seconds [09:06:19]: ▸ Downloading dependencies
cat ~/Downloads/slow.txt | ./fastlane-diff.sh -l 1 -m 2 -h 10 -r "Step:"
bundle exec fastlane build | ./fastlane-diff.sh -r "Step:"
@hfossli
Copy link
Author

hfossli commented Jun 29, 2018

2018-06-29 16 27 29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment