diff --git a/models/cloudbrain.go b/models/cloudbrain.go index 0f5707e3c..917de1ba0 100755 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -181,6 +181,7 @@ type Cloudbrain struct { type CloudbrainShow struct { ID int64 + JobID string RepoFullName string Type int JobType string @@ -201,8 +202,8 @@ type CloudbrainShow4Action struct { func (task *Cloudbrain) ToShow() *CloudbrainShow { c := &CloudbrainShow{ - ID: task.ID, - + ID: task.ID, + JobID: task.JobID, JobType: task.JobType, Type: task.Type, DisplayJobName: task.DisplayJobName, diff --git a/models/limit_config.go b/models/limit_config.go index 17f0c23a2..5b0adc5a2 100644 --- a/models/limit_config.go +++ b/models/limit_config.go @@ -102,6 +102,17 @@ func GetLimitConfigByLimitType(limitType LimitType) ([]LimitConfig, error) { return r, nil } +func GetLimitersByLimitTypeWithDeleted(limitType LimitType) ([]LimitConfig, error) { + r := make([]LimitConfig, 0) + err := x.Unscoped().Where(" limit_type = ?", limitType.Name()).Find(&r) + if err != nil { + return nil, err + } else if len(r) == 0 { + return nil, ErrRecordNotExist{} + } + return r, nil +} + func AddLimitConfig(l *LimitConfig) error { sess := x.NewSession() defer sess.Close() diff --git a/models/task_config.go b/models/task_config.go index eef3a6c33..221ef9c58 100644 --- a/models/task_config.go +++ b/models/task_config.go @@ -27,7 +27,7 @@ type TaskConfigWithLimit struct { TaskCode string `binding:"Required;MaxSize(256)"` Tittle string AwardType string - AwardAmount int64 `binding:"Required;MaxSize(256)"` + AwardAmount int64 Creator string CreatedUnix timeutil.TimeStamp Limiters []*LimitConfigVO @@ -63,6 +63,17 @@ func GetTaskConfigList() ([]*TaskConfig, error) { } return r, nil } +func GetTaskConfigPageWithDeleted(opt ListOptions) ([]*TaskConfig, error) { + r := make([]*TaskConfig, 0) + err := x.Unscoped().Limit(opt.PageSize, (opt.Page-1)*opt.PageSize).OrderBy("deleted_at asc,id desc").Find(&r) + if err != nil { + return nil, err + } + if len(r) == 0 { + return nil, ErrRecordNotExist{} + } + return r, nil +} func AddTaskConfig(config TaskConfigWithLimit, doer *User) error { sess := x.NewSession() @@ -128,3 +139,31 @@ func AddTaskConfig(config TaskConfigWithLimit, doer *User) error { sess.Commit() return nil } + +func DelTaskConfig(taskCode string) error { + sess := x.NewSession() + defer sess.Close() + + //delete old task config + p := &TaskConfig{ + TaskCode: taskCode, + } + _, err := sess.Delete(p) + if err != nil { + sess.Rollback() + return err + } + + //delete old limiter config + lp := &LimitConfig{ + LimitType: LimitTypeTask.Name(), + LimitCode: taskCode, + } + _, err = sess.Delete(lp) + if err != nil { + sess.Rollback() + return err + } + sess.Commit() + return nil +} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 52504c388..85cd99c36 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -613,8 +613,8 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/task/config", func() { m.Get("/list", task.GetTaskConfigList) - m.Post("/add", bindIgnErr(models.TaskConfigWithLimit{}), task.AddTaskConfig) m.Post("/add/batch", bindIgnErr(models.BatchLimitConfigVO{}), task.BatchAddTaskConfig) + m.Post("/^:action(new|edit|del)$", bindIgnErr(models.TaskConfigWithLimit{}), task.OperateTaskConfig) }) }, operationReq) // ***** END: Operation ***** diff --git a/routers/task/config.go b/routers/task/config.go index 0216ffea0..71f3c2a2b 100644 --- a/routers/task/config.go +++ b/routers/task/config.go @@ -3,13 +3,16 @@ package task import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/routers/response" "code.gitea.io/gitea/services/task" + "errors" "net/http" ) func GetTaskConfigList(ctx *context.Context) { - r, err := task.GetTaskConfigWithLimitList() + page := ctx.QueryInt("page") + r, err := task.GetTaskConfigWithLimitList(models.ListOptions{PageSize: 20, Page: page}) if err != nil { ctx.JSON(http.StatusOK, response.ServerError(err.Error())) return @@ -17,9 +20,23 @@ func GetTaskConfigList(ctx *context.Context) { ctx.JSON(http.StatusOK, response.SuccessWithData(r)) } -func AddTaskConfig(ctx *context.Context, config models.TaskConfigWithLimit) { - err := task.AddTaskConfig(config, ctx.User) +func OperateTaskConfig(ctx *context.Context, config models.TaskConfigWithLimit) { + action := ctx.Params(":action") + + var err error + switch action { + case "edit": + err = task.EditTaskConfig(config, ctx.User) + case "new": + err = task.AddTaskConfig(config, ctx.User) + case "del": + err = task.DelTaskConfig(config.TaskCode) + default: + err = errors.New("action type error") + } + if err != nil { + log.Error("OperateTaskConfig error ,%v", err) ctx.JSON(http.StatusOK, response.ServerError(err.Error())) return } diff --git a/services/reward/limiter/limiter.go b/services/reward/limiter/limiter.go index d357ceabd..88ce80c67 100644 --- a/services/reward/limiter/limiter.go +++ b/services/reward/limiter/limiter.go @@ -245,3 +245,14 @@ func GetLimitersByLimitType(limitType models.LimitType) ([]models.LimitConfig, e return limiters, nil } + +func GetLimitersByLimitTypeWithDeleted(limitType models.LimitType) ([]models.LimitConfig, error) { + limiters, err := models.GetLimitersByLimitTypeWithDeleted(limitType) + if err != nil { + if models.IsErrRecordNotExist(err) { + return nil, nil + } + return nil, err + } + return limiters, nil +} diff --git a/services/task/task_config.go b/services/task/task_config.go index 0404a6f06..cc4f3c934 100644 --- a/services/task/task_config.go +++ b/services/task/task_config.go @@ -52,8 +52,20 @@ func GetTaskConfigList() ([]*models.TaskConfig, error) { return config, nil } -func GetTaskConfigWithLimitList() ([]*models.TaskConfigWithLimit, error) { - list, err := GetTaskConfigList() +func GetTaskConfigPageWithDeleted(opt models.ListOptions) ([]*models.TaskConfig, error) { + config, err := models.GetTaskConfigPageWithDeleted(opt) + if err != nil { + log.Error(" GetTaskConfigPageWithDeleted from model error. %v", err) + if models.IsErrRecordNotExist(err) { + return nil, nil + } + return nil, err + } + return config, nil +} + +func GetTaskConfigWithLimitList(opt models.ListOptions) ([]*models.TaskConfigWithLimit, error) { + list, err := GetTaskConfigPageWithDeleted(opt) if err != nil { return nil, err } @@ -61,7 +73,7 @@ func GetTaskConfigWithLimitList() ([]*models.TaskConfigWithLimit, error) { return nil, nil } r := make([]*models.TaskConfigWithLimit, 0) - l, err := limiter.GetLimitersByLimitType(models.LimitTypeTask) + l, err := limiter.GetLimitersByLimitTypeWithDeleted(models.LimitTypeTask) if err != nil { log.Error(" GetLimitersByLimitType from redis error. %v", err) return nil, err @@ -90,10 +102,27 @@ func GetTaskConfigWithLimitList() ([]*models.TaskConfigWithLimit, error) { } func AddTaskConfig(config models.TaskConfigWithLimit, doer *models.User) error { + t, err := models.GetTaskConfigByTaskCode(config.TaskCode) + if err != nil && !models.IsErrRecordNotExist(err) { + return err + } + if t != nil { + return errors.New("task config is exist") + } + + return EditTaskConfig(config, doer) +} + +func EditTaskConfig(config models.TaskConfigWithLimit, doer *models.User) error { if config.TaskCode == "" || config.AwardType == "" { log.Error(" AddTaskConfig param error") return errors.New("param error") } + for i, l := range config.Limiters { + if l.Scope == "" { + config.Limiters[i].Scope = models.LimitScopeSingleUser.Name() + } + } err := models.AddTaskConfig(config, doer) if err != nil { log.Error("add task config error,config:%v err:%v", config, err) @@ -103,3 +132,18 @@ func AddTaskConfig(config models.TaskConfigWithLimit, doer *models.User) error { redis_client.Del(redis_key.TaskConfigList()) return nil } + +func DelTaskConfig(taskCode string) error { + if taskCode == "" { + log.Error(" AddTaskConfig param error") + return errors.New("param error") + } + err := models.DelTaskConfig(taskCode) + if err != nil { + log.Error("del task config error,err:%v", err) + return err + } + redis_client.Del(redis_key.LimitConfig(models.LimitTypeTask.Name())) + redis_client.Del(redis_key.TaskConfigList()) + return nil +}