github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/applet/applet_models.go (about)

     1  package applet
     2  
     3  import (
     4  	"context"
     5  	"mime/multipart"
     6  
     7  	confid "github.com/machinefi/w3bstream/pkg/depends/conf/id"
     8  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder"
     9  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/datatypes"
    10  	"github.com/machinefi/w3bstream/pkg/models"
    11  	"github.com/machinefi/w3bstream/pkg/modules/vm"
    12  	"github.com/machinefi/w3bstream/pkg/types"
    13  	"github.com/machinefi/w3bstream/pkg/types/wasm"
    14  )
    15  
    16  type CondArgs struct {
    17  	ProjectID types.SFID   `name:"-"`
    18  	AppletIDs []types.SFID `in:"query" name:"appletID,omitempty"`
    19  	Names     []string     `in:"query" name:"names,omitempty"`
    20  	NameLike  string       `in:"query" name:"name,omitempty"`
    21  	LNameLike string       `in:"query" name:"lName,omitempty"`
    22  }
    23  
    24  func (r *CondArgs) Condition() builder.SqlCondition {
    25  	var (
    26  		m = &models.Applet{}
    27  		c []builder.SqlCondition
    28  	)
    29  	if r.ProjectID != 0 {
    30  		c = append(c, m.ColProjectID().Eq(r.ProjectID))
    31  	}
    32  	if len(r.AppletIDs) > 0 {
    33  		c = append(c, m.ColAppletID().In(r.AppletIDs))
    34  	}
    35  	if len(r.Names) > 0 {
    36  		c = append(c, m.ColName().In(r.Names))
    37  	}
    38  	if r.NameLike != "" {
    39  		c = append(c, m.ColName().Like(r.NameLike))
    40  	}
    41  	if r.NameLike != "" {
    42  		c = append(c, m.ColName().LLike(r.LNameLike))
    43  	}
    44  	return builder.And(c...)
    45  }
    46  
    47  type ListReq struct {
    48  	CondArgs
    49  	datatypes.Pager
    50  }
    51  
    52  type ListRsp struct {
    53  	Data  []models.Applet `json:"data"`
    54  	Total int64           `json:"total"`
    55  }
    56  
    57  type Detail struct {
    58  	*models.Applet
    59  	models.ResourceInfo
    60  	models.RelInstance
    61  	*models.InstanceInfo
    62  }
    63  
    64  type ListDetailRsp struct {
    65  	Data  []*Detail `json:"data"`
    66  	Total int64     `json:"total"`
    67  }
    68  
    69  type Info struct {
    70  	AppletName string                `json:"appletName"`
    71  	WasmName   string                `json:"wasmName,omitempty"`
    72  	WasmMd5    string                `json:"wasmMd5,omitempty"`
    73  	WasmCache  *wasm.Cache           `json:"wasmCache,omitempty"`
    74  	Strategies []models.StrategyInfo `json:"strategies,omitempty"`
    75  }
    76  
    77  type CreateReq struct {
    78  	File *multipart.FileHeader `name:"file"`
    79  	Info `name:"info"`
    80  }
    81  
    82  // BuildStrategies must be built. if nil default strategy returned
    83  func (r *CreateReq) BuildStrategies(ctx context.Context) []models.Strategy {
    84  	ids := confid.MustSFIDGeneratorFromContext(ctx).MustGenSFIDs(len(r.Strategies) + 1)
    85  	app := types.MustAppletFromContext(ctx)
    86  	prj := types.MustProjectFromContext(ctx)
    87  	sty := make([]models.Strategy, 0, len(r.Strategies))
    88  	for i := range r.Strategies {
    89  		sty = append(sty, models.Strategy{
    90  			RelStrategy:  models.RelStrategy{StrategyID: ids[i]},
    91  			RelProject:   models.RelProject{ProjectID: prj.ProjectID},
    92  			RelApplet:    models.RelApplet{AppletID: app.AppletID},
    93  			StrategyInfo: r.Strategies[i],
    94  		})
    95  	}
    96  	if len(sty) == 0 {
    97  		sty = append(sty, models.Strategy{
    98  			RelStrategy:  models.RelStrategy{StrategyID: ids[0]},
    99  			RelProject:   models.RelProject{ProjectID: prj.ProjectID},
   100  			RelApplet:    models.RelApplet{AppletID: app.AppletID},
   101  			StrategyInfo: models.DefaultStrategyInfo,
   102  		})
   103  	}
   104  	return sty
   105  }
   106  
   107  type CreateRsp struct {
   108  	*models.Applet
   109  	*models.Instance `json:"instance"`
   110  	*models.Resource `json:"resource,omitempty"`
   111  	Strategies       []models.Strategy `json:"strategies,omitempty"`
   112  }
   113  
   114  type UpdateReq struct {
   115  	File *multipart.FileHeader `name:"file,omitempty"`
   116  	Info `name:"info"`
   117  }
   118  
   119  // BuildStrategies try build, if invalid nil return
   120  func (r *UpdateReq) BuildStrategies(ctx context.Context) []models.Strategy {
   121  	if len(r.Strategies) == 0 {
   122  		return nil
   123  	}
   124  	app := types.MustAppletFromContext(ctx)
   125  	prj := types.MustProjectFromContext(ctx)
   126  	sty := make([]models.Strategy, 0, len(r.Info.Strategies))
   127  	ids := confid.MustSFIDGeneratorFromContext(ctx).MustGenSFIDs(len(r.Info.Strategies))
   128  	for i := range r.Info.Strategies {
   129  		sty = append(sty, models.Strategy{
   130  			RelStrategy:  models.RelStrategy{StrategyID: ids[i]},
   131  			RelProject:   models.RelProject{ProjectID: prj.ProjectID},
   132  			RelApplet:    models.RelApplet{AppletID: app.AppletID},
   133  			StrategyInfo: r.Info.Strategies[i],
   134  		})
   135  	}
   136  	return sty
   137  }
   138  
   139  type UpdateRsp = CreateRsp
   140  
   141  func detail(app *models.Applet, ins *models.Instance, res *models.Resource) *Detail {
   142  	ret := &Detail{Applet: app}
   143  
   144  	if res != nil {
   145  		ret.ResourceInfo = res.ResourceInfo
   146  	}
   147  
   148  	if ins != nil {
   149  		ins.State, _ = vm.GetInstanceState(ins.InstanceID)
   150  		ret.InstanceInfo = &ins.InstanceInfo
   151  		ret.RelInstance = ins.RelInstance
   152  	}
   153  
   154  	return ret
   155  }