Repository navigation
Expand file tree
/
Copy pathdatabase.py
More file actions
526 lines (458 loc) · 17.5 KB
/
Copy pathdatabase.py
File metadata and controls
526 lines (458 loc) · 17.5 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
import boto3
import os
import time
import logging
from datetime import datetime
from typing import List, Dict, Set
from decimal import Decimal
from dotenv import load_dotenv
from models import ScamDetectionRecord, UserHistory, LeaderboardEntry
logger = logging.getLogger(__name__)
# 載入環境變數
load_dotenv()
# DynamoDB 設定
TABLE_NAME = "ScamDetectionRecords"
AWS_REGION = os.getenv("AWS_REGION", "us-east-1")
# 取得 AWS 憑證(支援大小寫)
AWS_ACCESS_KEY_ID = os.getenv("aws_access_key_id") or os.getenv("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = os.getenv("aws_secret_access_key") or os.getenv("AWS_SECRET_ACCESS_KEY")
# 檢查憑證是否存在
if not AWS_ACCESS_KEY_ID or not AWS_SECRET_ACCESS_KEY:
raise ValueError(
"❌ AWS 憑證未設定!\n"
"請確認 .env 檔案包含:\n"
" - aws_access_key_id=你的key\n"
" - aws_secret_access_key=你的secret\n"
" - AWS_REGION=us-east-1"
)
def get_dynamodb_resource():
"""依目前環境變數取得 DynamoDB resource。"""
aws_region = os.getenv("AWS_REGION", "us-east-1")
aws_access_key_id = os.getenv("aws_access_key_id") or os.getenv("AWS_ACCESS_KEY_ID")
aws_secret_access_key = os.getenv("aws_secret_access_key") or os.getenv("AWS_SECRET_ACCESS_KEY")
return boto3.resource(
'dynamodb',
region_name=aws_region,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key
)
# 初始化 DynamoDB client
dynamodb = get_dynamodb_resource()
def get_table():
"""取得 DynamoDB table"""
return dynamodb.Table(TABLE_NAME)
def convert_decimal_to_int(obj):
"""將 Decimal 轉換為 int(DynamoDB 回傳的數字是 Decimal 型別)"""
if isinstance(obj, list):
return [convert_decimal_to_int(i) for i in obj]
elif isinstance(obj, dict):
return {k: convert_decimal_to_int(v) for k, v in obj.items()}
elif isinstance(obj, Decimal):
return int(obj) if obj % 1 == 0 else float(obj)
else:
return obj
def add_detection_record(record: ScamDetectionRecord):
"""新增偵測記錄到 DynamoDB"""
table = get_table()
# 設定時間戳記
if record.created_at is None:
record.created_at = datetime.now()
# 生成唯一 ID(使用 user_id + timestamp)
record_id = f"{record.user_id}#{record.created_at.isoformat()}"
# 準備資料
item = {
'record_id': record_id, # Primary Key
'user_id': record.user_id, # GSI Partition Key
'created_at': record.created_at.isoformat(),
'timestamp': record.created_at.isoformat(), # 相容舊格式
'input_content': record.input_content,
'risk_score': record.risk_score,
'category': record.category,
'analysis': record.analysis,
'expert_warning': record.expert_warning
}
# 寫入 DynamoDB
table.put_item(Item=item)
return item
def get_user_history(user_id: str) -> List[UserHistory]:
"""取得使用者歷史記錄"""
table = get_table()
# 使用 GSI 查詢特定使用者的所有記錄
response = table.query(
IndexName='UserIdIndex',
KeyConditionExpression='user_id = :uid',
ExpressionAttributeValues={
':uid': user_id
},
ScanIndexForward=False # 降序排列(最新的在前)
)
items = response.get('Items', [])
# 轉換 Decimal 為 int
items = convert_decimal_to_int(items)
# 轉換為 UserHistory 物件
return [UserHistory(**item) for item in items]
def get_leaderboard() -> List[LeaderboardEntry]:
"""取得排行榜"""
table = get_table()
# Scan 所有記錄(注意:大量資料時效能較差,建議使用聚合表)
response = table.scan()
items = response.get('Items', [])
# 處理分頁(如果資料超過 1MB)
while 'LastEvaluatedKey' in response:
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
items.extend(response.get('Items', []))
# 轉換 Decimal 為 int
items = convert_decimal_to_int(items)
# 統計每個使用者的數據
user_stats = {}
for record in items:
user_id = record.get('user_id')
if not user_id:
continue
if user_id not in user_stats:
user_stats[user_id] = {
'user_id': user_id,
'total_scams': 0,
'total_points': 0,
'coupons': 0
}
user_stats[user_id]['total_scams'] += 1
user_stats[user_id]['total_points'] += record.get('risk_score', 0)
# 風險分數 >= 8 可獲得優惠券
if record.get('risk_score', 0) >= 8:
user_stats[user_id]['coupons'] += 1
# 轉換為列表並排序(按總分排序)
leaderboard = list(user_stats.values())
leaderboard.sort(key=lambda x: x['total_points'], reverse=True)
return [LeaderboardEntry(**entry) for entry in leaderboard]
def create_table_if_not_exists():
"""建立 DynamoDB table(如果不存在)"""
try:
# 檢查 table 是否存在
table = dynamodb.Table(TABLE_NAME)
table.load()
print(f"✅ DynamoDB table '{TABLE_NAME}' 已存在")
return True
except dynamodb.meta.client.exceptions.ResourceNotFoundException:
print(f"⚠️ DynamoDB table '{TABLE_NAME}' 不存在,正在建立...")
# 建立 table
table = dynamodb.create_table(
TableName=TABLE_NAME,
KeySchema=[
{
'AttributeName': 'record_id',
'KeyType': 'HASH' # Partition key
}
],
AttributeDefinitions=[
{
'AttributeName': 'record_id',
'AttributeType': 'S'
},
{
'AttributeName': 'user_id',
'AttributeType': 'S'
},
{
'AttributeName': 'created_at',
'AttributeType': 'S'
}
],
GlobalSecondaryIndexes=[
{
'IndexName': 'UserIdIndex',
'KeySchema': [
{
'AttributeName': 'user_id',
'KeyType': 'HASH'
},
{
'AttributeName': 'created_at',
'KeyType': 'RANGE'
}
],
'Projection': {
'ProjectionType': 'ALL'
},
'ProvisionedThroughput': {
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
# 等待 table 建立完成
print("⏳ 等待 table 建立完成...")
table.wait_until_exists()
print(f"✅ DynamoDB table '{TABLE_NAME}' 建立成功!")
return True
except Exception as e:
print(f"❌ 建立 DynamoDB table 失敗: {e}")
return False
def create_team_tables_if_not_exist():
"""建立團隊協作相關的 DynamoDB 資料表(Teams, TeamMembers, ScamReports)"""
tables_config = [
# Teams Table
{
'name': 'Teams',
'key_schema': [
{'AttributeName': 'team_id', 'KeyType': 'HASH'}
],
'attribute_definitions': [
{'AttributeName': 'team_id', 'AttributeType': 'S'}
],
'gsi': []
},
# TeamMembers Table
{
'name': 'TeamMembers',
'key_schema': [
{'AttributeName': 'member_id', 'KeyType': 'HASH'}
],
'attribute_definitions': [
{'AttributeName': 'member_id', 'AttributeType': 'S'},
{'AttributeName': 'team_id', 'AttributeType': 'S'},
{'AttributeName': 'line_uid', 'AttributeType': 'S'},
{'AttributeName': 'contribution_points', 'AttributeType': 'N'}
],
'gsi': [
{
'IndexName': 'TeamIdIndex',
'KeySchema': [
{'AttributeName': 'team_id', 'KeyType': 'HASH'},
{'AttributeName': 'contribution_points', 'KeyType': 'RANGE'}
],
'Projection': {'ProjectionType': 'ALL'},
'ProvisionedThroughput': {
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
},
{
'IndexName': 'LineUidIndex',
'KeySchema': [
{'AttributeName': 'line_uid', 'KeyType': 'HASH'}
],
'Projection': {'ProjectionType': 'ALL'},
'ProvisionedThroughput': {
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
}
]
},
# ScamCategoryStatsCache Table
{
'name': 'ScamCategoryStatsCache',
'key_schema': [
{'AttributeName': 'cache_key', 'KeyType': 'HASH'}
],
'attribute_definitions': [
{'AttributeName': 'cache_key', 'AttributeType': 'S'}
],
'gsi': []
},
# ScamReports Table
{
'name': 'ScamReports',
'key_schema': [
{'AttributeName': 'report_id', 'KeyType': 'HASH'}
],
'attribute_definitions': [
{'AttributeName': 'report_id', 'AttributeType': 'S'},
{'AttributeName': 'normalized_url', 'AttributeType': 'S'},
{'AttributeName': 'team_id', 'AttributeType': 'S'},
{'AttributeName': 'reported_at', 'AttributeType': 'S'}
],
'gsi': [
{
'IndexName': 'NormalizedUrlIndex',
'KeySchema': [
{'AttributeName': 'normalized_url', 'KeyType': 'HASH'}
],
'Projection': {'ProjectionType': 'KEYS_ONLY'},
'ProvisionedThroughput': {
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
},
{
'IndexName': 'TeamIdIndex',
'KeySchema': [
{'AttributeName': 'team_id', 'KeyType': 'HASH'},
{'AttributeName': 'reported_at', 'KeyType': 'RANGE'}
],
'Projection': {'ProjectionType': 'ALL'},
'ProvisionedThroughput': {
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
}
]
}
]
for table_config in tables_config:
table_name = table_config['name']
try:
# 檢查 table 是否存在
table = dynamodb.Table(table_name)
table.load()
print(f"✅ DynamoDB table '{table_name}' 已存在")
except dynamodb.meta.client.exceptions.ResourceNotFoundException:
print(f"⚠️ DynamoDB table '{table_name}' 不存在,正在建立...")
# 準備建立參數
create_params = {
'TableName': table_name,
'KeySchema': table_config['key_schema'],
'AttributeDefinitions': table_config['attribute_definitions'],
'ProvisionedThroughput': {
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
}
# 如果有 GSI,加入設定
if table_config['gsi']:
create_params['GlobalSecondaryIndexes'] = table_config['gsi']
# 建立 table
table = dynamodb.create_table(**create_params)
# 等待 table 建立完成
print(f"⏳ 等待 table '{table_name}' 建立完成...")
table.wait_until_exists()
print(f"✅ DynamoDB table '{table_name}' 建立成功!")
except Exception as e:
print(f"❌ 建立 DynamoDB table '{table_name}' 失敗: {e}")
return False
return True
def create_mass_report_alerts_table():
"""建立 MassReportAlerts DynamoDB 資料表"""
table_name = 'MassReportAlerts'
try:
# 檢查 table 是否存在
table = dynamodb.Table(table_name)
table.load()
print(f"✅ DynamoDB table '{table_name}' 已存在")
return True
except dynamodb.meta.client.exceptions.ResourceNotFoundException:
print(f"⚠️ DynamoDB table '{table_name}' 不存在,正在建立...")
# 建立 table
table = dynamodb.create_table(
TableName=table_name,
KeySchema=[
{
'AttributeName': 'alert_id',
'KeyType': 'HASH' # Partition key
}
],
AttributeDefinitions=[
{
'AttributeName': 'alert_id',
'AttributeType': 'S'
},
{
'AttributeName': 'normalized_url',
'AttributeType': 'S'
}
],
GlobalSecondaryIndexes=[
{
'IndexName': 'NormalizedUrlIndex',
'KeySchema': [
{
'AttributeName': 'normalized_url',
'KeyType': 'HASH'
}
],
'Projection': {
'ProjectionType': 'ALL'
},
'ProvisionedThroughput': {
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
# 等待 table 建立完成
print(f"⏳ 等待 table '{table_name}' 建立完成...")
table.wait_until_exists()
print(f"✅ DynamoDB table '{table_name}' 建立成功!")
return True
except Exception as e:
print(f"❌ 建立 DynamoDB table '{table_name}' 失敗: {e}")
return False
def retry_database_write(operation, max_retries: int = 3, base_delay: float = 0.1):
"""對 DynamoDB 寫入操作進行指數退避重試。"""
last_error = None
for attempt in range(max_retries):
try:
return operation()
except Exception as exc:
last_error = exc
logger.error(
"Database write failed: attempt=%s max_retries=%s error=%s",
attempt + 1,
max_retries,
exc,
)
if attempt == max_retries - 1:
break
time.sleep(base_delay * (2 ** attempt))
raise last_error
def _scan_all_items(table) -> List[Dict]:
"""掃描整張 DynamoDB 資料表並處理分頁。"""
response = table.scan()
items = response.get('Items', [])
while 'LastEvaluatedKey' in response:
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
items.extend(response.get('Items', []))
return items
def _collect_valid_line_uids(items: List[Dict], field_name: str) -> Set[str]:
"""從資料列中擷取有效的 LINE UID。"""
valid_uids = set()
for item in items:
user_id = item.get(field_name)
if isinstance(user_id, str) and user_id.startswith('U'):
valid_uids.add(user_id)
return valid_uids
def get_all_active_user_ids(scam_reports_table=None, team_members_table=None) -> List[str]:
"""取得所有活躍使用者的 LINE UID。"""
if scam_reports_table is None:
scam_reports_table = dynamodb.Table('ScamReports')
if team_members_table is None:
team_members_table = dynamodb.Table('TeamMembers')
active_users = set()
active_users.update(
_collect_valid_line_uids(_scan_all_items(scam_reports_table), 'reporter_uid')
)
active_users.update(
_collect_valid_line_uids(_scan_all_items(team_members_table), 'line_uid')
)
return sorted(active_users)
def get_report_count(normalized_url: str, scam_reports_table=None) -> int:
"""查詢指定 normalized_url 的通報數量。"""
table = scam_reports_table or dynamodb.Table('ScamReports')
response = table.query(
IndexName='NormalizedUrlIndex',
KeyConditionExpression='normalized_url = :url',
ExpressionAttributeValues={':url': normalized_url},
Select='COUNT'
)
report_count = response.get('Count', 0)
while 'LastEvaluatedKey' in response:
response = table.query(
IndexName='NormalizedUrlIndex',
KeyConditionExpression='normalized_url = :url',
ExpressionAttributeValues={':url': normalized_url},
Select='COUNT',
ExclusiveStartKey=response['LastEvaluatedKey']
)
report_count += response.get('Count', 0)
return report_count