fix: auto-fix code issues (cron)

- 修复重复导入/字段
- 修复异常处理
- 修复PEP8格式问题 (E302, E305, E501)
- 修复行长度超过100字符的问题
- 修复F821未定义名称错误
This commit is contained in:
AutoFix Bot
2026-03-01 18:19:06 +08:00
parent 8f59c7b17c
commit e46c938b40
36 changed files with 1102 additions and 28 deletions

View File

@@ -17,6 +17,7 @@ DB_PATH = os.getenv("DB_PATH", "/app/data/insightflow.db")
# Constants
UUID_LENGTH = 8 # UUID 截断长度
@dataclass
class Project:
id: str
@@ -25,6 +26,7 @@ class Project:
created_at: str = ""
updated_at: str = ""
@dataclass
class Entity:
id: str
@@ -45,6 +47,7 @@ class Entity:
if self.attributes is None:
self.attributes = {}
@dataclass
class AttributeTemplate:
"""属性模板定义"""
@@ -65,6 +68,7 @@ class AttributeTemplate:
if self.options is None:
self.options = []
@dataclass
class EntityAttribute:
"""实体属性值"""
@@ -85,6 +89,7 @@ class EntityAttribute:
if self.options is None:
self.options = []
@dataclass
class AttributeHistory:
"""属性变更历史"""
@@ -98,6 +103,7 @@ class AttributeHistory:
changed_at: str = ""
change_reason: str = ""
@dataclass
class EntityMention:
id: str
@@ -108,6 +114,7 @@ class EntityMention:
text_snippet: str
confidence: float = 1.0
class DatabaseManager:
def __init__(self, db_path: str = DB_PATH):
self.db_path = db_path
@@ -135,7 +142,8 @@ class DatabaseManager:
conn = self.get_conn()
now = datetime.now().isoformat()
conn.execute(
"INSERT INTO projects (id, name, description, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
"""INSERT INTO projects (id, name, description, created_at, updated_at)
VALUES (?, ?, ?, ?, ?)""",
(project_id, name, description, now, now),
)
conn.commit()
@@ -186,7 +194,8 @@ class DatabaseManager:
"""通过名称查找实体(用于对齐)"""
conn = self.get_conn()
row = conn.execute(
"SELECT * FROM entities WHERE project_id = ? AND (name = ? OR canonical_name = ? OR aliases LIKE ?)",
"""SELECT * FROM entities WHERE project_id = ?
AND (name = ? OR canonical_name = ? OR aliases LIKE ?)""",
(project_id, name, name, f'%"{name}"%'),
).fetchone()
conn.close()
@@ -322,8 +331,9 @@ class DatabaseManager:
def add_mention(self, mention: EntityMention) -> EntityMention:
conn = self.get_conn()
conn.execute(
"""INSERT INTO entity_mentions (id, entity_id, transcript_id, start_pos, end_pos, text_snippet, confidence)
VALUES (?, ?, ?, ?, ?, ?, ?)""",
"""INSERT INTO entity_mentions
(id, entity_id, transcript_id, start_pos, end_pos, text_snippet, confidence)
VALUES (?, ?, ?, ?, ?, ?, ?)""",
(mention.id,
mention.entity_id,
mention.transcript_id,
@@ -359,7 +369,9 @@ class DatabaseManager:
conn = self.get_conn()
now = datetime.now().isoformat()
conn.execute(
"INSERT INTO transcripts (id, project_id, filename, full_text, type, created_at) VALUES (?, ?, ?, ?, ?, ?)",
"""INSERT INTO transcripts
(id, project_id, filename, full_text, type, created_at)
VALUES (?, ?, ?, ?, ?, ?)""",
(transcript_id,
project_id,
filename,
@@ -412,8 +424,9 @@ class DatabaseManager:
now = datetime.now().isoformat()
conn.execute(
"""INSERT INTO entity_relations
(id, project_id, source_entity_id, target_entity_id, relation_type, evidence, transcript_id, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(id, project_id, source_entity_id, target_entity_id, relation_type,
evidence, transcript_id, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(
relation_id,
project_id,
@@ -494,7 +507,9 @@ class DatabaseManager:
term_id = str(uuid.uuid4())[:UUID_LENGTH]
conn.execute(
"INSERT INTO glossary (id, project_id, term, pronunciation, frequency) VALUES (?, ?, ?, ?, ?)",
"""INSERT INTO glossary
(id, project_id, term, pronunciation, frequency)
VALUES (?, ?, ?, ?, ?)""",
(term_id, project_id, term, pronunciation, 1),
)
conn.commit()
@@ -840,8 +855,9 @@ class DatabaseManager:
if old_value != attr.value:
conn.execute(
"""INSERT INTO attribute_history
(id, entity_id, template_id, old_value, new_value, changed_by, changed_at, change_reason)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(id, entity_id, template_id, old_value, new_value,
changed_by, changed_at, change_reason)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(
str(uuid.uuid4())[:UUID_LENGTH],
attr.entity_id,
@@ -856,12 +872,18 @@ class DatabaseManager:
conn.execute(
"""INSERT OR REPLACE INTO entity_attributes
(id, entity_id, template_id, value, created_at, updated_at)
VALUES (
COALESCE((SELECT id FROM entity_attributes WHERE entity_id = ? AND template_id = ?), ?),
?, ?, ?,
COALESCE((SELECT created_at FROM entity_attributes WHERE entity_id = ? AND template_id = ?), ?),
?)""",
(id, entity_id, template_id, value, created_at, updated_at)
VALUES (
COALESCE(
(SELECT id FROM entity_attributes
WHERE entity_id = ? AND template_id = ?), ?
),
?, ?, ?,
COALESCE(
(SELECT created_at FROM entity_attributes
WHERE entity_id = ? AND template_id = ?), ?
),
?)""",
(
attr.entity_id,
attr.template_id,
@@ -912,15 +934,17 @@ class DatabaseManager:
):
conn = self.get_conn()
old_row = conn.execute(
"SELECT value FROM entity_attributes WHERE entity_id = ? AND template_id = ?",
"""SELECT value FROM entity_attributes
WHERE entity_id = ? AND template_id = ?""",
(entity_id, template_id),
).fetchone()
if old_row:
conn.execute(
"""INSERT INTO attribute_history
(id, entity_id, template_id, old_value, new_value, changed_by, changed_at, change_reason)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(id, entity_id, template_id, old_value, new_value,
changed_by, changed_at, change_reason)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(
str(uuid.uuid4())[:UUID_LENGTH],
entity_id,
@@ -1107,8 +1131,9 @@ class DatabaseManager:
conn.execute(
"""INSERT INTO video_frames
(id, video_id, frame_number, timestamp, image_url, ocr_text, extracted_entities, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(id, video_id, frame_number, timestamp, image_url, ocr_text,
extracted_entities, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(
frame_id,
video_id,
@@ -1394,9 +1419,11 @@ class DatabaseManager:
conn.close()
return stats
# Singleton instance
_db_manager = None
def get_db_manager() -> DatabaseManager:
global _db_manager
if _db_manager is None: