-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_handler.py
More file actions
290 lines (243 loc) · 11.1 KB
/
database_handler.py
File metadata and controls
290 lines (243 loc) · 11.1 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
"""
Database handler for Neon PostgreSQL with vector support
"""
import os
import asyncio
import asyncpg
from typing import Dict, List, Any, Optional
from datetime import datetime
import json
import logging
from pathlib import Path
import numpy as np
from neon_integration import NeonIntegration
logger = logging.getLogger(__name__)
class DatabaseHandler:
def __init__(self, database_url: str):
self.database_url = database_url
self.pool: Optional[asyncpg.Pool] = None
self.neon_integration: Optional[NeonIntegration] = None
async def initialize(self):
"""Initialize database connection pool"""
try:
self.pool = await asyncpg.create_pool(
self.database_url,
min_size=5,
max_size=20,
command_timeout=60
)
logger.info("Database connection pool created")
# Ensure schema exists
await self.ensure_schema()
# Initialize Neon database integration
self.neon_integration = NeonIntegration(self.database_url)
await self.neon_integration.initialize()
except Exception as e:
logger.error(f"Failed to initialize database: {e}")
raise
async def ensure_schema(self):
"""Ensure database schema exists"""
schema_path = Path(__file__).parent / "database_schema.sql"
if schema_path.exists():
async with self.pool.acquire() as conn:
try:
with open(schema_path, 'r') as f:
schema_sql = f.read()
# Execute schema creation
await conn.execute(schema_sql)
logger.info("Database schema ensured")
except Exception as e:
logger.warning(f"Schema creation warning: {e}")
async def close(self):
"""Close database connection pool"""
if self.pool:
await self.pool.close()
logger.info("Database connection pool closed")
if self.neon_integration:
await self.neon_integration.close()
async def store_documents(self, documents: List[Dict[str, Any]]) -> List[str]:
"""Store documents in database"""
stored_ids = []
async with self.pool.acquire() as conn:
for doc in documents:
try:
# Check if document already exists
existing = await conn.fetchval(
"SELECT id FROM documents WHERE file_hash = $1",
doc.get("file_hash", "")
)
if existing:
stored_ids.append(str(existing))
continue
# Insert new document
doc_id = await conn.fetchval("""
INSERT INTO documents (
file_path, relative_path, file_name, file_type,
file_size, file_hash, category, content, metadata,
modified_at
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING id
""",
doc["file_path"],
doc["relative_path"],
doc["file_name"],
doc["file_type"],
doc["file_size"],
doc.get("file_hash", ""),
doc["category"],
doc.get("content", ""),
json.dumps(doc.get("metadata", {})),
datetime.fromisoformat(doc["modified_time"])
)
stored_ids.append(str(doc_id))
except Exception as e:
logger.error(f"Failed to store document {doc['file_name']}: {e}")
return stored_ids
async def store_timeline_events(self, events: List[Dict[str, Any]]) -> List[str]:
"""Store timeline events in database"""
stored_ids = []
async with self.pool.acquire() as conn:
for event in events:
try:
event_id = await conn.fetchval("""
INSERT INTO timeline_events (
event_date, event_type, description, amount,
source_account, destination_account,
source_institution, destination_institution,
reference_number, metadata
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING id
""",
datetime.fromisoformat(event["date"]).date(),
event["type"],
event["description"],
event.get("amount"),
event.get("source_account"),
event.get("destination_account"),
event.get("source_institution"),
event.get("destination_institution"),
event.get("reference_number"),
json.dumps(event.get("metadata", {}))
)
stored_ids.append(str(event_id))
# Link to source documents
if event.get("source_document_id"):
await conn.execute("""
INSERT INTO document_events (document_id, event_id, extracted_by)
VALUES ($1, $2, $3)
ON CONFLICT DO NOTHING
""",
event["source_document_id"],
event_id,
"claude"
)
except Exception as e:
logger.error(f"Failed to store event: {e}")
return stored_ids
async def store_exhibit(self, exhibit_data: Dict[str, Any]) -> str:
"""Store court exhibit in database"""
async with self.pool.acquire() as conn:
exhibit_id = await conn.fetchval("""
INSERT INTO court_exhibits (
exhibit_number, case_number, case_caption,
exhibit_description, status, date_marked, metadata
) VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id
""",
exhibit_data["exhibit_number"],
exhibit_data["case_number"],
exhibit_data["case_caption"],
exhibit_data.get("description"),
"draft",
datetime.now().date(),
json.dumps(exhibit_data.get("metadata", {}))
)
# Link documents to exhibit
for doc_info in exhibit_data.get("documents", []):
await conn.execute("""
INSERT INTO exhibit_documents (exhibit_id, document_id)
VALUES ($1, $2)
""",
exhibit_id,
doc_info["document_id"]
)
return str(exhibit_id)
async def store_analysis_query(self, query: str, response: str,
documents: List[str], metadata: Dict[str, Any]) -> str:
"""Store analysis query and response"""
async with self.pool.acquire() as conn:
query_id = await conn.fetchval("""
INSERT INTO analysis_queries (
query_text, response, status, model_used,
tokens_used, metadata, completed_at
) VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id
""",
query,
response,
"completed",
metadata.get("model", "claude-3"),
metadata.get("tokens_used"),
json.dumps(metadata),
datetime.now()
)
# Link related documents
for doc_id in documents:
await conn.execute("""
INSERT INTO query_documents (query_id, document_id)
VALUES ($1, $2)
""",
query_id,
doc_id
)
return str(query_id)
async def get_fund_flow_summary(self, start_date: Optional[str] = None,
end_date: Optional[str] = None) -> List[Dict[str, Any]]:
"""Get fund flow summary from materialized view"""
query = "SELECT * FROM fund_flow_summary WHERE 1=1"
params = []
if start_date:
params.append(datetime.fromisoformat(start_date).date())
query += f" AND event_date >= ${len(params)}"
if end_date:
params.append(datetime.fromisoformat(end_date).date())
query += f" AND event_date <= ${len(params)}"
query += " ORDER BY event_date"
async with self.pool.acquire() as conn:
rows = await conn.fetch(query, *params)
return [dict(row) for row in rows]
async def search_documents_vector(self, query_vector: np.ndarray,
limit: int = 10) -> List[Dict[str, Any]]:
"""Search documents using vector similarity"""
async with self.pool.acquire() as conn:
# Convert numpy array to list for PostgreSQL
vector_list = query_vector.tolist()
rows = await conn.fetch("""
SELECT id, file_name, relative_path, category,
content_vector <-> $1::vector as distance
FROM documents
WHERE content_vector IS NOT NULL
ORDER BY content_vector <-> $1::vector
LIMIT $2
""", vector_list, limit)
return [dict(row) for row in rows]
async def get_exhibit_package(self, package_id: str) -> Dict[str, Any]:
"""Get complete exhibit package with all exhibits"""
async with self.pool.acquire() as conn:
# Get package info
package = await conn.fetchrow("""
SELECT * FROM exhibit_packages WHERE id = $1
""", package_id)
if not package:
return None
package_dict = dict(package)
# Get exhibits in package
exhibits = await conn.fetch("""
SELECT e.*, pe.order_number
FROM court_exhibits e
JOIN package_exhibits pe ON e.id = pe.exhibit_id
WHERE pe.package_id = $1
ORDER BY pe.order_number
""", package_id)
package_dict["exhibits"] = [dict(e) for e in exhibits]
return package_dict