129 lines
4.3 KiB
PHP
129 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use App\Services\EtcdService;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Widgets\Widget;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Spatie\Activitylog\ActivityLogger;
|
|
use Spatie\Activitylog\ActivityLogStatus;
|
|
|
|
class UpdateConfigWidget extends Widget
|
|
{
|
|
protected static ?int $sort = 2;
|
|
|
|
protected static string $view = 'filament.widgets.update-config-widget';
|
|
protected $etcd;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->etcd = new EtcdService();
|
|
}
|
|
|
|
public function updateSettingsToEtcd()
|
|
{
|
|
try {
|
|
$settingsClasses = config('etcd-config.settings');
|
|
// dd(config('etcd-config.settings'));
|
|
|
|
foreach ($settingsClasses as $class) {
|
|
$settings = app($class);
|
|
|
|
// 调用静态方法获取组名
|
|
$group = $class::group();
|
|
|
|
// 使用反射获取所有公共属性
|
|
$properties = (new \ReflectionClass($settings))->getProperties(\ReflectionProperty::IS_PUBLIC);
|
|
$settingsArray = [];
|
|
|
|
foreach ($properties as $property) {
|
|
$name = $property->getName();
|
|
$val = $settings->$name;
|
|
$settingsArray[$name] = $val;
|
|
}
|
|
|
|
// 将所有设置存入 etcd
|
|
foreach ($settingsArray as $name => $val) {
|
|
$key = '/' . $group . '/' . $name; // 使用 group 动态生成 key
|
|
// dd($key, $val); // 调试输出
|
|
$this->etcd->put($key, $val); // 存入 etcd
|
|
}
|
|
}
|
|
// foreach ($settingsClasses as $class) {
|
|
// $settings = app($class);
|
|
//// dd($settings);
|
|
//
|
|
// foreach ($settings as $name => $val) {
|
|
// dd($name, $val);
|
|
// $key = '/' . $settings->group . '/' . $name;
|
|
// $this->etcd->put($key, $val);
|
|
// }
|
|
// }
|
|
|
|
$metrics = DB::table('metrics')->get(); // 获取所有 metrics 数据
|
|
|
|
foreach ($metrics as $index => $metric) {
|
|
$this->storeMetricToEtcd($metric, $index);
|
|
}
|
|
|
|
Notification::make()
|
|
->title('配置更新成功!')
|
|
->success() // 设置为成功类型
|
|
->send(); // 发送通知
|
|
|
|
app(ActivityLogger::class)
|
|
->useLog('导出')
|
|
->setLogStatus(app(ActivityLogStatus::class))
|
|
->withProperties($metrics)
|
|
->event('settings updated')
|
|
->log('settings updated');
|
|
|
|
} catch (\Exception $e) {
|
|
// 失败通知
|
|
Notification::make()
|
|
->title('配置更新失败!')
|
|
->danger() // 设置为错误类型
|
|
->body($e->getMessage()) // 显示异常信息
|
|
->send(); // 发送通知
|
|
|
|
app(ActivityLogger::class)
|
|
->useLog('导出失败')
|
|
->setLogStatus(app(ActivityLogStatus::class))
|
|
->event('Failed')
|
|
->log($e->getMessage());
|
|
}
|
|
}
|
|
|
|
protected function storeMetricToEtcd($metric, $index)
|
|
{
|
|
$baseKey = '/metrics/' . $index;
|
|
|
|
// 存储 name 到 etcd
|
|
$this->etcd->put($baseKey . '/name', $metric->name);
|
|
|
|
// 存储 identifier_type 到 etcd
|
|
$this->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;
|
|
}
|
|
$this->etcd->put($baseKey . '/nodeid', $nodeId);
|
|
|
|
// 存储 help 到 etcd
|
|
$this->etcd->put($baseKey . '/help', $metric->help ?? '');
|
|
}
|
|
}
|