forked from HG-ha/ICP_Query
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
424 lines (353 loc) · 15 KB
/
Copy pathdatabase.py
File metadata and controls
424 lines (353 loc) · 15 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# -*- coding: utf-8 -*-
import sqlite3
import json
from datetime import datetime
from mlog import logger
import os
class Database:
def __init__(self, db_path="icp_history.db"):
self.db_path = db_path
self.init_db()
def init_db(self):
"""初始化数据库表"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# 创建历史记录表
cursor.execute('''
CREATE TABLE IF NOT EXISTS search_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
search_type TEXT NOT NULL,
search_keyword TEXT NOT NULL,
result_count INTEGER DEFAULT 0,
search_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
result_data TEXT
)
''')
# 创建批量任务历史表
cursor.execute('''
CREATE TABLE IF NOT EXISTS batch_task_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task_name TEXT NOT NULL UNIQUE,
task_type TEXT NOT NULL,
total_count INTEGER DEFAULT 0,
completed_count INTEGER DEFAULT 0,
success_count INTEGER DEFAULT 0,
status TEXT DEFAULT 'running',
result_file TEXT,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
finish_time TIMESTAMP
)
''')
# 创建索引
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_search_time
ON search_history(search_time DESC)
''')
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_search_type
ON search_history(search_type)
''')
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_batch_create_time
ON batch_task_history(create_time DESC)
''')
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_batch_status
ON batch_task_history(status)
''')
conn.commit()
conn.close()
logger.info(f"数据库初始化完成: {self.db_path}")
except Exception as e:
logger.error(f"数据库初始化失败: {e}")
raise
def add_history(self, search_type, search_keyword, result_count=0, result_data=None):
"""添加搜索历史"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
result_json = json.dumps(result_data, ensure_ascii=False) if result_data else None
cursor.execute('''
INSERT INTO search_history (search_type, search_keyword, result_count, result_data)
VALUES (?, ?, ?, ?)
''', (search_type, search_keyword, result_count, result_json))
history_id = cursor.lastrowid
conn.commit()
conn.close()
logger.info(f"添加历史记录成功: {search_type} - {search_keyword}")
return history_id
except Exception as e:
logger.error(f"添加历史记录失败: {e}")
return None
def get_history(self, limit=100, offset=0, search_type=None):
"""获取历史记录"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
if search_type:
cursor.execute('''
SELECT id, search_type, search_keyword, result_count, search_time
FROM search_history
WHERE search_type = ?
ORDER BY search_time DESC
LIMIT ? OFFSET ?
''', (search_type, limit, offset))
else:
cursor.execute('''
SELECT id, search_type, search_keyword, result_count, search_time
FROM search_history
ORDER BY search_time DESC
LIMIT ? OFFSET ?
''', (limit, offset))
rows = cursor.fetchall()
conn.close()
history_list = []
for row in rows:
history_list.append({
'id': row[0],
'search_type': row[1],
'search_keyword': row[2],
'result_count': row[3],
'search_time': row[4]
})
return history_list
except Exception as e:
logger.error(f"获取历史记录失败: {e}")
return []
def get_history_detail(self, history_id):
"""获取历史记录详情"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
SELECT id, search_type, search_keyword, result_count, search_time, result_data
FROM search_history
WHERE id = ?
''', (history_id,))
row = cursor.fetchone()
conn.close()
if row:
result_data = json.loads(row[5]) if row[5] else None
return {
'id': row[0],
'search_type': row[1],
'search_keyword': row[2],
'result_count': row[3],
'search_time': row[4],
'result_data': result_data
}
return None
except Exception as e:
logger.error(f"获取历史记录详情失败: {e}")
return None
def delete_history(self, history_id):
"""删除历史记录"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('DELETE FROM search_history WHERE id = ?', (history_id,))
conn.commit()
conn.close()
logger.info(f"删除历史记录成功: ID={history_id}")
return True
except Exception as e:
logger.error(f"删除历史记录失败: {e}")
return False
def clear_history(self, search_type=None):
"""清空历史记录"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
if search_type:
cursor.execute('DELETE FROM search_history WHERE search_type = ?', (search_type,))
else:
cursor.execute('DELETE FROM search_history')
conn.commit()
conn.close()
logger.info(f"清空历史记录成功: {search_type if search_type else '全部'}")
return True
except Exception as e:
logger.error(f"清空历史记录失败: {e}")
return False
def get_history_count(self, search_type=None):
"""获取历史记录总数"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
if search_type:
cursor.execute('SELECT COUNT(*) FROM search_history WHERE search_type = ?', (search_type,))
else:
cursor.execute('SELECT COUNT(*) FROM search_history')
count = cursor.fetchone()[0]
conn.close()
return count
except Exception as e:
logger.error(f"获取历史记录总数失败: {e}")
return 0
# ============ 批量任务历史管理 ============
def add_batch_task(self, task_name, task_type, total_count=0):
"""添加批量任务"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO batch_task_history (task_name, task_type, total_count, status)
VALUES (?, ?, ?, 'running')
''', (task_name, task_type, total_count))
task_id = cursor.lastrowid
conn.commit()
conn.close()
logger.info(f"添加批量任务成功: {task_name}")
return task_id
except Exception as e:
logger.error(f"添加批量任务失败: {e}")
return None
def update_batch_task(self, task_name, completed_count=None, success_count=None,
status=None, result_file=None, finish_time=None):
"""更新批量任务"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
updates = []
params = []
if completed_count is not None:
updates.append('completed_count = ?')
params.append(completed_count)
if success_count is not None:
updates.append('success_count = ?')
params.append(success_count)
if status is not None:
updates.append('status = ?')
params.append(status)
if result_file is not None:
updates.append('result_file = ?')
params.append(result_file)
if finish_time is not None:
updates.append('finish_time = ?')
params.append(finish_time)
updates.append('update_time = CURRENT_TIMESTAMP')
params.append(task_name)
sql = f"UPDATE batch_task_history SET {', '.join(updates)} WHERE task_name = ?"
cursor.execute(sql, params)
conn.commit()
conn.close()
return True
except Exception as e:
logger.error(f"更新批量任务失败: {e}")
return False
def get_batch_tasks(self, limit=100, offset=0, status=None):
"""获取批量任务列表"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
if status:
cursor.execute('''
SELECT id, task_name, task_type, total_count, completed_count,
success_count, status, result_file, create_time, update_time, finish_time
FROM batch_task_history
WHERE status = ?
ORDER BY create_time DESC
LIMIT ? OFFSET ?
''', (status, limit, offset))
else:
cursor.execute('''
SELECT id, task_name, task_type, total_count, completed_count,
success_count, status, result_file, create_time, update_time, finish_time
FROM batch_task_history
ORDER BY create_time DESC
LIMIT ? OFFSET ?
''', (limit, offset))
rows = cursor.fetchall()
conn.close()
task_list = []
for row in rows:
task_list.append({
'id': row[0],
'task_name': row[1],
'task_type': row[2],
'total_count': row[3],
'completed_count': row[4],
'success_count': row[5],
'status': row[6],
'result_file': row[7],
'create_time': row[8],
'update_time': row[9],
'finish_time': row[10]
})
return task_list
except Exception as e:
logger.error(f"获取批量任务列表失败: {e}")
return []
def get_batch_task_detail(self, task_name):
"""获取批量任务详情"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
SELECT id, task_name, task_type, total_count, completed_count,
success_count, status, result_file, create_time, update_time, finish_time
FROM batch_task_history
WHERE task_name = ?
''', (task_name,))
row = cursor.fetchone()
conn.close()
if row:
return {
'id': row[0],
'task_name': row[1],
'task_type': row[2],
'total_count': row[3],
'completed_count': row[4],
'success_count': row[5],
'status': row[6],
'result_file': row[7],
'create_time': row[8],
'update_time': row[9],
'finish_time': row[10]
}
return None
except Exception as e:
logger.error(f"获取批量任务详情失败: {e}")
return None
def get_batch_tasks_count(self, status=None):
"""获取批量任务总数"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
if status:
cursor.execute('SELECT COUNT(*) FROM batch_task_history WHERE status = ?', (status,))
else:
cursor.execute('SELECT COUNT(*) FROM batch_task_history')
count = cursor.fetchone()[0]
conn.close()
return count
except Exception as e:
logger.error(f"获取批量任务总数失败: {e}")
return 0
def delete_batch_task(self, task_name):
"""删除批量任务记录"""
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# 先获取结果文件路径
cursor.execute('SELECT result_file FROM batch_task_history WHERE task_name = ?', (task_name,))
row = cursor.fetchone()
result_file = row[0] if row else None
# 删除数据库记录
cursor.execute('DELETE FROM batch_task_history WHERE task_name = ?', (task_name,))
conn.commit()
conn.close()
# 删除结果文件
if result_file and os.path.exists(result_file):
try:
os.remove(result_file)
logger.info(f"删除结果文件成功: {result_file}")
except Exception as e:
logger.error(f"删除结果文件失败: {e}")
logger.info(f"删除批量任务成功: {task_name}")
return True
except Exception as e:
logger.error(f"删除批量任务失败: {e}")
return False