jarvis-ai-assistant 0.2.3__py3-none-any.whl → 0.2.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. jarvis/__init__.py +1 -1
  2. jarvis/jarvis_agent/__init__.py +13 -7
  3. jarvis/jarvis_agent/edit_file_handler.py +4 -0
  4. jarvis/jarvis_agent/jarvis.py +22 -25
  5. jarvis/jarvis_agent/main.py +6 -6
  6. jarvis/jarvis_code_agent/code_agent.py +273 -11
  7. jarvis/jarvis_code_analysis/code_review.py +21 -19
  8. jarvis/jarvis_data/config_schema.json +25 -29
  9. jarvis/jarvis_git_squash/main.py +3 -3
  10. jarvis/jarvis_git_utils/git_commiter.py +32 -11
  11. jarvis/jarvis_mcp/sse_mcp_client.py +4 -6
  12. jarvis/jarvis_mcp/streamable_mcp_client.py +5 -9
  13. jarvis/jarvis_rag/retriever.py +1 -1
  14. jarvis/jarvis_smart_shell/main.py +2 -2
  15. jarvis/jarvis_stats/__init__.py +13 -0
  16. jarvis/jarvis_stats/cli.py +404 -0
  17. jarvis/jarvis_stats/stats.py +538 -0
  18. jarvis/jarvis_stats/storage.py +381 -0
  19. jarvis/jarvis_stats/visualizer.py +282 -0
  20. jarvis/jarvis_tools/cli/main.py +82 -15
  21. jarvis/jarvis_tools/registry.py +32 -16
  22. jarvis/jarvis_tools/search_web.py +3 -3
  23. jarvis/jarvis_tools/virtual_tty.py +315 -26
  24. jarvis/jarvis_utils/config.py +12 -8
  25. jarvis/jarvis_utils/git_utils.py +8 -16
  26. jarvis/jarvis_utils/methodology.py +74 -67
  27. jarvis/jarvis_utils/utils.py +468 -72
  28. {jarvis_ai_assistant-0.2.3.dist-info → jarvis_ai_assistant-0.2.5.dist-info}/METADATA +29 -3
  29. {jarvis_ai_assistant-0.2.3.dist-info → jarvis_ai_assistant-0.2.5.dist-info}/RECORD +33 -28
  30. {jarvis_ai_assistant-0.2.3.dist-info → jarvis_ai_assistant-0.2.5.dist-info}/entry_points.txt +2 -0
  31. {jarvis_ai_assistant-0.2.3.dist-info → jarvis_ai_assistant-0.2.5.dist-info}/WHEEL +0 -0
  32. {jarvis_ai_assistant-0.2.3.dist-info → jarvis_ai_assistant-0.2.5.dist-info}/licenses/LICENSE +0 -0
  33. {jarvis_ai_assistant-0.2.3.dist-info → jarvis_ai_assistant-0.2.5.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,538 @@
1
+ """
2
+ 统计管理模块
3
+
4
+ 提供统计数据的增加、查看、分析等功能的主接口
5
+ """
6
+
7
+ from datetime import datetime, timedelta
8
+ from typing import Dict, List, Optional, Union, Any
9
+
10
+ from jarvis.jarvis_stats.storage import StatsStorage
11
+ from jarvis.jarvis_stats.visualizer import StatsVisualizer
12
+
13
+
14
+ class StatsManager:
15
+ """统计管理器"""
16
+
17
+ # 类级别的存储和可视化器实例
18
+ _storage: Optional[StatsStorage] = None
19
+ _visualizer: Optional[StatsVisualizer] = None
20
+
21
+ @classmethod
22
+ def _get_storage(cls) -> StatsStorage:
23
+ """获取存储实例"""
24
+ if cls._storage is None:
25
+ cls._storage = StatsStorage()
26
+ return cls._storage
27
+
28
+ @classmethod
29
+ def _get_visualizer(cls) -> StatsVisualizer:
30
+ """获取可视化器实例"""
31
+ if cls._visualizer is None:
32
+ cls._visualizer = StatsVisualizer()
33
+ return cls._visualizer
34
+
35
+ def __init__(self, storage_dir: Optional[str] = None):
36
+ """
37
+ 初始化统计管理器(保留以兼容旧代码)
38
+
39
+ Args:
40
+ storage_dir: 存储目录路径
41
+ """
42
+ # 如果提供了特定的存储目录,则重新初始化存储
43
+ if storage_dir is not None:
44
+ StatsManager._storage = StatsStorage(storage_dir)
45
+
46
+ @staticmethod
47
+ def increment(
48
+ metric_name: str,
49
+ amount: Union[int, float] = 1,
50
+ tags: Optional[Dict[str, str]] = None,
51
+ group: Optional[str] = None,
52
+ unit: str = "count",
53
+ ):
54
+ """
55
+ 增加计数型指标
56
+
57
+ Args:
58
+ metric_name: 指标名称
59
+ amount: 增加的数量,默认为1
60
+ tags: 标签字典
61
+ group: 指标分组,会自动添加到 tags 中
62
+ unit: 计量单位,默认为 "count"
63
+
64
+ Examples:
65
+ >>> StatsManager.increment("page_views")
66
+ >>> StatsManager.increment("downloads", 5)
67
+ >>> StatsManager.increment("response_time", 0.123, unit="seconds")
68
+ >>> StatsManager.increment("execute_script", 1, group="tool")
69
+ """
70
+ # 如果指定了分组,自动添加到 tags 中
71
+ if group:
72
+ if tags is None:
73
+ tags = {}
74
+ tags["group"] = group
75
+
76
+ storage = StatsManager._get_storage()
77
+ storage.add_metric(
78
+ metric_name=metric_name,
79
+ value=float(amount),
80
+ unit=unit,
81
+ timestamp=datetime.now(),
82
+ tags=tags,
83
+ )
84
+
85
+ @staticmethod
86
+ def list_metrics() -> List[str]:
87
+ """
88
+ 列出所有指标
89
+
90
+ Returns:
91
+ 指标名称列表
92
+ """
93
+ storage = StatsManager._get_storage()
94
+ return storage.list_metrics()
95
+
96
+ @staticmethod
97
+ def show(
98
+ metric_name: Optional[str] = None,
99
+ last_hours: Optional[int] = None,
100
+ last_days: Optional[int] = None,
101
+ start_time: Optional[datetime] = None,
102
+ end_time: Optional[datetime] = None,
103
+ format: str = "table",
104
+ aggregation: str = "hourly",
105
+ tags: Optional[Dict[str, str]] = None,
106
+ ):
107
+ """
108
+ 显示统计数据
109
+
110
+ Args:
111
+ metric_name: 指标名称,如果不指定则显示所有指标摘要
112
+ last_hours: 最近N小时
113
+ last_days: 最近N天
114
+ start_time: 开始时间
115
+ end_time: 结束时间
116
+ format: 显示格式 (table, chart, summary)
117
+ aggregation: 聚合方式 (hourly, daily)
118
+ tags: 过滤标签
119
+
120
+ Examples:
121
+ >>> StatsManager.show() # 显示所有指标摘要
122
+ >>> StatsManager.show("api_calls", last_hours=24) # 显示最近24小时
123
+ >>> StatsManager.show("response_time", last_days=7, format="chart") # 图表显示
124
+ """
125
+ # 处理时间范围
126
+ if end_time is None:
127
+ end_time = datetime.now()
128
+
129
+ if start_time is None:
130
+ if last_hours:
131
+ start_time = end_time - timedelta(hours=last_hours)
132
+ elif last_days:
133
+ start_time = end_time - timedelta(days=last_days)
134
+ else:
135
+ start_time = end_time - timedelta(days=7) # 默认7天
136
+
137
+ if metric_name is None:
138
+ # 显示所有指标摘要
139
+ StatsManager._show_metrics_summary(start_time, end_time, tags)
140
+ else:
141
+ # 根据格式显示数据
142
+ if format == "chart":
143
+ StatsManager._show_chart(
144
+ metric_name, start_time, end_time, aggregation, tags
145
+ )
146
+ elif format == "summary":
147
+ StatsManager._show_summary(
148
+ metric_name, start_time, end_time, aggregation, tags
149
+ )
150
+ else:
151
+ StatsManager._show_table(metric_name, start_time, end_time, tags)
152
+
153
+ @staticmethod
154
+ def plot(
155
+ metric_name: Optional[str] = None,
156
+ last_hours: Optional[int] = None,
157
+ last_days: Optional[int] = None,
158
+ start_time: Optional[datetime] = None,
159
+ end_time: Optional[datetime] = None,
160
+ aggregation: str = "hourly",
161
+ tags: Optional[Dict[str, str]] = None,
162
+ width: Optional[int] = None,
163
+ height: Optional[int] = None,
164
+ ):
165
+ """
166
+ 绘制指标的折线图
167
+
168
+ Args:
169
+ metric_name: 指标名称(可选,不指定则根据标签过滤所有匹配的指标)
170
+ last_hours: 最近N小时
171
+ last_days: 最近N天
172
+ start_time: 开始时间
173
+ end_time: 结束时间
174
+ aggregation: 聚合方式
175
+ tags: 过滤标签
176
+ width: 图表宽度
177
+ height: 图表高度
178
+
179
+ Examples:
180
+ >>> StatsManager.plot("response_time", last_hours=24)
181
+ >>> StatsManager.plot(tags={"service": "api"}, last_days=7)
182
+ """
183
+ # 处理时间范围
184
+ if end_time is None:
185
+ end_time = datetime.now()
186
+
187
+ if start_time is None:
188
+ if last_hours:
189
+ start_time = end_time - timedelta(hours=last_hours)
190
+ elif last_days:
191
+ start_time = end_time - timedelta(days=last_days)
192
+ else:
193
+ start_time = end_time - timedelta(days=7)
194
+
195
+ # 如果指定了metric_name,显示单个图表
196
+ if metric_name:
197
+ StatsManager._show_chart(
198
+ metric_name, start_time, end_time, aggregation, tags, width, height
199
+ )
200
+ else:
201
+ # 如果没有指定metric_name,根据标签过滤获取所有匹配的指标
202
+ StatsManager._show_multiple_charts(
203
+ start_time, end_time, aggregation, tags, width, height
204
+ )
205
+
206
+ @staticmethod
207
+ def get_stats(
208
+ metric_name: str,
209
+ last_hours: Optional[int] = None,
210
+ last_days: Optional[int] = None,
211
+ start_time: Optional[datetime] = None,
212
+ end_time: Optional[datetime] = None,
213
+ aggregation: Optional[str] = None,
214
+ tags: Optional[Dict[str, str]] = None,
215
+ ) -> Dict[str, Any]:
216
+ """
217
+ 获取统计数据
218
+
219
+ Args:
220
+ metric_name: 指标名称
221
+ last_hours: 最近N小时
222
+ last_days: 最近N天
223
+ start_time: 开始时间
224
+ end_time: 结束时间
225
+ aggregation: 聚合方式,如果指定则返回聚合数据
226
+ tags: 过滤标签
227
+
228
+ Returns:
229
+ 统计数据字典
230
+ """
231
+ # 处理时间范围
232
+ if end_time is None:
233
+ end_time = datetime.now()
234
+
235
+ if start_time is None:
236
+ if last_hours:
237
+ start_time = end_time - timedelta(hours=last_hours)
238
+ elif last_days:
239
+ start_time = end_time - timedelta(days=last_days)
240
+ else:
241
+ start_time = end_time - timedelta(days=7)
242
+
243
+ storage = StatsManager._get_storage()
244
+ if aggregation:
245
+ # 返回聚合数据
246
+ return storage.aggregate_metrics(
247
+ metric_name, start_time, end_time, aggregation, tags
248
+ )
249
+ else:
250
+ # 返回原始数据
251
+ records = storage.get_metrics(metric_name, start_time, end_time, tags)
252
+ return {
253
+ "metric": metric_name,
254
+ "records": records,
255
+ "count": len(records),
256
+ "start_time": start_time.isoformat(),
257
+ "end_time": end_time.isoformat(),
258
+ }
259
+
260
+ @staticmethod
261
+ def clean_old_data(days_to_keep: int = 30):
262
+ """
263
+ 清理旧数据
264
+
265
+ Args:
266
+ days_to_keep: 保留最近N天的数据
267
+ """
268
+ storage = StatsManager._get_storage()
269
+ storage.delete_old_data(days_to_keep)
270
+ print(f"已清理 {days_to_keep} 天前的数据")
271
+
272
+ @staticmethod
273
+ def remove_metric(metric_name: str) -> bool:
274
+ """
275
+ 删除指定的指标及其所有数据
276
+
277
+ Args:
278
+ metric_name: 要删除的指标名称
279
+
280
+ Returns:
281
+ True 如果成功删除,False 如果指标不存在
282
+ """
283
+ storage = StatsManager._get_storage()
284
+ return storage.delete_metric(metric_name)
285
+
286
+ @staticmethod
287
+ def _show_metrics_summary(
288
+ start_time: Optional[datetime] = None,
289
+ end_time: Optional[datetime] = None,
290
+ tags: Optional[Dict[str, str]] = None,
291
+ ):
292
+ """显示所有指标摘要"""
293
+ from rich.console import Console
294
+ from rich.table import Table
295
+
296
+ console = Console()
297
+ storage = StatsManager._get_storage()
298
+ metrics = storage.list_metrics()
299
+
300
+ if not metrics:
301
+ console.print("[yellow]没有找到任何统计指标[/yellow]")
302
+ return
303
+
304
+ # 如果没有指定时间范围,使用默认值
305
+ if end_time is None:
306
+ end_time = datetime.now()
307
+ if start_time is None:
308
+ start_time = end_time - timedelta(days=7)
309
+
310
+ # 创建表格
311
+ table = Table(title="统计指标摘要")
312
+ table.add_column("指标名称", style="cyan")
313
+ table.add_column("单位", style="green")
314
+ table.add_column("最后更新", style="yellow")
315
+ table.add_column("7天数据点", style="magenta")
316
+
317
+ # 过滤满足标签条件的指标
318
+ displayed_count = 0
319
+ for metric in metrics:
320
+ # 获取该指标的记录
321
+ records = storage.get_metrics(metric, start_time, end_time, tags)
322
+
323
+ # 如果指定了标签过滤,但没有匹配的记录,跳过该指标
324
+ if tags and len(records) == 0:
325
+ continue
326
+
327
+ info = storage.get_metric_info(metric)
328
+ unit = "-"
329
+ last_updated = "-"
330
+
331
+ if info:
332
+ unit = info.get("unit", "-")
333
+ last_updated = info.get("last_updated", "-")
334
+
335
+ # 格式化时间
336
+ if last_updated != "-":
337
+ try:
338
+ dt = datetime.fromisoformat(last_updated)
339
+ last_updated = dt.strftime("%Y-%m-%d %H:%M")
340
+ except:
341
+ pass
342
+
343
+ count = len(records)
344
+ table.add_row(metric, unit, last_updated, str(count))
345
+ displayed_count += 1
346
+
347
+ if displayed_count == 0:
348
+ console.print("[yellow]没有找到符合条件的指标[/yellow]")
349
+ if tags:
350
+ console.print(f"过滤条件: {tags}")
351
+ else:
352
+ console.print(table)
353
+ console.print(f"\n[green]总计: {displayed_count} 个指标[/green]")
354
+ if tags:
355
+ console.print(f"过滤条件: {tags}")
356
+
357
+ @staticmethod
358
+ def _show_table(
359
+ metric_name: str,
360
+ start_time: datetime,
361
+ end_time: datetime,
362
+ tags: Optional[Dict[str, str]],
363
+ ):
364
+ """以表格形式显示数据"""
365
+ storage = StatsManager._get_storage()
366
+ visualizer = StatsManager._get_visualizer()
367
+ records = storage.get_metrics(metric_name, start_time, end_time, tags)
368
+
369
+ # 获取指标信息
370
+ info = storage.get_metric_info(metric_name)
371
+ unit = info.get("unit", "") if info else ""
372
+
373
+ # 使用visualizer显示表格
374
+ visualizer.show_table(
375
+ records=records,
376
+ metric_name=metric_name,
377
+ unit=unit,
378
+ start_time=start_time.strftime("%Y-%m-%d %H:%M"),
379
+ end_time=end_time.strftime("%Y-%m-%d %H:%M"),
380
+ tags_filter=tags,
381
+ )
382
+
383
+ @staticmethod
384
+ def _show_chart(
385
+ metric_name: str,
386
+ start_time: datetime,
387
+ end_time: datetime,
388
+ aggregation: str,
389
+ tags: Optional[Dict[str, str]],
390
+ width: Optional[int] = None,
391
+ height: Optional[int] = None,
392
+ ):
393
+ """显示图表"""
394
+ storage = StatsManager._get_storage()
395
+ visualizer = StatsManager._get_visualizer()
396
+
397
+ # 获取聚合数据
398
+ aggregated = storage.aggregate_metrics(
399
+ metric_name, start_time, end_time, aggregation, tags
400
+ )
401
+
402
+ if not aggregated:
403
+ print(f"没有找到指标 '{metric_name}' 的数据")
404
+ return
405
+
406
+ # 获取指标信息
407
+ info = storage.get_metric_info(metric_name)
408
+ unit = info.get("unit", "") if info else ""
409
+
410
+ # 准备数据
411
+ first_item = next(iter(aggregated.values()), None)
412
+ is_simple_count = (
413
+ first_item
414
+ and first_item.get("min") == 1
415
+ and first_item.get("max") == 1
416
+ and first_item.get("avg") == 1
417
+ )
418
+
419
+ if unit == "count" or is_simple_count:
420
+ # 对于计数类指标,使用总和更有意义
421
+ data = {k: v["sum"] for k, v in aggregated.items()}
422
+ else:
423
+ # 对于其他指标(如耗时),使用平均值
424
+ data = {k: v["avg"] for k, v in aggregated.items()} # 设置可视化器尺寸
425
+ if width or height:
426
+ visualizer.width = width or visualizer.width
427
+ visualizer.height = height or visualizer.height
428
+
429
+ # 绘制图表
430
+ chart = visualizer.plot_line_chart(
431
+ data=data,
432
+ title=f"{metric_name} - {aggregation}聚合",
433
+ unit=unit,
434
+ show_values=True,
435
+ )
436
+
437
+ print(chart)
438
+
439
+ # 显示时间范围
440
+ from rich.panel import Panel
441
+ from rich.console import Console
442
+
443
+ console = Console()
444
+ console.print(
445
+ Panel(
446
+ f"[cyan]{start_time.strftime('%Y-%m-%d %H:%M')}[/] ~ [cyan]{end_time.strftime('%Y-%m-%d %H:%M')}[/]",
447
+ title="[bold]时间范围[/bold]",
448
+ expand=False,
449
+ style="dim",
450
+ border_style="green",
451
+ )
452
+ )
453
+
454
+ @staticmethod
455
+ def _show_multiple_charts(
456
+ start_time: datetime,
457
+ end_time: datetime,
458
+ aggregation: str,
459
+ tags: Optional[Dict[str, str]],
460
+ width: Optional[int] = None,
461
+ height: Optional[int] = None,
462
+ ):
463
+ """根据标签过滤显示多个指标的图表"""
464
+ from rich.console import Console
465
+
466
+ console = Console()
467
+ storage = StatsManager._get_storage()
468
+
469
+ # 获取所有指标
470
+ all_metrics = StatsManager.list_metrics()
471
+
472
+ # 根据标签过滤指标
473
+ matched_metrics = []
474
+ for metric in all_metrics:
475
+ # 获取该指标在时间范围内的数据
476
+ records = storage.get_metrics(metric, start_time, end_time, tags)
477
+ if records: # 如果有匹配标签的数据
478
+ matched_metrics.append(metric)
479
+
480
+ if not matched_metrics:
481
+ console.print("[yellow]没有找到匹配标签的指标数据[/yellow]")
482
+ return
483
+
484
+ console.print(f"[green]找到 {len(matched_metrics)} 个匹配的指标[/green]")
485
+
486
+ # 为每个匹配的指标绘制图表
487
+ for i, metric in enumerate(matched_metrics):
488
+ if i > 0:
489
+ console.print("\n" + "=" * 80 + "\n") # 分隔符
490
+
491
+ StatsManager._show_chart(
492
+ metric, start_time, end_time, aggregation, tags, width, height
493
+ )
494
+
495
+ @staticmethod
496
+ def _show_summary(
497
+ metric_name: str,
498
+ start_time: datetime,
499
+ end_time: datetime,
500
+ aggregation: str,
501
+ tags: Optional[Dict[str, str]],
502
+ ):
503
+ """显示汇总信息"""
504
+ storage = StatsManager._get_storage()
505
+ visualizer = StatsManager._get_visualizer()
506
+
507
+ # 获取聚合数据
508
+ aggregated = storage.aggregate_metrics(
509
+ metric_name, start_time, end_time, aggregation, tags
510
+ )
511
+
512
+ if not aggregated:
513
+ print(f"没有找到指标 '{metric_name}' 的数据")
514
+ return
515
+
516
+ # 获取指标信息
517
+ info = storage.get_metric_info(metric_name)
518
+ unit = info.get("unit", "") if info else ""
519
+
520
+ # 显示汇总
521
+ summary = visualizer.show_summary(aggregated, metric_name, unit, tags)
522
+ if summary: # 如果返回了内容才打印(兼容性)
523
+ print(summary)
524
+
525
+ # 显示时间范围
526
+ from rich.panel import Panel
527
+ from rich.console import Console
528
+
529
+ console = Console()
530
+ console.print(
531
+ Panel(
532
+ f"[cyan]{start_time.strftime('%Y-%m-%d %H:%M')}[/] ~ [cyan]{end_time.strftime('%Y-%m-%d %H:%M')}[/]",
533
+ title="[bold]时间范围[/bold]",
534
+ expand=False,
535
+ style="dim",
536
+ border_style="green",
537
+ )
538
+ )