github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/cmd/srv-applet-mgr/apis/monitor/put.go (about) 1 package monitor 2 3 import ( 4 "context" 5 6 "github.com/machinefi/w3bstream/cmd/srv-applet-mgr/apis/middleware" 7 "github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/httpx" 8 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/datatypes" 9 "github.com/machinefi/w3bstream/pkg/enums" 10 "github.com/machinefi/w3bstream/pkg/errors/status" 11 "github.com/machinefi/w3bstream/pkg/modules/blockchain" 12 "github.com/machinefi/w3bstream/pkg/modules/project" 13 "github.com/machinefi/w3bstream/pkg/types" 14 ) 15 16 const monitorBatchUpdateLimit = 100 17 18 type ControlContractLog struct { 19 httpx.MethodPut 20 blockchain.BatchUpdateMonitorReq `in:"body"` 21 Cmd enums.MonitorCmd `in:"path" name:"cmd"` 22 } 23 24 func (r *ControlContractLog) Path() string { return "/contract_log/:cmd" } 25 26 func (r *ControlContractLog) Output(ctx context.Context) (interface{}, error) { 27 if err := r.validate(ctx); err != nil { 28 return nil, err 29 } 30 31 b, _ := convCmd(r.Cmd) 32 return nil, blockchain.BatchUpdateContractLogPausedBySFIDs(ctx, r.IDs, b) 33 } 34 35 func (r *ControlContractLog) validate(ctx context.Context) error { 36 if l := len(r.IDs); l == 0 || l > monitorBatchUpdateLimit { 37 return status.InvalidContractLogIDs 38 } 39 40 if _, err := convCmd(r.Cmd); err != nil { 41 return err 42 } 43 ca := middleware.MustCurrentAccountFromContext(ctx) 44 45 cs, err := blockchain.ListContractLogBySFIDs(ctx, r.IDs) 46 if err != nil { 47 return err 48 } 49 if len(cs) == 0 { 50 return status.InvalidContractLogIDs 51 } 52 pm := map[string]bool{} 53 for _, c := range cs { 54 pm[c.ProjectName] = true 55 } 56 57 return vaildateProjects(ctx, ca.AccountID, pm) 58 } 59 60 type ControlChainTx struct { 61 httpx.MethodPut 62 blockchain.BatchUpdateMonitorReq `in:"body"` 63 Cmd enums.MonitorCmd `in:"path" name:"cmd"` 64 } 65 66 func (r *ControlChainTx) Path() string { return "/chain_tx/:cmd" } 67 68 func (r *ControlChainTx) Output(ctx context.Context) (interface{}, error) { 69 if err := r.validate(ctx); err != nil { 70 return nil, err 71 } 72 73 b, _ := convCmd(r.Cmd) 74 return nil, blockchain.BatchUpdateChainTxPausedBySFIDs(ctx, r.IDs, b) 75 } 76 77 func (r *ControlChainTx) validate(ctx context.Context) error { 78 if l := len(r.IDs); l == 0 || l > monitorBatchUpdateLimit { 79 return status.InvalidChainTxIDs 80 } 81 82 if _, err := convCmd(r.Cmd); err != nil { 83 return err 84 } 85 ca := middleware.MustCurrentAccountFromContext(ctx) 86 87 cs, err := blockchain.ListChainTxBySFIDs(ctx, r.IDs) 88 if err != nil { 89 return err 90 } 91 if len(cs) == 0 { 92 return status.InvalidChainTxIDs 93 } 94 pm := map[string]bool{} 95 for _, c := range cs { 96 pm[c.ProjectName] = true 97 } 98 99 return vaildateProjects(ctx, ca.AccountID, pm) 100 } 101 102 type ControlChainHeight struct { 103 httpx.MethodPut 104 blockchain.BatchUpdateMonitorReq `in:"body"` 105 Cmd enums.MonitorCmd `in:"path" name:"cmd"` 106 } 107 108 func (r *ControlChainHeight) Path() string { return "/chain_height/:cmd" } 109 110 func (r *ControlChainHeight) Output(ctx context.Context) (interface{}, error) { 111 if err := r.validate(ctx); err != nil { 112 return nil, err 113 } 114 115 b, _ := convCmd(r.Cmd) 116 return nil, blockchain.BatchUpdateChainHeightPausedBySFIDs(ctx, r.IDs, b) 117 } 118 119 func (r *ControlChainHeight) validate(ctx context.Context) error { 120 if l := len(r.IDs); l == 0 || l > monitorBatchUpdateLimit { 121 return status.InvalidChainHeightIDs 122 } 123 124 if _, err := convCmd(r.Cmd); err != nil { 125 return err 126 } 127 ca := middleware.MustCurrentAccountFromContext(ctx) 128 129 cs, err := blockchain.ListChainHeightBySFIDs(ctx, r.IDs) 130 if err != nil { 131 return err 132 } 133 if len(cs) == 0 { 134 return status.InvalidChainHeightIDs 135 } 136 pm := map[string]bool{} 137 for _, c := range cs { 138 pm[c.ProjectName] = true 139 } 140 141 return vaildateProjects(ctx, ca.AccountID, pm) 142 } 143 144 func convCmd(c enums.MonitorCmd) (datatypes.Bool, error) { 145 switch c { 146 case enums.MONITOR_CMD__START: 147 return datatypes.FALSE, nil 148 case enums.MONITOR_CMD__PAUSE: 149 return datatypes.TRUE, nil 150 } 151 return datatypes.FALSE, status.UnknownMonitorCommand 152 } 153 154 func vaildateProjects(ctx context.Context, accountID types.SFID, pNames map[string]bool) error { 155 pns := []string{} 156 for p := range pNames { 157 pns = append(pns, p) 158 } 159 ps, err := project.ListByCond(ctx, &project.CondArgs{Names: pns}) 160 if err != nil { 161 return err 162 } 163 for _, p := range ps { 164 if p.AccountID != accountID { 165 return status.NoProjectPermission 166 } 167 } 168 return nil 169 }