edb_data.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_forum_hub/models"
  5. "eta/eta_forum_hub/services"
  6. "eta/eta_forum_hub/utils"
  7. "fmt"
  8. "strconv"
  9. "time"
  10. )
  11. type EdbDataController struct {
  12. BaseAuthController
  13. }
  14. // 根据binlog监听记录,更新指标数据
  15. // SaveByBinlog 根据binlog监听记录,更新指标数据
  16. // @Title 根据binlog监听记录,更新指标数据
  17. // @Description 根据binlog监听记录,更新指标数据
  18. // @Param request body models.EdbDataBinlogReq true "type json string"
  19. // @Success 200 {object} models.BaseResponse
  20. // @router /save_by_binlog [post]
  21. func (this *EdbDataController) SaveByBinlog() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. var req models.EdbDataBinlogReq
  28. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  29. if err != nil {
  30. br.Msg = "参数解析异常!"
  31. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  32. return
  33. }
  34. if len(req.List) == 0 {
  35. br.Msg = "参数异常!"
  36. br.ErrMsg = "参数异常,请检查!"
  37. return
  38. }
  39. edbInfoIdMap := make(map[int]*models.AddEdbDataBinlogReq)
  40. reqList := make([]*models.EdbDataBinlogItem, 0)
  41. // 判断指标库里是否存在这些指标,如果不存在,则丢弃,无需处理
  42. edbInfoIds := make([]int, 0)
  43. for _, item := range req.List {
  44. // 解析json
  45. var dataItem models.EdbDataBinlogItem
  46. err := json.Unmarshal([]byte(item.Item), &dataItem)
  47. if err != nil {
  48. utils.FileLog.Info("解析binlog数据失败,Err:" + err.Error(), "item", item)
  49. continue
  50. }
  51. dataItem.OpType = item.OpType
  52. reqList = append(reqList, &dataItem)
  53. edbInfoIds = append(edbInfoIds, dataItem.EdbInfoId)
  54. }
  55. // 查询指标信息
  56. edbInfoList, err := models.GetEdbInfoByIdList(edbInfoIds)
  57. if err != nil {
  58. br.Msg = "指标信息查询异常!"
  59. br.ErrMsg = "指标信息查询失败,Err:" + err.Error()
  60. return
  61. }
  62. existEdbInfoMap := make(map[int]*models.EdbInfo)
  63. for _, item := range edbInfoList {
  64. existEdbInfoMap[item.EdbInfoId] = item
  65. }
  66. // edbInfoList := []*models.EdbInfo{
  67. // {
  68. // EdbInfoId: 1,
  69. // EdbCode: "test_code_1",
  70. // EdbType: 1,
  71. // },
  72. // {
  73. // EdbInfoId: 2,
  74. // EdbCode: "test_code_2",
  75. // EdbType: 1,
  76. // },
  77. // }
  78. // existEdbInfoMap := make(map[int]*models.EdbInfo)
  79. // for _, item := range edbInfoList {
  80. // existEdbInfoMap[item.EdbInfoId] = item
  81. // }
  82. edbDataIdMap := make(map[string]*models.EdbDataBaseWithOpType)
  83. for _, item := range reqList {
  84. edbInfo, ok := existEdbInfoMap[item.EdbInfoId]
  85. if !ok {
  86. continue
  87. }
  88. if _, ok := edbInfoIdMap[item.EdbInfoId]; !ok {
  89. tmp := &models.AddEdbDataBinlogReq{
  90. EdbInfoId: item.EdbInfoId,
  91. EdbCode: item.EdbCode,
  92. EdbType: edbInfo.EdbType,
  93. }
  94. edbInfoIdMap[item.EdbInfoId] = tmp
  95. }
  96. value, ok := item.Value.(float64)
  97. if !ok {
  98. continue
  99. }
  100. createTime, _ := time.ParseInLocation("2006-01-02 15:04:05", item.CreateTime, time.Local)
  101. modifyTime, _ := time.ParseInLocation("2006-01-02 15:04:05", item.ModifyTime, time.Local)
  102. dataItem := &models.EdbDataBaseWithOpType{
  103. EdbDataBase: &models.EdbDataBase{
  104. EdbInfoId: item.EdbInfoId,
  105. EdbCode: item.EdbCode,
  106. Value: strconv.FormatFloat(value, 'f', -1, 64),
  107. DataTimestamp: item.DataTimestamp,
  108. DataTime: item.DataTime,
  109. CreateTime: createTime,
  110. ModifyTime: modifyTime,
  111. },
  112. OpType: item.OpType,
  113. }
  114. if _, ok := edbDataIdMap[fmt.Sprintf("%d_%s", item.EdbInfoId, item.DataTime)]; ok {
  115. edbDataIdMap[fmt.Sprintf("%d_%s", item.EdbInfoId, item.DataTime)] = dataItem
  116. } else {
  117. edbDataIdMap[fmt.Sprintf("%d_%s", item.EdbInfoId, item.DataTime)] = dataItem
  118. }
  119. //edbInfoIdMap[item.EdbInfoId].DataList = append(edbInfoIdMap[item.EdbInfoId].DataList, dataItem)
  120. }
  121. edbDataMinDataTimeMap := make(map[int]string)
  122. edbDataMaxDataTimeMap := make(map[int]string)
  123. for _, item := range edbDataIdMap {
  124. edbInfoIdMap[item.EdbInfoId].DataList = append(edbInfoIdMap[item.EdbInfoId].DataList, item)
  125. if edbDataMinDataTimeMap[item.EdbInfoId] == "" || edbDataMinDataTimeMap[item.EdbInfoId] > item.DataTime {
  126. edbDataMinDataTimeMap[item.EdbInfoId] = item.DataTime
  127. }
  128. if edbDataMaxDataTimeMap[item.EdbInfoId] == "" || edbDataMaxDataTimeMap[item.EdbInfoId] < item.DataTime {
  129. edbDataMaxDataTimeMap[item.EdbInfoId] = item.DataTime
  130. }
  131. }
  132. for _, item := range edbInfoIdMap {
  133. minDataTime := edbDataMinDataTimeMap[item.EdbInfoId]
  134. maxDataTime := edbDataMaxDataTimeMap[item.EdbInfoId]
  135. err := services.AddOrUpdateEdbDataWithOpType(item.EdbInfoId, item.DataList, minDataTime, maxDataTime)
  136. if err != nil {
  137. br.Msg = "更新指标数据失败!"
  138. br.ErrMsg = "更新指标数据失败,Err:" + err.Error()
  139. return
  140. }
  141. }
  142. br.Ret = 200
  143. br.Success = true
  144. br.Msg = "更新指标数据成功"
  145. return
  146. }
  147. // TestSaveByBinlog 测试SaveByBinlog接口的测试数据
  148. // @Title 测试SaveByBinlog接口的测试数据
  149. // @Description 测试SaveByBinlog接口的测试数据
  150. // @Success 200 {object} models.BaseResponse
  151. // @router /test_save_by_binlog [get]
  152. func (this *EdbDataController) TestSaveByBinlog() {
  153. br := new(models.BaseResponse).Init()
  154. defer func() {
  155. this.Data["json"] = br
  156. this.ServeJSON()
  157. }()
  158. // 构造测试数据
  159. testData := models.EdbDataBinlogReq{
  160. List: []*models.EdbDataBinlogDataReq{
  161. {
  162. OpType: "INSERT",
  163. Item: `{"create_time":"2024-10-12 17:03:04","data_time":"2024-09-08","data_timestamp":1725724800000,"edb_code":"test_code_1","edb_data_id":1001,"edb_info_id":1,"modify_time":"2024-10-14 13:37:30","value":71988}`,
  164. },
  165. {
  166. OpType: "UPDATE",
  167. Item: `{"create_time":"2024-10-12 17:03:04","data_time":"2024-09-09","data_timestamp":1725811200000,"edb_code":"test_code_1","edb_data_id":1002,"edb_info_id":1,"modify_time":"2024-10-14 13:37:30","value":72500}`,
  168. },
  169. {
  170. OpType: "DELETE",
  171. Item: `{"create_time":"2024-10-12 17:03:04","data_time":"2024-09-10","data_timestamp":1725897600000,"edb_code":"test_code_2","edb_data_id":2001,"edb_info_id":2,"modify_time":"2024-10-14 13:37:30","value":85600}`,
  172. },
  173. },
  174. }
  175. br.Ret = 200
  176. br.Success = true
  177. br.Msg = "获取测试数据成功"
  178. br.Data = testData
  179. return
  180. }