[优化]用实例化的方式把settings导入etcd

This commit is contained in:
2024-09-23 14:44:50 +08:00
committed by Coding
parent 7150515831
commit b7edc8f1a7
2 changed files with 81 additions and 35 deletions

View File

@@ -14,52 +14,57 @@ 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 {
$etcd = new EtcdService();
// $etcd->deleteByPrefix("datasource");
$settingsClasses = config('etcd-config.settings');
// dd(config('etcd-config.settings'));
$settings = DB::table('settings')
// ->where('group', 'datasource')
->get(); // 获取 group 为 datasource 的所有记录
foreach ($settingsClasses as $class) {
$settings = app($class);
foreach ($settings as $setting) {
$key = '/' . $setting->group . '/' . $setting->name;
$etcd->put($key, $setting->payload);
// 调用静态方法获取组名
$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) {
$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 ?? '');
$this->storeMetricToEtcd($metric, $index);
}
Notification::make()
@@ -89,4 +94,35 @@ class UpdateConfigWidget extends Widget
->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 ?? '');
}
}