[增添]添加了etcd服务与对应页面

This commit is contained in:
makotocc0107
2024-08-27 13:35:26 +08:00
parent 84e0393ebc
commit ad88fb8d6c
4 changed files with 159 additions and 5 deletions

22
.idea/workspace.xml generated
View File

@@ -4,7 +4,8 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="596fb1a0-d6fb-4db8-a922-13b01593ce79" name="更改" comment="[增添]添加了Manage Exposer的默认数据库以及Page页面">
<list default="true" id="596fb1a0-d6fb-4db8-a922-13b01593ce79" name="更改" comment="[增添]添加了MetricResource的数据库以及页面元素">
<change afterPath="$PROJECT_DIR$/management-panel/app/Services/EtcdService.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@@ -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 @@
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="E:\data-collection-terminal\management-panel\app" />
<recent name="E:\data-collection-terminal\management-panel" />
</key>
</component>
@@ -354,7 +356,7 @@
<workItem from="1724721535300" duration="1534000" />
<workItem from="1724723119931" duration="37000" />
<workItem from="1724723165774" duration="1511000" />
<workItem from="1724724784154" duration="2759000" />
<workItem from="1724724784154" duration="5935000" />
</task>
<task id="LOCAL-00001" summary="[增添]添加注册">
<option name="closed" value="true" />
@@ -404,7 +406,15 @@
<option name="project" value="LOCAL" />
<updated>1724726680511</updated>
</task>
<option name="localTasksCounter" value="7" />
<task id="LOCAL-00007" summary="[增添]添加了MetricResource的基础设置以及数据库">
<option name="closed" value="true" />
<created>1724727682739</created>
<option name="number" value="00007" />
<option name="presentableId" value="LOCAL-00007" />
<option name="project" value="LOCAL" />
<updated>1724727682739</updated>
</task>
<option name="localTasksCounter" value="8" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@@ -426,6 +436,8 @@
<MESSAGE value="[增添]添加了datasource的setting数据库以及默认值" />
<MESSAGE value="[增添]添加了ManageDataSource管理页面" />
<MESSAGE value="[增添]添加了Manage Exposer的默认数据库值以及Page页面" />
<option name="LAST_COMMIT_MESSAGE" value="[增添]添加了Manage Exposer的默认数据库以及Page页面" />
<MESSAGE value="[增添]添加了MetricResource的数据库以及页面元素" />
<MESSAGE value="[增添]添加了MetricResource的数据库以及页面元素" />
<option name="LAST_COMMIT_MESSAGE" value="[增添]添加了MetricResource的数据库以及页面元素" />
</component>
</project>

View File

@@ -0,0 +1,78 @@
<?php
namespace App\Filament\Pages;
use App\Services\EtcdService;
use Filament\Pages\Page;
use Illuminate\Support\Facades\DB;
use Filament\Notifications\Notification;
class UpdateToEtcd extends Page
{
protected static ?string $navigationIcon = 'heroicon-o-document-text';
protected static string $view = 'filament.pages.update-to-etcd';
public function updateSettingsToEtcd()
{
try {
$etcd = new EtcdService();
// $etcd->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(); // 发送通知
}
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Services;
use GuzzleHttp\Client;
class EtcdService
{
protected $client;
protected $baseUri = 'http://127.0.0.1:2379/v3/'; // etcd 默认地址
public function __construct()
{
$this->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,
],
]);
}
}

View File

@@ -0,0 +1,10 @@
<x-filament::page>
<form wire:submit.prevent="updateSettingsToEtcd" class="space-y-6">
{{$this->form }}
<div class="flex flex-wrap items-center gap-4 justify-start">
<x-filament::button type="submit">
导出配置到ETCD
</x-filament::button>
</div>
</form>
</x-filament::page>