-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_manager.py
More file actions
66 lines (56 loc) · 2.92 KB
/
Copy pathdb_manager.py
File metadata and controls
66 lines (56 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# reddit_style_discussion/db_manager.py
import os
from tinydb import TinyDB, Query
from datetime import datetime
import time
class DatabaseManager:
def __init__(self):
self.db_dir = 'database'
os.makedirs(self.db_dir, exist_ok=True)
self.db = TinyDB(os.path.join(self.db_dir, 'debate_db.json'),
indent=2, ensure_ascii=False)
self.CommentQuery = Query()
def save_comment(self, comment_data: dict):
"""새로운 댓글 저장"""
print(f"DB: Saving comment {comment_data.get('comment_id')}")
self.db.insert(comment_data)
def get_comment_by_id(self, comment_id: str) -> dict | None:
"""comment_id로 특정 댓글 조회"""
return self.db.get(self.CommentQuery.comment_id == comment_id)
def get_full_thread_by_id(self, thread_id: str) -> list[dict]:
"""thread_id로 전체 토론 기록 조회 (시간순 정렬)"""
thread_comments = self.db.search(self.CommentQuery.thread_id == thread_id)
return sorted(thread_comments, key=lambda x: x.get('timestamp', ''))
def get_latest_comments(self, limit: int = 10) -> list[dict]:
"""최신 댓글들 조회"""
all_comments = self.db.all()
return sorted(all_comments, key=lambda x: x.get('timestamp', ''), reverse=True)[:limit]
def update_evaluation_tag(self, comment_id: str, evaluator_id: str, tag: str):
"""평가 태그 추가/수정"""
print(f"DB: Updating evaluation for {comment_id} by {evaluator_id}")
comment = self.get_comment_by_id(comment_id)
if comment:
if 'evaluation_tags' not in comment:
comment['evaluation_tags'] = {}
comment['evaluation_tags'][evaluator_id] = tag
self.db.update({'evaluation_tags': comment['evaluation_tags']},
self.CommentQuery.comment_id == comment_id)
def get_debate_statistics(self, thread_id: str) -> dict:
"""토론 통계 조회"""
comments = self.get_full_thread_by_id(thread_id)
return {
'total_comments': len(comments),
'top_level_comments': len([c for c in comments if c.get('parent_id') is None]),
'replies': len([c for c in comments if c.get('parent_id') is not None]),
'participants': list(set([c['author_id'] for c in comments]))
}
def get_comments_by_author(self, thread_id: str, author_id: str) -> list[dict]:
"""특정 작성자의 댓글들 조회"""
return self.db.search(
(self.CommentQuery.thread_id == thread_id) &
(self.CommentQuery.author_id == author_id)
)
def get_recent_comments_by_thread(self, thread_id: str, limit: int = 5) -> list[dict]:
"""특정 스레드의 최신 댓글들 조회"""
thread_comments = self.get_full_thread_by_id(thread_id)
return thread_comments[-limit:] if thread_comments else []