log.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package http
  2. import (
  3. "context"
  4. "fmt"
  5. "reflect"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "go-common/app/admin/main/aegis/model"
  10. "go-common/library/database/elastic"
  11. "go-common/library/log"
  12. "go-common/library/xstr"
  13. )
  14. //const
  15. const (
  16. LogMinYear = 2018
  17. LogMinTime = "2018-11-01 10:00:00"
  18. )
  19. func setESParams(r *elastic.Request, args interface{}, cm model.EsCommon) {
  20. av := reflect.ValueOf(args)
  21. if av.Kind() == reflect.Ptr {
  22. av = av.Elem()
  23. }
  24. if av.Kind() != reflect.Struct {
  25. return
  26. }
  27. atp := av.Type()
  28. ranges := map[string]map[string]interface{}{}
  29. for i := atp.NumField() - 1; i >= 0; i-- {
  30. fdt := atp.Field(i)
  31. tag := fdt.Tag.Get("reflect")
  32. if tag == "ignore" || tag == "" {
  33. continue
  34. }
  35. fdv := av.Field(i)
  36. fdk := fdt.Type.Kind()
  37. if (fdk == reflect.Slice || fdk == reflect.String) && fdv.Len() == 0 {
  38. continue
  39. }
  40. //default处理
  41. omitdefault := strings.Index(tag, ",omitdefault")
  42. tag = strings.Replace(tag, ",omitdefault", "", -1)
  43. fdvv := fdv.Interface()
  44. fdvslice := false
  45. if omitdefault > -1 && fmt.Sprintf("%v", fdvv) == fdt.Tag.Get("default") {
  46. continue
  47. }
  48. //字段值处理,parse额外处理
  49. switch fdk {
  50. case reflect.Int64, reflect.Int32, reflect.Int, reflect.Int16, reflect.Int8:
  51. fdvv = fdv.Int()
  52. case reflect.String:
  53. if fdv.Len() == 0 {
  54. continue
  55. }
  56. v := fdv.String()
  57. parse := fdt.Tag.Get("parse")
  58. if parse == "int" {
  59. vi, err := strconv.ParseInt(v, 10, 64)
  60. if err != nil {
  61. log.Error("setESParams strconv.ParseInt(%s) error(%v)", v, err)
  62. continue
  63. }
  64. fdvv = vi
  65. } else if parse == "[]int" {
  66. vi, err := xstr.SplitInts(v)
  67. if err != nil {
  68. log.Error("setESParams xstr.SplitInts(%s) error(%v)", v, err)
  69. continue
  70. }
  71. fdvv = vi
  72. fdvslice = true
  73. } else {
  74. fdvv = v
  75. }
  76. case reflect.Slice:
  77. if fdv.Len() == 0 {
  78. continue
  79. }
  80. fdvslice = true
  81. default:
  82. log.Warn("setESParams not support kind(%s) for tag(%s)", fdk.String(), tag)
  83. continue
  84. }
  85. //范围处理
  86. from := strings.Index(tag, ",from")
  87. to := strings.Index(tag, ",to")
  88. if from > -1 {
  89. if _, exist := ranges[tag[:from]]; !exist {
  90. ranges[tag[:from]] = map[string]interface{}{}
  91. }
  92. ranges[tag[:from]]["from"] = fdvv
  93. continue
  94. }
  95. if to > -1 {
  96. if _, exist := ranges[tag[:to]]; !exist {
  97. ranges[tag[:to]] = map[string]interface{}{}
  98. }
  99. ranges[tag[:to]]["to"] = fdvv
  100. continue
  101. }
  102. if fdvslice {
  103. r.WhereIn(tag, fdvv)
  104. } else {
  105. r.WhereEq(tag, fdvv)
  106. }
  107. }
  108. for field, items := range ranges {
  109. r.WhereRange(field, items["from"], items["to"], elastic.RangeScopeLcRc)
  110. }
  111. r.Ps(cm.Ps).Pn(cm.Pn)
  112. order := []map[string]string{}
  113. if cm.Order != "" || cm.Sort != "" {
  114. r.Order(cm.Order, cm.Sort)
  115. order = append(order, map[string]string{cm.Order: cm.Sort})
  116. }
  117. if cm.Group != "" {
  118. r.GroupBy(elastic.EnhancedModeDistinct, cm.Group, order)
  119. }
  120. }
  121. //QueryLogSearch .
  122. func (d *Dao) QueryLogSearch(c context.Context, args *model.ParamsQueryLog, cm model.EsCommon) (resp *model.SearchLogResult, err error) {
  123. var (
  124. min int = LogMinYear
  125. max int
  126. ctimefrom, ctimeto time.Time
  127. )
  128. //默认获取所有行为日志,确定了时间范围的,只查询该段范围内的日志
  129. if args.CtimeFrom != "" {
  130. ctimefrom, _ = time.ParseInLocation("2006-01-02 15:04:05", args.CtimeFrom, time.Local)
  131. if ctimefrom.Year() > min {
  132. min = ctimefrom.Year()
  133. }
  134. }
  135. if args.CtimeTo != "" {
  136. ctimeto, _ = time.ParseInLocation("2006-01-02 15:04:05", args.CtimeTo, time.Local)
  137. if ctimeto.Year() >= min {
  138. max = ctimeto.Year()
  139. }
  140. } else {
  141. max = time.Now().Year()
  142. }
  143. tmpl := ",log_audit_%d_%d"
  144. index := ""
  145. for i := min; i <= max; i++ {
  146. index += fmt.Sprintf(tmpl, args.Business, i)
  147. }
  148. index = strings.TrimLeft(index, ",")
  149. r := d.es.NewRequest("log_audit").Index(index).Fields(
  150. "uid",
  151. "uname",
  152. "oid",
  153. "type",
  154. "action",
  155. "str_0",
  156. "str_1",
  157. "str_2",
  158. "int_0",
  159. "int_1",
  160. "int_2",
  161. "ctime",
  162. "extra_data")
  163. setESParams(r, args, cm)
  164. err = r.Scan(c, &resp)
  165. return
  166. }