veo3/extract_usage_logs.sh
2025-09-30 09:49:55 -05:00

71 lines
No EOL
2.1 KiB
Bash
Executable file

#!/bin/bash
# Script to extract user emails and prompts from Veo video generator logs
# Usage: ./extract_usage_logs.sh [output_file.csv]
# Set output file (default to usage_report.csv)
OUTPUT_FILE="${1:-usage_report.csv}"
SERVICE_NAME="veo-video-generator"
echo "Extracting usage data from $SERVICE_NAME logs..."
echo "Output file: $OUTPUT_FILE"
# Create CSV header
echo "timestamp,user_email,prompt" > "$OUTPUT_FILE"
# Extract logs and process them
journalctl -u "$SERVICE_NAME" --no-pager | \
grep "DEBUG: Raw JSON data received:" | \
while IFS= read -r line; do
# Extract timestamp (first 3 fields: month day time)
timestamp=$(echo "$line" | awk '{print $1 " " $2 " " $3}')
# Extract JSON part after "DEBUG: Raw JSON data received: "
json_part=$(echo "$line" | sed "s/.*DEBUG: Raw JSON data received: //")
# Convert Python dict format to JSON format for parsing
# Replace single quotes with double quotes, handle Python None/True/False
json_formatted=$(echo "$json_part" | \
sed "s/'/\"/g" | \
sed "s/None/null/g" | \
sed "s/True/true/g" | \
sed "s/False/false/g")
# Extract user_email and prompt using python
python3 -c "
import json
import sys
import csv
try:
data = json.loads('''$json_formatted''')
user_email = data.get('user_email', 'unknown')
prompt = data.get('prompt', '').replace('\"', '\"\"') # Escape quotes for CSV
# Print CSV row
print('\"$timestamp\",\"' + user_email + '\",\"' + prompt + '\"')
except Exception as e:
# Skip malformed entries
pass
" >> "$OUTPUT_FILE"
done
# Count total entries
total_entries=$(( $(wc -l < "$OUTPUT_FILE") - 1 ))
echo "Processing complete!"
echo "Total video generation requests found: $total_entries"
echo "Report saved to: $OUTPUT_FILE"
# Show preview of results
echo ""
echo "Preview of results:"
head -10 "$OUTPUT_FILE"
if [ $total_entries -gt 10 ]; then
echo "... (showing first 10 entries)"
fi
# Show unique users summary
echo ""
echo "Unique users found:"
tail -n +2 "$OUTPUT_FILE" | cut -d',' -f2 | sort | uniq -c | sort -nr