-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
311 lines (255 loc) · 9 KB
/
database.py
File metadata and controls
311 lines (255 loc) · 9 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import os
import sqlite3
import threading
from datetime import datetime
from pathlib import Path
from typing import Optional, Dict, Any
from contextlib import contextmanager
# Database path
raw_path = os.getenv("SQLITE_PATH", "task_status.db")
DB_PATH = Path(raw_path)
# Handle case where user provides a directory path
if DB_PATH.is_dir() or raw_path.endswith('/') or raw_path.endswith('\\'):
DB_PATH = DB_PATH / "task_status.db"
print(f"Using Database Path: {DB_PATH.absolute()}")
# Thread-local storage for database connections
_thread_local = threading.local()
def get_connection():
"""Get thread-local database connection"""
if not hasattr(_thread_local, "connection"):
_thread_local.connection = sqlite3.connect(str(DB_PATH), check_same_thread=False)
_thread_local.connection.row_factory = sqlite3.Row
return _thread_local.connection
@contextmanager
def get_db():
"""Context manager for database operations"""
conn = get_connection()
try:
yield conn
conn.commit()
except Exception:
conn.rollback()
raise
def init_database():
"""Initialize the database with required tables"""
# Ensure the parent directory exists
try:
if not DB_PATH.parent.exists():
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
except Exception as e:
print(f"Warning: Failed to create database directory {DB_PATH.parent}: {e}")
with get_db() as conn:
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS tasks (
job_id TEXT PRIMARY KEY,
status TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
error_message TEXT,
filename TEXT,
total_pages INTEGER,
processed_pages INTEGER,
file_hash TEXT
)
""")
# Migration: Add file_hash column if it doesn't exist (for existing databases)
cursor.execute("PRAGMA table_info(tasks)")
columns = [info[1] for info in cursor.fetchall()]
if 'file_hash' not in columns:
cursor.execute("ALTER TABLE tasks ADD COLUMN file_hash TEXT")
# Create index on status for faster queries
cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_status ON tasks(status)
""")
# Create index on created_at for cleanup queries
cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_created_at ON tasks(created_at)
""")
# Create index on file_hash for duplicate detection
cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_file_hash ON tasks(file_hash)
""")
conn.commit()
def create_task(job_id: str, filename: str, file_hash: Optional[str] = None) -> bool:
"""
Create a new task in the database
Args:
job_id: Unique job identifier
filename: Original filename
file_hash: SHA-256 hash of the file content
Returns:
True if successful, False otherwise
"""
try:
with get_db() as conn:
cursor = conn.cursor()
now = datetime.utcnow().isoformat()
cursor.execute("""
INSERT INTO tasks (job_id, status, created_at, updated_at, filename, processed_pages, file_hash)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (job_id, "pending", now, now, filename, 0, file_hash))
return True
except Exception as e:
print(f"Error creating task: {e}")
return False
def update_task_status(
job_id: str,
status: str,
error_message: Optional[str] = None,
total_pages: Optional[int] = None,
processed_pages: Optional[int] = None
) -> bool:
"""
Update task status
Args:
job_id: Job identifier
status: New status (pending, processing, completed, failed)
error_message: Error message if failed
total_pages: Total number of pages
processed_pages: Number of processed pages
Returns:
True if successful, False otherwise
"""
try:
with get_db() as conn:
cursor = conn.cursor()
now = datetime.utcnow().isoformat()
# Build update query dynamically
updates = ["status = ?", "updated_at = ?"]
params = [status, now]
if error_message is not None:
updates.append("error_message = ?")
params.append(error_message)
if total_pages is not None:
updates.append("total_pages = ?")
params.append(total_pages)
if processed_pages is not None:
updates.append("processed_pages = ?")
params.append(processed_pages)
params.append(job_id)
query = f"UPDATE tasks SET {', '.join(updates)} WHERE job_id = ?"
cursor.execute(query, params)
return True
except Exception as e:
print(f"Error updating task status: {e}")
return False
def get_task_status(job_id: str) -> Optional[Dict[str, Any]]:
"""
Get task status by job ID
Args:
job_id: Job identifier
Returns:
Dictionary with task information or None if not found
"""
try:
with get_db() as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT job_id, status, created_at, updated_at, error_message,
filename, total_pages, processed_pages, file_hash
FROM tasks
WHERE job_id = ?
""", (job_id,))
row = cursor.fetchone()
if row:
return dict(row)
return None
except Exception as e:
print(f"Error getting task status: {e}")
return None
def get_completed_task_by_hash(file_hash: str) -> Optional[Dict[str, Any]]:
"""
Find a completed task with the given file hash
Args:
file_hash: SHA-256 hash of file content
Returns:
Task dictionary or None
"""
try:
with get_db() as conn:
cursor = conn.cursor()
# Find the most recent completed task with this hash
cursor.execute("""
SELECT job_id, status, created_at, updated_at, error_message,
filename, total_pages, processed_pages, file_hash
FROM tasks
WHERE file_hash = ? AND status = 'completed'
ORDER BY created_at DESC
LIMIT 1
""", (file_hash,))
row = cursor.fetchone()
if row:
return dict(row)
return None
except Exception as e:
print(f"Error getting completed task by hash: {e}")
return None
def delete_task(job_id: str) -> bool:
"""
Delete a task from the database
Args:
job_id: Job identifier
Returns:
True if successful, False otherwise
"""
try:
with get_db() as conn:
cursor = conn.cursor()
cursor.execute("DELETE FROM tasks WHERE job_id = ?", (job_id,))
return True
except Exception as e:
print(f"Error deleting task: {e}")
return False
def get_all_tasks(status: Optional[str] = None) -> list[Dict[str, Any]]:
"""
Get all tasks, optionally filtered by status
Args:
status: Optional status filter
Returns:
List of task dictionaries
"""
try:
with get_db() as conn:
cursor = conn.cursor()
if status:
cursor.execute("""
SELECT job_id, status, created_at, updated_at, error_message,
filename, total_pages, processed_pages, file_hash
FROM tasks
WHERE status = ?
ORDER BY created_at DESC
""", (status,))
else:
cursor.execute("""
SELECT job_id, status, created_at, updated_at, error_message,
filename, total_pages, processed_pages, file_hash
FROM tasks
ORDER BY created_at DESC
""")
rows = cursor.fetchall()
return [dict(row) for row in rows]
except Exception as e:
print(f"Error getting all tasks: {e}")
return []
def cleanup_old_tasks(days: int = 7) -> int:
"""
Clean up tasks older than specified days
Args:
days: Number of days to keep
Returns:
Number of tasks deleted
"""
try:
with get_db() as conn:
cursor = conn.cursor()
cutoff_date = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
cutoff_date = cutoff_date.replace(day=cutoff_date.day - days)
cursor.execute("""
DELETE FROM tasks
WHERE created_at < ?
""", (cutoff_date.isoformat(),))
return cursor.rowcount
except Exception as e:
print(f"Error cleaning up old tasks: {e}")
return 0