|
| 1 | +import sqlite3 |
| 2 | +import os |
| 3 | +import re |
| 4 | +from collections import Counter |
| 5 | + |
| 6 | +# Paths |
| 7 | +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 8 | +DB_PATH = os.path.join(BASE_DIR, "functions.db") |
| 9 | + |
| 10 | +def categorize_type(entity_type, code): |
| 11 | + """Categorize entity into English labels based on native DB types.""" |
| 12 | + mapping = { |
| 13 | + 'function': "Functions", |
| 14 | + 'type': "Types", |
| 15 | + 'template': "Templates" |
| 16 | + } |
| 17 | + return mapping.get(entity_type, "Others") |
| 18 | + |
| 19 | +def get_file_extension(filepath): |
| 20 | + """Extract file extension from path.""" |
| 21 | + _, ext = os.path.splitext(filepath) |
| 22 | + return ext or "no extension" |
| 23 | + |
| 24 | +def generate_stats(): |
| 25 | + if not os.path.exists(DB_PATH): |
| 26 | + print(f"Error: {DB_PATH} not found.") |
| 27 | + return |
| 28 | + |
| 29 | + conn = sqlite3.connect(DB_PATH) |
| 30 | + cursor = conn.cursor() |
| 31 | + |
| 32 | + print("Fetching data from database...") |
| 33 | + cursor.execute("SELECT repo_group, filepath, type, code FROM functions") |
| 34 | + rows = cursor.fetchall() |
| 35 | + |
| 36 | + repo_group_stats = Counter() |
| 37 | + extension_stats = Counter() |
| 38 | + category_stats = Counter() |
| 39 | + |
| 40 | + for repo_group, filepath, entity_type, code in rows: |
| 41 | + # Category |
| 42 | + category = categorize_type(entity_type, code) |
| 43 | + category_stats[category] += 1 |
| 44 | + |
| 45 | + # Repo Group |
| 46 | + repo_group_stats[repo_group] += 1 |
| 47 | + |
| 48 | + # File Extension |
| 49 | + ext = get_file_extension(filepath) |
| 50 | + extension_stats[ext] += 1 |
| 51 | + |
| 52 | + conn.close() |
| 53 | + |
| 54 | + # Output Results |
| 55 | + print("\n" + "="*40) |
| 56 | + print(" MEDIAWIKI CODE ENTITY STATISTICS") |
| 57 | + print("="*40) |
| 58 | + |
| 59 | + print("\n--- Statistics by Category ---") |
| 60 | + for cat, count in category_stats.most_common(): |
| 61 | + print(f"{cat:<20}: {count:>8}") |
| 62 | + |
| 63 | + print("\n--- Statistics by Repository Group ---") |
| 64 | + for group, count in repo_group_stats.most_common(): |
| 65 | + print(f"{group:<20}: {count:>8}") |
| 66 | + |
| 67 | + print("\n--- Statistics by File Extension ---") |
| 68 | + for ext, count in extension_stats.most_common(): |
| 69 | + print(f"{ext:<20}: {count:>8}") |
| 70 | + |
| 71 | + print("\n" + "="*40) |
| 72 | + print(f" Total Entities: {len(rows):>18}") |
| 73 | + print("="*40) |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + generate_stats() |
0 commit comments