env.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package qinglong
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/cdle/sillyGirl/core"
  8. )
  9. type EnvResponse struct {
  10. Code int `json:"code"`
  11. Data []Env `json:"data"`
  12. }
  13. type Env struct {
  14. Value string `json:"value,omitempty"`
  15. ID string `json:"_id,omitempty"`
  16. Status int `json:"status,omitempty"`
  17. Name string `json:"name,omitempty"`
  18. Remarks string `json:"remarks,omitempty"`
  19. Timestamp string `json:"timestamp,omitempty"`
  20. Created int64 `json:"created,omitempty"`
  21. }
  22. func GetEnv(id string) (*Env, error) {
  23. envs, err := GetEnvs("")
  24. if err != nil {
  25. return nil, err
  26. }
  27. for _, env := range envs {
  28. if env.ID == id {
  29. return &env, nil
  30. }
  31. }
  32. return nil, nil
  33. }
  34. func GetEnvs(searchValue string) ([]Env, error) {
  35. er := EnvResponse{}
  36. if err := Config.Req(ENVS, &er, "?searchValue="+searchValue); err != nil {
  37. return nil, err
  38. }
  39. return er.Data, nil
  40. }
  41. func GetEnvss(searchValue string) ([]Env, error) {
  42. er := EnvResponse{}
  43. if err := Config.Req(ENVS, &er, "?searchValue="+searchValue); err != nil {
  44. return nil, err
  45. }
  46. return er.Data, nil
  47. }
  48. func SetEnv(e Env) error {
  49. envs, err := GetEnvs("")
  50. if err != nil {
  51. return err
  52. }
  53. for _, env := range envs {
  54. if env.Name == e.Name {
  55. if e.Remarks != "" {
  56. env.Remarks = e.Remarks
  57. }
  58. if e.Value != "" {
  59. env.Value = e.Value
  60. }
  61. if e.Name != "" {
  62. env.Name = e.Name
  63. }
  64. return Config.Req(PUT, ENVS, env)
  65. }
  66. }
  67. return AddEnv(e)
  68. }
  69. func UdpEnv(env Env) error {
  70. env.Created = 0
  71. env.Timestamp = ""
  72. return Config.Req(PUT, ENVS, env)
  73. }
  74. func ModEnv(e Env) error {
  75. envs, err := GetEnvs("")
  76. if err != nil {
  77. return err
  78. }
  79. for _, env := range envs {
  80. if env.ID == e.ID {
  81. if e.Remarks != "" {
  82. env.Remarks = e.Remarks
  83. }
  84. if e.Value != "" {
  85. env.Value = e.Value
  86. }
  87. if e.Name != "" {
  88. env.Name = e.Name
  89. }
  90. env.Created = 0
  91. env.Timestamp = ""
  92. return Config.Req(PUT, ENVS, env)
  93. }
  94. }
  95. return errors.New("找不到环境变量")
  96. }
  97. func AddEnv(e Env) error {
  98. e.Created = 0
  99. e.Timestamp = ""
  100. return Config.Req(POST, ENVS, []Env{e})
  101. }
  102. func RemEnv(e *Env) error {
  103. return Config.Req(DELETE, ENVS, []byte(`["`+e.ID+`"]`))
  104. }
  105. func init() {
  106. core.AddCommand("ql", []core.Function{
  107. {
  108. Rules: []string{`cookie status`},
  109. Admin: true,
  110. Handle: func(_ core.Sender) interface{} {
  111. type Count struct {
  112. Total int
  113. Disable int
  114. TodayCreate int
  115. TodayDisable int
  116. TodayUpdate int
  117. }
  118. envs, err := GetEnvs("")
  119. if err != nil {
  120. return err
  121. }
  122. today := time.Now()
  123. var cookies = map[string]*Count{}
  124. for _, env := range envs {
  125. var c *Count
  126. if _, ok := cookies[env.Name]; !ok {
  127. cookies[env.Name] = &Count{}
  128. }
  129. c = cookies[env.Name]
  130. c.Total++
  131. if strings.Contains(env.Timestamp, fmt.Sprintf(`%s %s`, today.Month().String()[0:3], today.Format("02 2006"))) {
  132. if env.Status != 0 {
  133. c.TodayDisable++
  134. } else {
  135. c.TodayUpdate++
  136. }
  137. }
  138. if env.Status != 0 {
  139. c.Disable++
  140. }
  141. if time.Unix(env.Created, 0).Format("2006-01-02") == today.Format("2006-01-02") {
  142. c.TodayCreate++
  143. }
  144. }
  145. ss := []string{}
  146. for name, c := range cookies {
  147. ss = append(ss, fmt.Sprintf(`%s 今日新增%d,今日更新%d,今日失效%d,总数%d,有效%d,无效%d`, name, c.TodayCreate, c.TodayUpdate, c.TodayDisable, c.Total, c.Total-c.Disable, c.Disable))
  148. }
  149. return strings.Join(ss, "\n")
  150. },
  151. },
  152. })
  153. }