map_data.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package model
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "go-common/library/log"
  9. )
  10. // MapData .
  11. type MapData map[string]interface{}
  12. // StrID .
  13. func (m MapData) StrID(attrs *Attrs) (string, bool) {
  14. if attrs.Index.IndexID == "UUID" {
  15. return "", true
  16. }
  17. var data []interface{}
  18. arr := strings.Split(attrs.Index.IndexID, ",")
  19. arrLen := len(arr)
  20. if arrLen >= 2 {
  21. for _, v := range arr[1:] {
  22. if item, ok := m[v].(*interface{}); ok {
  23. data = append(data, item)
  24. continue
  25. }
  26. if item, ok := m[v].(interface{}); ok {
  27. data = append(data, item)
  28. continue
  29. }
  30. log.Error("model.map_data.StrID err (%v)", v)
  31. return "", false
  32. }
  33. return fmt.Sprintf(arr[0], data...), true
  34. }
  35. return "", false
  36. }
  37. // Index .
  38. func (m MapData) Index(attrs *Attrs) (indexAliasName, indexEntityName string) {
  39. switch attrs.Index.IndexSplit {
  40. case "single":
  41. indexAliasName = attrs.Index.IndexAliasPrefix
  42. indexEntityName = attrs.Index.IndexEntityPrefix
  43. case "int":
  44. if attrs.DataSQL.DataIndexSuffix != "" {
  45. s := strings.Split(attrs.DataSQL.DataIndexSuffix, ";")
  46. v := strings.Split(s[1], ":")
  47. if id, ok := m[v[0]].(*interface{}); ok {
  48. // indexAliasName = fmt.Sprintf("%s%d", attrs.Index.IndexAliasPrefix, (*id).(int64)%100) // mod
  49. divisor, _ := strconv.ParseInt(v[2], 10, 64)
  50. indexAliasName = fmt.Sprintf("%s"+s[0], attrs.Index.IndexAliasPrefix, (*id).(int64)%divisor)
  51. indexEntityName = fmt.Sprintf("%s"+s[0], attrs.Index.IndexEntityPrefix, (*id).(int64)%divisor)
  52. }
  53. if id, ok := m[v[0]].(interface{}); ok {
  54. divisor, _ := strconv.ParseInt(v[2], 10, 64)
  55. indexAliasName = fmt.Sprintf("%s"+s[0], attrs.Index.IndexAliasPrefix, id.(int64)%divisor)
  56. indexEntityName = fmt.Sprintf("%s"+s[0], attrs.Index.IndexEntityPrefix, id.(int64)%divisor)
  57. }
  58. }
  59. }
  60. //fmt.Println("indexname", indexAliasName, indexEntityName)
  61. return
  62. }
  63. // DtbIndex .
  64. // func (m MapData) DtbIndex(attrs *Attrs) (indexName string) {
  65. // if attrs.Index.IndexZero == "0" {
  66. // indexName = attrs.Index.IndexAliasPrefix
  67. // return
  68. // }
  69. // if attrs.DataSQL.DataIndexSuffix != "" {
  70. // s := strings.Split(attrs.DataSQL.DataIndexSuffix, ";")
  71. // v := strings.Split(s[1], ":")
  72. // divisor, _ := strconv.ParseInt(v[2], 10, 64)
  73. // indexName = fmt.Sprintf("%s"+s[0], attrs.Index.IndexAliasPrefix, int64(m[v[0]].(float64))%divisor)
  74. // }
  75. // return
  76. // }
  77. // PrimaryID .
  78. func (m MapData) PrimaryID() int64 {
  79. if m["_id"] != nil {
  80. if id, ok := m["_id"].(*interface{}); ok {
  81. return (*id).(int64)
  82. }
  83. }
  84. return 0
  85. }
  86. // StrMTime .
  87. func (m MapData) StrMTime() string {
  88. if m["_mtime"] != nil {
  89. if mtime, ok := m["_mtime"].(*interface{}); ok {
  90. return (*mtime).(time.Time).Format("2006-01-02 15:04:05")
  91. } else if mtime, ok := m["_mtime"].(string); ok {
  92. return mtime
  93. }
  94. }
  95. return ""
  96. }
  97. // StrCTime .
  98. func (m MapData) StrCTime() string {
  99. if m["ctime"] != nil {
  100. if ctime, ok := m["ctime"].(*interface{}); ok {
  101. return (*ctime).(time.Time).Format("2006-01-02")
  102. } else if ctime, ok := m["ctime"].(string); ok {
  103. return ctime
  104. }
  105. }
  106. return ""
  107. }
  108. // InetNtoA int64 to string ip.
  109. func (m MapData) InetNtoA(ip int64) string {
  110. return fmt.Sprintf("%d.%d.%d.%d",
  111. byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip))
  112. }
  113. // TransData transfer address into value
  114. func (m MapData) TransData(attr *Attrs) {
  115. for k, v := range m {
  116. // transfer automaticlly
  117. if v2, ok := v.(*interface{}); ok {
  118. switch (*v2).(type) {
  119. case time.Time:
  120. m[k] = (*v2).(time.Time).Format("2006-01-02 15:04:05")
  121. case []uint, []uint8, []uint16, []uint32, []uint64:
  122. m[k] = string((*v2).([]byte))
  123. case int, int8, int16, int32, int64: // 一定要,用于extra_data查询
  124. m[k] = (*v2).(int64)
  125. case nil:
  126. m[k] = int64(0) //给个默认值,当查到为null时
  127. default:
  128. // other types
  129. }
  130. }
  131. // transfer again by custom
  132. if t, ok := attr.DataSQL.DataIndexFormatFields[k]; ok {
  133. if v3, ok := v.(*interface{}); ok {
  134. switch t {
  135. case "ip":
  136. if *v3 == nil {
  137. *v3 = int64(0)
  138. }
  139. ipFormat := m.InetNtoA((*v3).(int64))
  140. m[k+"_format"] = ipFormat
  141. case "arr":
  142. var arr []int
  143. binaryAttributes := strconv.FormatInt((*v3).(int64), 2)
  144. for i := len(binaryAttributes) - 1; i >= 0; i-- {
  145. b := fmt.Sprintf("%c", binaryAttributes[i])
  146. if b == "1" {
  147. arr = append(arr, len(binaryAttributes)-i)
  148. }
  149. }
  150. m[k+"_format"] = arr
  151. case "bin":
  152. var arr []int
  153. binaryAttributes := strconv.FormatInt((*v3).(int64), 2)
  154. for i := len(binaryAttributes) - 1; i >= 0; i-- {
  155. b := fmt.Sprintf("%c", binaryAttributes[i])
  156. if b == "1" {
  157. arr = append(arr, len(binaryAttributes)-i)
  158. }
  159. }
  160. m[k] = arr
  161. case "array_json":
  162. var arr []int64
  163. arr = []int64{}
  164. json.Unmarshal([]byte((*v3).([]uint8)), &arr) //如果不是json就是空数组
  165. // println(len(arr))
  166. m[k] = arr
  167. case "day":
  168. m[k] = (*v3).(time.Time).Format("2006-01-02")
  169. case "workflow":
  170. delete(m, k)
  171. default:
  172. // other types
  173. }
  174. }
  175. }
  176. }
  177. }
  178. // TransDtb transfer databus fields into es fields
  179. func (m MapData) TransDtb(attr *Attrs) {
  180. // TODO 注释要打开,不然无法移除不要的dtb字段
  181. // for k := range m {
  182. // if _, ok := attr.DataSQL.DataDtbFields[k]; !ok {
  183. // if k == "index_name" {
  184. // continue
  185. // }
  186. // delete(m, k)
  187. // }
  188. // }
  189. res := map[string]interface{}{}
  190. for k, dv := range attr.DataSQL.DataDtbFields {
  191. for _, dk := range dv {
  192. if v, ok := m[k]; ok {
  193. switch v.(type) {
  194. case float64:
  195. res[dk] = int64(v.(float64))
  196. default:
  197. res[dk] = v
  198. }
  199. }
  200. }
  201. }
  202. for k := range res {
  203. m[k] = res[k]
  204. }
  205. id, okID := attr.DataSQL.DataFieldsV2["_id"]
  206. key, okKey := attr.DataSQL.DataDtbFields[id.Field]
  207. if len(key) >= 1 && okID && okKey {
  208. m["_id"] = m[key[0]]
  209. }
  210. }