Files
SPARC/scripts/view_analytics.py
T
0xWheatyz 44456cb073 feat: add database mode for LLM message storage and analytics
Implements a database mode that stores LLM prompts and responses in PostgreSQL
instead of making API calls. This enables:
- Testing without consuming API credits
- Collecting analytics on usage patterns
- Development and debugging workflows

Changes:
- Added DatabaseClient class for PostgreSQL operations
- Modified LLMAnalyzer to support database/API mode toggle
- Added USE_DATABASE config flag to switch between modes
- Included Docker Compose setup for PostgreSQL
- Added utility scripts for database init and analytics viewing
- Comprehensive documentation in DATABASE_MODE.md

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-03-10 21:13:13 -04:00

68 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""View analytics from the message database.
This script displays statistics about stored LLM messages including
usage by company, analysis type, and time periods.
Usage:
python scripts/view_analytics.py [--days DAYS]
"""
import sys
import os
import argparse
import json
# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from SPARC import config
from SPARC.database import DatabaseClient
def main():
"""Display analytics from the database."""
parser = argparse.ArgumentParser(description="View SPARC message analytics")
parser.add_argument(
"--days",
type=int,
default=30,
help="Number of days to analyze (default: 30)"
)
args = parser.parse_args()
print(f"SPARC Analytics - Last {args.days} days")
print("=" * 70)
try:
db_client = DatabaseClient(config.database_url)
analytics = db_client.get_analytics(days=args.days)
print(f"\nTotal Messages: {analytics['total_messages']}")
print("\nMessages by Company:")
if analytics['by_company']:
for item in analytics['by_company']:
company = item['company_name'] or '(unknown)'
print(f" {company}: {item['count']}")
else:
print(" No data")
print("\nMessages by Analysis Type:")
if analytics['by_type']:
for item in analytics['by_type']:
analysis_type = item['analysis_type'] or '(unknown)'
print(f" {analysis_type}: {item['count']}")
else:
print(" No data")
print("\n" + "=" * 70)
except Exception as e:
print(f"Error retrieving analytics: {e}")
sys.exit(1)
if __name__ == "__main__":
main()