<commit_before>#!/usr/bin/env bash
set -e

# This script calculates code quality metrics.

RADON_METRICS_FILENAME="radon-code-metrics.txt"

# Run code complexity metrics on ., drop ANSI escape codes, and save to a file
radon cc . --min=C --total-average --exclude='node_modules/*,staticfiles/*' \
    | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' \
    > $RADON_METRICS_FILENAME

TOTAL_BLOCKS=$(grep 'blocks.*analyzed' $RADON_METRICS_FILENAME | grep -oE '[0-9]+')
COMPLEXITY=$(grep 'Average.complexity' $RADON_METRICS_FILENAME | grep -oE '[0-9.]+' | head -c 5)

echo "Average complexity:" $COMPLEXITY
echo $TOTAL_BLOCKS "blocks analyzed"
echo "Number of blocks below a 'B' grade:"

for GRADE in "C" "D" "E" "F"; do
    NUM_BLOCKS=$(cat $RADON_METRICS_FILENAME | grep " - $GRADE$" | wc -l)
    echo " " $GRADE ":" $NUM_BLOCKS
done

rm $RADON_METRICS_FILENAME<commit_msg>Report cmdline metrics to datadog<commit_after>#!/usr/bin/env bash
set -e

CURRENT_TIME=$(date +%s)

# This script calculates code quality metrics.
# These are evaluated in the daily travis build and reported to datadog.
# Other metrics are computed in the management command `report_code_metrics`

function send_metric_to_datadog() {
    if [ -z "$DATADOG_API_KEY" ]; then
        return
    fi

    curl  -X POST -H "Content-type: application/json" \
          -d "{ \"series\" :
             [{\"metric\":\"$1\",
              \"points\":[[$CURRENT_TIME, $2]],
              \"type\":\"$3\",
              \"host\":\"travis-ci.org\",
              \"tags\":[
                \"environment:travis\",
                \"travis_build:$TRAVIS_BUILD_ID\",
                \"travis_number:$TRAVIS_BUILD_NUMBER\",
                \"travis_job_number:$TRAVIS_JOB_NUMBER\"
              ]}
             ]
        }" \
          "https://app.datadoghq.com/api/v1/series?api_key=${DATADOG_API_KEY}" || true
}

RADON_METRICS_FILENAME="radon-code-metrics.txt"

# Run code complexity metrics on ., drop ANSI escape codes, and save to a file
radon cc . --min=C --total-average --exclude='node_modules/*,staticfiles/*' \
    | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' \
    > $RADON_METRICS_FILENAME

TOTAL_BLOCKS=$(grep 'blocks.*analyzed' $RADON_METRICS_FILENAME | grep -oE '[0-9]+')
COMPLEXITY=$(grep 'Average.complexity' $RADON_METRICS_FILENAME | grep -oE '[0-9.]+' | head -c 5)

echo "Average complexity:" $COMPLEXITY
echo $TOTAL_BLOCKS "blocks analyzed"
echo "Number of blocks below a 'B' grade:"

for GRADE in "C" "D" "E" "F"; do
    NUM_BLOCKS=$(cat $RADON_METRICS_FILENAME | grep " - $GRADE$" | wc -l)
    echo " " $GRADE ":" $NUM_BLOCKS
done

if [[ "$1" == "datadog" ]]; then
    send_metric_to_datadog "commcare.gtd.avg_complexity" $COMPLEXITY "gauge"
    send_metric_to_datadog "commcare.gtd.code_blocks" $TOTAL_BLOCKS "gauge"
fi

rm $RADON_METRICS_FILENAME
{'fraction_non_alphanumeric': 0.16062544420753375, 'fraction_numerical': 0.008173418621179816, 'mean_word_length': 3.1094890510948905, 'pattern_counts': {'":': 7, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5604958', 'n_tokens_mistral': 1113, 'n_tokens_neox': 1041, 'n_words': 235}