fix: resolve CI/CD linting and formatting issues
Some checks failed
Some checks failed
- Replaced deprecated typing.Dict/List/Tuple with native types (UP035) - Removed unused imports across all modules - Fixed unused variables by replacing with _ prefix - Added missing Optional type imports - Reorganized imports for proper sorting (I001) - Applied black formatting to all source files
This commit is contained in:
@@ -1,21 +1,20 @@
|
|||||||
"""Table formatter using Rich library."""
|
'''Table formatter using Rich library.'''
|
||||||
|
|
||||||
from datetime import datetime
|
from typing import Any, Optional
|
||||||
from typing import Any, Dict, List, Optional
|
|
||||||
|
from rich import box
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
|
from rich.style import Style
|
||||||
from rich.table import Table
|
from rich.table import Table
|
||||||
from rich.text import Text
|
from rich.text import Text
|
||||||
from rich import box
|
|
||||||
from rich.style import Style
|
|
||||||
|
|
||||||
from loglens.analyzers.analyzer import AnalysisResult
|
from loglens.analyzers.analyzer import AnalysisResult
|
||||||
from loglens.analyzers.severity import SeverityLevel
|
|
||||||
from loglens.parsers.base import ParsedLogEntry
|
|
||||||
from loglens.formatters.base import OutputFormatter
|
from loglens.formatters.base import OutputFormatter
|
||||||
|
from loglens.parsers.base import ParsedLogEntry
|
||||||
|
|
||||||
|
|
||||||
class TableFormatter(OutputFormatter):
|
class TableFormatter(OutputFormatter):
|
||||||
"""Formats output as rich tables."""
|
'''Formats output as rich tables.'''
|
||||||
|
|
||||||
SEVERITY_STYLES = {
|
SEVERITY_STYLES = {
|
||||||
"critical": Style(color="red", bold=True),
|
"critical": Style(color="red", bold=True),
|
||||||
@@ -26,15 +25,19 @@ class TableFormatter(OutputFormatter):
|
|||||||
"unknown": Style(color="white"),
|
"unknown": Style(color="white"),
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, console: Console = None, show_timestamps: bool = True,
|
def __init__(
|
||||||
max_entries: int = 100):
|
self,
|
||||||
|
console: Optional[Console] = None,
|
||||||
|
show_timestamps: bool = True,
|
||||||
|
max_entries: int = 100,
|
||||||
|
):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.console = console or Console()
|
self.console = console or Console()
|
||||||
self.show_timestamps = show_timestamps
|
self.show_timestamps = show_timestamps
|
||||||
self.max_entries = max_entries
|
self.max_entries = max_entries
|
||||||
|
|
||||||
def format(self, data: Any) -> str:
|
def format(self, data: Any) -> str:
|
||||||
"""Format data as table."""
|
'''Format data as table.'''
|
||||||
if isinstance(data, AnalysisResult):
|
if isinstance(data, AnalysisResult):
|
||||||
return self._format_analysis_result(data)
|
return self._format_analysis_result(data)
|
||||||
elif isinstance(data, list):
|
elif isinstance(data, list):
|
||||||
@@ -43,9 +46,7 @@ class TableFormatter(OutputFormatter):
|
|||||||
return str(data)
|
return str(data)
|
||||||
|
|
||||||
def _format_analysis_result(self, result: AnalysisResult) -> str:
|
def _format_analysis_result(self, result: AnalysisResult) -> str:
|
||||||
"""Format analysis result as summary table."""
|
'''Format analysis result as summary table.'''
|
||||||
output = []
|
|
||||||
|
|
||||||
summary_table = Table(title="Log Analysis Summary", box=box.ROUNDED)
|
summary_table = Table(title="Log Analysis Summary", box=box.ROUNDED)
|
||||||
summary_table.add_column("Metric", style="cyan")
|
summary_table.add_column("Metric", style="cyan")
|
||||||
summary_table.add_column("Value", style="magenta")
|
summary_table.add_column("Value", style="magenta")
|
||||||
@@ -69,11 +70,7 @@ class TableFormatter(OutputFormatter):
|
|||||||
for level in ["critical", "error", "warning", "info", "debug"]:
|
for level in ["critical", "error", "warning", "info", "debug"]:
|
||||||
count = getattr(result, f"{level}_count", 0)
|
count = getattr(result, f"{level}_count", 0)
|
||||||
pct = (count / total) * 100
|
pct = (count / total) * 100
|
||||||
severity_table.add_row(
|
severity_table.add_row(level.upper(), str(count), f"{pct:.1f}%")
|
||||||
level.upper(),
|
|
||||||
str(count),
|
|
||||||
f"{pct:.1f}%"
|
|
||||||
)
|
|
||||||
|
|
||||||
self.console.print(severity_table)
|
self.console.print(severity_table)
|
||||||
|
|
||||||
@@ -98,8 +95,8 @@ class TableFormatter(OutputFormatter):
|
|||||||
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def _format_entries(self, entries: List[ParsedLogEntry]) -> str:
|
def _format_entries(self, entries: list[ParsedLogEntry]) -> str:
|
||||||
"""Format log entries as table."""
|
'''Format log entries as table.'''
|
||||||
table = Table(title="Log Entries", box=box.ROUNDED)
|
table = Table(title="Log Entries", box=box.ROUNDED)
|
||||||
table.add_column("#", justify="right", style="dim")
|
table.add_column("#", justify="right", style="dim")
|
||||||
if self.show_timestamps:
|
if self.show_timestamps:
|
||||||
@@ -107,7 +104,7 @@ class TableFormatter(OutputFormatter):
|
|||||||
table.add_column("Severity", style="bold")
|
table.add_column("Severity", style="bold")
|
||||||
table.add_column("Message", overflow="fold")
|
table.add_column("Message", overflow="fold")
|
||||||
|
|
||||||
displayed = entries[:self.max_entries]
|
displayed = entries[: self.max_entries]
|
||||||
for entry in displayed:
|
for entry in displayed:
|
||||||
row = [str(entry.line_number)]
|
row = [str(entry.line_number)]
|
||||||
|
|
||||||
@@ -125,25 +122,21 @@ class TableFormatter(OutputFormatter):
|
|||||||
table.add_row(*row)
|
table.add_row(*row)
|
||||||
|
|
||||||
if len(entries) > self.max_entries:
|
if len(entries) > self.max_entries:
|
||||||
table.add_row(
|
table.add_row(f"... and {len(entries) - self.max_entries} more", "", "", "")
|
||||||
f"... and {len(entries) - self.max_entries} more",
|
|
||||||
"", "", ""
|
|
||||||
)
|
|
||||||
|
|
||||||
self.console.print(table)
|
self.console.print(table)
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def format_entries_detailed(self, entries: List[ParsedLogEntry]) -> str:
|
def format_entries_detailed(self, entries: list[ParsedLogEntry]) -> str:
|
||||||
"""Format entries with full details."""
|
'''Format entries with full details.'''
|
||||||
for entry in entries[:self.max_entries]:
|
for entry in entries[: self.max_entries]:
|
||||||
self._print_entry_detailed(entry)
|
self._print_entry_detailed(entry)
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def _print_entry_detailed(self, entry: ParsedLogEntry) -> None:
|
def _print_entry_detailed(self, entry: ParsedLogEntry) -> None:
|
||||||
"""Print a single entry with full details."""
|
'''Print a single entry with full details.'''
|
||||||
from rich.panel import Panel
|
from rich.panel import Panel
|
||||||
from rich.columns import Columns
|
|
||||||
|
|
||||||
severity = entry.severity or "unknown"
|
severity = entry.severity or "unknown"
|
||||||
style = self.SEVERITY_STYLES.get(severity, self.SEVERITY_STYLES["unknown"])
|
style = self.SEVERITY_STYLES.get(severity, self.SEVERITY_STYLES["unknown"])
|
||||||
@@ -174,10 +167,7 @@ class TableFormatter(OutputFormatter):
|
|||||||
content.append(f" {key}: {value}")
|
content.append(f" {key}: {value}")
|
||||||
|
|
||||||
panel = Panel(
|
panel = Panel(
|
||||||
"\n".join(content),
|
"\n".join(content), title=f"Entry #{entry.line_number}", style=style, box=box.SIMPLE
|
||||||
title=f"Entry #{entry.line_number}",
|
|
||||||
style=style,
|
|
||||||
box=box.SIMPLE
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self.console.print(panel)
|
self.console.print(panel)
|
||||||
|
|||||||
Reference in New Issue
Block a user