|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Update README.md with current system prompt count and growth visualization. |
| 4 | +
|
| 5 | +This script reads from growth_history.json and index_metadata.json to add |
| 6 | +current statistics and a sparkline chart to the README header. |
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +import re |
| 11 | +from pathlib import Path |
| 12 | +from datetime import datetime |
| 13 | +from typing import List, Dict |
| 14 | + |
| 15 | + |
| 16 | +def generate_sparkline(counts: List[int]) -> str: |
| 17 | + """Generate a sparkline chart from count data.""" |
| 18 | + if len(counts) < 2: |
| 19 | + return "📈 ▁" |
| 20 | + |
| 21 | + min_val = min(counts) |
| 22 | + max_val = max(counts) |
| 23 | + |
| 24 | + if min_val == max_val: |
| 25 | + return "📈 " + "▄" * len(counts) |
| 26 | + |
| 27 | + # Normalize to 0-7 range for Unicode blocks |
| 28 | + normalized = [] |
| 29 | + for count in counts: |
| 30 | + norm = int(7 * (count - min_val) / (max_val - min_val)) |
| 31 | + normalized.append(norm) |
| 32 | + |
| 33 | + # Unicode block characters for sparkline |
| 34 | + blocks = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"] |
| 35 | + sparkline = "📈 " + "".join(blocks[n] for n in normalized) |
| 36 | + |
| 37 | + return sparkline |
| 38 | + |
| 39 | + |
| 40 | +def load_growth_data(repo_root: Path) -> tuple: |
| 41 | + """Load growth history and current metadata.""" |
| 42 | + growth_file = repo_root / "growth_history.json" |
| 43 | + metadata_file = repo_root / "index_metadata.json" |
| 44 | + |
| 45 | + # Load growth history |
| 46 | + if growth_file.exists(): |
| 47 | + with open(growth_file, 'r') as f: |
| 48 | + growth_data = json.load(f) |
| 49 | + else: |
| 50 | + growth_data = {"entries": []} |
| 51 | + |
| 52 | + # Load current metadata |
| 53 | + if metadata_file.exists(): |
| 54 | + with open(metadata_file, 'r') as f: |
| 55 | + metadata = json.load(f) |
| 56 | + else: |
| 57 | + metadata = {"prompt_count": 0} |
| 58 | + |
| 59 | + return growth_data, metadata |
| 60 | + |
| 61 | + |
| 62 | +def update_readme_with_stats(repo_root: Path): |
| 63 | + """Update README.md with current statistics and growth chart.""" |
| 64 | + readme_file = repo_root / "README.md" |
| 65 | + |
| 66 | + if not readme_file.exists(): |
| 67 | + print("❌ README.md not found") |
| 68 | + return False |
| 69 | + |
| 70 | + # Load data |
| 71 | + growth_data, metadata = load_growth_data(repo_root) |
| 72 | + |
| 73 | + current_count = metadata.get("prompt_count", 0) |
| 74 | + last_updated = metadata.get("generated_at", datetime.now().isoformat()) |
| 75 | + |
| 76 | + # Parse the date for display |
| 77 | + try: |
| 78 | + update_date = datetime.fromisoformat(last_updated.replace('Z', '+00:00')) |
| 79 | + formatted_date = update_date.strftime('%Y-%m-%d') |
| 80 | + except: |
| 81 | + formatted_date = datetime.now().strftime('%Y-%m-%d') |
| 82 | + |
| 83 | + # Generate sparkline from growth history |
| 84 | + counts = [entry["count"] for entry in growth_data.get("entries", [])] |
| 85 | + if not counts: |
| 86 | + counts = [current_count] |
| 87 | + |
| 88 | + sparkline = generate_sparkline(counts) |
| 89 | + |
| 90 | + # Create stats section |
| 91 | + stats_section = f""" |
| 92 | +## 📊 Library Statistics |
| 93 | +
|
| 94 | +{sparkline} |
| 95 | +
|
| 96 | +**Total System Prompts:** {current_count:,} | **Last Updated:** {formatted_date} |
| 97 | +
|
| 98 | +*This library has grown from {counts[0] if counts else 0} to {current_count} prompts since tracking began* |
| 99 | +
|
| 100 | +""" |
| 101 | + |
| 102 | + # Read current README |
| 103 | + with open(readme_file, 'r', encoding='utf-8') as f: |
| 104 | + content = f.read() |
| 105 | + |
| 106 | + # Check if stats section already exists |
| 107 | + stats_pattern = r'## 📊 Library Statistics.*?(?=##|\Z)' |
| 108 | + |
| 109 | + if re.search(stats_pattern, content, re.DOTALL): |
| 110 | + # Replace existing stats section |
| 111 | + new_content = re.sub(stats_pattern, stats_section.strip() + '\n\n', content, flags=re.DOTALL) |
| 112 | + print("✅ Updated existing statistics section") |
| 113 | + else: |
| 114 | + # Insert stats section after the badges but before Table of Contents |
| 115 | + # Look for the pattern: badges -> image -> Table of Contents |
| 116 | + toc_pattern = r'(!\[alt text\].*?\n\n)(## Table of Contents)' |
| 117 | + |
| 118 | + if re.search(toc_pattern, content, re.DOTALL): |
| 119 | + new_content = re.sub( |
| 120 | + toc_pattern, |
| 121 | + r'\1' + stats_section + r'\2', |
| 122 | + content, |
| 123 | + flags=re.DOTALL |
| 124 | + ) |
| 125 | + print("✅ Added new statistics section before Table of Contents") |
| 126 | + else: |
| 127 | + # Fallback: add after the first heading |
| 128 | + lines = content.split('\n') |
| 129 | + insert_pos = 1 |
| 130 | + for i, line in enumerate(lines): |
| 131 | + if line.startswith('##') and 'Table of Contents' in line: |
| 132 | + insert_pos = i |
| 133 | + break |
| 134 | + |
| 135 | + lines.insert(insert_pos, stats_section.strip()) |
| 136 | + new_content = '\n'.join(lines) |
| 137 | + print("✅ Added new statistics section (fallback method)") |
| 138 | + |
| 139 | + # Write updated content |
| 140 | + with open(readme_file, 'w', encoding='utf-8') as f: |
| 141 | + f.write(new_content) |
| 142 | + |
| 143 | + print(f"📊 Updated README with {current_count:,} prompts and growth chart") |
| 144 | + return True |
| 145 | + |
| 146 | + |
| 147 | +def main(): |
| 148 | + """Main function.""" |
| 149 | + repo_root = Path(__file__).parent.parent |
| 150 | + |
| 151 | + print("🚀 Updating README with library statistics...") |
| 152 | + success = update_readme_with_stats(repo_root) |
| 153 | + |
| 154 | + if success: |
| 155 | + print("✅ README statistics update completed successfully") |
| 156 | + else: |
| 157 | + print("❌ Failed to update README statistics") |
| 158 | + return 1 |
| 159 | + |
| 160 | + return 0 |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + exit(main()) |
0 commit comments