about summary refs log tree commit diff stats
path: root/bash/logging.sh
blob: c37aaf47d2d5c211221901286355e071912493bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash

# Unified Logging System
# This script provides consistent logging and performance metrics across all thinking mechanisms.

# --- Logging Configuration ---
LOG_DIR=~/tmp/ai_thinking
METRICS_FILE="${LOG_DIR}/performance_metrics.json"
SESSION_LOG="${LOG_DIR}/session_$(date +%Y%m%d_%H%M%S).json"

# Create logging directory
mkdir -p "${LOG_DIR}"

# --- Timing Functions ---
start_timer() {
    local session_id="$1"
    local mechanism="$2"
    local start_time=$(date +%s.%N)
    
    # Store start time
    echo "$start_time" > "/tmp/${session_id}_start"
    
    # Log session start
    log_session_start "$session_id" "$mechanism" "$start_time"
}

end_timer() {
    local session_id="$1"
    local mechanism="$2"
    local end_time=$(date +%s.%N)
    local start_time=$(cat "/tmp/${session_id}_start" 2>/dev/null || echo "$end_time")
    
    # Calculate duration
    local duration=$(echo "$end_time - $start_time" | bc -l 2>/dev/null || echo "0")
    
    # Log session end
    log_session_end "$session_id" "$mechanism" "$end_time" "$duration"
    
    # Clean up
    rm -f "/tmp/${session_id}_start"
    
    echo "$duration"
}

# --- Session Logging ---
log_session_start() {
    local session_id="$1"
    local mechanism="$2"
    local start_time="$3"
    
    cat > "${SESSION_LOG}" << EOF
{
  "session_id": "${session_id}",
  "mechanism": "${mechanism}",
  "start_time": "${start_time}",
  "prompt": "${PROMPT:-""}",
  "status": "started"
}
EOF
}

log_session_end() {
    local session_id="$1"
    local mechanism="$2"
    local end_time="$3"
    local duration="$4"
    
    # Update session log
    cat > "${SESSION_LOG}" << EOF
{
  "session_id": "${session_id}",
  "mechanism": "${mechanism}",
  "start_time": "$(cat "${SESSION_LOG}" | jq -r '.start_time' 2>/dev/null || echo "")",
  "end_time": "${end_time}",
  "duration": "${duration}",
  "prompt": "${PROMPT:-""}",
  "status": "completed"
}
EOF
    
    # Update metrics file
    update_metrics "$mechanism" "$duration"
}

# --- Metrics Management ---
update_metrics() {
    local mechanism="$1"
    local duration="$2"
    
    # Create metrics file if it doesn't exist
    if [ ! -f "${METRICS_FILE}" ]; then
        cat > "${METRICS_FILE}" << EOF
{
  "total_sessions": 0,
  "mechanisms": {},
  "average_durations": {}
}
EOF
    fi
    
    # Update metrics using jq (if available) or simple text processing
    if command -v jq >/dev/null 2>&1; then
        # Use jq for proper JSON handling
        local temp_file=$(mktemp)
        jq --arg mechanism "$mechanism" --arg duration "$duration" '
            .total_sessions += 1 |
            .mechanisms[$mechanism] = (.mechanisms[$mechanism] // 0) + 1 |
            .average_durations[$mechanism] = (
                (.average_durations[$mechanism] // 0) * (.mechanisms[$mechanism] - 1) + ($duration | tonumber)
            ) / .mechanisms[$mechanism]
        ' "${METRICS_FILE}" > "$temp_file"
        mv "$temp_file" "${METRICS_FILE}"
    else
        # Fallback: simple text-based metrics
        echo "$(date +%Y%m%d_%H%M%S),${mechanism},${duration}" >> "${LOG_DIR}/simple_metrics.csv"
    fi
}

# --- Utility Functions ---
generate_session_id() {
    echo "session_$(date +%Y%m%d_%H%M%S)_$$"
}

get_metrics_summary() {
    if [ -f "${METRICS_FILE}" ]; then
        echo "=== Performance Metrics ==="
        if command -v jq >/dev/null 2>&1; then
            jq -r '.mechanisms | to_entries[] | "\(.key): \(.value) sessions"' "${METRICS_FILE}"
            echo ""
            jq -r '.average_durations | to_entries[] | "\(.key): \(.value | tonumber | floor)s average"' "${METRICS_FILE}"
        else
            echo "Metrics available in: ${METRICS_FILE}"
        fi
    else
        echo "No metrics available yet."
    fi
}

# --- Export Functions for Other Scripts ---
export -f start_timer
export -f end_timer
export -f log_session_start
export -f log_session_end
export -f update_metrics
export -f generate_session_id
export -f get_metrics_summary