Repository navigation
Expand file tree
/
Copy pathstorage.py
More file actions
265 lines (231 loc) · 10.8 KB
/
Copy pathstorage.py
File metadata and controls
265 lines (231 loc) · 10.8 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
import logging
from sqlmodel import Field, Session, SQLModel, create_engine, select
from singleton import Singleton
from enum import Enum
logger = logging.getLogger(__name__)
class IndexingStatus(Enum):
new_file = 1
need_reindexing = 2
no_need_reindexing = 3
class FileStatus(str, Enum):
uploaded = "uploaded"
indexing = "indexing"
indexed = "indexed"
failed = "failed"
class MinimaDoc(SQLModel, table=True):
fpath: str = Field(primary_key=True)
user_id: str = Field(index=True, default="default_user")
last_updated_seconds: int | None = Field(default=None, index=True)
indexing_time_seconds: float | None = Field(default=None)
status: str = Field(default=FileStatus.uploaded)
class MinimaDocUpdate(SQLModel):
fpath: str | None = None
user_id: str | None = None
last_updated_seconds: int | None = None
indexing_time_seconds: float | None = None
status: str | None = None
sqlite_file_name = "/indexer/storage/database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
connect_args = {"check_same_thread": False}
engine = create_engine(sqlite_url, connect_args=connect_args)
class MinimaStore(metaclass=Singleton):
@staticmethod
def create_db_and_tables():
SQLModel.metadata.create_all(engine)
# Run migration to add new columns if they don't exist
MinimaStore._migrate_schema()
@staticmethod
def _migrate_schema():
"""Add new columns to existing tables if they don't exist"""
import sqlite3
try:
# Use raw sqlite3 connection for schema migration
conn = sqlite3.connect(sqlite_file_name)
cursor = conn.cursor()
# Get existing columns
cursor.execute("PRAGMA table_info(minimadoc)")
existing_columns = {row[1] for row in cursor.fetchall()}
# Add indexing_time_seconds column if it doesn't exist
if 'indexing_time_seconds' not in existing_columns:
logger.info("Adding indexing_time_seconds column to minimadoc table")
cursor.execute("ALTER TABLE minimadoc ADD COLUMN indexing_time_seconds FLOAT")
conn.commit()
# Add status column if it doesn't exist
if 'status' not in existing_columns:
logger.info("Adding status column to minimadoc table")
cursor.execute(f"ALTER TABLE minimadoc ADD COLUMN status VARCHAR DEFAULT '{FileStatus.uploaded}'")
conn.commit()
# Add user_id column if it doesn't exist
if 'user_id' not in existing_columns:
logger.info("Adding user_id column to minimadoc table")
cursor.execute("ALTER TABLE minimadoc ADD COLUMN user_id VARCHAR DEFAULT 'default_user'")
cursor.execute("CREATE INDEX idx_user_id ON minimadoc(user_id)")
conn.commit()
logger.info("user_id column and index added successfully")
conn.close()
logger.info("Database schema migration completed successfully")
except Exception as e:
logger.error(f"Error during schema migration: {e}")
logger.error("You may need to manually update the database or delete it to recreate with new schema")
@staticmethod
def delete_m_doc(fpath: str, user_id: str = "default_user") -> None:
with Session(engine) as session:
statement = select(MinimaDoc).where(
MinimaDoc.fpath == fpath,
MinimaDoc.user_id == user_id
)
results = session.exec(statement)
doc = results.one()
session.delete(doc)
session.commit()
print("doc deleted:", doc)
@staticmethod
def select_m_doc(fpath: str, user_id: str = "default_user") -> MinimaDoc:
with Session(engine) as session:
statement = select(MinimaDoc).where(
MinimaDoc.fpath == fpath,
MinimaDoc.user_id == user_id
)
results = session.exec(statement)
doc = results.one()
print("doc:", doc)
return doc
@staticmethod
def find_removed_files(existing_file_paths: set[str], user_id: str = "default_user"):
removed_files: list[str] = []
with Session(engine) as session:
statement = select(MinimaDoc).where(MinimaDoc.user_id == user_id)
results = session.exec(statement)
logger.debug(f"find_removed_files count found {results}")
for doc in results:
logger.debug(f"find_removed_files file {doc.fpath} checking to remove")
if doc.fpath not in existing_file_paths:
logger.debug(f"find_removed_files file {doc.fpath} does not exist anymore, removing")
removed_files.append(doc.fpath)
for fpath in removed_files:
MinimaStore.delete_m_doc(fpath, us
BA28
er_id)
return removed_files
@staticmethod
def check_needs_indexing(fpath: str, last_updated_seconds: int, user_id: str = "default_user") -> IndexingStatus:
indexing_status: IndexingStatus = IndexingStatus.no_need_reindexing
try:
with Session(engine) as session:
statement = select(MinimaDoc).where(
MinimaDoc.fpath == fpath,
MinimaDoc.user_id == user_id
)
results = session.exec(statement)
doc = results.first()
if doc is not None:
logger.debug(
f"file {fpath} new last updated={last_updated_seconds} old last updated: {doc.last_updated_seconds}"
)
if doc.last_updated_seconds < last_updated_seconds:
indexing_status = IndexingStatus.need_reindexing
logger.debug(f"file {fpath} needs indexing, timestamp changed")
doc_update = MinimaDocUpdate(fpath=fpath, last_updated_seconds=last_updated_seconds)
doc_data = doc_update.model_dump(exclude_unset=True)
doc.sqlmodel_update(doc_data)
session.add(doc)
session.commit()
else:
logger.debug(f"file {fpath} doesn't need indexing, timestamp same")
else:
doc = MinimaDoc(fpath=fpath, last_updated_seconds=last_updated_seconds, user_id=user_id)
session.add(doc)
session.commit()
logger.debug(f"file {fpath} needs indexing, new file")
indexing_status = IndexingStatus.new_file
return indexing_status
except Exception as e:
logger.error(f"error updating file in the store {e}, skipping indexing")
return IndexingStatus.no_need_reindexing
@staticmethod
def update_indexing_time(fpath: str, indexing_time_seconds: float, user_id: str = "default_user") -> None:
try:
with Session(engine) as session:
statement = select(MinimaDoc).where(
MinimaDoc.fpath == fpath,
MinimaDoc.user_id == user_id
)
results = session.exec(statement)
doc = results.first()
if doc is not None:
doc_update = MinimaDocUpdate(indexing_time_seconds=indexing_time_seconds)
doc_data = doc_update.model_dump(exclude_unset=True)
doc.sqlmodel_update(doc_data)
session.add(doc)
session.commit()
logger.debug(f"Updated indexing time for {fpath}: {indexing_time_seconds}s")
except Exception as e:
logger.error(f"Error updating indexing time for {fpath}: {e}")
@staticmethod
def get_all_docs(user_id: str = "default_user") -> list[MinimaDoc]:
with Session(engine) as session:
statement = select(MinimaDoc).where(MinimaDoc.user_id == user_id)
results = session.exec(statement)
return list(results.all())
@staticmethod
def get_indexing_stats(user_id: str = "default_user") -> dict:
docs = MinimaStore.get_all_docs(user_id)
if not docs:
return {
"total_files": 0,
"total_indexing_time": 0.0,
"average_indexing_time": 0.0,
"files": []
}
total_time = sum(doc.indexing_time_seconds or 0.0 for doc in docs)
files_with_time = [doc for doc in docs if doc.indexing_time_seconds is not None]
avg_time = total_time / len(files_with_time) if files_with_time else 0.0
return {
"total_files": len(docs),
"total_indexing_time": round(total_time, 2),
"average_indexing_time": round(avg_time, 2),
"files": [
{
"path": doc.fpath,
"indexing_time_seconds": round(doc.indexing_time_seconds, 2) if doc.indexing_time_seconds else None,
"last_updated": doc.last_updated_seconds,
"status": doc.status
}
for doc in docs
]
}
@staticmethod
def update_file_status(fpath: str, status: FileStatus, user_id: str = "default_user") -> None:
try:
with Session(engine) as session:
statement = select(MinimaDoc).where(
MinimaDoc.fpath == fpath,
MinimaDoc.user_id == user_id
)
results = session.exec(statement)
doc = results.first()
if doc is not None:
doc_update = MinimaDocUpdate(status=status)
doc_data = doc_update.model_dump(exclude_unset=True)
doc.sqlmodel_update(doc_data)
session.add(doc)
session.commit()
logger.debug(f"Updated status for {fpath}: {status}")
except Exception as e:
logger.error(f"Error updating status for {fpath}: {e}")
@staticmethod
def get_files_status(fpaths: list[str], user_id: str = "default_user") -> list[dict]:
with Session(engine) as session:
statement = select(MinimaDoc).where(
MinimaDoc.fpath.in_(fpaths),
MinimaDoc.user_id == user_id
)
results = session.exec(statement)
docs = results.all()
return [
{
"path": doc.fpath,
"status": doc.status,
"indexing_time_seconds": round(doc.indexing_time_seconds, 2) if doc.indexing_time_seconds else None,
"last_updated": doc.last_updated_seconds
}
for doc in docs
]