优化结构

This commit is contained in:
Yvan
2026-08-22 15:53:14 +08:00
parent ca52de5db0
commit 491ed21d48
72 changed files with 746 additions and 695 deletions
+11 -11
View File
@@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"kra/internal/biz/system"
integrationbiz "kra/internal/biz/integration"
"strings"
"time"
@@ -31,23 +31,23 @@ type integrationRuntimeProvider interface {
IntegrationRuntime() *runtimeconfig.Store
}
func NewIntegrationConfigRepo(data Provider) system.IntegrationConfigRepo {
func NewIntegrationConfigRepo(data Provider) integrationbiz.IntegrationConfigRepo {
return &integrationConfigRepo{data: data}
}
func (r *integrationConfigRepo) ListIntegrationConfigs(ctx context.Context, kind string) ([]*system.IntegrationConfig, error) {
func (r *integrationConfigRepo) ListIntegrationConfigs(ctx context.Context, kind string) ([]*integrationbiz.IntegrationConfig, error) {
var rows []integrationConfigPO
if err := r.data.DB().WithContext(ctx).Where("kind = ?", kind).Order("provider ASC").Find(&rows).Error; err != nil {
return nil, err
}
result := make([]*system.IntegrationConfig, 0, len(rows))
result := make([]*integrationbiz.IntegrationConfig, 0, len(rows))
for _, row := range rows {
result = append(result, integrationConfigFromPO(row))
}
return result, nil
}
func (r *integrationConfigRepo) FindIntegrationConfig(ctx context.Context, kind, provider string) (*system.IntegrationConfig, error) {
func (r *integrationConfigRepo) FindIntegrationConfig(ctx context.Context, kind, provider string) (*integrationbiz.IntegrationConfig, error) {
var row integrationConfigPO
if err := r.data.DB().WithContext(ctx).Where("kind = ? AND provider = ?", kind, provider).First(&row).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -58,14 +58,14 @@ func (r *integrationConfigRepo) FindIntegrationConfig(ctx context.Context, kind,
return integrationConfigFromPO(row), nil
}
func (r *integrationConfigRepo) SaveIntegrationConfig(ctx context.Context, config *system.IntegrationConfig) error {
func (r *integrationConfigRepo) SaveIntegrationConfig(ctx context.Context, config *integrationbiz.IntegrationConfig) error {
db := r.data.DB().WithContext(ctx)
var row integrationConfigPO
err := db.Where("kind = ? AND provider = ?", config.Kind, config.Provider).First(&row).Error
values := integrationObject(config.Values)
if errors.Is(err, gorm.ErrRecordNotFound) {
if config.Enabled {
if err = system.ValidateIntegrationConfig(config.Kind, config.Provider, values); err != nil {
if err = integrationbiz.ValidateIntegrationConfig(config.Kind, config.Provider, values); err != nil {
return err
}
}
@@ -81,7 +81,7 @@ func (r *integrationConfigRepo) SaveIntegrationConfig(ctx context.Context, confi
}
mergeIntegrationSecrets(config.Kind, config.Provider, values, integrationObject(json.RawMessage(row.Config)))
if config.Enabled {
if err = system.ValidateIntegrationConfig(config.Kind, config.Provider, values); err != nil {
if err = integrationbiz.ValidateIntegrationConfig(config.Kind, config.Provider, values); err != nil {
return err
}
}
@@ -116,11 +116,11 @@ func integrationRuntime(provider Provider) *runtimeconfig.Store {
return nil
}
func integrationConfigFromPO(row integrationConfigPO) *system.IntegrationConfig {
func integrationConfigFromPO(row integrationConfigPO) *integrationbiz.IntegrationConfig {
values := integrationObject(json.RawMessage(row.Config))
maskIntegrationSecrets(row.Kind, row.Provider, values)
encoded, _ := json.Marshal(values)
return &system.IntegrationConfig{Kind: row.Kind, Provider: row.Provider, Enabled: row.Enabled, Values: encoded}
return &integrationbiz.IntegrationConfig{Kind: row.Kind, Provider: row.Provider, Enabled: row.Enabled, Values: encoded}
}
func integrationObject(raw json.RawMessage) map[string]any {
@@ -165,7 +165,7 @@ func mergeIntegrationSecrets(kind, provider string, values, old map[string]any)
func integrationSecretFields(kind, provider string) map[string]bool {
result := map[string]bool{}
if definition, ok := system.IntegrationDefinition(kind, provider); ok {
if definition, ok := integrationbiz.IntegrationDefinition(kind, provider); ok {
for _, field := range definition.Fields {
if field.Secret {
result[field.Key] = true
@@ -3,7 +3,7 @@ package system
import (
"context"
"encoding/json"
"kra/internal/biz/system"
integrationbiz "kra/internal/biz/integration"
"testing"
"kra/internal/integration/runtimeconfig"
@@ -27,19 +27,19 @@ func TestIntegrationConfigSavePublishesUnmaskedRuntimeValues(t *testing.T) {
provider := &integrationRuntimeTestProvider{Data: &Data{gormDB: newReloadableDB(db, nil)}, store: runtimeconfig.NewStore()}
repo := &integrationConfigRepo{data: provider}
values := system.DefaultIntegrationConfig(system.IntegrationKindMQ, "rabbitmq")
values := integrationbiz.DefaultIntegrationConfig(integrationbiz.IntegrationKindMQ, "rabbitmq")
values["password"] = "runtime-secret"
raw, _ := json.Marshal(values)
if err = repo.SaveIntegrationConfig(context.Background(), &system.IntegrationConfig{Kind: system.IntegrationKindMQ, Provider: "rabbitmq", Enabled: true, Values: raw}); err != nil {
if err = repo.SaveIntegrationConfig(context.Background(), &integrationbiz.IntegrationConfig{Kind: integrationbiz.IntegrationKindMQ, Provider: "rabbitmq", Enabled: true, Values: raw}); err != nil {
t.Fatal(err)
}
values["password"] = "******"
raw, _ = json.Marshal(values)
if err = repo.SaveIntegrationConfig(context.Background(), &system.IntegrationConfig{Kind: system.IntegrationKindMQ, Provider: "rabbitmq", Enabled: true, Values: raw}); err != nil {
if err = repo.SaveIntegrationConfig(context.Background(), &integrationbiz.IntegrationConfig{Kind: integrationbiz.IntegrationKindMQ, Provider: "rabbitmq", Enabled: true, Values: raw}); err != nil {
t.Fatal(err)
}
active, ok := provider.store.Get(system.IntegrationKindMQ, "rabbitmq")
active, ok := provider.store.Get(integrationbiz.IntegrationKindMQ, "rabbitmq")
if !ok || !active.Enabled {
t.Fatalf("runtime config = %#v, ok=%v", active, ok)
}
+16 -16
View File
@@ -2,7 +2,7 @@ package system
import (
"context"
"kra/internal/biz/system"
taskbiz "kra/internal/biz/task"
"kra/pkg/database/gormkit"
"kra/pkg/database/pagination"
"time"
@@ -46,14 +46,14 @@ func (taskLogPO) TableName() string { return "sys_timed_task_logs" }
type taskRepo struct{ data Provider }
func NewTaskRepo(data Provider) system.TaskRepo { return &taskRepo{data: data} }
func taskToPO(v *system.TimedTask) taskPO {
func NewTaskRepo(data Provider) taskbiz.TaskRepo { return &taskRepo{data: data} }
func taskToPO(v *taskbiz.TimedTask) taskPO {
return taskPO{ID: v.ID, Name: v.Name, Description: v.Description, Spec: v.Spec, WithSeconds: v.WithSeconds, ExecutorType: v.ExecutorType, MethodName: v.MethodName, Params: gormkit.JSON(v.Params), HTTPURL: v.HTTPURL, HTTPMethod: v.HTTPMethod, HTTPHeader: gormkit.JSON(v.HTTPHeader), HTTPBody: v.HTTPBody, HTTPAllowPrivate: v.HTTPAllowPrivate, Enabled: v.Enabled}
}
func taskFromPO(v taskPO) *system.TimedTask {
return &system.TimedTask{ID: v.ID, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, Name: v.Name, Description: v.Description, Spec: v.Spec, WithSeconds: v.WithSeconds, ExecutorType: v.ExecutorType, MethodName: v.MethodName, Params: []byte(v.Params), HTTPURL: v.HTTPURL, HTTPMethod: v.HTTPMethod, HTTPHeader: []byte(v.HTTPHeader), HTTPBody: v.HTTPBody, HTTPAllowPrivate: v.HTTPAllowPrivate, Enabled: v.Enabled}
func taskFromPO(v taskPO) *taskbiz.TimedTask {
return &taskbiz.TimedTask{ID: v.ID, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, Name: v.Name, Description: v.Description, Spec: v.Spec, WithSeconds: v.WithSeconds, ExecutorType: v.ExecutorType, MethodName: v.MethodName, Params: []byte(v.Params), HTTPURL: v.HTTPURL, HTTPMethod: v.HTTPMethod, HTTPHeader: []byte(v.HTTPHeader), HTTPBody: v.HTTPBody, HTTPAllowPrivate: v.HTTPAllowPrivate, Enabled: v.Enabled}
}
func (r *taskRepo) CreateTask(ctx context.Context, v *system.TimedTask) error {
func (r *taskRepo) CreateTask(ctx context.Context, v *taskbiz.TimedTask) error {
po := taskToPO(v)
if err := r.data.DB().WithContext(ctx).Create(&po).Error; err != nil {
return err
@@ -70,26 +70,26 @@ func (r *taskRepo) TaskNameExists(ctx context.Context, name string, excludeID ui
err := db.Count(&count).Error
return count > 0, err
}
func (r *taskRepo) UpdateTask(ctx context.Context, v *system.TimedTask) error {
func (r *taskRepo) UpdateTask(ctx context.Context, v *taskbiz.TimedTask) error {
po := taskToPO(v)
return r.data.DB().WithContext(ctx).Model(&taskPO{}).Where("id = ?", v.ID).Select("name", "description", "spec", "with_seconds", "executor_type", "method_name", "params", "http_url", "http_method", "http_header", "http_body", "http_allow_private", "enabled").Updates(&po).Error
}
func (r *taskRepo) DeleteTask(ctx context.Context, id uint) error {
return r.data.DB().WithContext(ctx).Delete(&taskPO{}, id).Error
}
func (r *taskRepo) FindTask(ctx context.Context, id uint) (*system.TimedTask, error) {
func (r *taskRepo) FindTask(ctx context.Context, id uint) (*taskbiz.TimedTask, error) {
var po taskPO
if err := r.data.DB().WithContext(ctx).First(&po, id).Error; err != nil {
return nil, err
}
return taskFromPO(po), nil
}
func (r *taskRepo) ListTasks(ctx context.Context, page, size int, q *system.TimedTask) ([]*system.TimedTask, int64, error) {
func (r *taskRepo) ListTasks(ctx context.Context, page, size int, q *taskbiz.TimedTask) ([]*taskbiz.TimedTask, int64, error) {
// During first-install the data layer intentionally serves a bootstrap
// database without system tables. The scheduler starts before /init/initdb
// and should remain idle instead of logging a missing-table SQL error.
if !r.data.DatabaseReady() {
return []*system.TimedTask{}, 0, nil
return []*taskbiz.TimedTask{}, 0, nil
}
db := r.data.DB().WithContext(ctx).Model(&taskPO{})
if q != nil {
@@ -111,7 +111,7 @@ func (r *taskRepo) ListTasks(ctx context.Context, page, size int, q *system.Time
if err := pagination.Apply(db.Order("id desc"), page, size, 100).Find(&pos).Error; err != nil {
return nil, 0, err
}
out := make([]*system.TimedTask, 0, len(pos))
out := make([]*taskbiz.TimedTask, 0, len(pos))
for _, po := range pos {
out = append(out, taskFromPO(po))
}
@@ -120,13 +120,13 @@ func (r *taskRepo) ListTasks(ctx context.Context, page, size int, q *system.Time
func (r *taskRepo) ToggleTask(ctx context.Context, id uint, enabled bool) error {
return r.data.DB().WithContext(ctx).Model(&taskPO{}).Where("id = ?", id).Update("enabled", enabled).Error
}
func (r *taskRepo) RecordTaskLog(ctx context.Context, v *system.TimedTaskLog) error {
func (r *taskRepo) RecordTaskLog(ctx context.Context, v *taskbiz.TimedTaskLog) error {
return r.data.DB().WithContext(ctx).Create(&taskLogPO{TaskID: v.TaskID, TaskName: v.TaskName, TriggerType: v.TriggerType, StartedAt: v.StartedAt, FinishedAt: v.FinishedAt, DurationMS: v.DurationMS, Status: v.Status, ErrorMsg: v.ErrorMsg, Output: v.Output}).Error
}
func taskLogFromPO(v taskLogPO) *system.TimedTaskLog {
return &system.TimedTaskLog{ID: v.ID, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, TaskID: v.TaskID, TaskName: v.TaskName, TriggerType: v.TriggerType, StartedAt: v.StartedAt, FinishedAt: v.FinishedAt, DurationMS: v.DurationMS, Status: v.Status, ErrorMsg: v.ErrorMsg, Output: v.Output}
func taskLogFromPO(v taskLogPO) *taskbiz.TimedTaskLog {
return &taskbiz.TimedTaskLog{ID: v.ID, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, TaskID: v.TaskID, TaskName: v.TaskName, TriggerType: v.TriggerType, StartedAt: v.StartedAt, FinishedAt: v.FinishedAt, DurationMS: v.DurationMS, Status: v.Status, ErrorMsg: v.ErrorMsg, Output: v.Output}
}
func (r *taskRepo) ListTaskLogs(ctx context.Context, page, size int, taskID uint, status string) ([]*system.TimedTaskLog, int64, error) {
func (r *taskRepo) ListTaskLogs(ctx context.Context, page, size int, taskID uint, status string) ([]*taskbiz.TimedTaskLog, int64, error) {
db := r.data.DB().WithContext(ctx).Model(&taskLogPO{})
if taskID != 0 {
db = db.Where("task_id = ?", taskID)
@@ -142,7 +142,7 @@ func (r *taskRepo) ListTaskLogs(ctx context.Context, page, size int, taskID uint
if err := pagination.Apply(db.Order("id desc"), page, size, 100).Find(&pos).Error; err != nil {
return nil, 0, err
}
out := make([]*system.TimedTaskLog, 0, len(pos))
out := make([]*taskbiz.TimedTaskLog, 0, len(pos))
for _, po := range pos {
out = append(out, taskLogFromPO(po))
}