github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/event/event_models.go (about) 1 package event 2 3 import ( 4 "bytes" 5 "math" 6 "strings" 7 "time" 8 9 "github.com/google/uuid" 10 11 "github.com/machinefi/w3bstream/pkg/enums" 12 "github.com/machinefi/w3bstream/pkg/types" 13 ) 14 15 const ( 16 eventTypeDataPush = "DA-TA_PU-SH" 17 ) 18 19 type EventReq struct { 20 // From transport source 21 From enums.EventSource `in:"query" name:"from,omitempty"` 22 // Channel message channel named (intact project name) 23 Channel string `in:"path" name:"channel"` 24 // EventType used for filter strategies created in w3b before 25 EventType string `in:"query" name:"eventType,omitempty"` 26 // EventID unique id for tracing event under channel 27 EventID string `in:"query" name:"eventID,omitempty"` 28 // Timestamp event time when publisher do send 29 Timestamp int64 `in:"query" name:"timestamp,omitempty"` 30 // Payload event payload (binary only) 31 Payload bytes.Buffer `in:"body" mime:"stream"` 32 } 33 34 func (r *EventReq) SetDefault() { 35 if r.EventType == "" { 36 r.EventType = enums.EVENTTYPEDEFAULT 37 } 38 if r.EventID == "" { 39 r.EventID = uuid.NewString() + "_w3b" // flag generated by w3b node 40 } 41 r.EventID = strings.ToLower(r.EventID) 42 // to make sure input timestamp using milliseconds epoch 43 if now := time.Now().UTC().UnixMilli(); math.Abs(float64(now-r.Timestamp)) > 1e6 { 44 r.Timestamp = time.Now().UTC().UnixMilli() 45 } 46 } 47 48 func (r *EventReq) IsDataPush() bool { 49 return r.EventType == eventTypeDataPush 50 } 51 52 type Result struct { 53 // AppletName applet name(unique) under published channel(project) 54 AppletName string `json:"appletName"` 55 // InstanceID the unique wasm vm id 56 InstanceID types.SFID `json:"instanceID"` 57 // Handler invoked wasm entry name 58 Handler string `json:"handler"` 59 // ReturnValue wasm call returned value 60 ReturnValue []byte `json:"returnValue"` 61 // ReturnCode wasm call returned code 62 ReturnCode int `json:"code"` 63 // Error message instance module, presents result for wasm invoking 64 Error string `json:"error,omitempty"` 65 } 66 67 type EventRsp struct { 68 // Channel intact project name 69 Channel string `json:"channel"` 70 // PublisherID publisher(device) unique id in w3b node 71 PublisherID types.SFID `json:"publisherID"` 72 // PublisherKey publisher(device) mn unique id of this device 73 PublisherKey string `json:"publisherKey"` 74 // EventID same as EventReq.EventID 75 EventID string `json:"eventID"` 76 // Timestamp event respond time when event handled done. 77 Timestamp int64 `json:"timestamp"` 78 // Results result for each wasm invoke, which hits strategies. 79 Results []*Result `json:"results"` 80 // Error error message from w3b node (api level), different from Result.Error 81 Error string `json:"error,omitempty"` 82 } 83 84 type DataPushReqs []*DataPushReq 85 86 func (reqs DataPushReqs) SetDefault() { 87 for _, r := range reqs { 88 if r.EventType == "" { 89 r.EventType = enums.EVENTTYPEDEFAULT 90 } 91 if r.Timestamp == 0 { 92 r.Timestamp = time.Now().UTC().UnixMilli() 93 } 94 } 95 } 96 97 type DataPushReq struct { 98 DeviceID string `json:"device_id"` 99 EventType string `json:"event_type,omitempty"` 100 Payload string `json:"payload"` 101 Timestamp int64 `json:"timestamp,omitempty"` 102 } 103 104 type DataPushRsps []*DataPushRsp 105 106 type DataPushRsp struct { 107 Index int `json:"index"` 108 Results []*Result `json:"results"` 109 }