video-query/extract_user_logs.sh
2025-09-18 14:25:24 -05:00

66 lines
No EOL
2.5 KiB
Bash
Executable file

#!/bin/bash
# Script to extract user emails and prompts from veo-video-generator systemd logs
# Usage: ./extract_user_logs.sh [output_file.csv]
# Set default output file if not provided
OUTPUT_FILE="${1:-video_generation_usage.csv}"
# Service name (adjust if different)
SERVICE_NAME="veo-video-generator"
# Temporary file for processing
TEMP_FILE=$(mktemp)
echo "Extracting logs from systemd service: $SERVICE_NAME"
echo "Output file: $OUTPUT_FILE"
# Create CSV header
echo "timestamp,user_email,prompt,video_length_sec,aspect_ratio,person_generation" > "$OUTPUT_FILE"
# Extract logs from journalctl and process them
journalctl -u "$SERVICE_NAME" --no-pager --output=short-iso | \
grep "Raw JSON data received:" | \
while IFS= read -r line; do
# Extract timestamp (everything before the hostname)
timestamp=$(echo "$line" | awk '{print $1}')
# Extract the JSON part (everything after "Raw JSON data received: ")
json_part=$(echo "$line" | sed -n "s/.*Raw JSON data received: \(.*\)/\1/p")
# Check if we got valid JSON
if [ -n "$json_part" ]; then
# Use jq to parse JSON and extract fields
# Handle case where jq might fail on malformed JSON
user_email=$(echo "$json_part" | jq -r '.user_email // "N/A"' 2>/dev/null)
prompt=$(echo "$json_part" | jq -r '.prompt // "N/A"' 2>/dev/null | sed 's/,/;/g' | sed 's/"/\\"/g')
video_length=$(echo "$json_part" | jq -r '.video_length_sec // "N/A"' 2>/dev/null)
aspect_ratio=$(echo "$json_part" | jq -r '.aspect_ratio // "N/A"' 2>/dev/null)
person_generation=$(echo "$json_part" | jq -r '.person_generation // "N/A"' 2>/dev/null)
# Only add to CSV if we successfully extracted data
if [ "$user_email" != "null" ] && [ "$user_email" != "N/A" ] && [ "$user_email" != "" ]; then
echo "\"$timestamp\",\"$user_email\",\"$prompt\",\"$video_length\",\"$aspect_ratio\",\"$person_generation\"" >> "$OUTPUT_FILE"
fi
fi
done
# Count total records
record_count=$(wc -l < "$OUTPUT_FILE")
record_count=$((record_count - 1)) # Subtract header row
echo "Processing complete!"
echo "Total records extracted: $record_count"
echo "Output saved to: $OUTPUT_FILE"
# Show summary of unique users
echo ""
echo "Unique users found:"
if [ $record_count -gt 0 ]; then
tail -n +2 "$OUTPUT_FILE" | cut -d',' -f2 | sort | uniq -c | sort -nr
else
echo "No records found."
fi
# Clean up temp file
rm -f "$TEMP_FILE"