forked from wang4386/CDT-Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigManager.php
More file actions
192 lines (161 loc) · 7.24 KB
/
ConfigManager.php
File metadata and controls
192 lines (161 loc) · 7.24 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
<?php
class ConfigManager
{
private $db;
private $configCache = [];
private $accountsCache = [];
public function __construct(Database $db)
{
$this->db = $db->getPdo();
$this->load();
}
public function load()
{
$stmt = $this->db->query("SELECT key, value FROM settings");
while ($row = $stmt->fetch()) {
$this->configCache[$row['key']] = $row['value'];
}
$stmt = $this->db->query("SELECT * FROM accounts ORDER BY id ASC");
$this->accountsCache = $stmt->fetchAll();
}
public function get($key, $default = null)
{
return $this->configCache[$key] ?? $default;
}
public function getAllSettings()
{
return $this->configCache;
}
public function getAccounts()
{
return $this->accountsCache;
}
public function getAccountById($id)
{
foreach ($this->accountsCache as $acc) {
if ($acc['id'] == $id) return $acc;
}
return null;
}
public function isInitialized()
{
return !empty($this->configCache['admin_password']);
}
private function saveSetting($key, $value)
{
$stmt = $this->db->prepare("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)");
$stmt->execute([$key, $value]);
$this->configCache[$key] = $value;
}
// --- 新增:心跳时间管理 ---
public function updateLastRunTime($time)
{
$this->saveSetting('last_monitor_run', $time);
}
public function getLastRunTime()
{
return (int)($this->configCache['last_monitor_run'] ?? 0);
}
// ------------------------
public function updateConfig($data)
{
try {
$this->db->beginTransaction();
// 1. 保存全局设置
$this->saveSetting('admin_password', $data['admin_password']);
$this->saveSetting('traffic_threshold', $data['traffic_threshold']);
$this->saveSetting('enable_schedule_email', $data['enable_schedule_email'] ? '1' : '0');
$this->saveSetting('shutdown_mode', $data['shutdown_mode']);
$this->saveSetting('threshold_action', $data['threshold_action']);
$this->saveSetting('keep_alive', isset($data['keep_alive']) && $data['keep_alive'] ? '1' : '0');
$this->saveSetting('api_interval', $data['api_interval'] ?? 600);
if (isset($data['Notification'])) {
$this->saveSetting('notify_email', $data['Notification']['email']);
$this->saveSetting('notify_host', $data['Notification']['host']);
$this->saveSetting('notify_port', $data['Notification']['port']);
$this->saveSetting('notify_username', $data['Notification']['username']);
$this->saveSetting('notify_password', $data['Notification']['password']);
$this->saveSetting('notify_secure', $data['Notification']['secure']);
}
// 2. 账号增量同步
$newAccounts = $data['Accounts'] ?? [];
$stmt = $this->db->query("SELECT id, access_key_id FROM accounts");
$existingMap = [];
while ($row = $stmt->fetch()) {
$existingMap[$row['access_key_id']] = $row['id'];
}
$keptIds = [];
$insertStmt = $this->db->prepare("INSERT INTO accounts (access_key_id, access_key_secret, region_id, instance_id, max_traffic, schedule_enabled, start_time, stop_time, traffic_used, instance_status, updated_at, last_keep_alive_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 'Unknown', 0, 0)");
$updateStmt = $this->db->prepare("UPDATE accounts SET access_key_secret = ?, region_id = ?, instance_id = ?, max_traffic = ?, schedule_enabled = ?, start_time = ?, stop_time = ? WHERE id = ?");
foreach ($newAccounts as $acc) {
$key = $acc['AccessKeyId'];
$params = [
$acc['AccessKeySecret'], $acc['regionId'], $acc['instanceId'] ?? '', $acc['maxTraffic'],
($acc['schedule']['enabled'] ?? false) ? 1 : 0,
$acc['schedule']['startTime'] ?? '', $acc['schedule']['stopTime'] ?? ''
];
if (isset($existingMap[$key])) {
$id = $existingMap[$key];
$params[] = $id;
$updateStmt->execute($params);
$keptIds[] = $id;
} else {
$insertParams = [$key];
array_push($insertParams, ...$params);
$insertStmt->execute($insertParams);
}
}
// 3. 删除移除的账号
$idsToDelete = array_diff(array_values($existingMap), $keptIds);
if (!empty($idsToDelete)) {
$placeholders = implode(',', array_fill(0, count($idsToDelete), '?'));
$deleteStmt = $this->db->prepare("DELETE FROM accounts WHERE id IN ($placeholders)");
$deleteStmt->execute($idsToDelete);
}
$this->db->commit();
// 4. 重排 ID
$this->reorderIds();
// 5. 刷新缓存
$this->load();
return true;
} catch (Exception $e) {
if ($this->db->inTransaction()) $this->db->rollBack();
return false;
}
}
private function reorderIds()
{
try {
$this->db->beginTransaction();
$stmt = $this->db->query("SELECT * FROM accounts ORDER BY id ASC");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!empty($rows)) {
$this->db->exec("DELETE FROM accounts");
$this->db->exec("DELETE FROM sqlite_sequence WHERE name='accounts'");
$insertStmt = $this->db->prepare("INSERT INTO accounts (id, access_key_id, access_key_secret, region_id, instance_id, max_traffic, schedule_enabled, start_time, stop_time, traffic_used, instance_status, updated_at, last_keep_alive_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$newId = 1;
foreach ($rows as $row) {
$insertStmt->execute([
$newId++, $row['access_key_id'], $row['access_key_secret'], $row['region_id'],
$row['instance_id'], $row['max_traffic'], $row['schedule_enabled'],
$row['start_time'], $row['stop_time'], $row['traffic_used'],
$row['instance_status'], $row['updated_at'], $row['last_keep_alive_at']
]);
}
}
$this->db->commit();
} catch (Exception $e) {
if ($this->db->inTransaction()) $this->db->rollBack();
}
}
public function updateAccountStatus($id, $traffic, $status, $updatedAt)
{
$stmt = $this->db->prepare("UPDATE accounts SET traffic_used = ?, instance_status = ?, updated_at = ? WHERE id = ?");
return $stmt->execute([$traffic, $status, $updatedAt, $id]);
}
public function updateLastKeepAlive($id, $time)
{
$stmt = $this->db->prepare("UPDATE accounts SET last_keep_alive_at = ? WHERE id = ?");
return $stmt->execute([$time, $id]);
}
}