feat: complete Kra admin backend migration

This commit is contained in:
2026-08-14 00:28:37 +08:00
parent c4bf7ee03b
commit acba9624d7
88 changed files with 1765 additions and 8623 deletions
+20 -9
View File
@@ -2,9 +2,10 @@ package data
import (
"context"
"gorm.io/gorm"
"kra/internal/biz"
"time"
"gorm.io/gorm"
)
type taskPO struct {
@@ -16,10 +17,10 @@ type taskPO struct {
Description, Spec string
WithSeconds bool
ExecutorType, MethodName string
Params []byte `gorm:"type:json"`
Params jsonPO
HTTPURL string
HTTPMethod string
HTTPHeader []byte `gorm:"type:json"`
HTTPHeader jsonPO
HTTPBody string `gorm:"type:text"`
HTTPAllowPrivate, Enabled bool
}
@@ -45,10 +46,10 @@ type taskRepo struct{ data *Data }
func NewTaskRepo(data *Data) biz.TaskRepo { return &taskRepo{data: data} }
func taskToPO(v *biz.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: v.Params, HTTPURL: v.HTTPURL, HTTPMethod: v.HTTPMethod, HTTPHeader: v.HTTPHeader, HTTPBody: v.HTTPBody, HTTPAllowPrivate: v.HTTPAllowPrivate, Enabled: v.Enabled}
return taskPO{ID: v.ID, Name: v.Name, Description: v.Description, Spec: v.Spec, WithSeconds: v.WithSeconds, ExecutorType: v.ExecutorType, MethodName: v.MethodName, Params: jsonPO(v.Params), HTTPURL: v.HTTPURL, HTTPMethod: v.HTTPMethod, HTTPHeader: jsonPO(v.HTTPHeader), HTTPBody: v.HTTPBody, HTTPAllowPrivate: v.HTTPAllowPrivate, Enabled: v.Enabled}
}
func taskFromPO(v taskPO) *biz.TimedTask {
return &biz.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: v.Params, HTTPURL: v.HTTPURL, HTTPMethod: v.HTTPMethod, HTTPHeader: v.HTTPHeader, HTTPBody: v.HTTPBody, HTTPAllowPrivate: v.HTTPAllowPrivate, Enabled: v.Enabled}
return &biz.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 *biz.TimedTask) error {
po := taskToPO(v)
@@ -58,6 +59,15 @@ func (r *taskRepo) CreateTask(ctx context.Context, v *biz.TimedTask) error {
v.ID = po.ID
return nil
}
func (r *taskRepo) TaskNameExists(ctx context.Context, name string, excludeID uint) (bool, error) {
var count int64
db := r.data.gormDB.WithContext(ctx).Model(&taskPO{}).Where("name = ?", name)
if excludeID > 0 {
db = db.Where("id <> ?", excludeID)
}
err := db.Count(&count).Error
return count > 0, err
}
func (r *taskRepo) UpdateTask(ctx context.Context, v *biz.TimedTask) error {
po := taskToPO(v)
return r.data.gormDB.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
@@ -142,14 +152,15 @@ func (r *taskRepo) ListTaskLogs(ctx context.Context, page, size int, taskID uint
}
return out, total, nil
}
func (r *taskRepo) CleanupLogs(ctx context.Context, before time.Time) error {
func (r *taskRepo) CleanupLogs(ctx context.Context) error {
now := time.Now()
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Unscoped().Where("created_at < ?", before).Delete(&operationPO{}).Error; err != nil {
if err := tx.Unscoped().Where("created_at < ?", now.Add(-2160*time.Hour)).Delete(&operationPO{}).Error; err != nil {
return err
}
if err := tx.Unscoped().Where("created_at < ?", before).Delete(&loginLogPO{}).Error; err != nil {
if err := tx.Unscoped().Where("created_at < ?", now.Add(-168*time.Hour)).Delete(&jwtBlacklistPO{}).Error; err != nil {
return err
}
return tx.Unscoped().Where("created_at < ?", before).Delete(&taskLogPO{}).Error
return tx.Unscoped().Where("created_at < ?", now.Add(-720*time.Hour)).Delete(&taskLogPO{}).Error
})
}