avr_archive.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package business
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/search/dao"
  7. "go-common/app/job/main/search/model"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. )
  12. // AvrArchive .
  13. type AvrArchive struct {
  14. d *dao.Dao
  15. appid string
  16. attrs *model.Attrs
  17. db *xsql.DB
  18. offset *model.LoopOffset
  19. mapData []model.MapData
  20. }
  21. // NewAvrArchive .
  22. func NewAvrArchive(d *dao.Dao, appid string) (av *AvrArchive) {
  23. av = &AvrArchive{
  24. d: d,
  25. appid: appid,
  26. attrs: d.AttrPool[appid],
  27. offset: &model.LoopOffset{},
  28. mapData: []model.MapData{},
  29. db: d.DBPool[d.AttrPool[appid].DBName],
  30. }
  31. return
  32. }
  33. // Business return business.
  34. func (av *AvrArchive) Business() string {
  35. return av.attrs.Business
  36. }
  37. // InitIndex init index.
  38. func (av *AvrArchive) InitIndex(c context.Context) {
  39. if aliases, err := av.d.GetAliases(av.attrs.ESName, av.attrs.Index.IndexAliasPrefix); err != nil {
  40. av.d.InitIndex(c, nil, av.attrs.ESName, av.attrs.Index.IndexAliasPrefix, av.attrs.Index.IndexEntityPrefix, av.attrs.Index.IndexMapping)
  41. } else {
  42. av.d.InitIndex(c, aliases, av.attrs.ESName, av.attrs.Index.IndexAliasPrefix, av.attrs.Index.IndexEntityPrefix, av.attrs.Index.IndexMapping)
  43. }
  44. }
  45. // InitOffset insert init value to offset.
  46. func (av *AvrArchive) InitOffset(c context.Context) {
  47. av.d.InitOffset(c, av.offset, av.attrs, []string{})
  48. nowFormat := time.Now().Format("2006-01-02 15:04:05")
  49. av.offset.SetOffset(0, nowFormat)
  50. }
  51. // Offset get offset.
  52. func (av *AvrArchive) Offset(c context.Context) {
  53. for {
  54. offset, err := av.d.Offset(c, av.appid, av.attrs.Table.TablePrefix)
  55. if err != nil {
  56. log.Error("ac.d.Offset error(%v)", err)
  57. time.Sleep(time.Second * 3)
  58. continue
  59. }
  60. av.offset.SetReview(offset.ReviewID, offset.ReviewTime)
  61. av.offset.SetOffset(offset.OffsetID(), offset.OffsetTime())
  62. break
  63. }
  64. }
  65. // SetRecover set recover
  66. func (av *AvrArchive) SetRecover(c context.Context, recoverID int64, recoverTime string, i int) {
  67. av.offset.SetRecoverOffset(recoverID, recoverTime)
  68. }
  69. // IncrMessages .
  70. func (av *AvrArchive) IncrMessages(c context.Context) (length int, err error) {
  71. var rows *xsql.Rows
  72. log.Info("appid: %s IncrMessages Current OffsetTime: %s, OffsetID: %d", av.appid, av.offset.OffsetTime, av.offset.OffsetID)
  73. if !av.offset.IsLoop {
  74. rows, err = av.db.Query(c, av.attrs.DataSQL.SQLByMTime, av.offset.OffsetTime, av.attrs.Other.Size)
  75. } else {
  76. rows, err = av.db.Query(c, av.attrs.DataSQL.SQLByIDMTime, av.offset.OffsetID, av.offset.OffsetTime, av.attrs.Other.Size)
  77. }
  78. if err != nil {
  79. log.Error("db.Query error(%v)", err)
  80. return
  81. }
  82. defer rows.Close()
  83. for rows.Next() {
  84. item, row := dao.InitMapData(av.attrs.DataSQL.DataIndexFields)
  85. if err = rows.Scan(row...); err != nil {
  86. log.Error("IncrMessages rows.Scan() error(%v)", err)
  87. return
  88. }
  89. av.mapData = append(av.mapData, item)
  90. }
  91. length = len(av.mapData)
  92. if length > 0 {
  93. // offset
  94. dao.UpdateOffsetByMap(av.offset, av.mapData...)
  95. // extra relevant data
  96. length, err = av.extraData(c, "db", map[string]bool{"Avr": true})
  97. }
  98. return
  99. }
  100. // AllMessages .
  101. func (av *AvrArchive) AllMessages(c context.Context) (length int, err error) {
  102. var rows *xsql.Rows
  103. log.Info("appid: %s allMessages Current RecoverID: %d", av.appid, av.offset.RecoverID)
  104. if rows, err = av.db.Query(c, av.attrs.DataSQL.SQLByID, av.offset.RecoverID, av.attrs.Other.Size); err != nil {
  105. log.Error("AllMessages db.Query error(%v)", err)
  106. return
  107. }
  108. defer rows.Close()
  109. for rows.Next() {
  110. item, row := dao.InitMapData(av.attrs.DataSQL.DataIndexFields)
  111. if err = rows.Scan(row...); err != nil {
  112. log.Error("AllMessages rows.Scan() error(%v)", err)
  113. continue
  114. }
  115. av.mapData = append(av.mapData, item)
  116. }
  117. length = len(av.mapData)
  118. if length > 0 {
  119. // offset
  120. if av.mapData[length-1]["_id"] != nil {
  121. v := av.mapData[length-1]["_id"]
  122. if v2, ok := v.(*interface{}); ok {
  123. av.offset.SetTempOffset((*v2).(int64), "")
  124. av.offset.SetRecoverTempOffset((*v2).(int64), "")
  125. }
  126. }
  127. // extra relevant data
  128. length, err = av.extraData(c, "db", map[string]bool{"Avr": true})
  129. }
  130. return
  131. }
  132. // extraData extra data for appid
  133. func (av *AvrArchive) extraData(c context.Context, way string, tags map[string]bool) (length int, err error) {
  134. switch way {
  135. case "db":
  136. for i, item := range av.mapData {
  137. item.TransData(av.attrs)
  138. for k, v := range item {
  139. av.mapData[i][k] = v
  140. }
  141. }
  142. case "dtb":
  143. for i, item := range av.mapData {
  144. item.TransDtb(av.attrs)
  145. av.mapData[i] = model.MapData{}
  146. for k, v := range item {
  147. av.mapData[i][k] = v
  148. }
  149. }
  150. }
  151. for _, ex := range av.attrs.DataExtras {
  152. if _, ok := tags[ex.Tag]; !ok {
  153. continue
  154. }
  155. switch ex.Type {
  156. case "slice":
  157. continue
  158. //av.extraDataSlice(c, ex)
  159. default:
  160. length, _ = av.extraDataDefault(c, ex)
  161. }
  162. }
  163. return
  164. }
  165. // extraData-default
  166. func (av *AvrArchive) extraDataDefault(c context.Context, ex model.AttrDataExtra) (length int, err error) {
  167. // filter ids from in_fields
  168. var (
  169. ids []int64
  170. items map[int64]model.MapData
  171. temp map[int64]model.MapData
  172. )
  173. cdtInField := ex.Condition["in_field"]
  174. items = make(map[int64]model.MapData)
  175. temp = make(map[int64]model.MapData)
  176. for _, md := range av.mapData {
  177. if v, ok := md[cdtInField]; ok {
  178. ids = append(ids, v.(int64)) // 加去重
  179. temp[v.(int64)] = md
  180. }
  181. }
  182. // query extra data
  183. if len(ids) > 0 {
  184. var rows *xsql.Rows
  185. rows, err = av.d.DBPool[ex.DBName].Query(c, fmt.Sprintf(ex.SQL, xstr.JoinInts(ids))+" and 1 = ? ", 1)
  186. if err != nil {
  187. log.Error("extraDataDefault db.Query error(%v)", err)
  188. return
  189. }
  190. for rows.Next() {
  191. item, row := dao.InitMapData(ex.Fields)
  192. if err = rows.Scan(row...); err != nil {
  193. log.Error("extraDataDefault rows.Scan() error(%v)", err)
  194. continue
  195. }
  196. if v, ok := item[ex.InField]; ok {
  197. if v2, ok := v.(*interface{}); ok {
  198. item.TransData(av.attrs)
  199. items[(*v2).(int64)] = item
  200. }
  201. }
  202. }
  203. rows.Close()
  204. }
  205. //fmt.Println("a.mapData", av.mapData, "ids", ids, "items", items)
  206. // merge data
  207. fds := []string{"_id", "cid", "vid", "aid", "v_ctime"}
  208. av.mapData = []model.MapData{}
  209. for k, item := range items {
  210. if v, ok := temp[k]; ok {
  211. for _, fd := range fds {
  212. if f, ok := item[fd]; ok {
  213. v[fd] = f
  214. }
  215. }
  216. av.mapData = append(av.mapData, v)
  217. }
  218. }
  219. length = len(av.mapData)
  220. //fmt.Println("a.mapData:after", av.mapData)
  221. return
  222. }
  223. // BulkIndex .
  224. func (av *AvrArchive) BulkIndex(c context.Context, start int, end int, writeEntityIndex bool) (err error) {
  225. partData := av.mapData[start:end]
  226. err = av.d.BulkDBData(c, av.attrs, writeEntityIndex, partData...)
  227. return
  228. }
  229. // Commit commit offset.
  230. func (av *AvrArchive) Commit(c context.Context) (err error) {
  231. err = av.d.CommitOffset(c, av.offset, av.attrs.AppID, av.attrs.Table.TablePrefix)
  232. av.mapData = []model.MapData{}
  233. return
  234. }
  235. // Sleep interval duration.
  236. func (av *AvrArchive) Sleep(c context.Context) {
  237. time.Sleep(time.Second * time.Duration(av.attrs.Other.Sleep))
  238. }
  239. // Size return size.
  240. func (av *AvrArchive) Size(c context.Context) int {
  241. return av.attrs.Other.Size
  242. }