mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-26 17:49:15 +01:00
refactor: enhance parameter handling and improve code clarity
- Updated GetListParams to set a default sort option for better query handling. - Enhanced PostSpiderRunParams to include a default mode and improved error handling for missing spider. - Added parameters field to ChatMessageContent for more flexible message content management. - Refactored getNodeIds method to simplify mode handling and removed unnecessary error checks. - Improved ChatMessageAction component to display parameters and response sections more effectively, enhancing user experience.
This commit is contained in:
@@ -63,7 +63,7 @@ type BaseController[T any] struct {
|
||||
|
||||
type GetListParams struct {
|
||||
Filter string `query:"filter" description:"Filter query"`
|
||||
Sort string `query:"sort" description:"Sort options"`
|
||||
Sort string `query:"sort" default:"-_id" description:"Sort options"`
|
||||
Page int `query:"page" default:"1" description:"Page number" minimum:"1"`
|
||||
Size int `query:"size" default:"10" description:"Page size" minimum:"1"`
|
||||
All bool `query:"all" default:"false" description:"Whether to get all items"`
|
||||
|
||||
@@ -674,7 +674,7 @@ func PostSpiderExport(c *gin.Context, _ *PostSpiderExportParams) (err error) {
|
||||
|
||||
type PostSpiderRunParams struct {
|
||||
Id string `path:"id" description:"Spider ID" format:"objectid" pattern:"^[0-9a-fA-F]{24}$"`
|
||||
Mode string `json:"mode" description:"Run mode" enum:"random,all,selected-nodes"`
|
||||
Mode string `json:"mode" description:"Run mode: random,all,selected-nodes" default:"random" enum:"random,all,selected-nodes"`
|
||||
NodeIds []string `json:"node_ids" description:"Node IDs, used in selected-nodes mode"`
|
||||
Cmd string `json:"cmd" description:"Command"`
|
||||
Param string `json:"param" description:"Parameters"`
|
||||
@@ -688,6 +688,12 @@ func PostSpiderRun(c *gin.Context, params *PostSpiderRunParams) (response *Respo
|
||||
return GetErrorResponse[[]primitive.ObjectID](errors.BadRequestf("invalid id format"))
|
||||
}
|
||||
|
||||
// get spider
|
||||
s, err := service.NewModelService[models.Spider]().GetById(id)
|
||||
if err != nil {
|
||||
return GetErrorResponse[[]primitive.ObjectID](errors.NotFoundf("spider not found"))
|
||||
}
|
||||
|
||||
// options
|
||||
var nodeIds []primitive.ObjectID
|
||||
if len(params.NodeIds) > 0 {
|
||||
@@ -714,6 +720,18 @@ func PostSpiderRun(c *gin.Context, params *PostSpiderRunParams) (response *Respo
|
||||
ScheduleId: scheduleId,
|
||||
Priority: params.Priority,
|
||||
}
|
||||
if opts.Mode == "" {
|
||||
opts.Mode = s.Mode
|
||||
}
|
||||
if opts.Cmd == "" {
|
||||
opts.Cmd = s.Cmd
|
||||
}
|
||||
if opts.Param == "" {
|
||||
opts.Param = s.Param
|
||||
}
|
||||
if opts.Priority == 0 {
|
||||
opts.Priority = s.Priority
|
||||
}
|
||||
|
||||
// user
|
||||
if u := GetUserFromContext(c); u != nil {
|
||||
|
||||
@@ -29,6 +29,7 @@ type ChatMessageContent struct {
|
||||
BaseModel `bson:",inline"`
|
||||
MessageId primitive.ObjectID `json:"message_id" bson:"message_id" description:"Message ID"`
|
||||
Key string `json:"key" bson:"key" description:"Message content key"`
|
||||
Parameters map[string]interface{} `json:"parameters" bson:"parameters" description:"Message content parameters"`
|
||||
Content string `json:"content" bson:"content" description:"Message content"`
|
||||
Type string `json:"type" bson:"type" description:"Message type (text/action)"`
|
||||
Action string `json:"action,omitempty" bson:"action,omitempty" description:"Action name"`
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/crawlab-team/crawlab/core/constants"
|
||||
"github.com/crawlab-team/crawlab/core/interfaces"
|
||||
"github.com/crawlab-team/crawlab/core/models/models"
|
||||
@@ -82,7 +81,8 @@ func (svc *Service) scheduleTasks(s *models.Spider, opts *interfaces.SpiderRunOp
|
||||
}
|
||||
|
||||
func (svc *Service) getNodeIds(opts *interfaces.SpiderRunOptions) (nodeIds []primitive.ObjectID, err error) {
|
||||
if opts.Mode == constants.RunTypeAllNodes {
|
||||
switch opts.Mode {
|
||||
case constants.RunTypeAllNodes:
|
||||
query := bson.M{
|
||||
"active": true,
|
||||
"enabled": true,
|
||||
@@ -95,12 +95,10 @@ func (svc *Service) getNodeIds(opts *interfaces.SpiderRunOptions) (nodeIds []pri
|
||||
for _, node := range nodes {
|
||||
nodeIds = append(nodeIds, node.Id)
|
||||
}
|
||||
} else if opts.Mode == constants.RunTypeSelectedNodes {
|
||||
case constants.RunTypeSelectedNodes:
|
||||
nodeIds = opts.NodeIds
|
||||
} else if opts.Mode == constants.RunTypeRandom {
|
||||
default:
|
||||
nodeIds = []primitive.ObjectID{primitive.NilObjectID}
|
||||
} else {
|
||||
return nil, errors.New("invalid run mode")
|
||||
}
|
||||
return nodeIds, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user