File size: 12,319 Bytes
143d94c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
"""
Скрипт для аналізу збережених логів MAI-DX
"""

import os
import json
import pandas as pd
from datetime import datetime
from collections import defaultdict
import argparse

def load_all_sessions(log_dir="mai_dx_logs"):
    """Завантажити всі сесії з директорії логів"""
    sessions = []
    
    if not os.path.exists(log_dir):
        print(f"❌ Директорія {log_dir} не існує")
        return sessions
        
    for filename in os.listdir(log_dir):
        if filename.endswith('.json') and not filename.endswith('_analytics.json'):
            filepath = os.path.join(log_dir, filename)
            try:
                with open(filepath, 'r', encoding='utf-8') as f:
                    session = json.load(f)
                    sessions.append(session)
            except Exception as e:
                print(f"⚠️ Помилка читання {filename}: {e}")
                
    return sessions

def analyze_session(session):
    """Детальний аналіз однієї сесії"""
    print(f"\n{'='*60}")
    print(f"📋 Аналіз сесії: {session['case_id']}")
    print(f"{'='*60}")
    
    print(f"\n📊 Основна інформація:")
    print(f"  - Назва випадку: {session['case_name']}")
    print(f"  - Час: {session['timestamp']}")
    print(f"  - Режим: {session['mode']}")
    print(f"  - Статус: {session['status']}")
    
    print(f"\n💊 Діагноз:")
    print(f"  - Фінальний діагноз: {session['diagnosis']}")
    print(f"  - Впевненість: {session['confidence']}/5.0")
    print(f"  - Обґрунтування: {session['reasoning'][:100]}...")
    
    print(f"\n💰 Економіка:")
    print(f"  - Витрачено: ${session['cost']}")
    print(f"  - Бюджет: ${session['budget']}")
    print(f"  - Ефективність: {((session['budget'] - session['cost']) / session['budget'] * 100):.1f}%")
    
    print(f"\n⏱️ Продуктивність:")
    print(f"  - Тривалість: {session['duration']:.1f} секунд")
    print(f"  - Ітерацій: {session['iterations']}")
    
    # Аналіз розмов
    conversations = session.get('conversations', [])
    if conversations:
        print(f"\n💬 Аналіз розмов:")
        print(f"  - Всього раундів: {len(conversations)}")
        
        # Підрахунок активності агентів
        agent_activity = defaultdict(int)
        message_types = defaultdict(int)
        total_messages = 0
        
        for conv in conversations:
            for msg in conv.get('messages', []):
                agent_activity[msg['agent_name']] += 1
                message_types[msg['message_type']] += 1
                total_messages += 1
                
        print(f"  - Всього повідомлень: {total_messages}")
        
        print(f"\n  🤖 Активність агентів:")
        for agent, count in sorted(agent_activity.items(), key=lambda x: x[1], reverse=True):
            print(f"    - {agent}: {count} повідомлень ({count/total_messages*100:.1f}%)")
            
        print(f"\n  📝 Типи повідомлень:")
        for msg_type, count in sorted(message_types.items(), key=lambda x: x[1], reverse=True):
            print(f"    - {msg_type}: {count} ({count/total_messages*100:.1f}%)")
            
        # Деталі раундів
        print(f"\n  🔄 Деталі раундів:")
        for i, conv in enumerate(conversations):
            msgs = len(conv.get('messages', []))
            cost = conv.get('cost_incurred', 0)
            decision = conv.get('decision', 'N/A')
            print(f"    Раунд {i+1}: {msgs} повідомлень, ${cost:.2f}, Рішення: {decision}")

