Add commands and parsers modules
This commit is contained in:
85
.termflow/commands/compare.py
Normal file
85
.termflow/commands/compare.py
Normal file
@@ -0,0 +1,85 @@
|
||||
"""Compare command for CLI."""
|
||||
|
||||
from typing import List, Optional
|
||||
|
||||
import click
|
||||
from rich.console import Console
|
||||
from rich.panel import Panel
|
||||
from rich.table import Table
|
||||
|
||||
from ..core.database import SessionDatabase
|
||||
from ..core.team_comparison import TeamComparisonAnalyzer
|
||||
|
||||
console = Console()
|
||||
|
||||
|
||||
@click.command(name="compare")
|
||||
@click.option(
|
||||
"--sessions",
|
||||
"-s",
|
||||
multiple=True,
|
||||
type=int,
|
||||
help="Session IDs to compare",
|
||||
)
|
||||
@click.option(
|
||||
"--all",
|
||||
is_flag=True,
|
||||
default=False,
|
||||
help="Compare all sessions",
|
||||
)
|
||||
@click.pass_context
|
||||
def compare_cmd(
|
||||
ctx: click.Context,
|
||||
sessions: tuple,
|
||||
all_sessions: bool,
|
||||
) -> None:
|
||||
"""Compare terminal sessions."""
|
||||
db_path = ctx.obj.get("db")
|
||||
db = SessionDatabase(db_path)
|
||||
|
||||
if all_sessions:
|
||||
all_sessions_data = db.get_all_sessions()
|
||||
session_ids = [s["id"] for s in all_sessions_data]
|
||||
else:
|
||||
session_ids = list(sessions)
|
||||
|
||||
if len(session_ids) < 2:
|
||||
console.print("[yellow]Please provide at least 2 session IDs to compare[/yellow]")
|
||||
return
|
||||
|
||||
sessions_data = []
|
||||
for sid in session_ids:
|
||||
session = db.get_session(sid)
|
||||
if session:
|
||||
commands = db.get_session_commands(sid)
|
||||
session["commands"] = commands
|
||||
sessions_data.append(session)
|
||||
|
||||
if not sessions_data:
|
||||
console.print("[yellow]No valid sessions found[/yellow]")
|
||||
return
|
||||
|
||||
analyzer = TeamComparisonAnalyzer()
|
||||
patterns = analyzer.analyze_sessions(sessions_data)
|
||||
|
||||
table = Table(title="Session Comparison")
|
||||
table.add_column("Session")
|
||||
table.add_column("Commands", justify="right")
|
||||
table.add_column("Common Patterns", justify="right")
|
||||
|
||||
for session in sessions_data:
|
||||
session_patterns = [p for p in patterns if session["name"] in p.users]
|
||||
table.add_row(
|
||||
session["name"],
|
||||
str(session["command_count"]),
|
||||
str(len(session_patterns)),
|
||||
)
|
||||
|
||||
console.print(table)
|
||||
|
||||
if patterns:
|
||||
pattern_text = click.style("Top Common Patterns:\n", bold=True)
|
||||
for i, pattern in enumerate(patterns[:5], 1):
|
||||
pattern_text += f"{i}. {pattern.commands} (used {pattern.frequency} times)\n"
|
||||
|
||||
console.print(Panel(pattern_text, title="Patterns"))
|
||||
Reference in New Issue
Block a user