#!/bin/bash
# Log pattern analyzer
# Usage: ./analyze_log_patterns.sh <log_file>
# Example: cat logs.txt | ./analyze_log_patterns.sh
#          ./analyze_log_patterns.sh logs.txt

if [ -n "$1" ] && [ -f "$1" ]; then
    input_cmd="cat $1"
else
    input_cmd="cat -"
fi

eval "$input_cmd" | awk -F'tidb ' '
{
    # Extract log-type: [LEVEL] [file:line] only (without message)
    if (match($2, /\[[A-Z]+\] \[[^\]]+\]/)) {
        log_type = substr($2, RSTART, RLENGTH)
        
        # Extract message: ["..."] part
        rest = substr($2, RSTART + RLENGTH)
        if (match(rest, /\["[^"]*"\]/)) {
            msg = substr(rest, RSTART, RLENGTH)
        } else {
            msg = "(no message)"
        }
        
        # Save first occurrence as message sample
        if (!(log_type in msg_samples)) {
            msg_samples[log_type] = msg
        }
        count[log_type]++
    }
}
END {
    # Print results - will be sorted by external sort command
    for (e in count) {
        msg = msg_samples[e]
        gsub(/\|/, "｜", msg)  # Replace with full-width pipe
        if (length(msg) > 100) msg = substr(msg, 1, 100) "..."
        printf "%d|%s|%s\n", count[e], e, msg
    }
}
' | sort -t'|' -k1 -rn | awk -F'|' '
BEGIN {
    print "=========================================================================================="
    printf "%6s | %-40s | %s\n", "COUNT", "LOG-TYPE", "MESSAGE-SAMPLE"
    print "------------------------------------------------------------------------------------------"
}
{
    printf "%6s | %-40s | %s\n", $1, $2, $3
}
END {
    print "------------------------------------------------------------------------------------------"
    printf "Total unique patterns: %d\n", NR
}
'
