-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhistory_manager.py
137 lines (116 loc) · 4.5 KB
/
history_manager.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import json
import os
from datetime import datetime
from tabulate import tabulate
from typing import List, Optional, Dict, Any
class HistoryManager:
def __init__(self, history_file='.mydb/history.json'):
self.history_file = history_file
self._ensure_history_file()
def _ensure_history_file(self):
"""Ensure history file exists"""
os.makedirs(os.path.dirname(self.history_file), exist_ok=True)
if not os.path.exists(self.history_file):
with open(self.history_file, 'w') as f:
json.dump([], f)
def add_entry(self, command, details, status='success', error=None):
"""Add a new entry to history"""
entry = {
'timestamp': datetime.now().isoformat(),
'command': command,
'details': details,
'status': status,
'error': error
}
history = self._read_history()
history.append(entry)
self._write_history(history)
def _read_history(self):
"""Read history from file"""
with open(self.history_file, 'r') as f:
return json.load(f)
def _write_history(self, history):
"""Write history to file"""
with open(self.history_file, 'w') as f:
json.dump(history, f, indent=2)
def get_history(self, limit=None):
"""Get formatted history"""
history = self._read_history()
if limit:
history = history[-limit:]
formatted_entries = []
for entry in history:
timestamp = datetime.fromisoformat(entry['timestamp']).strftime('%Y-%m-%d %H:%M:%S')
status_symbol = '✓' if entry['status'] == 'success' else '✗'
formatted_entries.append([
timestamp,
status_symbol,
entry['command'],
entry['details'],
entry['error'] if entry['error'] else ''
])
return formatted_entries
def clear_history(self, before_date: Optional[datetime] = None) -> bool:
"""
Clear history entries.
Args:
before_date (datetime, optional): If provided, only clear entries before this date
Returns:
bool: True if operation was successful, False otherwise
"""
try:
if before_date:
history = self._read_history()
filtered_history = [
entry for entry in history
if datetime.fromisoformat(entry['timestamp']) >= before_date
]
self._write_history(filtered_history)
else:
self._write_history([])
# Add entry about clearing history
self.add_entry(
command='clear_history',
details=f"History cleared {'before ' + before_date.strftime('%Y-%m-%d') if before_date else 'completely'}",
status='success'
)
return True
except Exception as e:
self.add_entry(
command='clear_history',
details='Failed to clear history',
status='failed',
error=str(e)
)
return False
def backup_history(self, backup_path: Optional[str] = None) -> bool:
"""
Create a backup of the history file.
Args:
backup_path (str, optional): Custom path for backup file
If not provided, uses timestamp-based name
Returns:
bool: True if backup was successful, False otherwise
"""
try:
if not backup_path:
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
backup_path = f'.mydb/history_backup_{timestamp}.json'
os.makedirs(os.path.dirname(backup_path), exist_ok=True)
history = self._read_history()
with open(backup_path, 'w') as f:
json.dump(history, f, indent=2)
self.add_entry(
command='backup_history',
details=f'History backed up to {backup_path}',
status='success'
)
return True
except Exception as e:
self.add_entry(
command='backup_history',
details='Failed to backup history',
status='failed',
error=str(e)
)
return False