FixServiceDevies
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
from config.database import get_connection, release_connection
|
||||
from modules.monitor_config.repository import insert_default_monitor_config_db
|
||||
|
||||
def _row_to_dict(row):
|
||||
"""
|
||||
@@ -135,9 +134,7 @@ def find_device_by_ip(ip_address):
|
||||
|
||||
def insert_device(data):
|
||||
"""
|
||||
Tạo mới một thiết bị và cấu hình mặc định (MonitorConfig & AlertConfig)
|
||||
trong cùng một database transaction.
|
||||
Trả về: tuple (device_dict, monitor_config_dict)
|
||||
Tạo mới một thiết bị mạng (chỉ ghi nhận vào bảng device).
|
||||
"""
|
||||
conn = get_connection()
|
||||
cur = None
|
||||
@@ -165,23 +162,7 @@ def insert_device(data):
|
||||
device_row = cur.fetchone()
|
||||
device_id = device_row[0]
|
||||
|
||||
# 2. Thêm cấu hình giám sát mặc định (MonitorConfig) bằng repository chuyên biệt
|
||||
monitor_config = insert_default_monitor_config_db(device_id, cursor=cur)
|
||||
|
||||
# 3. Thêm cấu hình cảnh báo mặc định (AlertConfig) cho thiết bị vừa tạo
|
||||
cur.execute("""
|
||||
INSERT INTO alert_config (device_id, is_enabled, fail_threshold, cooldown_minutes, notify_web, notify_email)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
""", (
|
||||
device_id,
|
||||
True, # is_enabled
|
||||
3, # fail_threshold (số lần fail liên tiếp trước khi alert)
|
||||
30, # cooldown_minutes (thời gian tránh spam alert)
|
||||
True, # notify_web (hiển thị thông báo trên web)
|
||||
False # notify_email (mặc định tắt gửi email)
|
||||
))
|
||||
|
||||
# 4. Lấy lại thiết bị kèm thông tin DeviceType đã JOIN để trả về đầy đủ dữ liệu
|
||||
# 2. Lấy lại thiết bị kèm thông tin DeviceType đã JOIN để trả về đầy đủ dữ liệu
|
||||
cur.execute("""
|
||||
SELECT
|
||||
d.id, d.device_type_id, d.name, d.description, d.ip_address, d.port,
|
||||
@@ -193,10 +174,10 @@ def insert_device(data):
|
||||
""", (device_id,))
|
||||
full_row = cur.fetchone()
|
||||
|
||||
# Commit toàn bộ transaction
|
||||
# Commit transaction
|
||||
conn.commit()
|
||||
|
||||
return _row_to_dict(full_row), monitor_config
|
||||
return _row_to_dict(full_row)
|
||||
|
||||
except Exception:
|
||||
conn.rollback()
|
||||
|
||||
Reference in New Issue
Block a user