Refactor report generation in LogAnalyzer to improve readability and handle empty logs
Browse files- api/analysis.py +39 -11
api/analysis.py
CHANGED
@@ -64,23 +64,51 @@ class LogAnalyzer:
|
|
64 |
|
65 |
def generate_report(self) -> str:
|
66 |
"""Generate comprehensive analysis report"""
|
|
|
|
|
|
|
67 |
stats = self.get_basic_stats()
|
68 |
temporal = self.temporal_analysis()
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
"
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
f"Total Interactions: {stats['total_interactions']}\n"
|
75 |
f"Unique Users: {stats['unique_users']}\n"
|
76 |
-
f"Average Response Length: {stats['avg_response_length']:.1f} chars
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
80 |
f"- With context: {stats['knowledge_base_usage'].get('with_context', 0)}\n"
|
81 |
-
f"- Without context: {stats['knowledge_base_usage'].get('without_context', 0)}
|
|
|
|
|
|
|
82 |
"Usage Patterns:\n"
|
83 |
f"- Daily Activity: {temporal['daily_activity']}\n"
|
84 |
-
f"- Hourly Distribution: {temporal['hourly_pattern']}
|
85 |
)
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
def generate_report(self) -> str:
|
66 |
"""Generate comprehensive analysis report"""
|
67 |
+
if not self.logs:
|
68 |
+
return "No logs available for analysis"
|
69 |
+
|
70 |
stats = self.get_basic_stats()
|
71 |
temporal = self.temporal_analysis()
|
72 |
|
73 |
+
# Format top questions
|
74 |
+
top_questions = "\n".join(
|
75 |
+
f"- {q['question']}: {q['count']}"
|
76 |
+
for q in stats['most_common_questions']
|
77 |
+
)
|
78 |
+
|
79 |
+
# Build report sections separately
|
80 |
+
header = "Legal Assistant Usage Report\n----------------------------"
|
81 |
+
|
82 |
+
period = (f"Period: {self.logs[0]['timestamp']} - {self.logs[-1]['timestamp']}")
|
83 |
+
|
84 |
+
basic_stats = (
|
85 |
f"Total Interactions: {stats['total_interactions']}\n"
|
86 |
f"Unique Users: {stats['unique_users']}\n"
|
87 |
+
f"Average Response Length: {stats['avg_response_length']:.1f} chars"
|
88 |
+
)
|
89 |
+
|
90 |
+
kb_usage = (
|
91 |
+
"Knowledge Base Usage:\n"
|
92 |
f"- With context: {stats['knowledge_base_usage'].get('with_context', 0)}\n"
|
93 |
+
f"- Without context: {stats['knowledge_base_usage'].get('without_context', 0)}"
|
94 |
+
)
|
95 |
+
|
96 |
+
patterns = (
|
97 |
"Usage Patterns:\n"
|
98 |
f"- Daily Activity: {temporal['daily_activity']}\n"
|
99 |
+
f"- Hourly Distribution: {temporal['hourly_pattern']}"
|
100 |
)
|
101 |
+
|
102 |
+
# Combine all sections
|
103 |
+
report_sections = [
|
104 |
+
header,
|
105 |
+
period,
|
106 |
+
basic_stats,
|
107 |
+
"Top Questions:",
|
108 |
+
top_questions,
|
109 |
+
kb_usage,
|
110 |
+
patterns
|
111 |
+
]
|
112 |
+
|
113 |
+
# Join sections with double newlines
|
114 |
+
return "\n\n".join(report_sections)
|