github.com/hungdoo/bot@v0.0.0-20240325145135-dd1f386f7b81/src/packages/command/common/command.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 8 "github.com/shopspring/decimal" 9 ) 10 11 type Command struct { 12 ICommand `json:"-" bson:"-"` 13 Name string `json:"name" bson:"name,omitempty"` 14 Type CommandType `json:"type" bson:"type"` 15 Data []string `json:"data" bson:"data"` 16 ExecutedTime time.Time `json:"-" bson:"executed_time"` 17 IdleTime time.Duration `json:"idletime" bson:"idletime"` 18 Enabled bool `json:"-" bson:"enabled"` 19 Prev string `json:"-" bson:"prev"` 20 DisplayMsg string `json:"-" bson:"display_msg"` 21 Error string `json:"-" bson:"error"` 22 } 23 24 type CustomCommand struct { 25 Command 26 Id string `bson:"_id,unique"` 27 } 28 29 // Setters 30 func (c *Command) SetDisplayMsg(msg string) { 31 c.DisplayMsg = msg 32 } 33 func (c *Command) SetPrev(newValue decimal.Decimal) { 34 c.Prev = newValue.String() 35 } 36 func (c *Command) SetEnabled(newValue bool) { 37 c.Enabled = newValue 38 } 39 func (c *Command) SetExecutedTime(newValue time.Time) { 40 c.ExecutedTime = newValue 41 } 42 func (c *Command) SetIdleTime(newValue time.Duration) { 43 c.IdleTime = newValue 44 } 45 func (c *Command) SetError(err string) { 46 c.Error = err 47 } 48 49 // Getters 50 func (c *Command) GetPrev() decimal.Decimal { 51 d := decimal.Zero 52 if len(strings.TrimSpace(c.Prev)) == 0 { 53 return decimal.Zero 54 } 55 err := d.Scan(c.Prev) 56 if err != nil { 57 return decimal.Zero 58 } 59 return d 60 } 61 func (c *Command) GetData() []string { 62 return c.Data 63 } 64 func (c *Command) GetError() string { 65 return c.Error 66 } 67 func (c *Command) GetOverview() string { 68 lastErr := c.GetError() 69 70 if len(lastErr) != 0 { 71 return fmt.Sprintf("[%s:%v]\n%v - %.2fm\nlastErr: %s", c.Type.String(), c.Name, c.GetDisplayMsg(), time.Since(c.ExecutedTime).Minutes(), lastErr) 72 } 73 return fmt.Sprintf("[%s:%v]\n%v - %.2fm", c.Type.String(), c.Name, c.GetDisplayMsg(), time.Since(c.ExecutedTime).Minutes()) 74 75 } 76 func (c *Command) GetType() CommandType { 77 return c.Type 78 } 79 80 func (c *Command) GetDisplayMsg() string { 81 return c.DisplayMsg 82 } 83 84 // @dev don't change. need consistency for db access 85 func (c *Command) GetName() string { 86 return c.Name 87 } 88 func (c *Command) IsEnabled() bool { 89 return c.Enabled 90 } 91 func (c *Command) IsIdle() bool { 92 return time.Since(c.ExecutedTime) < c.IdleTime 93 } 94 95 func (c *Command) GetUnderlying() interface{} { 96 return *c 97 }