base_from_predict.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/utils"
  7. "fmt"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // PredictController 预测指标
  13. type PredictController struct {
  14. BaseAuthController
  15. }
  16. // Save
  17. // @Title 新增/编辑预测指标运算接口
  18. // @Description 新增预测指标运算接口
  19. // @Success 200 {object} models.EdbInfoCalculateSaveReq
  20. // @router /save [post]
  21. func (this *PredictController) Save() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. var req models.AddPredictEdbInfoReq
  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 req.EdbInfoId <= 0 {
  35. addPredict(br, req)
  36. } else {
  37. editPredict(br, req)
  38. }
  39. }
  40. // AddPredictCalculate 添加预测指标运算
  41. func addPredict(br *models.BaseResponse, req models.AddPredictEdbInfoReq) {
  42. //加入缓存机制,避免创建同一个名称的指标 start
  43. redisKey := fmt.Sprint("predict_edb_info:calculate:batch:save:", utils.DATA_SOURCE_PREDICT, ":", req.EdbName)
  44. isExist := utils.Rc.IsExist(redisKey)
  45. if isExist {
  46. br.Msg = "指标正在处理,请勿重复提交"
  47. return
  48. } else {
  49. //设置3分钟缓存
  50. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  51. defer func() {
  52. _ = utils.Rc.Delete(redisKey)
  53. }()
  54. }
  55. // 添加指标
  56. edbInfo, err, errMsg := logic.AddPredictEdbInfo(req.SourceEdbInfoId, req.ClassifyId, req.EdbName, req.DataDateType, req.RuleList, req.MinValue, req.MaxValue, req.AdminId, req.AdminName)
  57. if err != nil {
  58. br.Msg = "保存失败"
  59. if errMsg != `` {
  60. br.Msg = errMsg
  61. }
  62. br.ErrMsg = err.Error()
  63. return
  64. }
  65. resp := models.AddEdbInfoResp{
  66. EdbInfoId: edbInfo.EdbInfoId,
  67. UniqueCode: edbInfo.UniqueCode,
  68. }
  69. br.Ret = 200
  70. br.Success = true
  71. br.Msg = "保存成功"
  72. br.Data = resp
  73. br.IsAddLog = true
  74. }
  75. // editPredictCalculate 编辑预测指标运算
  76. func editPredict(br *models.BaseResponse, req models.AddPredictEdbInfoReq) {
  77. req.EdbName = strings.Trim(req.EdbName, " ")
  78. //加入缓存机制,避免创建同一个名称的指标 start
  79. redisKey := fmt.Sprint("predict_edb_info:calculate:batch:save:", utils.DATA_SOURCE_PREDICT, ":", req.EdbName)
  80. isExist := utils.Rc.IsExist(redisKey)
  81. if isExist {
  82. br.Msg = "指标正在处理,请勿重复提交"
  83. return
  84. } else {
  85. //设置3分钟缓存
  86. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  87. defer func() {
  88. _ = utils.Rc.Delete(redisKey)
  89. }()
  90. }
  91. // 编辑指标
  92. edbInfo, err, errMsg := logic.EditPredictEdbInfo(req.EdbInfoId, req.ClassifyId, req.EdbName, req.DataDateType, req.RuleList, req.MinValue, req.MaxValue)
  93. if err != nil {
  94. br.Msg = "保存失败"
  95. if errMsg != `` {
  96. br.Msg = errMsg
  97. }
  98. br.ErrMsg = err.Error()
  99. return
  100. }
  101. resp := models.AddEdbInfoResp{
  102. EdbInfoId: edbInfo.EdbInfoId,
  103. UniqueCode: edbInfo.UniqueCode,
  104. }
  105. br.Ret = 200
  106. br.Success = true
  107. br.Msg = "保存成功"
  108. br.Data = resp
  109. br.IsAddLog = true
  110. }
  111. // Refresh
  112. // @Title 刷新预测指标接口(有些预测指标里面含有动态数据)
  113. // @Description 刷新预测指标接口(有些预测指标里面含有动态数据)
  114. // @Success 200 {object} models.RefreshEdbInfoReq
  115. // @router /refresh [post]
  116. func (this *PredictController) Refresh() {
  117. br := new(models.BaseResponse).Init()
  118. var cacheKey string
  119. defer func() {
  120. this.Data["json"] = br
  121. this.ServeJSON()
  122. }()
  123. var req models.RefreshEdbInfoReq
  124. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  125. if err != nil {
  126. br.Msg = "参数解析异常!"
  127. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  128. return
  129. }
  130. if req.EdbCode == "" {
  131. br.Msg = "请输入指标编码!"
  132. br.ErrMsg = "请输入指标编码,指标编码为空"
  133. return
  134. }
  135. if req.EdbInfoId <= 0 {
  136. br.Msg = "请输入指标ID!"
  137. br.ErrMsg = "请输入指标ID"
  138. return
  139. }
  140. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  141. if err != nil {
  142. br.Msg = "指标不存在!"
  143. br.ErrMsg = "指标不存在"
  144. return
  145. }
  146. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(edbInfo.Source) + "_" + req.EdbCode
  147. if utils.Rc.IsExist(cacheKey) {
  148. br.Ret = 501
  149. br.Success = true
  150. br.Msg = "系统处理中,请稍后重试"
  151. br.IsSendEmail = false
  152. return
  153. }
  154. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  155. defer func() {
  156. _ = utils.Rc.Delete(cacheKey)
  157. }()
  158. var errMsg string
  159. _, err, errMsg = logic.RefreshPredictEdbInfo(req.EdbInfoId)
  160. if err != nil {
  161. br.Msg = "刷新指标失败!"
  162. if errMsg != `` {
  163. br.ErrMsg = errMsg
  164. }
  165. br.ErrMsg = "刷新指标失败,err:" + err.Error()
  166. return
  167. }
  168. // 更新ES
  169. go logic.UpdateEs(edbInfo.EdbInfoId)
  170. br.Ret = 200
  171. br.Success = true
  172. br.Msg = "获取成功"
  173. }