Spaces:
Running
Running
File size: 29,478 Bytes
6c9bd20 c02312b 1d099b9 c02312b 6c9bd20 fb52b49 f158a0c c02312b 2f1547f a7af181 1d099b9 26e174a a7af181 c02312b 2f1547f c02312b f158a0c c02312b de3a153 c02312b de3a153 c02312b de3a153 c02312b de3a153 2e458fd fb52b49 c02312b de3a153 c02312b de3a153 7685f28 c02312b de3a153 ab16582 c02312b 2f1547f c02312b 31c764b 52968d0 31c764b c02312b fb52b49 c02312b fb52b49 c02312b a7af181 c02312b fec04e4 c02312b fb52b49 c02312b fb52b49 c02312b fb52b49 c02312b 31c764b 52968d0 31c764b 6c9bd20 c02312b 6c9bd20 c02312b fb52b49 31c764b c02312b 6c9bd20 c02312b 52968d0 31c764b 2f1547f 31c764b c02312b fb52b49 c02312b fb52b49 c02312b fb52b49 c02312b fb52b49 c02312b e5e4931 c02312b fb52b49 c02312b f158a0c 31c764b 52968d0 31c764b c02312b 2f1547f c02312b ab16582 c02312b 2f1547f 6c9bd20 f158a0c c02312b 2f1547f c02312b f158a0c 2f1547f c02312b 2f1547f c02312b fb52b49 c02312b 31c764b 52968d0 31c764b 6c9bd20 c02312b 6c9bd20 c02312b fb52b49 31c764b c02312b 6c9bd20 c02312b 52968d0 31c764b 2f1547f 31c764b c02312b 2f1547f fb52b49 c02312b 2f1547f c02312b fb52b49 c02312b 2f1547f c02312b e5e4931 2f1547f c02312b ab16582 c02312b 2f1547f c02312b 2f1547f c02312b 2f1547f c02312b 1d099b9 c02312b 1d099b9 c02312b cad0da8 c02312b cad0da8 2f1547f cad0da8 2f1547f cad0da8 c02312b 1d099b9 c02312b 2f1547f c02312b |
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 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
import asyncio
from collections import Counter
from datetime import datetime, timedelta
import re
import os
from itertools import permutations
import json
from src.firestore_db import FirestoreDB
from src.cache import Cache
from src.logger import logger
from config.settings import Settings
class LotteryCore:
def __init__(self, db: FirestoreDB, cache: Cache):
self.db = db
self.cache = cache
self.initialized = False
logger.info("[LotteryCore] Khởi tạo với Firestore")
self.prize_mapping = {
"đặc biệt": "DB",
"giải nhất": "Nhat",
"giải nhì": "Nhi",
"giải ba": "Ba",
"giải tư": "Tu",
"giải năm": "Nam",
"giải sáu": "Sau",
"giải bảy": "Bay",
"giải tám": "Tam"
}
async def initialize(self):
if self.initialized:
return
try:
logger.info("[initialize] Khởi tạo cơ bản, không dùng vector")
self.initialized = True
except Exception as e:
logger.error(f"[initialize] Lỗi khi khởi tạo: {str(e)}")
raise
def _get_day_name(self, weekday: int) -> str:
days = {0: "Hai", 1: "Ba", 2: "Tư", 3: "Năm", 4: "Sáu", 5: "Bảy", 6: "Chủ Nhật"}
return days.get(weekday, "Không xác định")
async def parse_date(self, date_str: str) -> tuple[str, datetime] | None:
formats = [
"%d-%m-%Y", "%Y-%m-%d", "%d/%m/%Y", "%Y/%m/%d",
"%d-%m-%y", "%y-%m-%d", "%d/%m/%y", "%y/%m/%d",
"%d.%m.%Y", "%d-%m-%Y", "%d/%m/%Y", "%Y.%m.%d",
"%m/%d/%Y", "%m-%d-%Y", "%d%m%Y", "%Y%m%d"
]
for fmt in formats:
try:
parsed = datetime.strptime(date_str, fmt)
if parsed.year < 100:
parsed = parsed.replace(year=parsed.year + 2000)
return parsed.strftime("%d-%m-%Y"), parsed
except ValueError:
continue
logger.warning(f"[parse_date] Invalid date format: {date_str}")
return None
def tach_hang(self, so):
so_clean = so.replace('@', '')
so = "@" * (6 - len(so_clean)) + so_clean
return {
"trieu": so[0] if so[0] != '@' else '@',
"tramnghin": so[1] if so[1] != '@' else '@',
"chucnghin": so[2] if so[2] != '@' else '@',
"nghin": so[3] if so[3] != '@' else '@',
"chuc": so[4] if so[4] != '@' else '@',
"donvi": so[5] if so[5] != '@' else '@'
}
def create_longs(self, data):
if not isinstance(data.get("giai"), dict):
logger.warning(f"[create_longs] 'giai' không phải map")
return {f"L{i}": ["@"] * 18 for i in range(1, 7)}
lo = []
for giai in ["DB", "Nhat", "Nhi", "Ba", "Tu", "Nam", "Sau", "Bay", "Tam"]:
if giai in data["giai"]:
for so in data["giai"][giai]:
if not so.isdigit():
logger.warning(f"[create_longs] Số không hợp lệ trong {giai}: {so}")
lo.append("@@@@@@")
continue
so_clean = so.replace('@', '')
so = "@" * (6 - len(so_clean)) + so_clean
lo.append(so)
logger.debug(f"[create_longs] Số lô thu thập: {len(lo)}")
while len(lo) < 18:
lo.append("@@@@@@")
lo = lo[:18]
longs = {f"L{i}": [] for i in range(1, 7)}
for so in lo:
hang = self.tach_hang(so)
longs["L1"].append(str(hang["donvi"]))
longs["L2"].append(str(hang["chuc"]))
longs["L3"].append(str(hang["nghin"]))
longs["L4"].append(str(hang["chucnghin"]))
longs["L5"].append(str(hang["tramnghin"]))
longs["L6"].append(str(hang["trieu"]))
return longs
def _map_dai_name(self, dai_name: str) -> str:
dai_name_clean = dai_name.lower().replace(" ", "").replace("chủnhật", "").replace("chu", "")
dai_mapping = {
"xskh": "xskh",
"khánhhòa": "xskh",
"khanhhoa": "xskh",
"khánhhoà": "xskh",
"khanhhoà": "xskh",
"xổsốkhánhhòa": "xskh",
"xosokhanhhoa": "xskh",
"kshoa": "xskh",
"khhoa": "xskh",
"xsdl": "xsdl",
"đắklắk": "xsdl",
"daklak": "xsdl",
"đăklăk": "xsdl",
"daklăk": "xsdl",
"xổsốđắklắk": "xsdl",
"xosodaklak": "xsdl",
"daklac": "xsdl",
"đăklăc": "xsdl",
"xspy": "xspy",
"phúyên": "xspy",
"phuyen": "xspy",
"phuyén": "xspy",
"phúyền": "xspy",
"phuyeen": "xspy",
"xổsốphúyên": "xspy",
"xosophuyen": "xspy",
"xsqng": "xsqng",
"quảngngãi": "xsqng",
"quangngai": "xsqng",
"quảngngai": "xsqng",
"quangngãi": "xsqng",
"xổsốquảngngãi": "xsqng",
"xosoquangngai": "xsqng",
"qngai": "xsqng",
"xsgl": "xsgl",
"gialai": "xsgl",
"gialay": "xsgl",
"xổsốgialai": "xsgl",
"xosogialai": "xsgl",
"xsbd": "xsbd",
"bìnhđịnh": "xsbd",
"binhdinh": "xsbd",
"bìnhđinh": "xsbd",
"binhđinh": "xsbd",
"binhdịnh": "xsbd",
"xổsốbìnhđịnh": "xsbd",
"xosobinhdinh": "xsbd",
"bdinh": "xsbd",
}
mapped_dai = dai_mapping.get(dai_name_clean, dai_name_clean)
if mapped_dai == dai_name_clean and mapped_dai not in Settings.DAI_CONFIG:
logger.warning(f"[_map_dai_name] Tên đài không được ánh xạ: {dai_name}")
else:
logger.info(f"[_map_dai_name] Ánh xạ đài: {dai_name} -> {mapped_dai}")
return mapped_dai
def _validate_number(self, prize: str, number: str) -> bool:
expected_lengths = {
"DB": 6, "Nhat": 5, "Nhi": 5, "Ba": 5, "Tu": 5,
"Nam": 4, "Sau": 4, "Bay": 3, "Tam": 2
}
expected_length = expected_lengths.get(prize)
if not expected_length:
logger.warning(f"[_validate_number] Unknown prize: {prize}")
return False
if not number.isdigit():
logger.warning(f"[_validate_number] Number is not numeric: {number}")
return False
if len(number) != expected_length:
logger.warning(f"[_validate_number] Number length {len(number)} does not match expected {expected_length} for prize {prize}")
return False
return True
async def add_lottery_result(self, text: str) -> str:
try:
# Chuẩn hóa toàn bộ văn bản: thay thế tab và khoảng trắng liên tiếp
text = re.sub(r'[\t\s]+', ' ', text.strip())
# Khôi phục xuống dòng trước tên giải
for prize in self.prize_mapping:
text = re.sub(rf'({prize}\s)', f'\n\\1', text, flags=re.IGNORECASE)
lines = text.split('\n')
if not lines:
logger.debug("[add_lottery_result] Empty input text")
return "Lỗi: Văn bản rỗng."
header_match = re.match(
r"Xổ số\s+(.+?)(?:\s+Thứ\s+\w+|Chủ\s+Nhật)?\s*(\d{2}[-./]\d{2}[-./]\d{4})\b",
lines[0],
re.IGNORECASE
)
if not header_match:
logger.debug(f"[add_lottery_result] Invalid header format: {lines[0]}")
return "Lỗi: Dòng đầu phải có định dạng 'Xổ số <Đài> [Thứ X/Chủ Nhật] DD-MM-YYYY'. Ví dụ: 'Xổ số Đắk Lắk 17-06-2025'."
dai_name = header_match.group(1).strip()
ngay = header_match.group(2).replace("/", "-").replace(".", "-")
dai = self._map_dai_name(dai_name)
parsed_date = await self.parse_date(ngay)
if not parsed_date:
logger.debug(f"[add_lottery_result] Invalid date format: {ngay}")
return f"Lỗi: Định dạng ngày không hợp lệ: {ngay}."
ngay_normalized, date = parsed_date
if dai in Settings.DAI_CONFIG and date.weekday() not in Settings.DAI_CONFIG[dai]["days"]:
day_name = date.strftime('%A')
expected_days = ', '.join([self._get_day_name(d) for d in Settings.DAI_CONFIG[dai]["days"]])
logger.debug(f"[add_lottery_result] Invalid day: {ngay} is {day_name}, expected {expected_days} for {dai}")
return f"Lỗi: Đài {dai} chỉ quay vào {expected_days}. Ngày {ngay} là {day_name}."
giai_map = self.prize_mapping
expected_counts = {"DB": 1, "Nhat": 1, "Nhi": 1, "Ba": 2, "Tu": 7, "Nam": 1, "Sau": 3, "Bay": 1, "Tam": 1}
expected_lengths = {"DB": 6, "Nhat": 5, "Nhi": 5, "Ba": 5, "Tu": 5, "Nam": 4, "Sau": 4, "Bay": 3, "Tam": 2}
giai = {}
current_giai = None
numbers = []
error_lines = []
for i, line in enumerate(lines[1:], 1):
line = re.sub(r'[\t\s]+', ' ', line.strip())
line = re.sub(r'[.\-,;\s]+', ' ', line).strip()
logger.debug(f"[add_lottery_result] Processing line {i}: '{line}'")
if not line or "xổ số miền" in line.lower():
continue
for name, key in giai_map.items():
if re.match(rf"^{name}\b", line, re.IGNORECASE):
if current_giai and numbers:
giai[current_giai] = numbers
numbers = []
current_giai = key
line = re.sub(rf"^{name}\b", "", line, flags=re.IGNORECASE).strip()
break
if current_giai:
number_list = [num.strip() for num in line.split() if num.strip().isdigit()]
if not number_list:
error_lines.append(f"Dòng {i}: '{line}' (không tìm thấy số hợp lệ)")
numbers.extend(number_list)
logger.debug(f"[add_lottery_result] Extracted numbers for {current_giai}: {number_list}")
if current_giai and numbers:
giai[current_giai] = numbers
if not giai:
error_msg = "Lỗi: Không tìm thấy giải nào. Vui lòng kiểm tra định dạng kết quả, ví dụ: '/load_lottery Xổ số Đắk Lắk 17-06-2025\nĐặc biệt 123456\nGiải nhất 12345'."
if error_lines:
error_msg += "\nCác dòng lỗi:\n" + "\n".join(error_lines)
logger.debug(f"[add_lottery_result] No results parsed from input: {text}")
return error_msg
for key in expected_counts:
if key not in giai:
logger.debug(f"[add_lottery_result] Missing prize: {key}")
return f"Lỗi: Thiếu giải {key}. Vui lòng kiểm tra đầu vào."
if len(giai[key]) != expected_counts[key]:
logger.debug(f"[add_lottery_result] Incorrect number count for {key}: expected {expected_counts[key]}, got {len(giai[key])}")
return f"Lỗi: Giải {key} phải có {expected_counts[key]} số, nhận được {len(giai[key])} số."
for num in giai[key]:
if not self._validate_number(key, num):
logger.debug(f"[add_lottery_result] Invalid number for {key}: {num}")
return f"Lỗi: Số trong giải {key} phải có {expected_lengths[key]} chữ số và chỉ chứa số, nhận được '{num}'."
doc_id = f"lottery-{ngay_normalized}_{dai}"
if (await self.db.lottery_results.document(doc_id).get()).exists:
logger.debug(f"[add_lottery_result] Data already exists: {doc_id}")
return f"Dữ liệu cho {dai} ngày {ngay} đã tồn tại."
data = {"ngay": ngay_normalized, "dai": dai, "giai": giai}
if await self.db.save_lottery(data, is_vector=False):
logger.info(f"[add_lottery_result] Lưu kết quả thành công cho {dai} ngày {ngay}")
await self.cache.clear(type="lottery") # Sửa từ delete_all thành clear
return f"Đã thêm kết quả xổ số cho {dai} ngày {ngay}."
logger.error(f"[add_lottery_result] Failed to save data for {dai} ngày {ngay}")
return f"Lỗi khi lưu kết quả xổ số cho {dai} ngày {ngay}."
except Exception as e:
logger.error(f"[add_lottery_result] Lỗi: {str(e)}")
return f"Lỗi: {str(e)}"
async def load_lottery(self, text: str, override: bool = False) -> str:
try:
# Chuẩn hóa toàn bộ văn bản: thay thế tab và khoảng trắng liên tiếp
text = re.sub(r'[\t\s]+', ' ', text.strip())
# Khôi phục xuống dòng trước tên giải
for prize in self.prize_mapping:
text = re.sub(rf'({prize}\s)', f'\n\\1', text, flags=re.IGNORECASE)
lines = text.split('\n')
if not lines:
logger.debug("[load_lottery] Empty input text")
return "Lỗi: Văn bản rỗng."
if lines[0].lower().startswith(tuple(Settings.DAI_CONFIG.keys())):
parts = lines[0].split(" ", 2)
if len(parts) >= 2 and parts[0].lower() in Settings.DAI_CONFIG:
dai = parts[0].lower()
ngay = parts[1].replace("/", "-").replace(".", "-")
lines = lines[1:] if len(parts) == 2 else [parts[2]] + lines[1:]
else:
logger.debug(f"[load_lottery] Invalid format: {lines[0]}")
return "Lỗi: Định dạng không đúng. Vui lòng dùng: '<mã đài> DD-MM-YYYY' hoặc 'Xổ số <Đài> [Thứ X] DD-MM-YYYY'. Ví dụ: 'Xổ số Đắk Lắk 17-06-2025'."
else:
header_match = re.match(
r"Xổ số\s+(.+?)(?:\s+Thứ\s+\w+|Chủ\s+Nhật)?\s*(\d{2}[-./]\d{2}[-./]\d{4})\b",
lines[0],
re.IGNORECASE
)
if not header_match:
logger.debug(f"[load_lottery] Invalid header format: {lines[0]}")
return "Lỗi: Dòng đầu phải có định dạng 'Xổ số <Đài> [Thứ X/Chủ Nhật] DD-MM-YYYY'. Ví dụ: 'Xổ số Đắk Lắk 17-06-2025'."
dai_name = header_match.group(1).strip()
ngay = header_match.group(2).replace("/", "-").replace(".", "-")
dai = self._map_dai_name(dai_name)
parsed_date = await self.parse_date(ngay)
if not parsed_date:
logger.debug(f"[load_lottery] Invalid date format: {ngay}")
return f"Lỗi: Định dạng ngày không hợp lệ: {ngay}."
ngay_normalized, date = parsed_date
if dai in Settings.DAI_CONFIG and date.weekday() not in Settings.DAI_CONFIG[dai]["days"]:
day_name = date.strftime('%A')
expected_days = ', '.join([self._get_day_name(d) for d in Settings.DAI_CONFIG[dai]["days"]])
logger.debug(f"[load_lottery] Invalid day: {ngay} is {day_name}, expected {expected_days} for {dai}")
return f"Lỗi: Đài {dai} chỉ quay vào {expected_days}. Ngày {ngay} là {day_name}."
giai_map = self.prize_mapping
expected_counts = {"DB": 1, "Nhat": 1, "Nhi": 1, "Ba": 2, "Tu": 7, "Nam": 1, "Sau": 3, "Bay": 1, "Tam": 1}
expected_lengths = {"DB": 6, "Nhat": 5, "Nhi": 5, "Ba": 5, "Tu": 5, "Nam": 4, "Sau": 4, "Bay": 3, "Tam": 2}
giai = {}
current_giai = None
numbers = []
error_lines = []
for i, line in enumerate(lines[1:], 1):
line = re.sub(r'[\t\s]+', ' ', line.strip())
line = re.sub(r'[.\-,;\s]+', ' ', line).strip()
logger.debug(f"[load_lottery] Processing line {i}: '{line}'")
if not line or "xổ số miền" in line.lower():
continue
for name, key in giai_map.items():
if re.match(rf"^{name}\b", line, re.IGNORECASE):
if current_giai and numbers:
giai[current_giai] = numbers
numbers = []
current_giai = key
line = re.sub(rf"^{name}\b", "", line, flags=re.IGNORECASE).strip()
break
if current_giai:
number_list = [num.strip() for num in line.split() if num.strip().isdigit()]
if not number_list:
error_lines.append(f"Dòng {i}: '{line}' (không tìm thấy số hợp lệ)")
numbers.extend(number_list)
logger.debug(f"[load_lottery] Extracted numbers for {current_giai}: {number_list}")
if current_giai and numbers:
giai[current_giai] = numbers
if not giai:
error_msg = "Lỗi: Không tìm thấy giải nào. Vui lòng kiểm tra định dạng kết quả, ví dụ: '/load_lottery Xổ số Đắk Lắk 17-06-2025\nĐặc biệt 123456\nGiải nhất 12345'."
if error_lines:
error_msg += "\nCác dòng lỗi:\n" + "\n".join(error_lines)
logger.debug(f"[load_lottery] No results parsed from input: {text}")
return error_msg
for key in expected_counts:
if key not in giai:
logger.debug(f"[load_lottery] Missing prize: {key}")
return f"Lỗi: Thiếu giải {key}. Vui lòng kiểm tra đầu vào."
if len(giai[key]) != expected_counts[key]:
logger.debug(f"[load_lottery] Incorrect number count for {key}: expected {expected_counts[key]}, got {len(giai[key])}")
return f"Lỗi: Giải {key} phải có {expected_counts[key]} số, nhận được {len(giai[key])} số."
for num in giai[key]:
if not self._validate_number(key, num):
logger.debug(f"[load_lottery] Invalid number for {key}: {num}")
return f"Lỗi: Số trong giải {key} phải có {expected_lengths[key]} chữ số và chỉ chứa số, nhận được '{num}'."
doc_id = f"lottery-{ngay_normalized}_{dai}"
if not override and (await self.db.lottery_results.document(doc_id).get()).exists:
logger.debug(f"[load_lottery] Data already exists: {doc_id}")
return f"Dữ liệu cho {dai} ngày {ngay} đã tồn tại."
data = {"ngay": ngay_normalized, "dai": dai, "giai": giai}
if await self.db.save_lottery(data, is_vector=False):
logger.info(f"[load_lottery] Lưu kết quả thành công cho {dai} ngày {ngay}")
await self.cache.clear(type="lottery") # Sửa từ delete_all thành clear
return f"Kết quả xổ số đã được nạp thành công: Đã nạp kết quả xổ số cho {dai} ngày {ngay}."
logger.error(f"[load_lottery] Failed to save data for {dai} ngày {ngay}")
return f"Lỗi khi lưu kết quả xổ số cho {dai} ngày {ngay}."
except Exception as e:
logger.error(f"[load_lottery] Lỗi: {str(e)}")
return f"Lỗi: {str(e)}"
async def load_from_json(self, json_file_path: str) -> str:
try:
if not os.path.exists(json_file_path) or os.path.getsize(json_file_path) == 0:
logger.debug(f"[load_from_json] File not found or empty: {json_file_path}")
return f"Tệp {json_file_path} không tồn tại hoặc rỗng."
with open(json_file_path, 'r', encoding='utf-8') as f:
data_list = json.load(f)
if not isinstance(data_list, list):
logger.debug("[load_from_json] JSON data is not a list")
return "Dữ liệu JSON không phải là danh sách."
batch_size = 100
success_count = 0
processed_keys = set()
batch = []
for data in data_list:
ngay = data.get("ngay")
dai = data.get("dai")
giai = data.get("giai")
if not all([ngay, dai, giai]):
logger.warning(f"[load_from_json] Bỏ qua bản ghi thiếu dữ liệu: ngay={ngay}, dai={dai}")
continue
parsed_date = await self.parse_date(ngay)
if not parsed_date:
logger.warning(f"[load_from_json] Ngày không hợp lệ: {ngay}")
continue
ngay_normalized, date = parsed_date
dai = self._map_dai_name(dai)
if dai not in Settings.DAI_CONFIG:
logger.error(f"[load_from_json] Đài không hợp lệ: {dai}, bỏ qua bản ghi.")
continue
if date.weekday() not in Settings.DAI_CONFIG[dai]["days"]:
logger.warning(f"[load_from_json] Ngày {ngay} không hợp lệ cho đài {dai}")
continue
key = f"lottery-{ngay_normalized}_{dai}"
if key in processed_keys:
continue
processed_keys.add(key)
data["ngay"] = ngay_normalized
data["dai"] = dai
batch.append(data)
if len(batch) >= batch_size:
for data in batch:
try:
async with asyncio.timeout(10):
if await self.db.save_lottery(data, is_vector=False):
success_count += 1
except asyncio.TimeoutError:
logger.error(f"[load_from_json] Timeout: {data['ngay']}_{data['dai']}")
except Exception as e:
logger.error(f"[load_from_json] Lỗi khi lưu: {str(e)}")
batch = []
if batch:
for data in batch:
try:
async with asyncio.timeout(10):
if await self.db.save_lottery(data, is_vector=False):
success_count += 1
except asyncio.TimeoutError:
logger.error(f"[load_from_json] Timeout: {data['ngay']}_{data['dai']}")
except Exception as e:
logger.error(f"[load_from_json] Lỗi khi lưu: {str(e)}")
logger.info(f"[load_from_json] Đã lưu {success_count} bản ghi")
return f"Đã tải và lưu {success_count} bản ghi từ {json_file_path}."
except json.JSONDecodeError:
logger.debug(f"[load_from_json] Invalid JSON in file: {json_file_path}")
return f"Lỗi: Tệp {json_file_path} không chứa JSON hợp lệ."
except Exception as e:
logger.error(f"[load_from_json] Lỗi: {str(e)}")
return f"Lỗi: {str(e)}"
async def migrate_lottery_data(self) -> str:
try:
logger.info("[migrate_lottery_data] Bắt đầu chuẩn hóa dữ liệu")
moved_results = 0
deleted_count = 0
async for doc in await self.db.stream(data_type="lottery-results"):
doc_id = doc.id
data = doc.to_dict()
ngay = data.get("ngay")
dai = data.get("dai")
if not ngay or not dai:
logger.warning(f"Skipping invalid document: {doc_id}, ngay={ngay}, dai={dai}")
continue
parsed_date = await self.parse_date(ngay)
if not parsed_date:
logger.warning(f"Skipping invalid date in {doc_id}: {ngay}")
continue
ngay_normalized, _ = parsed_date
new_dai = self._map_dai_name(dai)
data["ngay"] = ngay_normalized
data["dai"] = new_dai
new_doc_id = f"lottery-{ngay_normalized}_{new_dai}"
if doc_id != new_doc_id:
await self.db.lottery_results.document(new_doc_id).set(data, merge=True)
await self.db.lottery_results.document(doc_id).delete()
moved_results += 1
deleted_count += 1
else:
await self.db.lottery_results.document(doc_id).set(data, merge=True)
moved_results += 1
logger.info(f"[migrate_lottery_data] Hoàn tất: {moved_results} bản ghi, {deleted_count} bản xóa")
return f"Hoàn tất: {moved_results} bản ghi, {deleted_count} bản xóa."
except Exception as e:
logger.error(f"[migrate_lottery_data] Lỗi: {str(e)}")
return f"Lỗi: {str(e)}"
async def analyze(self, dai: str, start_date: str, end_date: str) -> dict:
try:
cache_key = f"lottery:{dai}:{start_date}:{end_date}"
cached = await self.cache.get(cache_key, type="lottery")
if cached:
return cached
parsed_start = await self.parse_date(start_date)
parsed_end = await self.parse_date(end_date)
if not parsed_start or not parsed_end:
logger.debug(f"[analyze] Invalid date format: start={start_date}, end={end_date}")
return {"frequencies": {}, "triplets": [], "error": "Định dạng ngày không hợp lệ."}
start_date_str, _ = parsed_start
end_date_str, _ = parsed_end
data = await self.db.get_lottery(dai, start_date_str, end_date_str, data_type="results", select_fields=["ngay", "dai", "giai"])
if not data:
logger.debug(f"[analyze] No data found for {dai} from {start_date} to {end_date}")
return {"frequencies": {}, "triplets": [], "error": f"Không tìm thấy dữ liệu cho {dai} từ {start_date} đến {end_date}."}
hang_data = {"trieu": [], "tramnghin": [], "chucnghin": [], "nghin": [], "chuc": [], "donvi": []}
for entry in data:
for giai, so_list in entry["giai"].items():
for so in so_list:
hang = self.tach_hang(so)
for key in hang_data:
hang_data[key].append(hang[key])
stats = {}
top_numbers = {}
for key in hang_data:
counter = Counter(x for x in hang_data[key] if x != '@')
stats[key] = [{"number": num, "count": count} for num, count in counter.items()]
top_numbers[key] = counter.most_common(1)[0][0] if counter else '@'
numbers = [top_numbers[key] for key in ["trieu", "tramnghin", "chucnghin", "nghin", "chuc", "donvi"]]
triplets = [''.join(t) for t in permutations(numbers, 3)]
counter = Counter(triplets)
top_triplets = [triplet for triplet, _ in counter.most_common(5)]
result = {
"frequencies": stats,
"triplets": top_triplets
}
await self.cache.set(cache_key, result, type="lottery")
return result
except Exception as e:
logger.error(f"[analyze] Lỗi: {str(e)}")
return {"frequencies": {}, "triplets": [], "error": f"Lỗi: {str(e)}"}
async def get_schedule(self, dai: str = None) -> str:
if dai:
dai = dai.lower()
if dai in Settings.DAI_CONFIG:
days = ', '.join([self._get_day_name(d) for d in Settings.DAI_CONFIG[dai]["days"]])
return f"Xổ số {Settings.DAI_CONFIG[dai]['name']} ({dai.upper()}) quay vào {days} hàng tuần."
logger.debug(f"[get_schedule] Unknown dai: {dai}")
return f"Lỗi: Không tìm thấy lịch cho đài {dai}."
result = "Lịch xổ số các đài:\n"
for dai_code, config in Settings.DAI_CONFIG.items():
days = ', '.join([self._get_day_name(d) for d in config["days"]])
result += f"- {dai_code.upper()} ({config['name']}): {days}\n"
result += "Gửi tên đài để xem chi tiết, ví dụ: 'Lịch XSKH'."
return result
async def add_lottery_vectors(self, json_file_path: str) -> str:
logger.info("[add_lottery_vectors] Tính năng vector đã bị tắt")
return "Tính năng lưu vector xổ số đã bị tắt." |