def generate_summary_report(sessions, output_file="mai_dx_summary_report.html"):
    """Генерація зведеного HTML звіту по всіх сесіях"""
    if not sessions:
        print("❌ Немає сесій для аналізу")
        return
        
    html_content = """
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>MAI-DX Summary Report</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; background: #f5f5f5; }
        .container { max-width: 1200px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        h1 { color: #333; text-align: center; }
        .summary { background: #e8f4f8; padding: 20px; border-radius: 8px; margin: 20px 0; }
        .metric { display: inline-block; margin: 10px 20px; }
        .metric-value { font-size: 24px; font-weight: bold; color: #007bff; }
        .metric-label { color: #666; }
        table { width: 100%; border-collapse: collapse; margin: 20px 0; }
        th { background: #007bff; color: white; padding: 12px; text-align: left; }
        td { padding: 10px; border-bottom: 1px solid #ddd; }
        tr:hover { background: #f5f5f5; }
        .success { color: #28a745; }
        .warning { color: #ffc107; }
        .danger { color: #dc3545; }
    </style>
</head>
<body>
    <div class="container">
        <h1>🏥 MAI-DX Summary Report</h1>
"""
    
    # Загальна статистика
    total_sessions = len(sessions)
    avg_accuracy = sum(s['confidence'] for s in sessions) / total_sessions
    avg_cost = sum(s['cost'] for s in sessions) / total_sessions
    avg_duration = sum(s['duration'] for s in sessions) / total_sessions
    success_rate = sum(1 for s in sessions if s['confidence'] >= 3.0) / total_sessions * 100
    
    html_content += f"""
        <div class="summary">
            <h2>📊 Загальна статистика</h2>
            <div class="metric">
                <div class="metric-value">{total_sessions}</div>
                <div class="metric-label">Всього сесій</div>
            </div>
            <div class="metric">
                <div class="metric-value">{avg_accuracy:.2f}/5.0</div>
                <div class="metric-label">Середня точність</div>
            </div>
            <div class="metric">
                <div class="metric-value">${avg_cost:.2f}</div>
                <div class="metric-label">Середня вартість</div>
            </div>
            <div class="metric">
                <div class="metric-value">{avg_duration:.1f}с</div>
                <div class="metric-label">Середній час</div>
            </div>
            <div class="metric">
                <div class="metric-value">{success_rate:.1f}%</div>
                <div class="metric-label">Успішність</div>
            </div>
        </div>
        
        <h2>📋 Детальна таблиця сесій</h2>
        <table>
            <thead>
                <tr>
                    <th>ID сесії</th>
                    <th>Час</th>
                    <th>Випадок</th>
                    <th>Діагноз</th>
                    <th>Точність</th>
                    <th>Вартість</th>
                    <th>Час</th>
                    <th>Статус</th>
                </tr>
            </thead>
            <tbody>
"""
    
    # Сортуємо сесії за часом
    sorted_sessions = sorted(sessions, key=lambda x: x['timestamp'], reverse=True)
    
    for session in sorted_sessions:
        status_class = 'success' if session['confidence'] >= 3.0 else 'warning'
        html_content += f"""
                <tr>
                    <td>{session['case_id']}</td>
                    <td>{session['timestamp'][:19]}</td>
                    <td>{session['case_name']}</td>
                    <td>{session['diagnosis']}</td>
                    <td class="{status_class}">{session['confidence']:.1f}/5.0</td>
                    <td>${session['cost']:.2f}</td>
                    <td>{session['duration']:.1f}с</td>
                    <td>{session['status']}</td>
                </tr>
"""
    
    html_content += """
            </tbody>
        </table>
        
        <p style="text-align: center; color: #666; margin-top: 30px;">
            Згенеровано: """ + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + """
        </p>
    </div>
</body>
</html>
"""
    
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write(html_content)
        
    print(f"\n✅ Зведений звіт збережено: {output_file}")

def export_to_csv(sessions, output_file="mai_dx_sessions.csv"):
    """Експорт сесій в CSV"""
    if not sessions:
        print("❌ Немає сесій для експорту")
        return
        
    # Створюємо DataFrame
    data = []
    for session in sessions:
        # Підраховуємо агентів та повідомлення
        total_messages = 0
        unique_agents = set()
        
        for conv in session.get('conversations', []):
            for msg in conv.get('messages', []):
                total_messages += 1
                unique_agents.add(msg['agent_name'])
                
        data.append({
            'case_id': session['case_id'],
            'timestamp': session['timestamp'],
            'case_name': session['case_name'],
            'mode': session['mode'],
            'diagnosis': session['diagnosis'],
            'confidence': session['confidence'],
            'cost': session['cost'],
            'budget': session['budget'],
            'duration': session['duration'],
            'iterations': session['iterations'],
            'status': session['status'],
            'total_messages': total_messages,
            'unique_agents': len(unique_agents)
        })
    
    df = pd.DataFrame(data)
    df.to_csv(output_file, index=False, encoding='utf-8')
    
    print(f"✅ Дані експортовано в CSV: {output_file}")
    print(f"   Всього записів: {len(df)}")

def main():
    parser = argparse.ArgumentParser(description='Аналіз логів MAI-DX')
    parser.add_argument('--dir', default='mai_dx_logs', help='Директорія з логами')
    parser.add_argument('--case', help='ID конкретної сесії для детального аналізу')
    parser.add_argument('--export-csv', action='store_true', help='Експортувати в CSV')
    parser.add_argument('--summary', action='store_true', help='Створити зведений HTML звіт')
    
    args = parser.parse_args()
    
    print("🔬 Аналіз логів MAI-DX")
    print("=" * 60)
    
    # Завантажуємо сесії
    sessions = load_all_sessions(args.dir)
    print(f"📁 Знайдено {len(sessions)} сесій в {args.dir}")
    
    if args.case:
        # Аналіз конкретної сесії
        session = next((s for s in sessions if s['case_id'] == args.case), None)
        if session:
            analyze_session(session)
        else:
            print(f"❌ Сесія {args.case} не знайдена")
    elif args.export_csv:
        # Експорт в CSV
        export_to_csv(sessions)
    elif args.summary:
        # Створення зведеного звіту
        generate_summary_report(sessions)
    else:
        # Загальна статистика
        if sessions:
            print(f"\n📊 Загальна статистика:")
            print(f"  - Середня точність: {sum(s['confidence'] for s in sessions) / len(sessions):.2f}/5.0")
            print(f"  - Середня вартість: ${sum(s['cost'] for s in sessions) / len(sessions):.2f}")
            print(f"  - Середній час: {sum(s['duration'] for s in sessions) / len(sessions):.1f} секунд")
            
            print(f"\n💡 Для детального аналізу використовуйте:")
            print(f"  python {__file__} --case <case_id>")
            print(f"  python {__file__} --export-csv")
            print(f"  python {__file__} --summary")

if __name__ == "__main__":
    main()