From ad88fb8d6c8556b25c8aae77339e91ed95ee2e1a Mon Sep 17 00:00:00 2001
From: makotocc0107 <1424018999@qq.com>
Date: Tue, 27 Aug 2024 13:35:26 +0800
Subject: [PATCH] =?UTF-8?q?[=E5=A2=9E=E6=B7=BB]=E6=B7=BB=E5=8A=A0=E4=BA=86?=
=?UTF-8?q?etcd=E6=9C=8D=E5=8A=A1=E4=B8=8E=E5=AF=B9=E5=BA=94=E9=A1=B5?=
=?UTF-8?q?=E9=9D=A2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.idea/workspace.xml | 22 ++++--
.../app/Filament/Pages/UpdateToEtcd.php | 78 +++++++++++++++++++
management-panel/app/Services/EtcdService.php | 54 +++++++++++++
.../filament/pages/update-to-etcd.blade.php | 10 +++
4 files changed, 159 insertions(+), 5 deletions(-)
create mode 100644 management-panel/app/Filament/Pages/UpdateToEtcd.php
create mode 100644 management-panel/app/Services/EtcdService.php
create mode 100644 management-panel/resources/views/filament/pages/update-to-etcd.blade.php
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 77bbbe2..f38434d 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,7 +4,8 @@
-
+
+
@@ -301,7 +302,7 @@
"keyToString": {
"RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "master",
- "last_opened_file_path": "E:/data-collection-terminal/management-panel",
+ "last_opened_file_path": "E:/data-collection-terminal/management-panel/app",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
@@ -312,6 +313,7 @@
}]]>
+
@@ -354,7 +356,7 @@
-
+
@@ -404,7 +406,15 @@
1724726680511
-
+
+
+ 1724727682739
+
+
+
+ 1724727682739
+
+
@@ -426,6 +436,8 @@
-
+
+
+
\ No newline at end of file
diff --git a/management-panel/app/Filament/Pages/UpdateToEtcd.php b/management-panel/app/Filament/Pages/UpdateToEtcd.php
new file mode 100644
index 0000000..84e5e51
--- /dev/null
+++ b/management-panel/app/Filament/Pages/UpdateToEtcd.php
@@ -0,0 +1,78 @@
+deleteByPrefix("datasource");
+
+ $settings = DB::table('settings')
+ ->where('group', 'datasource')
+ ->get(); // 获取 group 为 datasource 的所有记录
+
+ foreach ($settings as $setting) {
+ $key = '/' . $setting->group . '/' . $setting->name;
+ $etcd->put($key, $setting->payload);
+ }
+
+ $metrics = DB::table('metrics')->get(); // 获取所有 metrics 数据
+
+ foreach ($metrics as $index => $metric) {
+ $baseKey = '/metrics/' . $index;
+
+ // 存储 name 到 etcd
+ $etcd->put($baseKey . '/name', $metric->name);
+
+ // 存储 identifier_type 到 etcd
+ $etcd->put($baseKey . '/identifier_type', $metric->identifier_type);
+
+ // 根据 identifier_type 生成 nodeid 并存储到 etcd
+ switch ($metric->identifier_type) {
+ case 1: // Numeric
+ $nodeId = 'ns=' . $metric->namespace_index . ';i=' . $metric->numeric_id;
+ break;
+ case 2: // String
+ $nodeId = 'ns=' . $metric->namespace_index . ';s=' . $metric->string_id;
+ break;
+ case 3: // GUID
+ $nodeId = 'ns=' . $metric->namespace_index . ';g=' . $metric->guid_id;
+ break;
+ default:
+ $nodeId = ''; // 若 identifier_type 不符合预期,则 nodeId 为空
+ break;
+ }
+ $etcd->put($baseKey . '/nodeid', $nodeId);
+
+ // 存储 help 到 etcd
+ $etcd->put($baseKey . '/help', $metric->help ?? '');
+ }
+
+ Notification::make()
+ ->title('配置更新成功!')
+ ->success() // 设置为成功类型
+ ->send(); // 发送通知
+
+ } catch (\Exception $e) {
+ // 失败通知
+ Notification::make()
+ ->title('配置更新失败!')
+ ->danger() // 设置为错误类型
+ ->body($e->getMessage()) // 显示异常信息
+ ->send(); // 发送通知
+ }
+ }
+}
diff --git a/management-panel/app/Services/EtcdService.php b/management-panel/app/Services/EtcdService.php
new file mode 100644
index 0000000..9578693
--- /dev/null
+++ b/management-panel/app/Services/EtcdService.php
@@ -0,0 +1,54 @@
+client = new Client(['base_uri' => $this->baseUri]);
+ }
+
+ public function put($key, $value)
+ {
+ $this->client->post('kv/put', [
+ 'json' => [
+ 'key' => base64_encode($key),
+ 'value' => base64_encode($value),
+ ],
+ ]);
+ }
+
+ public function get($key)
+ {
+ $response = $this->client->post('kv/range', [
+ 'json' => [
+ 'key' => base64_encode($key),
+ ],
+ ]);
+ return json_decode($response->getBody()->getContents(), true);
+ }
+
+ public function deleteByPrefix($prefix)
+ {
+ // Encode the prefix
+ $encodedPrefix = base64_encode($prefix);
+
+ // Calculate the range end by incrementing the last character
+ $lastChar = $prefix[strlen($prefix) - 1];
+ $rangeEnd = $prefix . chr(ord($lastChar) + 1);
+ $encodedRangeEnd = base64_encode($rangeEnd);
+
+ // Send the delete request
+ $this->client->post('kv/deletion_range', [
+ 'json' => [
+ 'key' => $encodedPrefix,
+ 'range_end' => $encodedRangeEnd,
+ ],
+ ]);
+ }
+}
diff --git a/management-panel/resources/views/filament/pages/update-to-etcd.blade.php b/management-panel/resources/views/filament/pages/update-to-etcd.blade.php
new file mode 100644
index 0000000..b8b2dc9
--- /dev/null
+++ b/management-panel/resources/views/filament/pages/update-to-etcd.blade.php
@@ -0,0 +1,10 @@
+
+
+