mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-20 12:24:59 +00:00
refactor: Separate data, text, and HTML formatting into functions
This commit is contained in:
parent
66e5e9c1ce
commit
834e2f9304
1 changed files with 78 additions and 34 deletions
|
@ -4,23 +4,17 @@ import json
|
|||
from collections import defaultdict, deque
|
||||
from pathlib import Path
|
||||
|
||||
# Get the analytics file path
|
||||
analytics_path = Path.home() / ".aider" / "analytics.jsonl"
|
||||
|
||||
# Dictionary to store model stats
|
||||
def collect_model_stats(n_lines=1000):
|
||||
"""Collect model usage statistics from the analytics file."""
|
||||
analytics_path = Path.home() / ".aider" / "analytics.jsonl"
|
||||
model_stats = defaultdict(int)
|
||||
|
||||
# Number of lines to process from the end
|
||||
N = 1000
|
||||
|
||||
# Read and process the last N lines of the file
|
||||
with open(analytics_path) as f:
|
||||
# Get last N lines using deque
|
||||
lines = deque(f, N)
|
||||
lines = deque(f, n_lines)
|
||||
for line in lines:
|
||||
try:
|
||||
event = json.loads(line)
|
||||
# Check if this is a message_send event
|
||||
if event["event"] == "message_send":
|
||||
properties = event["properties"]
|
||||
main_model = properties.get("main_model")
|
||||
|
@ -30,18 +24,68 @@ with open(analytics_path) as f:
|
|||
except json.JSONDecodeError:
|
||||
continue
|
||||
|
||||
# Calculate total for percentages
|
||||
total_tokens = sum(model_stats.values())
|
||||
return model_stats
|
||||
|
||||
# Print results
|
||||
print("\nModel Token Usage Summary:")
|
||||
print("-" * 80)
|
||||
print(f"{'Model Name':<40} {'Total Tokens':>15} {'Percent':>10}")
|
||||
print("-" * 80)
|
||||
|
||||
def format_text_table(model_stats):
|
||||
"""Format model statistics as a text table."""
|
||||
total_tokens = sum(model_stats.values())
|
||||
lines = []
|
||||
|
||||
lines.append("\nModel Token Usage Summary:")
|
||||
lines.append("-" * 80)
|
||||
lines.append(f"{'Model Name':<40} {'Total Tokens':>15} {'Percent':>10}")
|
||||
lines.append("-" * 80)
|
||||
|
||||
for model, tokens in sorted(model_stats.items(), key=lambda x: x[1], reverse=True):
|
||||
percentage = (tokens / total_tokens) * 100 if total_tokens > 0 else 0
|
||||
print(f"{model:<40} {tokens:>15,} {percentage:>9.1f}%")
|
||||
lines.append(f"{model:<40} {tokens:>15,} {percentage:>9.1f}%")
|
||||
|
||||
print("-" * 80)
|
||||
print(f"{'TOTAL':<40} {total_tokens:>15,} {100:>9.1f}%")
|
||||
lines.append("-" * 80)
|
||||
lines.append(f"{'TOTAL':<40} {total_tokens:>15,} {100:>9.1f}%")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def format_html_table(model_stats):
|
||||
"""Format model statistics as an HTML table."""
|
||||
total_tokens = sum(model_stats.values())
|
||||
|
||||
html = [
|
||||
"<html>",
|
||||
"<head>",
|
||||
"<style>",
|
||||
"table { border-collapse: collapse; width: 100%; }",
|
||||
"th, td { padding: 8px; text-align: left; border-bottom: 1px solid #ddd; }",
|
||||
"th { background-color: #f2f2f2; }",
|
||||
"tr:hover { background-color: #f5f5f5; }",
|
||||
".right { text-align: right; }",
|
||||
"</style>",
|
||||
"</head>",
|
||||
"<body>",
|
||||
"<h2>Model Token Usage Summary</h2>",
|
||||
"<table>",
|
||||
"<tr><th>Model Name</th><th class='right'>Total Tokens</th><th class='right'>Percent</th></tr>"
|
||||
]
|
||||
|
||||
for model, tokens in sorted(model_stats.items(), key=lambda x: x[1], reverse=True):
|
||||
percentage = (tokens / total_tokens) * 100 if total_tokens > 0 else 0
|
||||
html.append(
|
||||
f"<tr><td>{model}</td>"
|
||||
f"<td class='right'>{tokens:,}</td>"
|
||||
f"<td class='right'>{percentage:.1f}%</td></tr>"
|
||||
)
|
||||
|
||||
html.append(
|
||||
f"<tr><td><strong>TOTAL</strong></td>"
|
||||
f"<td class='right'><strong>{total_tokens:,}</strong></td>"
|
||||
f"<td class='right'><strong>100.0%</strong></td></tr>"
|
||||
)
|
||||
|
||||
html.extend(["</table>", "</body>", "</html>"])
|
||||
return "\n".join(html)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
stats = collect_model_stats()
|
||||
print(format_text_table(stats))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue