4

For the curiosity's sake I created a script to visualize Git history week/hour-of-day basis timestamps in order to collect some stats. The following script

#!/bin/bash

# Declare the global heatmap hashmap
declare -A HEATMAP

# Composes a key in a form of a <WEEKDAY:1><HOUR:02>
function key {
    local WEEKDAY="$1"
    local HOUR="$2"
    echo -n "$WEEKDAY"
    if [[ "$HOUR" -lt 10 ]]; then
        echo -n '0'
    fi
    echo "$HOUR"
}

# Generate a sequence of weekdays (Mon-Sat,Sun)
function generate_weekdays {
    seq 1 6
    seq 0 0
}

# Generate a sequence of hours in day (00-23)
function generate_hours {
    seq 0 23
}

# Initialize and fill the heatmap with zeroes
for WEEKDAY in $(generate_weekdays); do
    for HOUR in $(generate_hours); do
        HEATMAP[$(key "$WEEKDAY" "$HOUR")]=0
    done
done

# Read the stats into the heatmap
MAX_PEAK=1
while read WEEKDAY_HOUR; do
    HEATMAP["$WEEKDAY_HOUR"]=$((${HEATMAP["$WEEKDAY_HOUR"]} + 1))
    if [[ ${HEATMAP["$WEEKDAY_HOUR"]} -gt "$MAX_PEAK" ]]; then
        MAX_PEAK=${HEATMAP["$WEEKDAY_HOUR"]}
    fi
done < <(git rev-list --date=format:'%w%H' --pretty='format:%ad' $@ | awk 'NR % 2 == 0')

# Render the heatmap
RATIO=0
for WEEKDAY in $(generate_weekdays); do
    for HOUR in $(generate_hours); do
        RATIO=$((100 * ${HEATMAP[$(key "$WEEKDAY" "$HOUR")]} / $MAX_PEAK))
        if [[ "$RATIO" -eq 0 ]]; then echo -ne '\e[38;5;236m'
        elif [[ "$RATIO" -lt 10 ]]; then echo -ne '\e[38;5;107m'
        elif [[ "$RATIO" -lt 20 ]]; then echo -ne '\e[38;5;22m'
        elif [[ "$RATIO" -lt 30 ]]; then echo -ne '\e[38;5;28m'
        elif [[ "$RATIO" -lt 40 ]]; then echo -ne '\e[38;5;70m'
        elif [[ "$RATIO" -lt 50 ]]; then echo -ne '\e[38;5;190m'
        elif [[ "$RATIO" -lt 60 ]]; then echo -ne '\e[38;5;226m'
        elif [[ "$RATIO" -lt 70 ]]; then echo -ne '\e[38;5;220m'
        elif [[ "$RATIO" -lt 80 ]]; then echo -ne '\e[38;5;214m'
        elif [[ "$RATIO" -lt 90 ]]; then echo -ne '\e[38;5;208m'
        elif [[ "$RATIO" -lt 100 ]]; then echo -ne '\e[38;5;160m'
        elif [[ "$RATIO" -eq 100 ]]; then echo -ne '\e[38;5;196m'; fi
        echo -n '*'
        echo -ne '\e[0m'
    done
    echo
done

invoked as ./git-heatmap --all produces a sample output as depicted below (not at a Linux machine currently, running on Windows/MinGW with the Fixedsys font, I prefer squares, \u2BC0, to asterisks, but Fixedsys cannot render the squares that I find much nicer):

./git-heatmap --all example output

Since the script does three jobs:

  • consumes Git weekday/hour-of-day timestamps output,
  • calculates the stats,
  • and renders the heatmap

... I'm wondering is there something like a standard tool that calculates/renders something similar to the heatmap above? My script processes the following sample data supplied by Git:

100
101
420
517
...

where the first digit designates a weekday, and the second and the third digits designate a day hour. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.