116 lines
4.5 KiB
Python
116 lines
4.5 KiB
Python
import json
|
||
import os
|
||
from datetime import datetime, timezone
|
||
import glob
|
||
|
||
def parse_iso(timestamp):
|
||
# Parse ISO timestamp with timezone
|
||
try:
|
||
dt = datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
|
||
return dt
|
||
except Exception as e:
|
||
print(f'Error parsing {timestamp}: {e}')
|
||
return None
|
||
|
||
def process_session_file(path):
|
||
today = datetime(2026, 3, 22, tzinfo=timezone.utc)
|
||
total_input = 0
|
||
total_output = 0
|
||
total_cost = 0.0
|
||
model_counts = {}
|
||
|
||
with open(path, 'r') as f:
|
||
for line_num, line in enumerate(f, 1):
|
||
line = line.strip()
|
||
if not line:
|
||
continue
|
||
try:
|
||
data = json.loads(line)
|
||
except json.JSONDecodeError as e:
|
||
continue
|
||
|
||
# Check timestamp for today
|
||
timestamp = data.get('timestamp')
|
||
if not timestamp:
|
||
continue
|
||
dt = parse_iso(timestamp)
|
||
if not dt:
|
||
continue
|
||
if dt.date() != today.date():
|
||
continue
|
||
|
||
# We need messages with usage (assistant responses)
|
||
if data.get('type') == 'message':
|
||
msg = data.get('message', {})
|
||
if msg.get('role') == 'assistant' and 'usage' in msg:
|
||
usage = msg['usage']
|
||
input_tokens = usage.get('input', 0)
|
||
output_tokens = usage.get('output', 0)
|
||
cache_read = usage.get('cacheRead', 0)
|
||
cache_write = usage.get('cacheWrite', 0)
|
||
# cost may be inside usage['cost']
|
||
cost = usage.get('cost', {})
|
||
cost_total = cost.get('total', 0.0)
|
||
model = msg.get('model', 'unknown')
|
||
|
||
total_input += input_tokens
|
||
total_output += output_tokens
|
||
total_cost += cost_total
|
||
|
||
# Track per model
|
||
if model not in model_counts:
|
||
model_counts[model] = {'input': 0, 'output': 0, 'cost': 0.0}
|
||
model_counts[model]['input'] += input_tokens
|
||
model_counts[model]['output'] += output_tokens
|
||
model_counts[model]['cost'] += cost_total
|
||
|
||
return total_input, total_output, total_cost, model_counts
|
||
|
||
sessions_dir = '/home/node/.openclaw/agents/main/sessions'
|
||
jsonl_files = glob.glob(os.path.join(sessions_dir, '*.jsonl'))
|
||
|
||
print('📊 Сводка использования токенов за сегодня (2026-03-22)')
|
||
print('=' * 60)
|
||
|
||
overall_input = 0
|
||
overall_output = 0
|
||
overall_cost = 0.0
|
||
all_model_counts = {}
|
||
|
||
for file_path in jsonl_files:
|
||
file_name = os.path.basename(file_path)
|
||
print(f'\n📁 Файл сессии: {file_name}')
|
||
inp, out, cost, models = process_session_file(file_path)
|
||
print(f' Входные токены: {inp:,}')
|
||
print(f' Выходные токены: {out:,}')
|
||
print(f' Примерная стоимость: ${cost:.6f}')
|
||
if models:
|
||
for model, counts in models.items():
|
||
print(f' Модель: {model}')
|
||
print(f' вход: {counts[\"input\"]:,}, выход: {counts[\"output\"]:,}, стоимость: ${counts[\"cost\"]:.6f}')
|
||
# Aggregate across files
|
||
if model not in all_model_counts:
|
||
all_model_counts[model] = counts.copy()
|
||
else:
|
||
all_model_counts[model]['input'] += counts['input']
|
||
all_model_counts[model]['output'] += counts['output']
|
||
all_model_counts[model]['cost'] += counts['cost']
|
||
|
||
overall_input += inp
|
||
overall_output += out
|
||
overall_cost += cost
|
||
|
||
print('\n' + '=' * 60)
|
||
print('📈 ОБЩИЙ ИТОГ за сегодня:')
|
||
print(f' Всего входных токенов: {overall_input:,}')
|
||
print(f' Всего выходных токенов: {overall_output:,}')
|
||
print(f' Общая стоимость: ${overall_cost:.6f}')
|
||
print()
|
||
print('📋 По моделям:')
|
||
for model, counts in all_model_counts.items():
|
||
print(f' • {model}')
|
||
print(f' вход: {counts[\"input\"]:,} токенов, выход: {counts[\"output\"]:,} токенов')
|
||
print(f' стоимость: ${counts[\"cost\"]:.6f}')
|
||
print()
|
||
print('💡 Примечание: стоимость может не включать кэшированные токены.')
|
||
print(' Данные основаны на записях сессий.') |