import sqlite3 import json import os from collections import defaultdict def process_daily_summary(log_paths): summary = defaultdict(lambda: { 'total_count': 0, 'first_hour': None, 'hourly_counts': {}, 'log_content': None }) # 按文件名排序确保时间顺序 sorted_files = sorted(log_paths, key=lambda x: os.path.basename(x)) for file_path in sorted_files: # 提取小时部分 filename = os.path.basename(file_path) hour = filename.split('.')[0][-2:] try: conn = sqlite3.connect(file_path) cursor = conn.cursor() cursor.execute('SELECT component, level, position, sample as log_content, count FROM log_stats') rows = cursor.fetchall() conn.close() except Exception as e: print(f"Error processing {file_path}: {str(e)}") continue for component, level, position, log_content, count in rows: key = (component, level, position) entry = summary[key] # 首次出现时记录时间和内容 if not entry['first_hour']: entry['first_hour'] = hour entry['log_content'] = log_content # 累加总数 entry['total_count'] += count # 记录每小时的数量(直接覆盖,保证每个小时只保留最新记录) entry['hourly_counts'][hour] = count # 计算总数和比例 total_all = sum(entry['total_count'] for entry in summary.values()) result = [] for key in summary: component, level, position = key entry = summary[key] # 找出最大出现次数的小时 max_count = max(entry['hourly_counts'].values(), default=0) max_hours = [h for h, c in entry['hourly_counts'].items() if c == max_count] most_frequent_hour = max(max_hours) if max_hours else None result.append({ 'component': component, 'level': level, 'position': position, 'total': entry['total_count'], 'ratio': entry['total_count'] / total_all, 'first_hour': entry['first_hour'], 'most_frequent_hour': most_frequent_hour, 'log_content': entry['log_content'] }) return result def process_eigenvalue(historical_files, today_data): # 读取历史数据 history_totals = defaultdict(int) # 处理过去7天文件 for h_file in historical_files: try: with open(h_file, 'r') as f: data = json.load(f) for entry in data: key = (entry["component"], entry["level"], entry["position"]) history_totals[key] += entry["total"] except Exception as e: print(f"Error processing {h_file}: {str(e)}") # 添加标记 for entry in today_data: key = (entry["component"], entry["level"], entry["position"]) total = entry["total"] # 初始化标记 tags = [] # 标记1:过去7天未出现 is_new = key not in history_totals if is_new: tags.append("new") # 标记2:超过历史平均值的两倍 history_sum = history_totals.get(key, 0) history_avg = history_sum / 7 # 过去7天平均值 if total > 2 * history_avg: tags.append("surge") entry["tags"] = tags return today_data